1 #define SQUID_UNIT_TEST 1
6 #include "DiskIO/DiskIOModule.h"
7 #include "fs/ufs/ufscommon.h"
8 #include "fs/null/store_null.h"
10 #include "MemObject.h"
11 #include "HttpHeader.h"
12 #include "HttpReply.h"
13 #include "StoreFileSystem.h"
14 #include "testStoreSupport.h"
20 #define TESTDIR "testNull__testNullSearch"
22 CPPUNIT_TEST_SUITE_REGISTRATION( testNull
);
24 typedef RefCount
<NullSwapDir
> SwapDirPointer
;
25 extern REMOVALPOLICYCREATE createRemovalPolicy_lru
;
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
;
36 testNull::commonInit()
38 static bool inited
= false;
43 StoreFileSystem::SetupAllFs();
45 Config
.Store
.avgObjectSize
= 1024;
47 Config
.Store
.objectsPerBucket
= 20;
49 Config
.Store
.maxObjectSize
= 2048;
51 Config
.store_dir_select_algorithm
= xstrdup("round-robin");
53 Config
.replPolicy
= new RemovalPolicySettings
;
55 Config
.replPolicy
->type
= xstrdup ("lru");
57 Config
.replPolicy
->args
= NULL
;
60 storeReplAdd("lru", createRemovalPolicy_lru
);
62 visible_appname_string
= xstrdup(APP_FULLNAME
);
68 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
70 httpReplyInitModule(); /* must go before accepting replies */
72 mem_policy
= createRemovalPolicy(Config
.replPolicy
);
78 testNull::testNullCreate()
80 StorePointer
aRoot (new StoreController
);
82 SwapDirPointer
aStore (new NullSwapDir());
87 char *path
=xstrdup(TESTDIR
);
88 char *config_line
=xstrdup("foo");
89 strtok(config_line
, w_space
);
90 aStore
->parse(0, path
);
92 safe_free(config_line
);
94 /* ok, ready to create */
97 free_cachedir(&Config
.cacheSwap
);
100 /* todo: here we should test a dirty rebuild */
102 // safe_free(Config.replPolicy->type);
103 // delete Config.replPolicy;
106 /* TODO make this a cbdata class */
108 static bool cbcalled
;
111 searchCallback(void *cbdata
)
117 testNull::testNullSearch()
120 * make a valid working ufs swapdir
121 * put two entries in it and sync logs
123 * check the entries we find are what we want
125 StorePointer
aRoot (new StoreController
);
127 SwapDirPointer
aStore (new NullSwapDir());
132 char *path
=xstrdup(TESTDIR
);
133 char *config_line
=xstrdup("foo");
134 strtok(config_line
, w_space
);
135 aStore
->parse(0, path
);
137 safe_free(config_line
);
139 /* ok, ready to create */
142 /* ok, ready to use */
143 Store::Root().init();
145 /* rebuild is a scheduled event */
148 /* our swapdir must be scheduled to rebuild - though it does not
149 * make sense to rebuild Null stores at all. store_dirs_rebuilding
150 * is initialized to _1_ and adding our swapdir makes it 2.
152 CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding
);
156 /* nothing left to rebuild */
157 CPPUNIT_ASSERT_EQUAL(1, StoreController::store_dirs_rebuilding
);
161 /* Create "vary" base object */
164 StoreEntry
*pe
= storeCreateEntry("dummy url", "dummy log url", flags
, METHOD_GET
);
165 /* We are allowed to do this typecast */
166 HttpReply
*rep
= (HttpReply
*) pe
->getReply(); // bypass const
167 rep
->setHeaders(HTTP_OK
, "dummy test object", "x-squid-internal/test", -1, -1, squid_curtime
+ 100000);
172 /* TODO: remove this when the metadata is separated */
175 packerToStoreInit(&p
, pe
);
176 pe
->getReply()->packHeadersInto(&p
);
184 /* Null does not accept store entries */
185 CPPUNIT_ASSERT(pe
->swap_dirn
== -1);
189 storeDirWriteCleanLogs(0);
191 /* here we cheat: we know that UFSSwapDirs search off disk. If we did an init call to a new
192 * swapdir instance, we'd not be testing a clean build.
194 StoreSearchPointer search
= aStore
->search (NULL
, NULL
); /* search for everything in the store */
196 /* nothing should be available */
197 CPPUNIT_ASSERT(search
->error() == false);
198 CPPUNIT_ASSERT(search
->isDone() == true);
199 CPPUNIT_ASSERT(search
->currentItem() == NULL
);
200 CPPUNIT_ASSERT(search
->next() == false);
202 /* trigger a callback */
204 search
->next(searchCallback
, NULL
);
205 CPPUNIT_ASSERT(cbcalled
== true);
208 CPPUNIT_ASSERT(search
->error() == false);
209 CPPUNIT_ASSERT(search
->isDone() == true);
210 CPPUNIT_ASSERT(search
->currentItem() == NULL
);
211 CPPUNIT_ASSERT(search
->next() == false);
213 free_cachedir(&Config
.cacheSwap
);
216 //TODO: do this once, or each time. safe_free(Config.replPolicy->type);
217 // delete Config.replPolicy;