testRock.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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 "ConfigParser.h"
11#include "DiskIO/DiskIOModule.h"
12#include "fde.h"
13#include "fs/rock/RockSwapDir.h"
14#include "globals.h"
15#include "HttpHeader.h"
16#include "HttpReply.h"
17#include "MemObject.h"
18#include "RequestFlags.h"
19#include "SquidConfig.h"
20#include "Store.h"
21#include "store/Disk.h"
22#include "store/Disks.h"
23#include "StoreFileSystem.h"
24#include "StoreSearch.h"
25#include "testRock.h"
26#include "testStoreSupport.h"
27#include "unitTestMain.h"
28
29#include <stdexcept>
30#if HAVE_SYS_STAT_H
31#include <sys/stat.h>
32#endif
33#if HAVE_UNISTD_H
34#include <unistd.h>
35#endif
36
37#define TESTDIR "tr"
38
40
42
43static char cwd[MAXPATHLEN];
44
45static void
47{
51}
52
53void
55{
56 CPPUNIT_NS::TestFixture::setUp();
57
58 if (0 > system ("rm -rf " TESTDIR))
59 throw std::runtime_error("Failed to clean test work directory");
60
63
64 // use current directory for shared segments (on path-based OSes)
66 if (Ipc::Mem::Segment::BasePath == nullptr)
68
70
71 store = new Rock::SwapDir();
72
74
75 commonInit();
76
77 char *path=xstrdup(TESTDIR);
78
79 char *config_line=xstrdup("10 max-size=16384");
80
81 ConfigParser::SetCfgLine(config_line);
82
83 store->parse(0, path);
84 store_maxobjsize = 1024*1024*2;
85
86 safe_free(path);
87
88 safe_free(config_line);
89
90 /* ok, ready to create */
91 store->create();
92
93 rr = new Rock::SwapDirRr;
94 rr->useConfig();
95}
96
97void
99{
100 CPPUNIT_NS::TestFixture::tearDown();
101
103
104 store = nullptr;
105
107
108 rr->finishShutdown(); // deletes rr
109 rr = nullptr;
110
111 // TODO: do this once, or each time.
112 // safe_free(Config.replPolicy->type);
113 // delete Config.replPolicy;
114
115 if (0 > system ("rm -rf " TESTDIR))
116 throw std::runtime_error("Failed to clean test work directory");
117}
118
119void
121{
122 static bool inited = false;
123
124 if (inited)
125 return;
126
130
132
134 Config.replPolicy->type = xstrdup("lru");
135 Config.replPolicy->args = nullptr;
136
137 /* garh garh */
139
141
142 Mem::Init();
143
144 fde::Init();
145
146 comm_init();
147
148 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
149
151
152 inited = true;
153}
154
155void
157{
158 /* ok, ready to use */
159 Store::Root().init();
160
161 /* rebuild is a scheduled event */
162 StockEventLoop loop;
163
164 /* our swapdir must be scheduled to rebuild */
165 CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding);
166
167 loop.run();
168
169 /* cannot use loop.run(); as the loop will never idle: the store-dir
170 * clean() scheduled event prevents it
171 */
172
173 /* nothing left to rebuild */
174 CPPUNIT_ASSERT_EQUAL(0, StoreController::store_dirs_rebuilding);
175}
176
177static const char *
178storeId(const int i)
179{
180 static char buf[64];
181 snprintf(buf, sizeof(buf), "dummy url %i", i);
182 buf[sizeof(buf) - 1] = '\0';
183 return buf;
184}
185
188{
189 RequestFlags flags;
190 flags.cachable = true;
191 StoreEntry *const pe =
192 storeCreateEntry(storeId(i), "dummy log url", flags, Http::METHOD_GET);
193 auto &rep = pe->mem().adjustableBaseReply();
194 rep.setHeaders(Http::scOkay, "dummy test object", "x-squid-internal/test", 0, -1, squid_curtime + 100000);
195
196 pe->setPublicKey();
197
198 return pe;
199}
200
203{
204 StoreEntry *const pe = createEntry(i);
205
206 pe->buffer();
208 pe->flush();
209 pe->timestampsSet();
210 pe->complete();
211 pe->swapOut();
212
213 return pe;
214}
215
218{
220}
221
222void
224{
225 struct stat sb;
226
227 CPPUNIT_ASSERT_EQUAL(0, ::stat(TESTDIR, &sb));
228
229 /* TODO: check the size */
230
231 /* TODO: test rebuild */
232}
233
234void
236{
237 storeInit();
238
239 // add few entries to prime the database
240 for (int i = 0; i < 5; ++i) {
241 CPPUNIT_ASSERT_EQUAL((uint64_t)i, store->currentCount());
242
243 StoreEntry *const pe = addEntry(i);
244
245 CPPUNIT_ASSERT_EQUAL(SWAPOUT_WRITING, pe->swap_status);
246 CPPUNIT_ASSERT_EQUAL(0, pe->swap_dirn);
247 CPPUNIT_ASSERT(pe->swap_filen >= 0);
248
249 // Rock::IoState::finishedWriting() schedules an AsyncCall
250 // storeSwapOutFileClosed(). Let it fire.
251 StockEventLoop loop;
252 loop.run();
253
254 CPPUNIT_ASSERT_EQUAL(SWAPOUT_DONE, pe->swap_status);
255
256 pe->unlock("testRock::testRockSwapOut priming");
257 }
258
259 CPPUNIT_ASSERT_EQUAL((uint64_t)5, store->currentCount());
260
261 // try to swap out entry to a used unlocked slot
262 {
263 // without marking the old entry as deleted
264 StoreEntry *const pe = addEntry(3);
265
266 CPPUNIT_ASSERT_EQUAL(SWAPOUT_NONE, pe->swap_status);
267 CPPUNIT_ASSERT_EQUAL(-1, pe->swap_dirn);
268 CPPUNIT_ASSERT_EQUAL(-1, pe->swap_filen);
269 pe->unlock("testRock::testRockSwapOut e#3");
270
271 // after marking the old entry as deleted
272 StoreEntry *const pe2 = getEntry(4);
273 CPPUNIT_ASSERT(pe2 != nullptr);
274 pe2->release();
275
276 StoreEntry *const pe3 = addEntry(4);
277 CPPUNIT_ASSERT_EQUAL(SWAPOUT_WRITING, pe3->swap_status);
278 CPPUNIT_ASSERT_EQUAL(0, pe3->swap_dirn);
279 CPPUNIT_ASSERT(pe3->swap_filen >= 0);
280
281 StockEventLoop loop;
282 loop.run();
283
284 CPPUNIT_ASSERT_EQUAL(SWAPOUT_DONE, pe3->swap_status);
285
286 pe->unlock("testRock::testRockSwapOut e#4");
287 }
288
289 // try to swap out entry to a used locked slot
290 {
291 StoreEntry *const pe = addEntry(5);
292
293 CPPUNIT_ASSERT_EQUAL(SWAPOUT_WRITING, pe->swap_status);
294 CPPUNIT_ASSERT_EQUAL(0, pe->swap_dirn);
295 CPPUNIT_ASSERT(pe->swap_filen >= 0);
296
297 // the slot is locked here because the async calls have not run yet
298 StoreEntry *const pe2 = addEntry(5);
299 CPPUNIT_ASSERT_EQUAL(SWAPOUT_NONE, pe2->swap_status);
300 CPPUNIT_ASSERT_EQUAL(MemObject::SwapOut::swImpossible, pe2->mem_obj->swapout.decision);
301 CPPUNIT_ASSERT_EQUAL(-1, pe2->swap_dirn);
302 CPPUNIT_ASSERT_EQUAL(-1, pe2->swap_filen);
303
304 StockEventLoop loop;
305 loop.run();
306
307 pe->unlock("testRock::testRockSwapOut e#5.1");
308 pe2->unlock("testRock::testRockSwapOut e#5.2");
309
310 // pe2 has the same public key as pe so it marks old pe for release
311 // here, we add another entry #5 into the now-available slot
312 StoreEntry *const pe3 = addEntry(5);
313 CPPUNIT_ASSERT_EQUAL(SWAPOUT_WRITING, pe3->swap_status);
314 CPPUNIT_ASSERT_EQUAL(0, pe3->swap_dirn);
315 CPPUNIT_ASSERT(pe3->swap_filen >= 0);
316 loop.run();
317 CPPUNIT_ASSERT_EQUAL(SWAPOUT_DONE, pe3->swap_status);
318 pe3->unlock("testRock::testRockSwapOut e#5.3");
319 }
320
321 CPPUNIT_ASSERT_EQUAL((uint64_t)6, store->currentCount());
322
323 // try to get and release all entries
324 for (int i = 0; i < 6; ++i) {
325 StoreEntry *const pe = getEntry(i);
326 CPPUNIT_ASSERT(pe != nullptr);
327
328 pe->release(); // destroys pe
329
330 StoreEntry *const pe2 = getEntry(i);
331 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(nullptr), pe2);
332 }
333}
334
void free_cachedir(Store::DiskConfig *swap)
Definition: Disks.cc:802
void allocate_new_swapdir(Store::DiskConfig &swap)
Definition: Disks.cc:783
void httpHeaderInitModule(void)
Definition: HttpHeader.cc:119
RemovalPolicy * mem_policy
Definition: MemObject.cc:44
time_t squid_curtime
Definition: stub_libtime.cc:20
RemovalPolicy * REMOVALPOLICYCREATE(wordlist *args)
Definition: RemovalPolicy.h:80
RemovalPolicy * createRemovalPolicy(RemovalPolicySettings *settings)
Definition: store.cc:1638
class SquidConfig Config
Definition: SquidConfig.cc:12
static void SetCfgLine(char *line)
Set the configuration file line to parse.
void run()
Definition: EventLoop.cc:76
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition: HttpReply.cc:167
void packHeadersUsingSlowPacker(Packable &p) const
same as packHeadersUsingFastPacker() but assumes that p cannot quickly process small additions
Definition: HttpReply.cc:93
virtual void useConfig()
Definition: Segment.cc:377
static const char * BasePath
common path of all segment names in path-based environments
Definition: Segment.h:45
Decision decision
current decision state
Definition: MemObject.h:156
SwapOut swapout
Definition: MemObject.h:159
const HttpReply & freshestReply() const
Definition: MemObject.h:67
HttpReply & adjustableBaseReply()
Definition: MemObject.cc:121
C * getRaw() const
Definition: RefCount.h:80
virtual void finishShutdown()
Meant for cleanup of services needed by the already destroyed objects.
initializes shared memory segments used by Rock::SwapDir
Definition: RockSwapDir.h:157
virtual void create()
create system resources needed for this store to operate in the future
Definition: RockSwapDir.cc:216
virtual void parse(int index, char *path)
Definition: RockSwapDir.cc:325
virtual uint64_t currentCount() const
the total number of objects stored right now
Definition: RockSwapDir.cc:161
int objectsPerBucket
Definition: SquidConfig.h:262
int64_t avgObjectSize
Definition: SquidConfig.h:263
RemovalPolicySettings * replPolicy
Definition: SquidConfig.h:97
Store::DiskConfig cacheSwap
Definition: SquidConfig.h:421
YesNoNone shmLocking
shared_memory_locking
Definition: SquidConfig.h:88
struct SquidConfig::@109 Store
int64_t maxObjectSize
Definition: SquidConfig.h:264
char * store_dir_select_algorithm
Definition: SquidConfig.h:508
YesNoNone memShared
whether the memory cache is shared among workers
Definition: SquidConfig.h:87
MemObject & mem()
Definition: Store.h:51
sdirno swap_dirn
Definition: Store.h:238
virtual void buffer()
Definition: store.cc:1568
virtual void flush()
Definition: store.cc:1579
int unlock(const char *context)
Definition: store.cc:442
void complete()
Definition: store.cc:1004
void release(const bool shareable=false)
Definition: store.cc:1119
swap_status_t swap_status
Definition: Store.h:246
bool timestampsSet()
Definition: store.cc:1360
sfileno swap_filen
unique ID inside a cache_dir for swapped out entries; -1 for others
Definition: Store.h:236
MemObject * mem_obj
Definition: Store.h:221
bool setPublicKey(const KeyScope keyScope=ksDefault)
Definition: store.cc:548
void swapOut()
static int store_dirs_rebuilding
the number of cache_dirs being rebuilt; TODO: move to Disks::Rebuilding
Definition: Controller.h:133
virtual void init() override
Definition: Controller.cc:58
RefCount< SwapDir > * swapDirs
Definition: SquidConfig.h:66
void defaultTo(bool beSet)
enables or disables the option; updating to 'implicit' state
Definition: YesNoNone.h:59
static void Init()
Definition: fde.cc:141
Rock::SwapDirRr * rr
Definition: testRock.h:43
virtual void setUp()
Definition: testRock.cc:54
void testRockCreate()
Definition: testRock.cc:223
void testRockSwapOut()
Definition: testRock.cc:235
StoreEntry * createEntry(const int i)
Definition: testRock.cc:187
void commonInit()
Definition: testRock.cc:120
void storeInit()
Definition: testRock.cc:156
StoreEntry * addEntry(const int i)
Definition: testRock.cc:202
SwapDirPointer store
Definition: testRock.h:42
virtual void tearDown()
Definition: testRock.cc:98
StoreEntry * getEntry(const int i)
Definition: testRock.cc:217
void comm_init(void)
Definition: comm.cc:1164
@ SWAPOUT_NONE
Definition: enums.h:58
@ SWAPOUT_WRITING
Definition: enums.h:61
@ SWAPOUT_DONE
Definition: enums.h:64
char const * visible_appname_string
int64_t store_maxobjsize
@ scOkay
Definition: StatusCode.h:26
@ METHOD_GET
Definition: MethodType.h:25
void Init()
Definition: old_api.cc:425
Controller & Root()
safely access controller singleton
Definition: Controller.cc:926
void FreeMemory()
undo Init()
Definition: Controller.cc:939
void Init(Controller *root=nullptr)
initialize the storage module; a custom root is used by unit tests only
Definition: Controller.cc:933
#define xstrdup
static struct stat sb
Definition: squidclient.cc:71
#define MAXPATHLEN
Definition: stdio.h:62
Store::Disk SwapDir
Definition: forward.h:58
StoreEntry * storeCreateEntry(const char *url, const char *logUrl, const RequestFlags &flags, const HttpRequestMethod &method)
Definition: store.cc:732
StoreEntry * storeGetPublic(const char *uri, const HttpRequestMethod &method)
Definition: store.cc:477
void storeReplAdd(const char *type, REMOVALPOLICYCREATE *create)
Definition: store.cc:1612
static void addSwapDir(testRock::SwapDirPointer aStore)
Definition: testRock.cc:46
static const char * storeId(const int i)
Definition: testRock.cc:178
REMOVALPOLICYCREATE createRemovalPolicy_lru
#define TESTDIR
Definition: testRock.cc:37
CPPUNIT_TEST_SUITE_REGISTRATION(testRock)
static char cwd[MAXPATHLEN]
Definition: testRock.cc:43
#define APP_FULLNAME
Definition: version.h:25
#define safe_free(x)
Definition: xalloc.h:73

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors