1 #define SQUID_UNIT_TEST 1
5 #include "testStoreController.h"
8 #include "TestSwapDir.h"
10 #include "SquidTime.h"
11 #include "StoreSearch.h"
13 CPPUNIT_TEST_SUITE_REGISTRATION( testStoreController
);
16 addSwapDir(TestSwapDirPointer aStore
)
18 allocate_new_swapdir(&Config
.cacheSwap
);
19 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
20 ++Config
.cacheSwap
.n_configured
;
25 testStoreController::testStats()
27 StoreEntry
* logEntry
= new StoreEntry("dummy_url", "dummy_log_url");
28 logEntry
->store_status
= STORE_PENDING
;
29 StorePointer
aRoot (new StoreController
);
31 TestSwapDirPointer
aStore (new TestSwapDir
);
32 TestSwapDirPointer
aStore2 (new TestSwapDir
);
35 CPPUNIT_ASSERT(aStore
->statsCalled
== false);
36 CPPUNIT_ASSERT(aStore2
->statsCalled
== false);
37 Store::Stats(logEntry
);
38 free_cachedir(&Config
.cacheSwap
);
39 CPPUNIT_ASSERT(aStore
->statsCalled
== true);
40 CPPUNIT_ASSERT(aStore2
->statsCalled
== true);
47 static bool inited
= false;
54 Config
.Store
.avgObjectSize
= 1024;
56 Config
.Store
.objectsPerBucket
= 20;
58 Config
.Store
.maxObjectSize
= 2048;
60 Config
.store_dir_select_algorithm
= xstrdup("round-robin");
64 testStoreController::testMaxSize()
67 StoreEntry
* logEntry
= new StoreEntry("dummy_url", "dummy_log_url");
68 logEntry
->store_status
= STORE_PENDING
;
69 StorePointer
aRoot (new StoreController
);
71 TestSwapDirPointer
aStore (new TestSwapDir
);
72 TestSwapDirPointer
aStore2 (new TestSwapDir
);
75 CPPUNIT_ASSERT(Store::Root().maxSize() == 6);
76 free_cachedir(&Config
.cacheSwap
);
81 addedEntry(StorePointer hashStore
,
89 StoreEntry
*e
= new StoreEntry();
90 e
->store_status
= STORE_OK
;
91 e
->setMemStatus(NOT_IN_MEMORY
);
92 e
->swap_status
= SWAPOUT_DONE
; /* bogus haha */
93 e
->swap_filen
= 0; /* garh - lower level*/
96 for (int i
=0; i
< Config
.cacheSwap
.n_configured
; i
++) {
97 if (INDEXSD (i
) == aStore
.getRaw())
101 CPPUNIT_ASSERT (e
->swap_dirn
!= -1);
102 e
->swap_file_sz
= 0; /* garh lower level */
104 e
->lastref
= squid_curtime
;
105 e
->timestamp
= squid_curtime
;
106 e
->expires
= squid_curtime
;
107 e
->lastmod
= squid_curtime
;
109 EBIT_SET(e
->flags
, ENTRY_CACHABLE
);
110 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
111 EBIT_CLR(e
->flags
, KEY_PRIVATE
);
112 e
->ping_status
= PING_NONE
;
113 EBIT_CLR(e
->flags
, ENTRY_VALIDATED
);
114 e
->hashInsert((const cache_key
*)name
.termedBuf()); /* do it after we clear KEY_PRIVATE */
118 /* TODO make this a cbdata class */
120 static bool cbcalled
;
123 searchCallback(void *cbdata
)
129 testStoreController::testSearch()
132 StorePointer
aRoot (new StoreController());
134 TestSwapDirPointer
aStore (new TestSwapDir
);
135 TestSwapDirPointer
aStore2 (new TestSwapDir
);
138 Store::Root().init();
139 StoreEntry
* entry1
= addedEntry (&Store::Root(), aStore
.getRaw(), "name", NULL
, NULL
);
140 StoreEntry
* entry2
= addedEntry (&Store::Root(), aStore2
.getRaw(), "name2", NULL
, NULL
);
141 StoreSearchPointer search
= aRoot
->search (NULL
, NULL
); /* search for everything in the store */
143 /* nothing should be immediately available */
144 CPPUNIT_ASSERT(search
->error() == false);
145 CPPUNIT_ASSERT(search
->isDone() == false);
146 CPPUNIT_ASSERT(search
->currentItem() == NULL
);
149 CPPUNIT_ASSERT(search
->next() == false);
152 /* trigger a callback */
154 search
->next(searchCallback
, NULL
);
155 CPPUNIT_ASSERT(cbcalled
== true);
157 /* we should have access to a entry now, that matches the entry we had before */
158 CPPUNIT_ASSERT(search
->error() == false);
159 CPPUNIT_ASSERT(search
->isDone() == false);
160 /* note the hash order is random - the test happens to be in a nice order */
161 CPPUNIT_ASSERT(search
->currentItem() == entry1
);
162 //CPPUNIT_ASSERT(search->next() == false);
164 /* trigger another callback */
166 search
->next(searchCallback
, NULL
);
167 CPPUNIT_ASSERT(cbcalled
== true);
169 /* we should have access to a entry now, that matches the entry we had before */
170 CPPUNIT_ASSERT(search
->error() == false);
171 CPPUNIT_ASSERT(search
->isDone() == false);
172 CPPUNIT_ASSERT(search
->currentItem() == entry2
);
173 //CPPUNIT_ASSERT(search->next() == false);
175 /* trigger another callback */
177 search
->next(searchCallback
, NULL
);
178 CPPUNIT_ASSERT(cbcalled
== true);
180 /* now we should have no error, we should have finished and have no current item */
181 CPPUNIT_ASSERT(search
->error() == false);
182 CPPUNIT_ASSERT(search
->isDone() == true);
183 CPPUNIT_ASSERT(search
->currentItem() == NULL
);
184 //CPPUNIT_ASSERT(search->next() == false);