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"
13 #include "testStoreSupport.h"
20 #define TESTDIR "testUfs__testUfsSearch"
22 CPPUNIT_TEST_SUITE_REGISTRATION( testUfs
);
24 typedef RefCount
<Fs::Ufs::UFSSwapDir
> SwapDirPointer
;
25 extern REMOVALPOLICYCREATE createRemovalPolicy_lru
; /* XXX fails with --enable-removal-policies=heap */
28 addSwapDir(SwapDirPointer aStore
)
30 allocate_new_swapdir(&Config
.cacheSwap
);
31 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
32 ++Config
.cacheSwap
.n_configured
;
35 /* TODO make this a cbdata class */
40 searchCallback(void *cbdata
)
48 static bool inited
= false;
53 Config
.Store
.avgObjectSize
= 1024;
55 Config
.Store
.objectsPerBucket
= 20;
57 Config
.Store
.maxObjectSize
= 2048;
59 Config
.store_dir_select_algorithm
= xstrdup("round-robin");
61 Config
.replPolicy
= new RemovalPolicySettings
;
63 Config
.replPolicy
->type
= xstrdup ("lru");
66 storeReplAdd("lru", createRemovalPolicy_lru
);
72 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
74 httpReplyInitModule(); /* must go before accepting replies */
80 testUfs::testUfsSearch()
83 * make a valid working ufs swapdir
84 * put two entries in it and sync logs
86 * check the entries we find are what we want
89 if (0 > system ("rm -rf " TESTDIR
))
90 throw std::runtime_error("Failed to clean test work directory");
92 Store::Root(new StoreController
);
94 SwapDirPointer
aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
96 aStore
->IO
= new Fs::Ufs::UFSStrategy(DiskIOModule::Find("Blocking")->createStrategy());
101 mem_policy
= createRemovalPolicy(Config
.replPolicy
);
103 char *path
=xstrdup(TESTDIR
);
105 char *config_line
=xstrdup("foo 100 1 1");
107 visible_appname_string
= xstrdup(PACKAGE
"/" VERSION
);
109 strtok(config_line
, w_space
);
111 aStore
->parse(0, path
);
115 safe_free(config_line
);
117 /* ok, ready to create */
120 /* ok, ready to use - inits store & hash too */
121 Store::Root().init();
123 /* our swapdir must be scheduled to rebuild */
124 CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding
);
126 /* rebuild is a scheduled event */
129 while (StoreController::store_dirs_rebuilding
> 1)
132 /* cannot use loop.run(); as the loop will never idle: the store-dir
133 * clean() scheduled event prevents it
136 /* nothing left to rebuild */
137 CPPUNIT_ASSERT_EQUAL(1, StoreController::store_dirs_rebuilding
);
141 /* Create "vary" base object */
144 StoreEntry
*pe
= storeCreateEntry("dummy url", "dummy log url", flags
, METHOD_GET
);
145 HttpReply
*rep
= (HttpReply
*) pe
->getReply(); // bypass const
146 rep
->setHeaders(HTTP_OK
, "dummy test object", "x-squid-internal/test", -1, -1, squid_curtime
+ 100000);
151 /* TODO: remove this when the metadata is separated */
154 packerToStoreInit(&p
, pe
);
155 pe
->getReply()->packHeadersInto(&p
);
163 CPPUNIT_ASSERT(pe
->swap_dirn
== 0);
164 CPPUNIT_ASSERT(pe
->swap_filen
== 0);
168 storeDirWriteCleanLogs(0);
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.
173 StoreSearchPointer search
= aStore
->search (NULL
, NULL
); /* search for everything in the store */
175 /* nothing should be immediately available */
178 CPPUNIT_ASSERT(search
->next() == false);
181 CPPUNIT_ASSERT(search
->error() == false);
182 CPPUNIT_ASSERT(search
->isDone() == false);
183 CPPUNIT_ASSERT(search
->currentItem() == NULL
);
185 /* trigger a callback */
187 search
->next(searchCallback
, NULL
);
188 CPPUNIT_ASSERT(cbcalled
== true);
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
);
196 /* trigger another callback */
198 search
->next(searchCallback
, NULL
);
199 CPPUNIT_ASSERT(cbcalled
== true);
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
);
209 free_cachedir(&Config
.cacheSwap
);
211 /* todo: here we should test a dirty rebuild */
213 safe_free(Config
.replPolicy
->type
);
214 delete Config
.replPolicy
;
216 if (0 > system ("rm -rf " TESTDIR
))
217 throw std::runtime_error("Failed to clean test work directory");
220 /* The UFS store should always configure an IO engine even if none is
221 * supplied on the configuration line.
224 testUfs::testUfsDefaultEngine()
226 /* boring common test boilerplate */
227 if (0 > system ("rm -rf " TESTDIR
))
228 throw std::runtime_error("Failed to clean test work directory");
230 // This assertion may fail if previous test cases fail.
231 // Apparently, CPPUNIT_ASSERT* failure may prevent destructors of local
232 // objects such as "StorePointer aRoot" from being called.
233 CPPUNIT_ASSERT(!store_table
); // or StoreHashIndex ctor will abort below
235 Store::Root(new StoreController
);
236 SwapDirPointer
aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
239 Config
.replPolicy
= new RemovalPolicySettings
;
240 Config
.replPolicy
->type
= xstrdup ("lru");
241 mem_policy
= createRemovalPolicy(Config
.replPolicy
);
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
);
248 safe_free(config_line
);
249 CPPUNIT_ASSERT(aStore
->IO
->io
!= NULL
);
252 free_cachedir(&Config
.cacheSwap
);
253 safe_free(Config
.replPolicy
->type
);
254 delete Config
.replPolicy
;
256 if (0 > system ("rm -rf " TESTDIR
))
257 throw std::runtime_error("Failed to clean test work directory");