testStoreController.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2018 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;
114  e->ping_status = PING_NONE;
116  e->hashInsert((const cache_key *)name.termedBuf()); /* do it after we clear KEY_PRIVATE */
117  return e;
118 }
119 
120 /* TODO make this a cbdata class */
121 
122 static bool cbcalled;
123 
124 static void
126 {
127  cbcalled = true;
128 }
129 
130 void
132 {
133  commonInit();
134  Store::Init();
135  TestSwapDirPointer aStore (new TestSwapDir);
136  TestSwapDirPointer aStore2 (new TestSwapDir);
137  addSwapDir(aStore);
138  addSwapDir(aStore2);
139  Store::Root().init();
140  StoreEntry * entry1 = addedEntry(aStore.getRaw(), "name", NULL, NULL);
141  StoreEntry * entry2 = addedEntry(aStore2.getRaw(), "name2", NULL, NULL);
142  StoreSearchPointer search = Store::Root().search(); /* search for everything in the store */
143 
144  /* nothing should be immediately available */
145  CPPUNIT_ASSERT_EQUAL(false, search->error());
146  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
147  CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
148 #if 0
149 
150  CPPUNIT_ASSERT_EQUAL(false, search->next());
151 #endif
152 
153  /* trigger a callback */
154  cbcalled = false;
155  search->next(searchCallback, NULL);
156  CPPUNIT_ASSERT_EQUAL(true, cbcalled);
157 
158  /* we should have access to a entry now, that matches the entry we had before */
159  CPPUNIT_ASSERT_EQUAL(false, search->error());
160  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
161  /* note the hash order is random - the test happens to be in a nice order */
162  CPPUNIT_ASSERT_EQUAL(entry1, search->currentItem());
163  //CPPUNIT_ASSERT_EQUAL(false, search->next());
164 
165  /* trigger another callback */
166  cbcalled = false;
167  search->next(searchCallback, NULL);
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  CPPUNIT_ASSERT_EQUAL(entry2, search->currentItem());
174  //CPPUNIT_ASSERT_EQUAL(false, search->next());
175 
176  /* trigger another callback */
177  cbcalled = false;
178  search->next(searchCallback, NULL);
179  CPPUNIT_ASSERT_EQUAL(true, cbcalled);
180 
181  /* now we should have no error, we should have finished and have no current item */
182  CPPUNIT_ASSERT_EQUAL(false, search->error());
183  CPPUNIT_ASSERT_EQUAL(true, search->isDone());
184  CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
185  //CPPUNIT_ASSERT_EQUAL(false, search->next());
186 
188 }
189 
sdirno swap_dirn
Definition: Store.h:208
void Init(Controller *root=nullptr)
initialize the storage module; a custom root is used by unit tests only
Definition: Controller.cc:852
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:157
void Stats(StoreEntry *output)
Definition: store.cc:116
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:206
Controller & Root()
safely access controller singleton
Definition: Controller.cc:845
int64_t maxObjectSize
Definition: SquidConfig.h:262
time_t expires
Definition: Store.h:196
time_t squid_curtime
Definition: stub_time.cc:17
time_t timestamp
Definition: Store.h:194
uint64_t swap_file_sz
Definition: Store.h:200
int64_t avgObjectSize
Definition: SquidConfig.h:261
ping_status_t ping_status
Definition: Store.h:212
void hashInsert(const cache_key *)
Definition: store.cc:415
#define EBIT_CLR(flag, bit)
Definition: defines.h:106
void Init()
Definition: old_api.cc:412
char const * termedBuf() const
Definition: SquidString.h:91
static void addSwapDir(TestSwapDirPointer aStore)
uint16_t flags
Definition: Store.h:202
void free_cachedir(Store::DiskConfig *swap)
Definition: Disks.cc:698
int objectsPerBucket
Definition: SquidConfig.h:260
void allocate_new_swapdir(Store::DiskConfig *swap)
Definition: Disks.cc:681
#define INDEXSD(i)
Definition: SquidConfig.h:64
uint16_t refcount
Definition: Store.h:201
Definition: enums.h:46
store_status_t store_status
Definition: Store.h:214
RefCount< SwapDir > * swapDirs
Definition: SquidConfig.h:58
void FreeMemory()
undo Init()
Definition: Controller.cc:858
Store::DiskConfig cacheSwap
Definition: SquidConfig.h:418
static void commonInit()
swap_status_t swap_status
Definition: Store.h:216
virtual void init() override
Definition: Controller.cc:58
time_t lastref
Definition: Store.h:195
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:59
manages a single cache_dir
Definition: Disk.h:21
void setMemStatus(mem_status_t)
Definition: store.cc:1574
void createMemObject()
Definition: store.cc:1625

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors