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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors