1 #define SQUID_UNIT_TEST 1
6 #include "DiskIO/DiskIOModule.h"
7 #include "fs/null/store_null.h"
10 #include "HttpHeader.h"
11 #include "HttpReply.h"
12 #include "StoreFileSystem.h"
13 #include "testStoreSupport.h"
19 #define TESTDIR "testNull__testNullSearch"
21 CPPUNIT_TEST_SUITE_REGISTRATION( testNull
);
23 typedef RefCount
<NullSwapDir
> SwapDirPointer
;
24 extern REMOVALPOLICYCREATE createRemovalPolicy_lru
;
27 addSwapDir(SwapDirPointer aStore
)
29 allocate_new_swapdir(&Config
.cacheSwap
);
30 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
31 ++Config
.cacheSwap
.n_configured
;
35 testNull::commonInit()
37 static bool inited
= false;
42 StoreFileSystem::SetupAllFs();
44 Config
.Store
.avgObjectSize
= 1024;
46 Config
.Store
.objectsPerBucket
= 20;
48 Config
.Store
.maxObjectSize
= 2048;
50 Config
.store_dir_select_algorithm
= xstrdup("round-robin");
52 Config
.replPolicy
= new RemovalPolicySettings
;
54 Config
.replPolicy
->type
= xstrdup ("lru");
56 Config
.replPolicy
->args
= NULL
;
59 storeReplAdd("lru", createRemovalPolicy_lru
);
61 visible_appname_string
= xstrdup(APP_FULLNAME
);
67 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
69 httpReplyInitModule(); /* must go before accepting replies */
71 mem_policy
= createRemovalPolicy(Config
.replPolicy
);
77 testNull::testNullCreate()
79 StorePointer
aRoot (new StoreController
);
81 SwapDirPointer
aStore (new NullSwapDir());
86 char *path
=xstrdup(TESTDIR
);
87 char *config_line
=xstrdup("foo");
88 strtok(config_line
, w_space
);
89 aStore
->parse(0, path
);
91 safe_free(config_line
);
93 /* ok, ready to create */
96 free_cachedir(&Config
.cacheSwap
);
99 /* todo: here we should test a dirty rebuild */
101 // safe_free(Config.replPolicy->type);
102 // delete Config.replPolicy;
105 /* TODO make this a cbdata class */
107 static bool cbcalled
;
110 searchCallback(void *cbdata
)
116 testNull::testNullSearch()
119 * make a valid working ufs swapdir
120 * put two entries in it and sync logs
122 * check the entries we find are what we want
124 StorePointer
aRoot (new StoreController
);
126 SwapDirPointer
aStore (new NullSwapDir());
131 char *path
=xstrdup(TESTDIR
);
132 char *config_line
=xstrdup("foo");
133 strtok(config_line
, w_space
);
134 aStore
->parse(0, path
);
136 safe_free(config_line
);
138 /* ok, ready to create */
141 /* ok, ready to use */
142 Store::Root().init();
144 /* rebuild is a scheduled event */
147 /* our swapdir must be scheduled to rebuild - though it does not
148 * make sense to rebuild Null stores at all. store_dirs_rebuilding
149 * is initialized to _1_ and adding our swapdir makes it 2.
151 CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding
);
155 /* nothing left to rebuild */
156 CPPUNIT_ASSERT_EQUAL(1, StoreController::store_dirs_rebuilding
);
160 /* Create "vary" base object */
163 StoreEntry
*pe
= storeCreateEntry("dummy url", "dummy log url", flags
, METHOD_GET
);
164 /* We are allowed to do this typecast */
165 HttpReply
*rep
= (HttpReply
*) pe
->getReply(); // bypass const
166 rep
->setHeaders(HTTP_OK
, "dummy test object", "x-squid-internal/test", -1, -1, squid_curtime
+ 100000);
171 /* TODO: remove this when the metadata is separated */
174 packerToStoreInit(&p
, pe
);
175 pe
->getReply()->packHeadersInto(&p
);
183 /* Null does not accept store entries */
184 CPPUNIT_ASSERT(pe
->swap_dirn
== -1);
188 storeDirWriteCleanLogs(0);
190 /* here we cheat: we know that UFSSwapDirs search off disk. If we did an init call to a new
191 * swapdir instance, we'd not be testing a clean build.
193 StoreSearchPointer search
= aStore
->search (NULL
, NULL
); /* search for everything in the store */
195 /* nothing should be available */
196 CPPUNIT_ASSERT(search
->error() == false);
197 CPPUNIT_ASSERT(search
->isDone() == true);
198 CPPUNIT_ASSERT(search
->currentItem() == NULL
);
199 CPPUNIT_ASSERT(search
->next() == false);
201 /* trigger a callback */
203 search
->next(searchCallback
, NULL
);
204 CPPUNIT_ASSERT(cbcalled
== true);
207 CPPUNIT_ASSERT(search
->error() == false);
208 CPPUNIT_ASSERT(search
->isDone() == true);
209 CPPUNIT_ASSERT(search
->currentItem() == NULL
);
210 CPPUNIT_ASSERT(search
->next() == false);
212 free_cachedir(&Config
.cacheSwap
);
215 //TODO: do this once, or each time. safe_free(Config.replPolicy->type);
216 // delete Config.replPolicy;