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