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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors