]>
Commit | Line | Data |
---|---|---|
8e46cda5 | 1 | #define SQUID_UNIT_TEST 1 |
f7f3304a | 2 | #include "squid-old.h" |
c8f4eac4 | 3 | #include "testNull.h" |
c8f4eac4 | 4 | #include "Store.h" |
5 | #include "SwapDir.h" | |
6 | #include "DiskIO/DiskIOModule.h" | |
7 | #include "fs/ufs/ufscommon.h" | |
8 | #include "fs/null/store_null.h" | |
9 | #include "Mem.h" | |
aa839030 | 10 | #include "MemObject.h" |
c8f4eac4 | 11 | #include "HttpHeader.h" |
12 | #include "HttpReply.h" | |
13 | #include "StoreFileSystem.h" | |
8ff3fa2e | 14 | #include "testStoreSupport.h" |
c8f4eac4 | 15 | |
27e059d4 AJ |
16 | #if HAVE_STDEXCEPT |
17 | #include <stdexcept> | |
18 | #endif | |
19 | ||
2e050051 | 20 | #define TESTDIR "testNull__testNullSearch" |
21 | ||
c8f4eac4 | 22 | CPPUNIT_TEST_SUITE_REGISTRATION( testNull ); |
23 | ||
24 | typedef RefCount<NullSwapDir> SwapDirPointer; | |
25 | extern REMOVALPOLICYCREATE createRemovalPolicy_lru; | |
26 | ||
27 | static void | |
28 | addSwapDir(SwapDirPointer aStore) | |
29 | { | |
30 | allocate_new_swapdir(&Config.cacheSwap); | |
31 | Config.cacheSwap.swapDirs[Config.cacheSwap.n_configured] = aStore.getRaw(); | |
32 | ++Config.cacheSwap.n_configured; | |
33 | } | |
34 | ||
35 | void | |
36 | testNull::commonInit() | |
37 | { | |
38 | static bool inited = false; | |
39 | ||
40 | if (inited) | |
41 | return; | |
42 | ||
43 | StoreFileSystem::SetupAllFs(); | |
44 | ||
45 | Config.Store.avgObjectSize = 1024; | |
46 | ||
47 | Config.Store.objectsPerBucket = 20; | |
48 | ||
49 | Config.Store.maxObjectSize = 2048; | |
50 | ||
51 | Config.store_dir_select_algorithm = xstrdup("round-robin"); | |
52 | ||
53 | Config.replPolicy = new RemovalPolicySettings; | |
54 | ||
55 | Config.replPolicy->type = xstrdup ("lru"); | |
56 | ||
57 | Config.replPolicy->args = NULL; | |
58 | ||
59 | /* garh garh */ | |
60 | storeReplAdd("lru", createRemovalPolicy_lru); | |
61 | ||
7dbca7a4 | 62 | visible_appname_string = xstrdup(APP_FULLNAME); |
474dfa84 | 63 | |
c8f4eac4 | 64 | Mem::Init(); |
65 | ||
c8f4eac4 | 66 | comm_init(); |
67 | ||
68 | httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */ | |
69 | ||
70 | httpReplyInitModule(); /* must go before accepting replies */ | |
71 | ||
72 | mem_policy = createRemovalPolicy(Config.replPolicy); | |
73 | ||
74 | inited = true; | |
75 | } | |
76 | ||
77 | void | |
78 | testNull::testNullCreate() | |
79 | { | |
80 | StorePointer aRoot (new StoreController); | |
81 | Store::Root(aRoot); | |
82 | SwapDirPointer aStore (new NullSwapDir()); | |
83 | addSwapDir(aStore); | |
84 | ||
85 | commonInit(); | |
86 | ||
2e050051 | 87 | char *path=xstrdup(TESTDIR); |
c8f4eac4 | 88 | char *config_line=xstrdup("foo"); |
89 | strtok(config_line, w_space); | |
90 | aStore->parse(0, path); | |
91 | safe_free(path); | |
92 | safe_free(config_line); | |
93 | ||
94 | /* ok, ready to create */ | |
95 | aStore->create(); | |
96 | ||
97 | free_cachedir(&Config.cacheSwap); | |
98 | Store::Root(NULL); | |
99 | ||
100 | /* todo: here we should test a dirty rebuild */ | |
101 | ||
102 | // safe_free(Config.replPolicy->type); | |
103 | // delete Config.replPolicy; | |
104 | } | |
105 | ||
106 | /* TODO make this a cbdata class */ | |
107 | ||
108 | static bool cbcalled; | |
109 | ||
110 | static void | |
111 | searchCallback(void *cbdata) | |
112 | { | |
113 | cbcalled = true; | |
114 | } | |
115 | ||
116 | void | |
117 | testNull::testNullSearch() | |
118 | { | |
119 | /* test sequence | |
120 | * make a valid working ufs swapdir | |
121 | * put two entries in it and sync logs | |
122 | * search the ufs dir | |
123 | * check the entries we find are what we want | |
124 | */ | |
125 | StorePointer aRoot (new StoreController); | |
126 | Store::Root(aRoot); | |
127 | SwapDirPointer aStore (new NullSwapDir()); | |
128 | addSwapDir(aStore); | |
129 | ||
130 | commonInit(); | |
131 | ||
2e050051 | 132 | char *path=xstrdup(TESTDIR); |
c8f4eac4 | 133 | char *config_line=xstrdup("foo"); |
134 | strtok(config_line, w_space); | |
135 | aStore->parse(0, path); | |
136 | safe_free(path); | |
137 | safe_free(config_line); | |
138 | ||
139 | /* ok, ready to create */ | |
140 | aStore->create(); | |
141 | ||
142 | /* ok, ready to use */ | |
143 | Store::Root().init(); | |
144 | ||
8ff3fa2e | 145 | /* rebuild is a scheduled event */ |
146 | StockEventLoop loop; | |
c8f4eac4 | 147 | |
bef81ea5 | 148 | /* our swapdir must be scheduled to rebuild - though it does not |
f463e774 | 149 | * make sense to rebuild Null stores at all. store_dirs_rebuilding |
150 | * is initialized to _1_ and adding our swapdir makes it 2. | |
bef81ea5 | 151 | */ |
f463e774 | 152 | CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding); |
bef81ea5 | 153 | |
aa839030 | 154 | loop.run(); |
c8f4eac4 | 155 | |
bef81ea5 | 156 | /* nothing left to rebuild */ |
f463e774 | 157 | CPPUNIT_ASSERT_EQUAL(1, StoreController::store_dirs_rebuilding); |
c8f4eac4 | 158 | |
159 | /* add an entry */ | |
160 | { | |
161 | /* Create "vary" base object */ | |
162 | request_flags flags; | |
163 | flags.cachable = 1; | |
164 | StoreEntry *pe = storeCreateEntry("dummy url", "dummy log url", flags, METHOD_GET); | |
c8f4eac4 | 165 | /* We are allowed to do this typecast */ |
f46fe759 | 166 | HttpReply *rep = (HttpReply *) pe->getReply(); // bypass const |
11992b6f | 167 | rep->setHeaders(HTTP_OK, "dummy test object", "x-squid-internal/test", -1, -1, squid_curtime + 100000); |
c8f4eac4 | 168 | |
d88e3c49 | 169 | pe->setPublicKey(); |
c8f4eac4 | 170 | |
3900307b | 171 | pe->buffer(); |
c8f4eac4 | 172 | /* TODO: remove this when the metadata is separated */ |
173 | { | |
174 | Packer p; | |
175 | packerToStoreInit(&p, pe); | |
f46fe759 | 176 | pe->getReply()->packHeadersInto(&p); |
c8f4eac4 | 177 | packerClean(&p); |
178 | } | |
179 | ||
3900307b | 180 | pe->flush(); |
181 | pe->timestampsSet(); | |
c8f4eac4 | 182 | pe->complete(); |
f0d1a2b9 | 183 | pe->swapOut(); |
c8f4eac4 | 184 | /* Null does not accept store entries */ |
185 | CPPUNIT_ASSERT(pe->swap_dirn == -1); | |
97b5e68f | 186 | pe->unlock(); |
c8f4eac4 | 187 | } |
188 | ||
189 | storeDirWriteCleanLogs(0); | |
190 | ||
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. | |
193 | */ | |
194 | StoreSearchPointer search = aStore->search (NULL, NULL); /* search for everything in the store */ | |
195 | ||
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); | |
201 | ||
202 | /* trigger a callback */ | |
203 | cbcalled = false; | |
204 | search->next(searchCallback, NULL); | |
205 | CPPUNIT_ASSERT(cbcalled == true); | |
206 | ||
207 | /* still nothing */ | |
208 | CPPUNIT_ASSERT(search->error() == false); | |
209 | CPPUNIT_ASSERT(search->isDone() == true); | |
210 | CPPUNIT_ASSERT(search->currentItem() == NULL); | |
211 | CPPUNIT_ASSERT(search->next() == false); | |
212 | ||
213 | free_cachedir(&Config.cacheSwap); | |
214 | Store::Root(NULL); | |
215 | ||
216 | //TODO: do this once, or each time. safe_free(Config.replPolicy->type); | |
217 | // delete Config.replPolicy; | |
218 | } |