2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 #include "MemObject.h"
11 #include "SquidConfig.h"
12 #include "SquidTime.h"
14 #include "StoreSearch.h"
16 #include "testStoreController.h"
17 #include "TestSwapDir.h"
19 CPPUNIT_TEST_SUITE_REGISTRATION( testStoreController
);
22 addSwapDir(TestSwapDirPointer aStore
)
24 allocate_new_swapdir(&Config
.cacheSwap
);
25 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
26 ++Config
.cacheSwap
.n_configured
;
30 testStoreController::testStats()
32 StoreEntry
*logEntry
= new StoreEntry
;
33 logEntry
->makeMemObject();
34 logEntry
->mem_obj
->setUris("dummy_storeId", NULL
, HttpRequestMethod());
35 logEntry
->store_status
= STORE_PENDING
;
36 StorePointer
aRoot (new StoreController
);
38 TestSwapDirPointer
aStore (new TestSwapDir
);
39 TestSwapDirPointer
aStore2 (new TestSwapDir
);
42 CPPUNIT_ASSERT_EQUAL(false, aStore
->statsCalled
);
43 CPPUNIT_ASSERT_EQUAL(false, aStore2
->statsCalled
);
44 Store::Stats(logEntry
);
45 free_cachedir(&Config
.cacheSwap
);
46 CPPUNIT_ASSERT_EQUAL(true, aStore
->statsCalled
);
47 CPPUNIT_ASSERT_EQUAL(true, aStore2
->statsCalled
);
54 static bool inited
= false;
61 Config
.Store
.avgObjectSize
= 1024;
63 Config
.Store
.objectsPerBucket
= 20;
65 Config
.Store
.maxObjectSize
= 2048;
67 Config
.store_dir_select_algorithm
= xstrdup("round-robin");
71 testStoreController::testMaxSize()
74 StoreEntry
*logEntry
= new StoreEntry
;
75 logEntry
->makeMemObject();
76 logEntry
->mem_obj
->setUris("dummy_storeId", NULL
, HttpRequestMethod());
77 logEntry
->store_status
= STORE_PENDING
;
78 StorePointer
aRoot (new StoreController
);
80 TestSwapDirPointer
aStore (new TestSwapDir
);
81 TestSwapDirPointer
aStore2 (new TestSwapDir
);
84 CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize());
85 free_cachedir(&Config
.cacheSwap
);
90 addedEntry(StorePointer hashStore
,
98 StoreEntry
*e
= new StoreEntry();
99 e
->store_status
= STORE_OK
;
100 e
->setMemStatus(NOT_IN_MEMORY
);
101 e
->swap_status
= SWAPOUT_DONE
; /* bogus haha */
102 e
->swap_filen
= 0; /* garh - lower level*/
105 for (int i
=0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
106 if (INDEXSD (i
) == aStore
.getRaw())
110 CPPUNIT_ASSERT (e
->swap_dirn
!= -1);
111 e
->swap_file_sz
= 0; /* garh lower level */
112 e
->lastref
= squid_curtime
;
113 e
->timestamp
= squid_curtime
;
114 e
->expires
= squid_curtime
;
115 e
->lastmod
= squid_curtime
;
117 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
118 EBIT_CLR(e
->flags
, KEY_PRIVATE
);
119 e
->ping_status
= PING_NONE
;
120 EBIT_CLR(e
->flags
, ENTRY_VALIDATED
);
121 e
->hashInsert((const cache_key
*)name
.termedBuf()); /* do it after we clear KEY_PRIVATE */
125 /* TODO make this a cbdata class */
127 static bool cbcalled
;
130 searchCallback(void *cbdata
)
136 testStoreController::testSearch()
139 StorePointer
aRoot (new StoreController());
141 TestSwapDirPointer
aStore (new TestSwapDir
);
142 TestSwapDirPointer
aStore2 (new TestSwapDir
);
145 Store::Root().init();
146 StoreEntry
* entry1
= addedEntry (&Store::Root(), aStore
.getRaw(), "name", NULL
, NULL
);
147 StoreEntry
* entry2
= addedEntry (&Store::Root(), aStore2
.getRaw(), "name2", NULL
, NULL
);
148 StoreSearchPointer search
= aRoot
->search (NULL
, NULL
); /* search for everything in the store */
150 /* nothing should be immediately available */
151 CPPUNIT_ASSERT_EQUAL(false, search
->error());
152 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
153 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(NULL
), search
->currentItem());
156 CPPUNIT_ASSERT_EQUAL(false, search
->next());
159 /* trigger a callback */
161 search
->next(searchCallback
, NULL
);
162 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
164 /* we should have access to a entry now, that matches the entry we had before */
165 CPPUNIT_ASSERT_EQUAL(false, search
->error());
166 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
167 /* note the hash order is random - the test happens to be in a nice order */
168 CPPUNIT_ASSERT_EQUAL(entry1
, search
->currentItem());
169 //CPPUNIT_ASSERT_EQUAL(false, search->next());
171 /* trigger another callback */
173 search
->next(searchCallback
, NULL
);
174 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
176 /* we should have access to a entry now, that matches the entry we had before */
177 CPPUNIT_ASSERT_EQUAL(false, search
->error());
178 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
179 CPPUNIT_ASSERT_EQUAL(entry2
, search
->currentItem());
180 //CPPUNIT_ASSERT_EQUAL(false, search->next());
182 /* trigger another callback */
184 search
->next(searchCallback
, NULL
);
185 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
187 /* now we should have no error, we should have finished and have no current item */
188 CPPUNIT_ASSERT_EQUAL(false, search
->error());
189 CPPUNIT_ASSERT_EQUAL(true, search
->isDone());
190 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(NULL
), search
->currentItem());
191 //CPPUNIT_ASSERT_EQUAL(false, search->next());