Squid Web Cache master
Loading...
Searching...
No Matches
testStoreController.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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 "compat/cppunit.h"
11#include "MemObject.h"
12#include "SquidConfig.h"
13#include "Store.h"
14#include "store/Disks.h"
15#include "StoreSearch.h"
16#include "TestSwapDir.h"
17
18/*
19 * test the store framework
20 */
21
22class TestStoreController : public CPPUNIT_NS::TestFixture
23{
29
30public:
31protected:
32 void testStats();
33 void testMaxSize();
34 void testSearch();
35};
37
38static void
45
46void
48{
49 StoreEntry *logEntry = new StoreEntry;
50 logEntry->createMemObject("dummy_storeId", nullptr, HttpRequestMethod());
51 logEntry->store_status = STORE_PENDING;
53 TestSwapDirPointer aStore2 (new TestSwapDir);
54 addSwapDir(aStore);
55 addSwapDir(aStore2);
56 CPPUNIT_ASSERT_EQUAL(false, aStore->statsCalled);
57 CPPUNIT_ASSERT_EQUAL(false, aStore2->statsCalled);
58 Store::Stats(logEntry);
60 CPPUNIT_ASSERT_EQUAL(true, aStore->statsCalled);
61 CPPUNIT_ASSERT_EQUAL(true, aStore2->statsCalled);
62}
63
64static void
66{
67 static bool inited = false;
68
69 if (inited)
70 return;
71
73
74 Mem::Init();
75
77
79
81
83}
84
85void
87{
88 commonInit();
89 StoreEntry *logEntry = new StoreEntry;
90 logEntry->createMemObject("dummy_storeId", nullptr, HttpRequestMethod());
91 logEntry->store_status = STORE_PENDING;
93 TestSwapDirPointer aStore2 (new TestSwapDir);
94 addSwapDir(aStore);
95 addSwapDir(aStore2);
96 CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize());
98}
99
100static StoreEntry *
102 String name,
103 String,
104 String
105 )
106{
107 StoreEntry *e = new StoreEntry();
110 e->swap_status = SWAPOUT_DONE; /* bogus haha */
111 e->swap_filen = 0; /* garh - lower level*/
112 e->swap_dirn = -1;
113
114 for (size_t i = 0; i < Config.cacheSwap.n_configured; ++i) {
115 if (INDEXSD(i) == aStore)
116 e->swap_dirn = i;
117 }
118
119 CPPUNIT_ASSERT (e->swap_dirn != -1);
120 e->swap_file_sz = 0; /* garh lower level */
125 e->refcount = 1;
128 e->hashInsert((const cache_key *)name.termedBuf()); /* do it after we clear KEY_PRIVATE */
129 return e;
130}
131
132/* TODO make this a cbdata class */
133
134static bool cbcalled;
135
136static void
138{
139 cbcalled = true;
140}
141
142void
144{
145 commonInit();
146 TestSwapDirPointer aStore (new TestSwapDir);
147 TestSwapDirPointer aStore2 (new TestSwapDir);
148 addSwapDir(aStore);
149 addSwapDir(aStore2);
150 Store::Root().init();
151 StoreEntry * entry1 = addedEntry(aStore.getRaw(), "name", nullptr, nullptr);
152 StoreEntry * entry2 = addedEntry(aStore2.getRaw(), "name2", nullptr, nullptr);
153 StoreSearchPointer search = Store::Root().search(); /* search for everything in the store */
154
155 /* nothing should be immediately available */
156 CPPUNIT_ASSERT_EQUAL(false, search->error());
157 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
158 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(nullptr), search->currentItem());
159
160 /* trigger a callback */
161 cbcalled = false;
162 search->next(searchCallback, nullptr);
163 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
164
165 /* we should have access to a entry now, that matches the entry we had before */
166 CPPUNIT_ASSERT_EQUAL(false, search->error());
167 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
168 /* note the hash order is random - the test happens to be in a nice order */
169 CPPUNIT_ASSERT_EQUAL(entry1, search->currentItem());
170 //CPPUNIT_ASSERT_EQUAL(false, search->next());
171
172 /* trigger another callback */
173 cbcalled = false;
174 search->next(searchCallback, nullptr);
175 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
176
177 /* we should have access to a entry now, that matches the entry we had before */
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());
182
183 /* trigger another callback */
184 cbcalled = false;
185 search->next(searchCallback, nullptr);
186 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
187
188 /* now we should have no error, we should have finished and have no current item */
189 CPPUNIT_ASSERT_EQUAL(false, search->error());
190 CPPUNIT_ASSERT_EQUAL(true, search->isDone());
191 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(nullptr), search->currentItem());
192 //CPPUNIT_ASSERT_EQUAL(false, search->next());
193}
194
195// This test uses main() from ./testStore.cc.
196
void free_cachedir(Store::DiskConfig *swap)
Definition Disks.cc:800
void allocate_new_swapdir(Store::DiskConfig &swap)
Definition Disks.cc:781
time_t squid_curtime
class SquidConfig Config
#define INDEXSD(i)
Definition SquidConfig.h:74
C * getRaw() const
Definition RefCount.h:89
int objectsPerBucket
int64_t avgObjectSize
Store::DiskConfig cacheSwap
int64_t maxObjectSize
char * store_dir_select_algorithm
struct SquidConfig::@88 Store
YesNoNone memShared
whether the memory cache is shared among workers
Definition SquidConfig.h:89
void hashInsert(const cache_key *)
Definition store.cc:424
uint16_t flags
Definition Store.h:231
sdirno swap_dirn
Definition Store.h:237
time_t expires
Definition Store.h:225
void lastModified(const time_t when)
Definition Store.h:175
swap_status_t swap_status
Definition Store.h:245
time_t timestamp
Definition Store.h:223
sfileno swap_filen
unique ID inside a cache_dir for swapped out entries; -1 for others
Definition Store.h:235
ping_status_t ping_status
Definition Store.h:241
store_status_t store_status
Definition Store.h:243
time_t lastref
Definition Store.h:224
void createMemObject()
Definition store.cc:1575
uint64_t swap_file_sz
Definition Store.h:229
uint16_t refcount
Definition Store.h:230
void setMemStatus(mem_status_t)
Definition store.cc:1524
virtual bool error() const =0
virtual void next(void(callback)(void *cbdata), void *cbdata)=0
virtual StoreEntry * currentItem()=0
virtual bool isDone() const =0
StoreSearch * search()
void init() override
Definition Controller.cc:53
RefCount< SwapDir > * swapDirs
Definition SquidConfig.h:68
manages a single cache_dir
Definition Disk.h:22
char const * termedBuf() const
Definition SquidString.h:93
CPPUNIT_TEST(testStats)
CPPUNIT_TEST_SUITE(TestStoreController)
CPPUNIT_TEST(testMaxSize)
CPPUNIT_TEST(testSearch)
void defaultTo(bool beSet)
enables or disables the option; updating to 'implicit' state
Definition YesNoNone.h:59
#define EBIT_CLR(flag, bit)
Definition defines.h:66
@ ENTRY_VALIDATED
Definition enums.h:108
@ NOT_IN_MEMORY
Definition enums.h:30
@ PING_NONE
Has not considered whether to send ICP queries to peers yet.
Definition enums.h:36
@ SWAPOUT_DONE
Definition enums.h:59
@ STORE_PENDING
Definition enums.h:46
@ STORE_OK
Definition enums.h:45
void Init()
Definition old_api.cc:281
Controller & Root()
safely access controller singleton
void Stats(StoreEntry *output)
Definition store.cc:126
#define xstrdup
unsigned char cache_key
Store key.
Definition forward.h:29
CPPUNIT_TEST_SUITE_REGISTRATION(TestStoreController)
static void commonInit()
static void addSwapDir(TestSwapDirPointer aStore)
static StoreEntry * addedEntry(Store::Disk *aStore, String name, String, String)
static void searchCallback(void *)
static bool cbcalled