1 #define SQUID_UNIT_TEST 1
4 #include "testStoreController.h"
7 #include "TestSwapDir.h"
9 #include "SquidConfig.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
;
24 testStoreController::testStats()
26 StoreEntry
* logEntry
= new StoreEntry("dummy_url", "dummy_log_url");
27 logEntry
->store_status
= STORE_PENDING
;
28 StorePointer
aRoot (new StoreController
);
30 TestSwapDirPointer
aStore (new TestSwapDir
);
31 TestSwapDirPointer
aStore2 (new TestSwapDir
);
34 CPPUNIT_ASSERT(aStore
->statsCalled
== false);
35 CPPUNIT_ASSERT(aStore2
->statsCalled
== false);
36 Store::Stats(logEntry
);
37 free_cachedir(&Config
.cacheSwap
);
38 CPPUNIT_ASSERT(aStore
->statsCalled
== true);
39 CPPUNIT_ASSERT(aStore2
->statsCalled
== true);
46 static bool inited
= false;
53 Config
.Store
.avgObjectSize
= 1024;
55 Config
.Store
.objectsPerBucket
= 20;
57 Config
.Store
.maxObjectSize
= 2048;
59 Config
.store_dir_select_algorithm
= xstrdup("round-robin");
63 testStoreController::testMaxSize()
66 StoreEntry
* logEntry
= new StoreEntry("dummy_url", "dummy_log_url");
67 logEntry
->store_status
= STORE_PENDING
;
68 StorePointer
aRoot (new StoreController
);
70 TestSwapDirPointer
aStore (new TestSwapDir
);
71 TestSwapDirPointer
aStore2 (new TestSwapDir
);
74 CPPUNIT_ASSERT(Store::Root().maxSize() == 6);
75 free_cachedir(&Config
.cacheSwap
);
80 addedEntry(StorePointer hashStore
,
88 StoreEntry
*e
= new StoreEntry();
89 e
->store_status
= STORE_OK
;
90 e
->setMemStatus(NOT_IN_MEMORY
);
91 e
->swap_status
= SWAPOUT_DONE
; /* bogus haha */
92 e
->swap_filen
= 0; /* garh - lower level*/
95 for (int i
=0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
96 if (INDEXSD (i
) == aStore
.getRaw())
100 CPPUNIT_ASSERT (e
->swap_dirn
!= -1);
101 e
->swap_file_sz
= 0; /* garh lower level */
103 e
->lastref
= squid_curtime
;
104 e
->timestamp
= squid_curtime
;
105 e
->expires
= squid_curtime
;
106 e
->lastmod
= squid_curtime
;
108 EBIT_SET(e
->flags
, ENTRY_CACHABLE
);
109 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
110 EBIT_CLR(e
->flags
, KEY_PRIVATE
);
111 e
->ping_status
= PING_NONE
;
112 EBIT_CLR(e
->flags
, ENTRY_VALIDATED
);
113 e
->hashInsert((const cache_key
*)name
.termedBuf()); /* do it after we clear KEY_PRIVATE */
117 /* TODO make this a cbdata class */
119 static bool cbcalled
;
122 searchCallback(void *cbdata
)
128 testStoreController::testSearch()
131 StorePointer
aRoot (new StoreController());
133 TestSwapDirPointer
aStore (new TestSwapDir
);
134 TestSwapDirPointer
aStore2 (new TestSwapDir
);
137 Store::Root().init();
138 StoreEntry
* entry1
= addedEntry (&Store::Root(), aStore
.getRaw(), "name", NULL
, NULL
);
139 StoreEntry
* entry2
= addedEntry (&Store::Root(), aStore2
.getRaw(), "name2", NULL
, NULL
);
140 StoreSearchPointer search
= aRoot
->search (NULL
, NULL
); /* search for everything in the store */
142 /* nothing should be immediately available */
143 CPPUNIT_ASSERT(search
->error() == false);
144 CPPUNIT_ASSERT(search
->isDone() == false);
145 CPPUNIT_ASSERT(search
->currentItem() == NULL
);
148 CPPUNIT_ASSERT(search
->next() == false);
151 /* trigger a callback */
153 search
->next(searchCallback
, NULL
);
154 CPPUNIT_ASSERT(cbcalled
== true);
156 /* we should have access to a entry now, that matches the entry we had before */
157 CPPUNIT_ASSERT(search
->error() == false);
158 CPPUNIT_ASSERT(search
->isDone() == false);
159 /* note the hash order is random - the test happens to be in a nice order */
160 CPPUNIT_ASSERT(search
->currentItem() == entry1
);
161 //CPPUNIT_ASSERT(search->next() == false);
163 /* trigger another callback */
165 search
->next(searchCallback
, NULL
);
166 CPPUNIT_ASSERT(cbcalled
== true);
168 /* we should have access to a entry now, that matches the entry we had before */
169 CPPUNIT_ASSERT(search
->error() == false);
170 CPPUNIT_ASSERT(search
->isDone() == false);
171 CPPUNIT_ASSERT(search
->currentItem() == entry2
);
172 //CPPUNIT_ASSERT(search->next() == false);
174 /* trigger another callback */
176 search
->next(searchCallback
, NULL
);
177 CPPUNIT_ASSERT(cbcalled
== true);
179 /* now we should have no error, we should have finished and have no current item */
180 CPPUNIT_ASSERT(search
->error() == false);
181 CPPUNIT_ASSERT(search
->isDone() == true);
182 CPPUNIT_ASSERT(search
->currentItem() == NULL
);
183 //CPPUNIT_ASSERT(search->next() == false);