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