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