1 #define SQUID_UNIT_TEST 1
4 #include "DiskIO/DiskIOModule.h"
5 #include "fs/ufs/UFSSwapDir.h"
7 #include "HttpHeader.h"
10 #include "MemObject.h"
11 #include "RequestFlags.h"
12 #include "SquidConfig.h"
15 #include "testStoreSupport.h"
22 #define TESTDIR "testUfs__testUfsSearch"
24 CPPUNIT_TEST_SUITE_REGISTRATION( testUfs
);
26 typedef RefCount
<Fs::Ufs::UFSSwapDir
> SwapDirPointer
;
27 extern REMOVALPOLICYCREATE createRemovalPolicy_lru
; /* XXX fails with --enable-removal-policies=heap */
30 addSwapDir(SwapDirPointer aStore
)
32 allocate_new_swapdir(&Config
.cacheSwap
);
33 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
34 ++Config
.cacheSwap
.n_configured
;
37 /* TODO make this a cbdata class */
42 searchCallback(void *cbdata
)
50 static bool inited
= false;
55 Config
.Store
.avgObjectSize
= 1024;
57 Config
.Store
.objectsPerBucket
= 20;
59 Config
.Store
.maxObjectSize
= 2048;
61 Config
.store_dir_select_algorithm
= xstrdup("round-robin");
63 Config
.replPolicy
= new RemovalPolicySettings
;
65 Config
.replPolicy
->type
= xstrdup ("lru");
68 storeReplAdd("lru", createRemovalPolicy_lru
);
74 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
76 httpReplyInitModule(); /* must go before accepting replies */
82 testUfs::testUfsSearch()
85 * make a valid working ufs swapdir
86 * put two entries in it and sync logs
88 * check the entries we find are what we want
91 if (0 > system ("rm -rf " TESTDIR
))
92 throw std::runtime_error("Failed to clean test work directory");
94 Store::Root(new StoreController
);
96 SwapDirPointer
aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
98 aStore
->IO
= new Fs::Ufs::UFSStrategy(DiskIOModule::Find("Blocking")->createStrategy());
103 mem_policy
= createRemovalPolicy(Config
.replPolicy
);
105 char *path
=xstrdup(TESTDIR
);
107 char *config_line
=xstrdup("foo 100 1 1");
109 visible_appname_string
= xstrdup(PACKAGE
"/" VERSION
);
111 strtok(config_line
, w_space
);
113 aStore
->parse(0, path
);
117 safe_free(config_line
);
119 /* ok, ready to create */
122 /* ok, ready to use - inits store & hash too */
123 Store::Root().init();
125 /* our swapdir must be scheduled to rebuild */
126 CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding
);
128 /* rebuild is a scheduled event */
131 while (StoreController::store_dirs_rebuilding
> 1)
134 /* cannot use loop.run(); as the loop will never idle: the store-dir
135 * clean() scheduled event prevents it
138 /* nothing left to rebuild */
139 CPPUNIT_ASSERT_EQUAL(1, StoreController::store_dirs_rebuilding
);
143 /* Create "vary" base object */
145 flags
.cachable
= true;
146 StoreEntry
*pe
= storeCreateEntry("dummy url", "dummy log url", flags
, Http::METHOD_GET
);
147 HttpReply
*rep
= (HttpReply
*) pe
->getReply(); // bypass const
148 rep
->setHeaders(HTTP_OK
, "dummy test object", "x-squid-internal/test", -1, -1, squid_curtime
+ 100000);
153 /* TODO: remove this when the metadata is separated */
156 packerToStoreInit(&p
, pe
);
157 pe
->getReply()->packHeadersInto(&p
);
165 CPPUNIT_ASSERT(pe
->swap_dirn
== 0);
166 CPPUNIT_ASSERT(pe
->swap_filen
== 0);
170 storeDirWriteCleanLogs(0);
172 /* here we cheat: we know that UFSSwapDirs search off disk. If we did an init call to a new
173 * swapdir instance, we'd not be testing a clean build.
175 StoreSearchPointer search
= aStore
->search (NULL
, NULL
); /* search for everything in the store */
177 /* nothing should be immediately available */
180 CPPUNIT_ASSERT(search
->next() == false);
183 CPPUNIT_ASSERT(search
->error() == false);
184 CPPUNIT_ASSERT(search
->isDone() == false);
185 CPPUNIT_ASSERT(search
->currentItem() == NULL
);
187 /* trigger a callback */
189 search
->next(searchCallback
, NULL
);
190 CPPUNIT_ASSERT(cbcalled
== true);
192 /* we should have access to a entry now, that matches the entry we had before */
193 //CPPUNIT_ASSERT(search->next() == false);
194 CPPUNIT_ASSERT(search
->error() == false);
195 CPPUNIT_ASSERT(search
->isDone() == false);
196 CPPUNIT_ASSERT(search
->currentItem() != NULL
);
198 /* trigger another callback */
200 search
->next(searchCallback
, NULL
);
201 CPPUNIT_ASSERT(cbcalled
== true);
203 /* now we should have no error, we should have finished and have no current item */
204 //CPPUNIT_ASSERT(search->next() == false);
205 CPPUNIT_ASSERT(search
->error() == false);
206 CPPUNIT_ASSERT(search
->isDone() == true);
207 CPPUNIT_ASSERT(search
->currentItem() == NULL
);
211 free_cachedir(&Config
.cacheSwap
);
213 /* todo: here we should test a dirty rebuild */
215 safe_free(Config
.replPolicy
->type
);
216 delete Config
.replPolicy
;
218 if (0 > system ("rm -rf " TESTDIR
))
219 throw std::runtime_error("Failed to clean test work directory");
222 /* The UFS store should always configure an IO engine even if none is
223 * supplied on the configuration line.
226 testUfs::testUfsDefaultEngine()
228 /* boring common test boilerplate */
229 if (0 > system ("rm -rf " TESTDIR
))
230 throw std::runtime_error("Failed to clean test work directory");
232 // This assertion may fail if previous test cases fail.
233 // Apparently, CPPUNIT_ASSERT* failure may prevent destructors of local
234 // objects such as "StorePointer aRoot" from being called.
235 CPPUNIT_ASSERT(!store_table
); // or StoreHashIndex ctor will abort below
237 Store::Root(new StoreController
);
238 SwapDirPointer
aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
241 Config
.replPolicy
= new RemovalPolicySettings
;
242 Config
.replPolicy
->type
= xstrdup ("lru");
243 mem_policy
= createRemovalPolicy(Config
.replPolicy
);
245 char *path
=xstrdup(TESTDIR
);
246 char *config_line
=xstrdup("foo 100 1 1");
247 strtok(config_line
, w_space
);
248 aStore
->parse(0, path
);
250 safe_free(config_line
);
251 CPPUNIT_ASSERT(aStore
->IO
->io
!= NULL
);
254 free_cachedir(&Config
.cacheSwap
);
255 safe_free(Config
.replPolicy
->type
);
256 delete Config
.replPolicy
;
258 if (0 > system ("rm -rf " TESTDIR
))
259 throw std::runtime_error("Failed to clean test work directory");