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