]> git.ipfire.org Git - thirdparty/squid.git/blame - src/tests/testUfs.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / tests / testUfs.cc
CommitLineData
4e0938ef
AJ
1/*
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
f7f3304a 9#include "squid.h"
c8f4eac4 10#include "DiskIO/DiskIOModule.h"
582c2af2
FC
11#include "fs/ufs/UFSSwapDir.h"
12#include "globals.h"
c8f4eac4 13#include "HttpHeader.h"
14#include "HttpReply.h"
58373ff8 15#include "MemObject.h"
f206b652 16#include "RequestFlags.h"
4d5904f7 17#include "SquidConfig.h"
58373ff8
FC
18#include "Store.h"
19#include "SwapDir.h"
582c2af2
FC
20#include "testStoreSupport.h"
21#include "testUfs.h"
c8f4eac4 22
27e059d4 23#include <stdexcept>
27e059d4 24
0795b38d 25#define TESTDIR "testUfs_Store"
2e050051 26
c8f4eac4 27CPPUNIT_TEST_SUITE_REGISTRATION( testUfs );
28
58373ff8 29typedef RefCount<Fs::Ufs::UFSSwapDir> SwapDirPointer;
f53969cc 30extern REMOVALPOLICYCREATE createRemovalPolicy_lru; /* XXX fails with --enable-removal-policies=heap */
c8f4eac4 31
32static void
33addSwapDir(SwapDirPointer aStore)
34{
35 allocate_new_swapdir(&Config.cacheSwap);
36 Config.cacheSwap.swapDirs[Config.cacheSwap.n_configured] = aStore.getRaw();
37 ++Config.cacheSwap.n_configured;
38}
39
40/* TODO make this a cbdata class */
41
42static bool cbcalled;
43
44static void
45searchCallback(void *cbdata)
46{
47 cbcalled = true;
48}
49
50void
b7717b61 51testUfs::commonInit()
c8f4eac4 52{
b7717b61 53 static bool inited = false;
c8f4eac4 54
b7717b61 55 if (inited)
56 return;
c8f4eac4 57
58 Config.Store.avgObjectSize = 1024;
c8f4eac4 59 Config.Store.objectsPerBucket = 20;
c8f4eac4 60 Config.Store.maxObjectSize = 2048;
61
62 Config.store_dir_select_algorithm = xstrdup("round-robin");
63
64 Config.replPolicy = new RemovalPolicySettings;
86c63190 65 Config.replPolicy->type = xstrdup("lru");
c8f4eac4 66
67 /* garh garh */
68 storeReplAdd("lru", createRemovalPolicy_lru);
69
70 Mem::Init();
71
c8f4eac4 72 comm_init();
73
f53969cc 74 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
c8f4eac4 75
f53969cc 76 httpReplyInitModule(); /* must go before accepting replies */
c8f4eac4 77
b7717b61 78 inited = true;
79}
80
81void
82testUfs::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);
f53969cc 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 */
226void
227testUfs::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;
86c63190 243 Config.replPolicy->type = xstrdup("lru");
b7717b61 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}
f53969cc 262