2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
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.
10 #include "DiskIO/DiskIOModule.h"
11 #include "fs/ufs/UFSSwapDir.h"
13 #include "HttpHeader.h"
14 #include "HttpReply.h"
15 #include "MemObject.h"
16 #include "RequestFlags.h"
17 #include "SquidConfig.h"
20 #include "testStoreSupport.h"
22 #include "unitTestMain.h"
26 #define TESTDIR "testUfs_Store"
28 CPPUNIT_TEST_SUITE_REGISTRATION( testUfs
);
30 typedef RefCount
<Fs::Ufs::UFSSwapDir
> SwapDirPointer
;
31 extern REMOVALPOLICYCREATE createRemovalPolicy_lru
; /* XXX fails with --enable-removal-policies=heap */
34 addSwapDir(SwapDirPointer aStore
)
36 allocate_new_swapdir(&Config
.cacheSwap
);
37 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
38 ++Config
.cacheSwap
.n_configured
;
41 /* TODO make this a cbdata class */
46 searchCallback(void *cbdata
)
54 static bool inited
= false;
59 Config
.Store
.avgObjectSize
= 1024;
60 Config
.Store
.objectsPerBucket
= 20;
61 Config
.Store
.maxObjectSize
= 2048;
63 Config
.store_dir_select_algorithm
= xstrdup("round-robin");
65 Config
.replPolicy
= new RemovalPolicySettings
;
66 Config
.replPolicy
->type
= xstrdup("lru");
69 storeReplAdd("lru", createRemovalPolicy_lru
);
75 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
77 httpReplyInitModule(); /* must go before accepting replies */
83 testUfs::testUfsSearch()
86 * make a valid working ufs swapdir
87 * put two entries in it and sync logs
89 * check the entries we find are what we want
92 if (0 > system ("rm -rf " TESTDIR
))
93 throw std::runtime_error("Failed to clean test work directory");
95 Store::Root(new StoreController
);
97 SwapDirPointer
aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
99 aStore
->IO
= new Fs::Ufs::UFSStrategy(DiskIOModule::Find("Blocking")->createStrategy());
104 mem_policy
= createRemovalPolicy(Config
.replPolicy
);
106 char *path
=xstrdup(TESTDIR
);
108 char *config_line
=xstrdup("100 1 1");
110 visible_appname_string
= xstrdup(PACKAGE
"/" VERSION
);
112 ConfigParser::SetCfgLine(config_line
);
114 aStore
->parse(0, path
);
115 store_maxobjsize
= 1024*1024*2;
119 safe_free(config_line
);
121 /* ok, ready to create */
124 /* ok, ready to use - inits store & hash too */
125 Store::Root().init();
127 /* our swapdir must be scheduled to rebuild */
128 CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding
);
130 /* rebuild is a scheduled event */
133 while (StoreController::store_dirs_rebuilding
)
136 /* cannot use loop.run(); as the loop will never idle: the store-dir
137 * clean() scheduled event prevents it
140 /* nothing left to rebuild */
141 CPPUNIT_ASSERT_EQUAL(0, StoreController::store_dirs_rebuilding
);
145 /* Create "vary" base object */
147 flags
.cachable
= true;
148 StoreEntry
*pe
= storeCreateEntry("dummy url", "dummy log url", flags
, Http::METHOD_GET
);
149 HttpReply
*rep
= (HttpReply
*) pe
->getReply(); // bypass const
150 rep
->setHeaders(Http::scOkay
, "dummy test object", "x-squid-internal/test", 0, -1, squid_curtime
+ 100000);
155 pe
->getReply()->packHeadersInto(pe
);
160 CPPUNIT_ASSERT_EQUAL(0, pe
->swap_dirn
);
161 CPPUNIT_ASSERT_EQUAL(0, pe
->swap_filen
);
162 pe
->unlock("testUfs::testUfsSearch vary");
165 storeDirWriteCleanLogs(0);
167 /* here we cheat: we know that UFSSwapDirs search off disk. If we did an init call to a new
168 * swapdir instance, we'd not be testing a clean build.
170 StoreSearchPointer search
= aStore
->search (NULL
, NULL
); /* search for everything in the store */
172 /* nothing should be immediately available */
175 CPPUNIT_ASSERT_EQUAL(false, search
->next());
178 CPPUNIT_ASSERT_EQUAL(false, search
->error());
179 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
180 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(NULL
), search
->currentItem());
182 /* trigger a callback */
184 search
->next(searchCallback
, NULL
);
185 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
187 /* we should have access to a entry now, that matches the entry we had before */
188 //CPPUNIT_ASSERT_EQUAL(false, search->next());
189 CPPUNIT_ASSERT_EQUAL(false, search
->error());
190 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
191 CPPUNIT_ASSERT(search
->currentItem() != NULL
);
193 /* trigger another callback */
195 search
->next(searchCallback
, NULL
);
196 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
198 /* now we should have no error, we should have finished and have no current item */
199 //CPPUNIT_ASSERT_EQUAL(false, search->next());
200 CPPUNIT_ASSERT_EQUAL(false, search
->error());
201 CPPUNIT_ASSERT_EQUAL(true, search
->isDone());
202 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(NULL
), search
->currentItem());
206 free_cachedir(&Config
.cacheSwap
);
208 /* todo: here we should test a dirty rebuild */
210 safe_free(Config
.replPolicy
->type
);
211 delete Config
.replPolicy
;
213 if (0 > system ("rm -rf " TESTDIR
))
214 throw std::runtime_error("Failed to clean test work directory");
217 /* The UFS store should always configure an IO engine even if none is
218 * supplied on the configuration line.
221 testUfs::testUfsDefaultEngine()
223 /* boring common test boilerplate */
224 if (0 > system ("rm -rf " TESTDIR
))
225 throw std::runtime_error("Failed to clean test work directory");
227 // This assertion may fail if previous test cases fail.
228 // Apparently, CPPUNIT_ASSERT* failure may prevent destructors of local
229 // objects such as "StorePointer aRoot" from being called.
230 CPPUNIT_ASSERT(!store_table
); // or StoreHashIndex ctor will abort below
232 Store::Root(new StoreController
);
233 SwapDirPointer
aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
236 Config
.replPolicy
= new RemovalPolicySettings
;
237 Config
.replPolicy
->type
= xstrdup("lru");
238 mem_policy
= createRemovalPolicy(Config
.replPolicy
);
240 char *path
=xstrdup(TESTDIR
);
241 char *config_line
=xstrdup("100 1 1");
242 ConfigParser::SetCfgLine(config_line
);
243 aStore
->parse(0, path
);
245 safe_free(config_line
);
246 CPPUNIT_ASSERT(aStore
->IO
->io
!= NULL
);
249 free_cachedir(&Config
.cacheSwap
);
250 safe_free(Config
.replPolicy
->type
);
251 delete Config
.replPolicy
;
253 if (0 > system ("rm -rf " TESTDIR
))
254 throw std::runtime_error("Failed to clean test work directory");