]> git.ipfire.org Git - thirdparty/squid.git/blame - src/tests/testStoreHashIndex.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / tests / testStoreHashIndex.cc
CommitLineData
4e0938ef 1/*
bde978a6 2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
4e0938ef
AJ
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
582c2af2 9#include "squid.h"
cb868059 10#include "MemObject.h"
4d5904f7 11#include "SquidConfig.h"
985c86bc 12#include "SquidTime.h"
602d9612
A
13#include "Store.h"
14#include "StoreHashIndex.h"
15#include "StoreSearch.h"
16#include "SwapDir.h"
17#include "testStoreHashIndex.h"
18#include "TestSwapDir.h"
c8f4eac4 19
20CPPUNIT_TEST_SUITE_REGISTRATION( testStoreHashIndex );
21
22static void
23addSwapDir(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
c8f4eac4 30void
31testStoreHashIndex::testStats()
32{
cb868059
AR
33 StoreEntry *logEntry = new StoreEntry;
34 logEntry->makeMemObject();
35 logEntry->mem_obj->setUris("dummy_storeId", NULL, HttpRequestMethod());
c8f4eac4 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);
5d1dff27
FC
43 CPPUNIT_ASSERT_EQUAL(false, aStore->statsCalled);
44 CPPUNIT_ASSERT_EQUAL(false, aStore2->statsCalled);
c8f4eac4 45 Store::Stats(logEntry);
46 free_cachedir(&Config.cacheSwap);
5d1dff27
FC
47 CPPUNIT_ASSERT_EQUAL(true, aStore->statsCalled);
48 CPPUNIT_ASSERT_EQUAL(true, aStore2->statsCalled);
c8f4eac4 49 Store::Root(NULL);
50}
51
52void
53testStoreHashIndex::testMaxSize()
54{
cb868059
AR
55 StoreEntry *logEntry = new StoreEntry;
56 logEntry->makeMemObject();
57 logEntry->mem_obj->setUris("dummy_storeId", NULL, HttpRequestMethod());
c8f4eac4 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);
5d1dff27 65 CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize());
c8f4eac4 66 free_cachedir(&Config.cacheSwap);
67 Store::Root(NULL);
68}
69
70StoreEntry *
71addedEntry(StorePointer hashStore,
72 StorePointer aStore,
30abd221 73 String name,
74 String varySpec,
75 String varyKey
c8f4eac4 76
77 )
78{
79 StoreEntry *e = new StoreEntry();
80 e->store_status = STORE_OK;
3900307b 81 e->setMemStatus(NOT_IN_MEMORY);
c8f4eac4 82 e->swap_status = SWAPOUT_DONE; /* bogus haha */
83 e->swap_filen = 0; /* garh - lower level*/
84 e->swap_dirn = -1;
85
d7ae3534 86 for (int i=0; i < Config.cacheSwap.n_configured; ++i) {
c8f4eac4 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 */
c8f4eac4 93 e->lastref = squid_curtime;
94 e->timestamp = squid_curtime;
95 e->expires = squid_curtime;
96 e->lastmod = squid_curtime;
97 e->refcount = 1;
c8f4eac4 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);
f53969cc 102 e->hashInsert((const cache_key *)name.termedBuf()); /* do it after we clear KEY_PRIVATE */
c8f4eac4 103 return e;
104}
105
106void 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
124static bool cbcalled;
125
126static void
127searchCallback(void *cbdata)
128{
129 cbcalled = true;
130}
131
132void
133testStoreHashIndex::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 */
5d1dff27
FC
148 CPPUNIT_ASSERT_EQUAL(false, search->error());
149 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
150 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
c8f4eac4 151#if 0
152
5d1dff27 153 CPPUNIT_ASSERT_EQUAL(false, search->next());
c8f4eac4 154#endif
155
156 /* trigger a callback */
157 cbcalled = false;
158 search->next(searchCallback, NULL);
5d1dff27 159 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
c8f4eac4 160
161 /* we should have access to a entry now, that matches the entry we had before */
5d1dff27
FC
162 CPPUNIT_ASSERT_EQUAL(false, search->error());
163 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
c8f4eac4 164 /* note the hash order is random - the test happens to be in a nice order */
5d1dff27
FC
165 CPPUNIT_ASSERT_EQUAL(entry1, search->currentItem());
166 //CPPUNIT_ASSERT_EQUAL(false, search->next());
c8f4eac4 167
168 /* trigger another callback */
169 cbcalled = false;
170 search->next(searchCallback, NULL);
5d1dff27 171 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
c8f4eac4 172
173 /* we should have access to a entry now, that matches the entry we had before */
5d1dff27
FC
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());
c8f4eac4 178
179 /* trigger another callback */
180 cbcalled = false;
181 search->next(searchCallback, NULL);
5d1dff27 182 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
c8f4eac4 183
184 /* now we should have no error, we should have finished and have no current item */
5d1dff27
FC
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());
c8f4eac4 189
190 Store::Root(NULL);
191}
f53969cc 192