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