]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testStoreHashIndex.cc
Merged from upstream
[thirdparty/squid.git] / src / tests / testStoreHashIndex.cc
1 #define SQUID_UNIT_TEST 1
2
3 #include "squid.h"
4 #include "Mem.h"
5 #include "MemObject.h"
6 #include "SquidConfig.h"
7 #include "SquidTime.h"
8 #include "Store.h"
9 #include "StoreHashIndex.h"
10 #include "StoreSearch.h"
11 #include "SwapDir.h"
12 #include "testStoreHashIndex.h"
13 #include "TestSwapDir.h"
14
15 CPPUNIT_TEST_SUITE_REGISTRATION( testStoreHashIndex );
16
17 static void
18 addSwapDir(TestSwapDirPointer aStore)
19 {
20 allocate_new_swapdir(&Config.cacheSwap);
21 Config.cacheSwap.swapDirs[Config.cacheSwap.n_configured] = aStore.getRaw();
22 ++Config.cacheSwap.n_configured;
23 }
24
25 void
26 testStoreHashIndex::testStats()
27 {
28 StoreEntry *logEntry = new StoreEntry;
29 logEntry->makeMemObject();
30 logEntry->mem_obj->setUris("dummy_storeId", NULL, HttpRequestMethod());
31 logEntry->store_status = STORE_PENDING;
32 StorePointer aRoot (new StoreHashIndex());
33 Store::Root(aRoot);
34 TestSwapDirPointer aStore (new TestSwapDir);
35 TestSwapDirPointer aStore2 (new TestSwapDir);
36 addSwapDir(aStore);
37 addSwapDir(aStore2);
38 CPPUNIT_ASSERT_EQUAL(false, aStore->statsCalled);
39 CPPUNIT_ASSERT_EQUAL(false, aStore2->statsCalled);
40 Store::Stats(logEntry);
41 free_cachedir(&Config.cacheSwap);
42 CPPUNIT_ASSERT_EQUAL(true, aStore->statsCalled);
43 CPPUNIT_ASSERT_EQUAL(true, aStore2->statsCalled);
44 Store::Root(NULL);
45 }
46
47 void
48 testStoreHashIndex::testMaxSize()
49 {
50 StoreEntry *logEntry = new StoreEntry;
51 logEntry->makeMemObject();
52 logEntry->mem_obj->setUris("dummy_storeId", NULL, HttpRequestMethod());
53 logEntry->store_status = STORE_PENDING;
54 StorePointer aRoot (new StoreHashIndex());
55 Store::Root(aRoot);
56 TestSwapDirPointer aStore (new TestSwapDir);
57 TestSwapDirPointer aStore2 (new TestSwapDir);
58 addSwapDir(aStore);
59 addSwapDir(aStore2);
60 CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize());
61 free_cachedir(&Config.cacheSwap);
62 Store::Root(NULL);
63 }
64
65 StoreEntry *
66 addedEntry(StorePointer hashStore,
67 StorePointer aStore,
68 String name,
69 String varySpec,
70 String varyKey
71
72 )
73 {
74 StoreEntry *e = new StoreEntry();
75 e->store_status = STORE_OK;
76 e->setMemStatus(NOT_IN_MEMORY);
77 e->swap_status = SWAPOUT_DONE; /* bogus haha */
78 e->swap_filen = 0; /* garh - lower level*/
79 e->swap_dirn = -1;
80
81 for (int i=0; i < Config.cacheSwap.n_configured; ++i) {
82 if (INDEXSD (i) == aStore.getRaw())
83 e->swap_dirn = i;
84 }
85
86 CPPUNIT_ASSERT (e->swap_dirn != -1);
87 e->swap_file_sz = 0; /* garh lower level */
88 e->lastref = squid_curtime;
89 e->timestamp = squid_curtime;
90 e->expires = squid_curtime;
91 e->lastmod = squid_curtime;
92 e->refcount = 1;
93 EBIT_CLR(e->flags, RELEASE_REQUEST);
94 EBIT_CLR(e->flags, KEY_PRIVATE);
95 e->ping_status = PING_NONE;
96 EBIT_CLR(e->flags, ENTRY_VALIDATED);
97 e->hashInsert((const cache_key *)name.termedBuf()); /* do it after we clear KEY_PRIVATE */
98 return e;
99 }
100
101 void commonInit()
102 {
103 static bool inited = false;
104
105 if (inited)
106 return;
107
108 Mem::Init();
109
110 Config.Store.avgObjectSize = 1024;
111
112 Config.Store.objectsPerBucket = 20;
113
114 Config.Store.maxObjectSize = 2048;
115 }
116
117 /* TODO make this a cbdata class */
118
119 static bool cbcalled;
120
121 static void
122 searchCallback(void *cbdata)
123 {
124 cbcalled = true;
125 }
126
127 void
128 testStoreHashIndex::testSearch()
129 {
130 commonInit();
131 StorePointer aRoot (new StoreHashIndex());
132 Store::Root(aRoot);
133 TestSwapDirPointer aStore (new TestSwapDir);
134 TestSwapDirPointer aStore2 (new TestSwapDir);
135 addSwapDir(aStore);
136 addSwapDir(aStore2);
137 Store::Root().init();
138 StoreEntry * entry1 = addedEntry (&Store::Root(), aStore.getRaw(), "name", NULL, NULL);
139 StoreEntry * entry2 = addedEntry (&Store::Root(), aStore2.getRaw(), "name2", NULL, NULL);
140 StoreSearchPointer search = aRoot->search (NULL, NULL); /* search for everything in the store */
141
142 /* nothing should be immediately available */
143 CPPUNIT_ASSERT_EQUAL(false, search->error());
144 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
145 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
146 #if 0
147
148 CPPUNIT_ASSERT_EQUAL(false, search->next());
149 #endif
150
151 /* trigger a callback */
152 cbcalled = false;
153 search->next(searchCallback, NULL);
154 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
155
156 /* we should have access to a entry now, that matches the entry we had before */
157 CPPUNIT_ASSERT_EQUAL(false, search->error());
158 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
159 /* note the hash order is random - the test happens to be in a nice order */
160 CPPUNIT_ASSERT_EQUAL(entry1, search->currentItem());
161 //CPPUNIT_ASSERT_EQUAL(false, search->next());
162
163 /* trigger another callback */
164 cbcalled = false;
165 search->next(searchCallback, NULL);
166 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
167
168 /* we should have access to a entry now, that matches the entry we had before */
169 CPPUNIT_ASSERT_EQUAL(false, search->error());
170 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
171 CPPUNIT_ASSERT_EQUAL(entry2, search->currentItem());
172 //CPPUNIT_ASSERT_EQUAL(false, search->next());
173
174 /* trigger another callback */
175 cbcalled = false;
176 search->next(searchCallback, NULL);
177 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
178
179 /* now we should have no error, we should have finished and have no current item */
180 CPPUNIT_ASSERT_EQUAL(false, search->error());
181 CPPUNIT_ASSERT_EQUAL(true, search->isDone());
182 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
183 //CPPUNIT_ASSERT_EQUAL(false, search->next());
184
185 Store::Root(NULL);
186 }