testStoreController.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 "MemObject.h"
11 #include "SquidConfig.h"
12 #include "SquidTime.h"
13 #include "Store.h"
14 #include "store/Disks.h"
15 #include "StoreSearch.h"
16 #include "testStoreController.h"
17 #include "TestSwapDir.h"
18 
20 
21 static void
23 {
27 }
28 
29 void
31 {
32  Store::Init();
33  StoreEntry *logEntry = new StoreEntry;
34  logEntry->createMemObject("dummy_storeId", NULL, HttpRequestMethod());
35  logEntry->store_status = STORE_PENDING;
36  TestSwapDirPointer aStore (new TestSwapDir);
37  TestSwapDirPointer aStore2 (new TestSwapDir);
38  addSwapDir(aStore);
39  addSwapDir(aStore2);
40  CPPUNIT_ASSERT_EQUAL(false, aStore->statsCalled);
41  CPPUNIT_ASSERT_EQUAL(false, aStore2->statsCalled);
42  Store::Stats(logEntry);
44  CPPUNIT_ASSERT_EQUAL(true, aStore->statsCalled);
45  CPPUNIT_ASSERT_EQUAL(true, aStore2->statsCalled);
47 }
48 
49 static void
51 {
52  static bool inited = false;
53 
54  if (inited)
55  return;
56 
57  Config.memShared.defaultTo(false);
58 
59  Mem::Init();
60 
61  Config.Store.avgObjectSize = 1024;
62 
64 
65  Config.Store.maxObjectSize = 2048;
66 
67  Config.store_dir_select_algorithm = xstrdup("round-robin");
68 }
69 
70 void
72 {
73  commonInit();
74  StoreEntry *logEntry = new StoreEntry;
75  logEntry->createMemObject("dummy_storeId", NULL, HttpRequestMethod());
76  logEntry->store_status = STORE_PENDING;
77  Store::Init();
78  TestSwapDirPointer aStore (new TestSwapDir);
79  TestSwapDirPointer aStore2 (new TestSwapDir);
80  addSwapDir(aStore);
81  addSwapDir(aStore2);
82  CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize());
85 }
86 
87 static StoreEntry *
89  String name,
90  String varySpec,
91  String varyKey
92 
93  )
94 {
95  StoreEntry *e = new StoreEntry();
98  e->swap_status = SWAPOUT_DONE; /* bogus haha */
99  e->swap_filen = 0; /* garh - lower level*/
100  e->swap_dirn = -1;
101 
102  for (int i=0; i < Config.cacheSwap.n_configured; ++i) {
103  if (INDEXSD(i) == aStore)
104  e->swap_dirn = i;
105  }
106 
107  CPPUNIT_ASSERT (e->swap_dirn != -1);
108  e->swap_file_sz = 0; /* garh lower level */
109  e->lastref = squid_curtime;
111  e->expires = squid_curtime;
113  e->refcount = 1;
115  e->clearPrivate();
116  e->ping_status = PING_NONE;
118  e->hashInsert((const cache_key *)name.termedBuf()); /* do it after we clear KEY_PRIVATE */
119  return e;
120 }
121 
122 /* TODO make this a cbdata class */
123 
124 static bool cbcalled;
125 
126 static void
128 {
129  cbcalled = true;
130 }
131 
132 void
134 {
135  commonInit();
136  Store::Init();
137  TestSwapDirPointer aStore (new TestSwapDir);
138  TestSwapDirPointer aStore2 (new TestSwapDir);
139  addSwapDir(aStore);
140  addSwapDir(aStore2);
141  Store::Root().init();
142  StoreEntry * entry1 = addedEntry(aStore.getRaw(), "name", NULL, NULL);
143  StoreEntry * entry2 = addedEntry(aStore2.getRaw(), "name2", NULL, NULL);
144  StoreSearchPointer search = Store::Root().search(); /* search for everything in the store */
145 
146  /* nothing should be immediately available */
147  CPPUNIT_ASSERT_EQUAL(false, search->error());
148  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
149  CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
150 #if 0
151 
152  CPPUNIT_ASSERT_EQUAL(false, search->next());
153 #endif
154 
155  /* trigger a callback */
156  cbcalled = false;
157  search->next(searchCallback, NULL);
158  CPPUNIT_ASSERT_EQUAL(true, cbcalled);
159 
160  /* we should have access to a entry now, that matches the entry we had before */
161  CPPUNIT_ASSERT_EQUAL(false, search->error());
162  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
163  /* note the hash order is random - the test happens to be in a nice order */
164  CPPUNIT_ASSERT_EQUAL(entry1, search->currentItem());
165  //CPPUNIT_ASSERT_EQUAL(false, search->next());
166 
167  /* trigger another callback */
168  cbcalled = false;
169  search->next(searchCallback, NULL);
170  CPPUNIT_ASSERT_EQUAL(true, cbcalled);
171 
172  /* we should have access to a entry now, that matches the entry we had before */
173  CPPUNIT_ASSERT_EQUAL(false, search->error());
174  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
175  CPPUNIT_ASSERT_EQUAL(entry2, search->currentItem());
176  //CPPUNIT_ASSERT_EQUAL(false, search->next());
177 
178  /* trigger another callback */
179  cbcalled = false;
180  search->next(searchCallback, NULL);
181  CPPUNIT_ASSERT_EQUAL(true, cbcalled);
182 
183  /* now we should have no error, we should have finished and have no current item */
184  CPPUNIT_ASSERT_EQUAL(false, search->error());
185  CPPUNIT_ASSERT_EQUAL(true, search->isDone());
186  CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
187  //CPPUNIT_ASSERT_EQUAL(false, search->next());
188 
190 }
191 
sdirno swap_dirn
Definition: Store.h:179
void Init(Controller *root=nullptr)
initialize the storage module; a custom root is used by unit tests only
Definition: Controller.cc:626
StoreSearch * search()
Definition: Controller.cc:205
static StoreEntry * addedEntry(Store::Disk *aStore, String name, String varySpec, String varyKey)
static void searchCallback(void *cbdata)
Definition: cbdata.cc:60
unsigned char cache_key
Store key.
Definition: forward.h:29
int i
Definition: membanger.c:49
#define xstrdup
void lastModified(const time_t when)
Definition: Store.h:142
void Stats(StoreEntry *output)
Definition: store.cc:115
YesNoNone memShared
whether the memory cache is shared among workers
Definition: SquidConfig.h:79
struct SquidConfig::@110 Store
sfileno swap_filen
unique ID inside a cache_dir for swapped out entries; -1 for others
Definition: Store.h:177
Controller & Root()
safely access controller singleton
Definition: Controller.cc:619
int64_t maxObjectSize
Definition: SquidConfig.h:262
time_t expires
Definition: Store.h:167
time_t squid_curtime
Definition: stub_time.cc:17
time_t timestamp
Definition: Store.h:165
uint64_t swap_file_sz
Definition: Store.h:171
int64_t avgObjectSize
Definition: SquidConfig.h:261
ping_status_t ping_status
Definition: Store.h:183
void hashInsert(const cache_key *)
Definition: store.cc:413
#define EBIT_CLR(flag, bit)
Definition: defines.h:106
void Init()
Definition: old_api.cc:412
char const * termedBuf() const
Definition: SquidString.h:90
static void addSwapDir(TestSwapDirPointer aStore)
uint16_t flags
Definition: Store.h:173
void free_cachedir(Store::DiskConfig *swap)
Definition: Disks.cc:677
int objectsPerBucket
Definition: SquidConfig.h:260
void allocate_new_swapdir(Store::DiskConfig *swap)
Definition: Disks.cc:660
#define INDEXSD(i)
Definition: SquidConfig.h:64
uint16_t refcount
Definition: Store.h:172
Definition: enums.h:46
store_status_t store_status
Definition: Store.h:185
RefCount< SwapDir > * swapDirs
Definition: SquidConfig.h:58
void FreeMemory()
undo Init()
Definition: Controller.cc:632
Store::DiskConfig cacheSwap
Definition: SquidConfig.h:418
static void commonInit()
swap_status_t swap_status
Definition: Store.h:187
virtual void init() override
Definition: Controller.cc:58
void clearPrivate()
Definition: store.cc:158
time_t lastref
Definition: Store.h:166
static bool cbcalled
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
CPPUNIT_TEST_SUITE_REGISTRATION(testStoreController)
void defaultTo(bool beSet)
enables or disables the option; updating to 'implicit' state
Definition: YesNoNone.h:58
manages a single cache_dir
Definition: Disk.h:21
void setMemStatus(mem_status_t)
Definition: store.cc:1630
void createMemObject()
Definition: store.cc:1681

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors