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