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