1 #define SQUID_UNIT_TEST 1
6 #include "SquidConfig.h"
9 #include "StoreSearch.h"
11 #include "testStoreController.h"
12 #include "TestSwapDir.h"
14 CPPUNIT_TEST_SUITE_REGISTRATION( testStoreController
);
17 addSwapDir(TestSwapDirPointer aStore
)
19 allocate_new_swapdir(&Config
.cacheSwap
);
20 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
21 ++Config
.cacheSwap
.n_configured
;
25 testStoreController::testStats()
27 StoreEntry
*logEntry
= new StoreEntry
;
28 logEntry
->makeMemObject();
29 logEntry
->mem_obj
->setUris("dummy_storeId", NULL
, HttpRequestMethod());
30 logEntry
->store_status
= STORE_PENDING
;
31 StorePointer
aRoot (new StoreController
);
33 TestSwapDirPointer
aStore (new TestSwapDir
);
34 TestSwapDirPointer
aStore2 (new TestSwapDir
);
37 CPPUNIT_ASSERT_EQUAL(false, aStore
->statsCalled
);
38 CPPUNIT_ASSERT_EQUAL(false, aStore2
->statsCalled
);
39 Store::Stats(logEntry
);
40 free_cachedir(&Config
.cacheSwap
);
41 CPPUNIT_ASSERT_EQUAL(true, aStore
->statsCalled
);
42 CPPUNIT_ASSERT_EQUAL(true, aStore2
->statsCalled
);
49 static bool inited
= false;
56 Config
.Store
.avgObjectSize
= 1024;
58 Config
.Store
.objectsPerBucket
= 20;
60 Config
.Store
.maxObjectSize
= 2048;
62 Config
.store_dir_select_algorithm
= xstrdup("round-robin");
66 testStoreController::testMaxSize()
69 StoreEntry
*logEntry
= new StoreEntry
;
70 logEntry
->makeMemObject();
71 logEntry
->mem_obj
->setUris("dummy_storeId", NULL
, HttpRequestMethod());
72 logEntry
->store_status
= STORE_PENDING
;
73 StorePointer
aRoot (new StoreController
);
75 TestSwapDirPointer
aStore (new TestSwapDir
);
76 TestSwapDirPointer
aStore2 (new TestSwapDir
);
79 CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize());
80 free_cachedir(&Config
.cacheSwap
);
85 addedEntry(StorePointer hashStore
,
93 StoreEntry
*e
= new StoreEntry();
94 e
->store_status
= STORE_OK
;
95 e
->setMemStatus(NOT_IN_MEMORY
);
96 e
->swap_status
= SWAPOUT_DONE
; /* bogus haha */
97 e
->swap_filen
= 0; /* garh - lower level*/
100 for (int i
=0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
101 if (INDEXSD (i
) == aStore
.getRaw())
105 CPPUNIT_ASSERT (e
->swap_dirn
!= -1);
106 e
->swap_file_sz
= 0; /* garh lower level */
107 e
->lastref
= squid_curtime
;
108 e
->timestamp
= squid_curtime
;
109 e
->expires
= squid_curtime
;
110 e
->lastmod
= squid_curtime
;
112 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
113 EBIT_CLR(e
->flags
, KEY_PRIVATE
);
114 e
->ping_status
= PING_NONE
;
115 EBIT_CLR(e
->flags
, ENTRY_VALIDATED
);
116 e
->hashInsert((const cache_key
*)name
.termedBuf()); /* do it after we clear KEY_PRIVATE */
120 /* TODO make this a cbdata class */
122 static bool cbcalled
;
125 searchCallback(void *cbdata
)
131 testStoreController::testSearch()
134 StorePointer
aRoot (new StoreController());
136 TestSwapDirPointer
aStore (new TestSwapDir
);
137 TestSwapDirPointer
aStore2 (new TestSwapDir
);
140 Store::Root().init();
141 StoreEntry
* entry1
= addedEntry (&Store::Root(), aStore
.getRaw(), "name", NULL
, NULL
);
142 StoreEntry
* entry2
= addedEntry (&Store::Root(), aStore2
.getRaw(), "name2", NULL
, NULL
);
143 StoreSearchPointer search
= aRoot
->search (NULL
, NULL
); /* search for everything in the store */
145 /* nothing should be immediately available */
146 CPPUNIT_ASSERT_EQUAL(false, search
->error());
147 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
148 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(NULL
), search
->currentItem());
151 CPPUNIT_ASSERT_EQUAL(false, search
->next());
154 /* trigger a callback */
156 search
->next(searchCallback
, NULL
);
157 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
159 /* we should have access to a entry now, that matches the entry we had before */
160 CPPUNIT_ASSERT_EQUAL(false, search
->error());
161 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
162 /* note the hash order is random - the test happens to be in a nice order */
163 CPPUNIT_ASSERT_EQUAL(entry1
, search
->currentItem());
164 //CPPUNIT_ASSERT_EQUAL(false, search->next());
166 /* trigger another callback */
168 search
->next(searchCallback
, NULL
);
169 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
171 /* we should have access to a entry now, that matches the entry we had before */
172 CPPUNIT_ASSERT_EQUAL(false, search
->error());
173 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
174 CPPUNIT_ASSERT_EQUAL(entry2
, search
->currentItem());
175 //CPPUNIT_ASSERT_EQUAL(false, search->next());
177 /* trigger another callback */
179 search
->next(searchCallback
, NULL
);
180 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
182 /* now we should have no error, we should have finished and have no current item */
183 CPPUNIT_ASSERT_EQUAL(false, search
->error());
184 CPPUNIT_ASSERT_EQUAL(true, search
->isDone());
185 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(NULL
), search
->currentItem());
186 //CPPUNIT_ASSERT_EQUAL(false, search->next());