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