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"
20 #define TESTDIR "testUfs_Store"
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;
54 Config
.Store
.objectsPerBucket
= 20;
55 Config
.Store
.maxObjectSize
= 2048;
57 Config
.store_dir_select_algorithm
= xstrdup("round-robin");
59 Config
.replPolicy
= new RemovalPolicySettings
;
60 Config
.replPolicy
->type
= xstrdup("lru");
63 storeReplAdd("lru", createRemovalPolicy_lru
);
69 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
71 httpReplyInitModule(); /* must go before accepting replies */
77 testUfs::testUfsSearch()
80 * make a valid working ufs swapdir
81 * put two entries in it and sync logs
83 * check the entries we find are what we want
86 if (0 > system ("rm -rf " TESTDIR
))
87 throw std::runtime_error("Failed to clean test work directory");
89 Store::Root(new StoreController
);
91 SwapDirPointer
aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
93 aStore
->IO
= new Fs::Ufs::UFSStrategy(DiskIOModule::Find("Blocking")->createStrategy());
98 mem_policy
= createRemovalPolicy(Config
.replPolicy
);
100 char *path
=xstrdup(TESTDIR
);
102 char *config_line
=xstrdup("100 1 1");
104 visible_appname_string
= xstrdup(PACKAGE
"/" VERSION
);
106 ConfigParser::SetCfgLine(config_line
);
108 aStore
->parse(0, path
);
109 store_maxobjsize
= 1024*1024*2;
113 safe_free(config_line
);
115 /* ok, ready to create */
118 /* ok, ready to use - inits store & hash too */
119 Store::Root().init();
121 /* our swapdir must be scheduled to rebuild */
122 CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding
);
124 /* rebuild is a scheduled event */
127 while (StoreController::store_dirs_rebuilding
)
130 /* cannot use loop.run(); as the loop will never idle: the store-dir
131 * clean() scheduled event prevents it
134 /* nothing left to rebuild */
135 CPPUNIT_ASSERT_EQUAL(0, StoreController::store_dirs_rebuilding
);
139 /* Create "vary" base object */
141 flags
.cachable
= true;
142 StoreEntry
*pe
= storeCreateEntry("dummy url", "dummy log url", flags
, Http::METHOD_GET
);
143 HttpReply
*rep
= (HttpReply
*) pe
->getReply(); // bypass const
144 rep
->setHeaders(Http::scOkay
, "dummy test object", "x-squid-internal/test", 0, -1, squid_curtime
+ 100000);
149 /* TODO: remove this when the metadata is separated */
152 packerToStoreInit(&p
, pe
);
153 pe
->getReply()->packHeadersInto(&p
);
161 CPPUNIT_ASSERT_EQUAL(0, pe
->swap_dirn
);
162 CPPUNIT_ASSERT_EQUAL(0, pe
->swap_filen
);
163 pe
->unlock("testUfs::testUfsSearch vary");
166 storeDirWriteCleanLogs(0);
168 /* here we cheat: we know that UFSSwapDirs search off disk. If we did an init call to a new
169 * swapdir instance, we'd not be testing a clean build.
171 StoreSearchPointer search
= aStore
->search (NULL
, NULL
); /* search for everything in the store */
173 /* nothing should be immediately available */
176 CPPUNIT_ASSERT_EQUAL(false, search
->next());
179 CPPUNIT_ASSERT_EQUAL(false, search
->error());
180 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
181 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(NULL
), search
->currentItem());
183 /* trigger a callback */
185 search
->next(searchCallback
, NULL
);
186 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
188 /* we should have access to a entry now, that matches the entry we had before */
189 //CPPUNIT_ASSERT_EQUAL(false, search->next());
190 CPPUNIT_ASSERT_EQUAL(false, search
->error());
191 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
192 CPPUNIT_ASSERT(search
->currentItem() != NULL
);
194 /* trigger another callback */
196 search
->next(searchCallback
, NULL
);
197 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
199 /* now we should have no error, we should have finished and have no current item */
200 //CPPUNIT_ASSERT_EQUAL(false, search->next());
201 CPPUNIT_ASSERT_EQUAL(false, search
->error());
202 CPPUNIT_ASSERT_EQUAL(true, search
->isDone());
203 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(NULL
), search
->currentItem());
207 free_cachedir(&Config
.cacheSwap
);
209 /* todo: here we should test a dirty rebuild */
211 safe_free(Config
.replPolicy
->type
);
212 delete Config
.replPolicy
;
214 if (0 > system ("rm -rf " TESTDIR
))
215 throw std::runtime_error("Failed to clean test work directory");
218 /* The UFS store should always configure an IO engine even if none is
219 * supplied on the configuration line.
222 testUfs::testUfsDefaultEngine()
224 /* boring common test boilerplate */
225 if (0 > system ("rm -rf " TESTDIR
))
226 throw std::runtime_error("Failed to clean test work directory");
228 // This assertion may fail if previous test cases fail.
229 // Apparently, CPPUNIT_ASSERT* failure may prevent destructors of local
230 // objects such as "StorePointer aRoot" from being called.
231 CPPUNIT_ASSERT(!store_table
); // or StoreHashIndex ctor will abort below
233 Store::Root(new StoreController
);
234 SwapDirPointer
aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
237 Config
.replPolicy
= new RemovalPolicySettings
;
238 Config
.replPolicy
->type
= xstrdup("lru");
239 mem_policy
= createRemovalPolicy(Config
.replPolicy
);
241 char *path
=xstrdup(TESTDIR
);
242 char *config_line
=xstrdup("100 1 1");
243 ConfigParser::SetCfgLine(config_line
);
244 aStore
->parse(0, path
);
246 safe_free(config_line
);
247 CPPUNIT_ASSERT(aStore
->IO
->io
!= NULL
);
250 free_cachedir(&Config
.cacheSwap
);
251 safe_free(Config
.replPolicy
->type
);
252 delete Config
.replPolicy
;
254 if (0 > system ("rm -rf " TESTDIR
))
255 throw std::runtime_error("Failed to clean test work directory");