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"
14 #include "testStoreSupport.h"
21 #define TESTDIR "testUfs__testUfsSearch"
23 CPPUNIT_TEST_SUITE_REGISTRATION( testUfs
);
25 typedef RefCount
<Fs::Ufs::UFSSwapDir
> SwapDirPointer
;
26 extern REMOVALPOLICYCREATE createRemovalPolicy_lru
; /* XXX fails with --enable-removal-policies=heap */
29 addSwapDir(SwapDirPointer aStore
)
31 allocate_new_swapdir(&Config
.cacheSwap
);
32 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
33 ++Config
.cacheSwap
.n_configured
;
36 /* TODO make this a cbdata class */
41 searchCallback(void *cbdata
)
49 static bool inited
= false;
54 Config
.Store
.avgObjectSize
= 1024;
56 Config
.Store
.objectsPerBucket
= 20;
58 Config
.Store
.maxObjectSize
= 2048;
60 Config
.store_dir_select_algorithm
= xstrdup("round-robin");
62 Config
.replPolicy
= new RemovalPolicySettings
;
64 Config
.replPolicy
->type
= xstrdup ("lru");
67 storeReplAdd("lru", createRemovalPolicy_lru
);
73 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
75 httpReplyInitModule(); /* must go before accepting replies */
81 testUfs::testUfsSearch()
84 * make a valid working ufs swapdir
85 * put two entries in it and sync logs
87 * check the entries we find are what we want
90 if (0 > system ("rm -rf " TESTDIR
))
91 throw std::runtime_error("Failed to clean test work directory");
93 Store::Root(new StoreController
);
95 SwapDirPointer
aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
97 aStore
->IO
= new Fs::Ufs::UFSStrategy(DiskIOModule::Find("Blocking")->createStrategy());
102 mem_policy
= createRemovalPolicy(Config
.replPolicy
);
104 char *path
=xstrdup(TESTDIR
);
106 char *config_line
=xstrdup("foo 100 1 1");
108 visible_appname_string
= xstrdup(PACKAGE
"/" VERSION
);
110 strtok(config_line
, w_space
);
112 aStore
->parse(0, path
);
116 safe_free(config_line
);
118 /* ok, ready to create */
121 /* ok, ready to use - inits store & hash too */
122 Store::Root().init();
124 /* our swapdir must be scheduled to rebuild */
125 CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding
);
127 /* rebuild is a scheduled event */
130 while (StoreController::store_dirs_rebuilding
> 1)
133 /* cannot use loop.run(); as the loop will never idle: the store-dir
134 * clean() scheduled event prevents it
137 /* nothing left to rebuild */
138 CPPUNIT_ASSERT_EQUAL(1, StoreController::store_dirs_rebuilding
);
142 /* Create "vary" base object */
145 StoreEntry
*pe
= storeCreateEntry("dummy url", "dummy log url", flags
, METHOD_GET
);
146 HttpReply
*rep
= (HttpReply
*) pe
->getReply(); // bypass const
147 rep
->setHeaders(HTTP_OK
, "dummy test object", "x-squid-internal/test", -1, -1, squid_curtime
+ 100000);
152 /* TODO: remove this when the metadata is separated */
155 packerToStoreInit(&p
, pe
);
156 pe
->getReply()->packHeadersInto(&p
);
164 CPPUNIT_ASSERT(pe
->swap_dirn
== 0);
165 CPPUNIT_ASSERT(pe
->swap_filen
== 0);
169 storeDirWriteCleanLogs(0);
171 /* here we cheat: we know that UFSSwapDirs search off disk. If we did an init call to a new
172 * swapdir instance, we'd not be testing a clean build.
174 StoreSearchPointer search
= aStore
->search (NULL
, NULL
); /* search for everything in the store */
176 /* nothing should be immediately available */
179 CPPUNIT_ASSERT(search
->next() == false);
182 CPPUNIT_ASSERT(search
->error() == false);
183 CPPUNIT_ASSERT(search
->isDone() == false);
184 CPPUNIT_ASSERT(search
->currentItem() == NULL
);
186 /* trigger a callback */
188 search
->next(searchCallback
, NULL
);
189 CPPUNIT_ASSERT(cbcalled
== true);
191 /* we should have access to a entry now, that matches the entry we had before */
192 //CPPUNIT_ASSERT(search->next() == false);
193 CPPUNIT_ASSERT(search
->error() == false);
194 CPPUNIT_ASSERT(search
->isDone() == false);
195 CPPUNIT_ASSERT(search
->currentItem() != NULL
);
197 /* trigger another callback */
199 search
->next(searchCallback
, NULL
);
200 CPPUNIT_ASSERT(cbcalled
== true);
202 /* now we should have no error, we should have finished and have no current item */
203 //CPPUNIT_ASSERT(search->next() == false);
204 CPPUNIT_ASSERT(search
->error() == false);
205 CPPUNIT_ASSERT(search
->isDone() == true);
206 CPPUNIT_ASSERT(search
->currentItem() == NULL
);
210 free_cachedir(&Config
.cacheSwap
);
212 /* todo: here we should test a dirty rebuild */
214 safe_free(Config
.replPolicy
->type
);
215 delete Config
.replPolicy
;
217 if (0 > system ("rm -rf " TESTDIR
))
218 throw std::runtime_error("Failed to clean test work directory");
221 /* The UFS store should always configure an IO engine even if none is
222 * supplied on the configuration line.
225 testUfs::testUfsDefaultEngine()
227 /* boring common test boilerplate */
228 if (0 > system ("rm -rf " TESTDIR
))
229 throw std::runtime_error("Failed to clean test work directory");
231 // This assertion may fail if previous test cases fail.
232 // Apparently, CPPUNIT_ASSERT* failure may prevent destructors of local
233 // objects such as "StorePointer aRoot" from being called.
234 CPPUNIT_ASSERT(!store_table
); // or StoreHashIndex ctor will abort below
236 Store::Root(new StoreController
);
237 SwapDirPointer
aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
240 Config
.replPolicy
= new RemovalPolicySettings
;
241 Config
.replPolicy
->type
= xstrdup ("lru");
242 mem_policy
= createRemovalPolicy(Config
.replPolicy
);
244 char *path
=xstrdup(TESTDIR
);
245 char *config_line
=xstrdup("foo 100 1 1");
246 strtok(config_line
, w_space
);
247 aStore
->parse(0, path
);
249 safe_free(config_line
);
250 CPPUNIT_ASSERT(aStore
->IO
->io
!= NULL
);
253 free_cachedir(&Config
.cacheSwap
);
254 safe_free(Config
.replPolicy
->type
);
255 delete Config
.replPolicy
;
257 if (0 > system ("rm -rf " TESTDIR
))
258 throw std::runtime_error("Failed to clean test work directory");