]>
Commit | Line | Data |
---|---|---|
8e46cda5 | 1 | #define SQUID_UNIT_TEST 1 |
f7f3304a | 2 | #include "squid-old.h" |
c8f4eac4 | 3 | #include "testCoss.h" |
c8f4eac4 | 4 | #include "Store.h" |
5 | #include "SwapDir.h" | |
6 | #include "DiskIO/DiskIOModule.h" | |
7 | #include "fs/ufs/ufscommon.h" | |
8 | #include "fs/coss/CossSwapDir.h" | |
9 | #include "Mem.h" | |
aa839030 | 10 | #include "MemObject.h" |
c8f4eac4 | 11 | #include "HttpHeader.h" |
12 | #include "HttpReply.h" | |
13 | #include "StoreFileSystem.h" | |
8ff3fa2e | 14 | #include "testStoreSupport.h" |
c8f4eac4 | 15 | |
27e059d4 AJ |
16 | #if HAVE_STDEXCEPT |
17 | #include <stdexcept> | |
18 | #endif | |
19 | ||
2e050051 | 20 | #define TESTDIR "testCoss__testCossSearch" |
21 | ||
c8f4eac4 | 22 | CPPUNIT_TEST_SUITE_REGISTRATION( testCoss ); |
23 | ||
24 | typedef RefCount<CossSwapDir> SwapDirPointer; | |
25 | extern REMOVALPOLICYCREATE createRemovalPolicy_lru; | |
26 | ||
27 | static void | |
28 | addSwapDir(SwapDirPointer aStore) | |
29 | { | |
30 | allocate_new_swapdir(&Config.cacheSwap); | |
31 | Config.cacheSwap.swapDirs[Config.cacheSwap.n_configured] = aStore.getRaw(); | |
32 | ++Config.cacheSwap.n_configured; | |
33 | } | |
34 | ||
35 | void | |
36 | testCoss::commonInit() | |
37 | { | |
38 | static bool inited = false; | |
39 | ||
40 | if (inited) | |
41 | return; | |
42 | ||
43 | StoreFileSystem::SetupAllFs(); | |
44 | ||
45 | Config.Store.avgObjectSize = 1024; | |
46 | ||
47 | Config.Store.objectsPerBucket = 20; | |
48 | ||
49 | Config.Store.maxObjectSize = 2048; | |
50 | ||
51 | Config.store_dir_select_algorithm = xstrdup("round-robin"); | |
52 | ||
53 | Config.replPolicy = new RemovalPolicySettings; | |
54 | ||
55 | Config.replPolicy->type = xstrdup ("lru"); | |
56 | ||
57 | Config.replPolicy->args = NULL; | |
58 | ||
59 | /* garh garh */ | |
60 | storeReplAdd("lru", createRemovalPolicy_lru); | |
61 | ||
7dbca7a4 | 62 | visible_appname_string = xstrdup(APP_FULLNAME); |
474dfa84 | 63 | |
c8f4eac4 | 64 | Mem::Init(); |
65 | ||
c8f4eac4 | 66 | comm_init(); |
67 | ||
68 | httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */ | |
69 | ||
70 | httpReplyInitModule(); /* must go before accepting replies */ | |
71 | ||
72 | mem_policy = createRemovalPolicy(Config.replPolicy); | |
73 | ||
74 | inited = true; | |
75 | } | |
76 | ||
77 | void | |
78 | testCoss::testCossCreate() | |
79 | { | |
2e050051 | 80 | if (0 > system ("rm -rf " TESTDIR)) |
c8f4eac4 | 81 | throw std::runtime_error("Failed to clean test work directory"); |
82 | ||
d5d5493b | 83 | Store::Root(new StoreController); |
c8f4eac4 | 84 | |
85 | SwapDirPointer aStore (new CossSwapDir()); | |
86 | ||
87 | addSwapDir(aStore); | |
88 | ||
89 | commonInit(); | |
90 | ||
2e050051 | 91 | char *path=xstrdup(TESTDIR); |
c8f4eac4 | 92 | |
93 | char *config_line=xstrdup("foo 100 max-size=102400 block-size=512 IOEngine=Blocking"); | |
94 | ||
95 | strtok(config_line, w_space); | |
96 | ||
97 | aStore->parse(0, path); | |
98 | ||
99 | safe_free(path); | |
100 | ||
101 | safe_free(config_line); | |
102 | ||
103 | /* ok, ready to create */ | |
104 | aStore->create(); | |
105 | ||
106 | struct stat sb; | |
107 | ||
2e050051 | 108 | CPPUNIT_ASSERT(::stat(TESTDIR, &sb) == 0); |
c8f4eac4 | 109 | |
110 | /* TODO: check the size */ | |
111 | ||
c8f4eac4 | 112 | Store::Root(NULL); |
113 | ||
d5d5493b DK |
114 | free_cachedir(&Config.cacheSwap); |
115 | ||
c8f4eac4 | 116 | /* todo: here we should test a dirty rebuild */ |
117 | ||
118 | // safe_free(Config.replPolicy->type); | |
119 | // delete Config.replPolicy; | |
2e050051 | 120 | if (0 > system ("rm -rf " TESTDIR)) |
c8f4eac4 | 121 | throw std::runtime_error("Failed to clean test work directory"); |
122 | } | |
123 | ||
124 | /* TODO make this a cbdata class */ | |
125 | ||
126 | static bool cbcalled; | |
127 | ||
128 | static void | |
129 | searchCallback(void *cbdata) | |
130 | { | |
131 | cbcalled = true; | |
132 | } | |
133 | ||
134 | void | |
135 | testCoss::testCossSearch() | |
136 | { | |
137 | /* test sequence | |
138 | * make a valid working ufs swapdir | |
139 | * put two entries in it and sync logs | |
140 | * search the ufs dir | |
141 | * check the entries we find are what we want | |
142 | */ | |
143 | ||
2e050051 | 144 | if (0 > system ("rm -rf " TESTDIR)) |
c8f4eac4 | 145 | throw std::runtime_error("Failed to clean test work directory"); |
146 | ||
d5d5493b | 147 | Store::Root(new StoreController); |
c8f4eac4 | 148 | |
149 | SwapDirPointer aStore (new CossSwapDir()); | |
150 | ||
151 | addSwapDir(aStore); | |
152 | ||
153 | commonInit(); | |
154 | ||
2e050051 | 155 | char *path=xstrdup(TESTDIR); |
c8f4eac4 | 156 | |
157 | char *config_line=xstrdup("foo 100 max-size=102400 block-size=512 IOEngine=Blocking"); | |
158 | ||
159 | strtok(config_line, w_space); | |
160 | ||
161 | aStore->parse(0, path); | |
162 | ||
163 | safe_free(path); | |
164 | ||
165 | safe_free(config_line); | |
166 | ||
167 | /* ok, ready to create */ | |
168 | aStore->create(); | |
169 | ||
170 | /* ok, ready to use */ | |
171 | Store::Root().init(); | |
172 | ||
8ff3fa2e | 173 | /* rebuild is a scheduled event */ |
174 | StockEventLoop loop; | |
175 | ||
bef81ea5 | 176 | /* our swapdir must be scheduled to rebuild */ |
63cfb666 | 177 | CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding); |
c8f4eac4 | 178 | |
aa839030 | 179 | loop.run(); |
c8f4eac4 | 180 | |
bef81ea5 | 181 | /* cannot use loop.run(); as the loop will never idle: the store-dir |
26ac0430 | 182 | * clean() scheduled event prevents it |
bef81ea5 | 183 | */ |
184 | ||
185 | /* nothing left to rebuild */ | |
63cfb666 | 186 | CPPUNIT_ASSERT_EQUAL(1, StoreController::store_dirs_rebuilding); |
c8f4eac4 | 187 | |
188 | /* add an entry */ | |
189 | { | |
190 | /* Create "vary" base object */ | |
191 | request_flags flags; | |
192 | flags.cachable = 1; | |
193 | StoreEntry *pe = storeCreateEntry("dummy url", "dummy log url", flags, METHOD_GET); | |
f46fe759 | 194 | HttpReply *rep = (HttpReply *) pe->getReply(); // bypass const |
11992b6f | 195 | rep->setHeaders(HTTP_OK, "dummy test object", "x-squid-internal/test", -1, -1, squid_curtime + 100000); |
c8f4eac4 | 196 | |
d88e3c49 | 197 | pe->setPublicKey(); |
c8f4eac4 | 198 | |
3900307b | 199 | pe->buffer(); |
c8f4eac4 | 200 | /* TODO: remove this when the metadata is separated */ |
201 | { | |
202 | Packer p; | |
203 | packerToStoreInit(&p, pe); | |
f46fe759 | 204 | pe->getReply()->packHeadersInto(&p); |
c8f4eac4 | 205 | packerClean(&p); |
206 | } | |
207 | ||
3900307b | 208 | pe->flush(); |
209 | pe->timestampsSet(); | |
c8f4eac4 | 210 | pe->complete(); |
1863360d | 211 | pe->swapOut(); |
c8f4eac4 | 212 | CPPUNIT_ASSERT(pe->swap_dirn == 0); |
213 | CPPUNIT_ASSERT(pe->swap_filen == 0); | |
97b5e68f | 214 | pe->unlock(); |
c8f4eac4 | 215 | } |
216 | ||
217 | storeDirWriteCleanLogs(0); | |
218 | ||
219 | /* here we cheat: we know that UFSSwapDirs search off disk. If we did an init call to a new | |
220 | * swapdir instance, we'd not be testing a clean build. | |
221 | */ | |
222 | StoreSearchPointer search = aStore->search (NULL, NULL); /* search for everything in the store */ | |
223 | ||
224 | /* nothing should be immediately available */ | |
225 | #if 0 | |
226 | ||
227 | CPPUNIT_ASSERT(search->next() == false); | |
228 | #endif | |
229 | ||
230 | CPPUNIT_ASSERT(search->error() == false); | |
231 | CPPUNIT_ASSERT(search->isDone() == false); | |
232 | CPPUNIT_ASSERT(search->currentItem() == NULL); | |
233 | ||
234 | /* trigger a callback */ | |
235 | cbcalled = false; | |
236 | search->next(searchCallback, NULL); | |
237 | CPPUNIT_ASSERT(cbcalled == true); | |
238 | ||
239 | /* we should have access to a entry now, that matches the entry we had before */ | |
240 | //CPPUNIT_ASSERT(search->next() == false); | |
241 | CPPUNIT_ASSERT(search->error() == false); | |
242 | CPPUNIT_ASSERT(search->isDone() == false); | |
243 | CPPUNIT_ASSERT(search->currentItem() != NULL); | |
244 | ||
245 | /* trigger another callback */ | |
246 | cbcalled = false; | |
247 | search->next(searchCallback, NULL); | |
248 | CPPUNIT_ASSERT(cbcalled == true); | |
249 | ||
250 | /* now we should have no error, we should have finished and have no current item */ | |
251 | //CPPUNIT_ASSERT(search->next() == false); | |
252 | CPPUNIT_ASSERT(search->error() == false); | |
253 | CPPUNIT_ASSERT(search->isDone() == true); | |
254 | CPPUNIT_ASSERT(search->currentItem() == NULL); | |
255 | ||
c8f4eac4 | 256 | Store::Root(NULL); |
257 | ||
d5d5493b DK |
258 | free_cachedir(&Config.cacheSwap); |
259 | ||
c8f4eac4 | 260 | /* todo: here we should test a dirty rebuild */ |
261 | ||
262 | //TODO: do this once, or each time. safe_free(Config.replPolicy->type); | |
263 | // delete Config.replPolicy; | |
264 | ||
2e050051 | 265 | if (0 > system ("rm -rf " TESTDIR)) |
c8f4eac4 | 266 | throw std::runtime_error("Failed to clean test work directory"); |
267 | } | |
b7717b61 | 268 | |
26ac0430 | 269 | /* The COSS store should always configure an IO engine even if none is |
b7717b61 | 270 | * supplied on the configuration line. |
271 | */ | |
272 | void | |
273 | testCoss::testDefaultEngine() | |
274 | { | |
275 | /* boring common test boilerplate */ | |
276 | if (0 > system ("rm -rf " TESTDIR)) | |
277 | throw std::runtime_error("Failed to clean test work directory"); | |
278 | ||
d5d5493b | 279 | Store::Root(new StoreController); |
b7717b61 | 280 | SwapDirPointer aStore (new CossSwapDir()); |
281 | addSwapDir(aStore); | |
282 | commonInit(); | |
283 | ||
284 | char *path=xstrdup(TESTDIR); | |
285 | char *config_line=xstrdup("foo 100 max-size=102400 block-size=512"); | |
286 | strtok(config_line, w_space); | |
287 | aStore->parse(0, path); | |
288 | safe_free(path); | |
289 | safe_free(config_line); | |
290 | CPPUNIT_ASSERT(aStore->io != NULL); | |
291 | ||
b7717b61 | 292 | Store::Root(NULL); |
d5d5493b | 293 | free_cachedir(&Config.cacheSwap); |
b7717b61 | 294 | if (0 > system ("rm -rf " TESTDIR)) |
295 | throw std::runtime_error("Failed to clean test work directory"); | |
296 | } |