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