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