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