]> git.ipfire.org Git - thirdparty/squid.git/blame - src/tests/testUfs.cc
Author: Various Translators
[thirdparty/squid.git] / src / tests / testUfs.cc
CommitLineData
f5bffbd5 1#include "config.h"
c8f4eac4 2#include "testUfs.h"
c8f4eac4 3#include "Store.h"
4#include "SwapDir.h"
5#include "DiskIO/DiskIOModule.h"
6#include "fs/ufs/ufscommon.h"
7#include "Mem.h"
aa839030 8#include "MemObject.h"
c8f4eac4 9#include "HttpHeader.h"
10#include "HttpReply.h"
8ff3fa2e 11#include "testStoreSupport.h"
c8f4eac4 12
27e059d4
AJ
13#if HAVE_STDEXCEPT
14#include <stdexcept>
15#endif
16
2e050051 17#define TESTDIR "testUfs__testUfsSearch"
18
c8f4eac4 19CPPUNIT_TEST_SUITE_REGISTRATION( testUfs );
20
21typedef RefCount<UFSSwapDir> SwapDirPointer;
adf8c195 22extern REMOVALPOLICYCREATE createRemovalPolicy_lru; /* XXX fails with --enable-removal-policies=heap */
c8f4eac4 23
24static void
25addSwapDir(SwapDirPointer aStore)
26{
27 allocate_new_swapdir(&Config.cacheSwap);
28 Config.cacheSwap.swapDirs[Config.cacheSwap.n_configured] = aStore.getRaw();
29 ++Config.cacheSwap.n_configured;
30}
31
32/* TODO make this a cbdata class */
33
34static bool cbcalled;
35
36static void
37searchCallback(void *cbdata)
38{
39 cbcalled = true;
40}
41
42void
b7717b61 43testUfs::commonInit()
c8f4eac4 44{
b7717b61 45 static bool inited = false;
c8f4eac4 46
b7717b61 47 if (inited)
48 return;
c8f4eac4 49
50 Config.Store.avgObjectSize = 1024;
51
52 Config.Store.objectsPerBucket = 20;
53
54 Config.Store.maxObjectSize = 2048;
55
56 Config.store_dir_select_algorithm = xstrdup("round-robin");
57
58 Config.replPolicy = new RemovalPolicySettings;
59
60 Config.replPolicy->type = xstrdup ("lru");
61
62 /* garh garh */
63 storeReplAdd("lru", createRemovalPolicy_lru);
64
65 Mem::Init();
66
c8f4eac4 67 comm_init();
68
69 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
70
71 httpReplyInitModule(); /* must go before accepting replies */
72
b7717b61 73 inited = true;
74}
75
76void
77testUfs::testUfsSearch()
78{
79 /* test sequence
80 * make a valid working ufs swapdir
81 * put two entries in it and sync logs
82 * search the ufs dir
83 * check the entries we find are what we want
84 */
85
86 if (0 > system ("rm -rf " TESTDIR))
87 throw std::runtime_error("Failed to clean test work directory");
88
89 StorePointer aRoot (new StoreController);
90
91 Store::Root(aRoot);
92
93 SwapDirPointer aStore (new UFSSwapDir("ufs", "Blocking"));
94
95 aStore->IO = new UFSStrategy(DiskIOModule::Find("Blocking")->createStrategy());
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
207 free_cachedir(&Config.cacheSwap);
208
209 /* todo: here we should test a dirty rebuild */
210
b7717b61 211 Store::Root(NULL);
212 safe_free(Config.replPolicy->type);
213 delete Config.replPolicy;
214
215 if (0 > system ("rm -rf " TESTDIR))
216 throw std::runtime_error("Failed to clean test work directory");
217}
218
26ac0430 219/* The UFS store should always configure an IO engine even if none is
b7717b61 220 * supplied on the configuration line.
221 */
222void
223testUfs::testUfsDefaultEngine()
224{
225 /* boring common test boilerplate */
226 if (0 > system ("rm -rf " TESTDIR))
227 throw std::runtime_error("Failed to clean test work directory");
228
fe36168c 229 // This assertion may fail if previous test cases fail.
230 // Apparently, CPPUNIT_ASSERT* failure may prevent destructors of local
231 // objects such as "StorePointer aRoot" from being called.
232 CPPUNIT_ASSERT(!store_table); // or StoreHashIndex ctor will abort below
233
b7717b61 234 StorePointer aRoot (new StoreController);
235 Store::Root(aRoot);
236 SwapDirPointer aStore (new UFSSwapDir("ufs", "Blocking"));
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
251 free_cachedir(&Config.cacheSwap);
252 Store::Root(NULL);
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}