testStoreController.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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
40{
44}
45
46void
48{
50 StoreEntry *logEntry = new StoreEntry;
51 logEntry->createMemObject("dummy_storeId", nullptr, HttpRequestMethod());
52 logEntry->store_status = STORE_PENDING;
54 TestSwapDirPointer aStore2 (new TestSwapDir);
55 addSwapDir(aStore);
56 addSwapDir(aStore2);
57 CPPUNIT_ASSERT_EQUAL(false, aStore->statsCalled);
58 CPPUNIT_ASSERT_EQUAL(false, aStore2->statsCalled);
59 Store::Stats(logEntry);
61 CPPUNIT_ASSERT_EQUAL(true, aStore->statsCalled);
62 CPPUNIT_ASSERT_EQUAL(true, aStore2->statsCalled);
64}
65
66static void
68{
69 static bool inited = false;
70
71 if (inited)
72 return;
73
75
76 Mem::Init();
77
79
81
83
85}
86
87void
89{
90 commonInit();
91 StoreEntry *logEntry = new StoreEntry;
92 logEntry->createMemObject("dummy_storeId", nullptr, HttpRequestMethod());
93 logEntry->store_status = STORE_PENDING;
96 TestSwapDirPointer aStore2 (new TestSwapDir);
97 addSwapDir(aStore);
98 addSwapDir(aStore2);
99 CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize());
102}
103
104static StoreEntry *
106 String name,
107 String,
108 String
109 )
110{
111 StoreEntry *e = new StoreEntry();
114 e->swap_status = SWAPOUT_DONE; /* bogus haha */
115 e->swap_filen = 0; /* garh - lower level*/
116 e->swap_dirn = -1;
117
118 for (int i=0; i < Config.cacheSwap.n_configured; ++i) {
119 if (INDEXSD(i) == aStore)
120 e->swap_dirn = i;
121 }
122
123 CPPUNIT_ASSERT (e->swap_dirn != -1);
124 e->swap_file_sz = 0; /* garh lower level */
129 e->refcount = 1;
132 e->hashInsert((const cache_key *)name.termedBuf()); /* do it after we clear KEY_PRIVATE */
133 return e;
134}
135
136/* TODO make this a cbdata class */
137
138static bool cbcalled;
139
140static void
142{
143 cbcalled = true;
144}
145
146void
148{
149 commonInit();
150 Store::Init();
151 TestSwapDirPointer aStore (new TestSwapDir);
152 TestSwapDirPointer aStore2 (new TestSwapDir);
153 addSwapDir(aStore);
154 addSwapDir(aStore2);
155 Store::Root().init();
156 StoreEntry * entry1 = addedEntry(aStore.getRaw(), "name", nullptr, nullptr);
157 StoreEntry * entry2 = addedEntry(aStore2.getRaw(), "name2", nullptr, nullptr);
158 StoreSearchPointer search = Store::Root().search(); /* search for everything in the store */
159
160 /* nothing should be immediately available */
161 CPPUNIT_ASSERT_EQUAL(false, search->error());
162 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
163 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(nullptr), search->currentItem());
164
165 /* trigger a callback */
166 cbcalled = false;
167 search->next(searchCallback, nullptr);
168 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
169
170 /* we should have access to a entry now, that matches the entry we had before */
171 CPPUNIT_ASSERT_EQUAL(false, search->error());
172 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
173 /* note the hash order is random - the test happens to be in a nice order */
174 CPPUNIT_ASSERT_EQUAL(entry1, search->currentItem());
175 //CPPUNIT_ASSERT_EQUAL(false, search->next());
176
177 /* trigger another callback */
178 cbcalled = false;
179 search->next(searchCallback, nullptr);
180 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
181
182 /* we should have access to a entry now, that matches the entry we had before */
183 CPPUNIT_ASSERT_EQUAL(false, search->error());
184 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
185 CPPUNIT_ASSERT_EQUAL(entry2, search->currentItem());
186 //CPPUNIT_ASSERT_EQUAL(false, search->next());
187
188 /* trigger another callback */
189 cbcalled = false;
190 search->next(searchCallback, nullptr);
191 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
192
193 /* now we should have no error, we should have finished and have no current item */
194 CPPUNIT_ASSERT_EQUAL(false, search->error());
195 CPPUNIT_ASSERT_EQUAL(true, search->isDone());
196 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(nullptr), search->currentItem());
197 //CPPUNIT_ASSERT_EQUAL(false, search->next());
198
200}
201
202// This test uses main() from ./testStore.cc.
203
void free_cachedir(Store::DiskConfig *swap)
Definition: Disks.cc:805
void allocate_new_swapdir(Store::DiskConfig &swap)
Definition: Disks.cc:786
time_t squid_curtime
Definition: stub_libtime.cc:20
class SquidConfig Config
Definition: SquidConfig.cc:12
#define INDEXSD(i)
Definition: SquidConfig.h:74
C * getRaw() const
Definition: RefCount.h:89
int objectsPerBucket
Definition: SquidConfig.h:264
int64_t avgObjectSize
Definition: SquidConfig.h:265
Store::DiskConfig cacheSwap
Definition: SquidConfig.h:423
struct SquidConfig::@104 Store
int64_t maxObjectSize
Definition: SquidConfig.h:266
char * store_dir_select_algorithm
Definition: SquidConfig.h:500
YesNoNone memShared
whether the memory cache is shared among workers
Definition: SquidConfig.h:89
void hashInsert(const cache_key *)
Definition: store.cc:410
uint16_t flags
Definition: Store.h:232
sdirno swap_dirn
Definition: Store.h:238
time_t expires
Definition: Store.h:226
void lastModified(const time_t when)
Definition: Store.h:176
swap_status_t swap_status
Definition: Store.h:246
time_t timestamp
Definition: Store.h:224
sfileno swap_filen
unique ID inside a cache_dir for swapped out entries; -1 for others
Definition: Store.h:236
ping_status_t ping_status
Definition: Store.h:242
store_status_t store_status
Definition: Store.h:244
time_t lastref
Definition: Store.h:225
void createMemObject()
Definition: store.cc:1561
uint64_t swap_file_sz
Definition: Store.h:230
uint16_t refcount
Definition: Store.h:231
void setMemStatus(mem_status_t)
Definition: store.cc:1510
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()
Definition: Controller.cc:211
void init() override
Definition: Controller.cc:58
RefCount< SwapDir > * swapDirs
Definition: SquidConfig.h:68
manages a single cache_dir
Definition: Disk.h:22
char const * termedBuf() const
Definition: SquidString.h:92
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:68
@ NOT_IN_MEMORY
Definition: enums.h:35
@ PING_NONE
Has not considered whether to send ICP queries to peers yet.
Definition: enums.h:41
@ SWAPOUT_DONE
Definition: enums.h:64
@ ENTRY_VALIDATED
Definition: enums.h:113
@ STORE_PENDING
Definition: enums.h:51
@ STORE_OK
Definition: enums.h:50
void Init()
Definition: old_api.cc:425
Controller & Root()
safely access controller singleton
Definition: Controller.cc:938
void FreeMemory()
undo Init()
Definition: Controller.cc:951
void Stats(StoreEntry *output)
Definition: store.cc:125
void Init(Controller *root=nullptr)
initialize the storage module; a custom root is used by unit tests only
Definition: Controller.cc:945
#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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors