testStoreController.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 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,
91  String
92  )
93 {
94  StoreEntry *e = new StoreEntry();
97  e->swap_status = SWAPOUT_DONE; /* bogus haha */
98  e->swap_filen = 0; /* garh - lower level*/
99  e->swap_dirn = -1;
100 
101  for (int i=0; i < Config.cacheSwap.n_configured; ++i) {
102  if (INDEXSD(i) == aStore)
103  e->swap_dirn = i;
104  }
105 
106  CPPUNIT_ASSERT (e->swap_dirn != -1);
107  e->swap_file_sz = 0; /* garh lower level */
108  e->lastref = squid_curtime;
110  e->expires = squid_curtime;
112  e->refcount = 1;
113  e->ping_status = PING_NONE;
115  e->hashInsert((const cache_key *)name.termedBuf()); /* do it after we clear KEY_PRIVATE */
116  return e;
117 }
118 
119 /* TODO make this a cbdata class */
120 
121 static bool cbcalled;
122 
123 static void
125 {
126  cbcalled = true;
127 }
128 
129 void
131 {
132  commonInit();
133  Store::Init();
134  TestSwapDirPointer aStore (new TestSwapDir);
135  TestSwapDirPointer aStore2 (new TestSwapDir);
136  addSwapDir(aStore);
137  addSwapDir(aStore2);
138  Store::Root().init();
139  StoreEntry * entry1 = addedEntry(aStore.getRaw(), "name", NULL, NULL);
140  StoreEntry * entry2 = addedEntry(aStore2.getRaw(), "name2", NULL, NULL);
141  StoreSearchPointer search = Store::Root().search(); /* search for everything in the store */
142 
143  /* nothing should be immediately available */
144  CPPUNIT_ASSERT_EQUAL(false, search->error());
145  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
146  CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
147 
148  /* trigger a callback */
149  cbcalled = false;
150  search->next(searchCallback, NULL);
151  CPPUNIT_ASSERT_EQUAL(true, cbcalled);
152 
153  /* we should have access to a entry now, that matches the entry we had before */
154  CPPUNIT_ASSERT_EQUAL(false, search->error());
155  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
156  /* note the hash order is random - the test happens to be in a nice order */
157  CPPUNIT_ASSERT_EQUAL(entry1, search->currentItem());
158  //CPPUNIT_ASSERT_EQUAL(false, search->next());
159 
160  /* trigger another callback */
161  cbcalled = false;
162  search->next(searchCallback, NULL);
163  CPPUNIT_ASSERT_EQUAL(true, cbcalled);
164 
165  /* we should have access to a entry now, that matches the entry we had before */
166  CPPUNIT_ASSERT_EQUAL(false, search->error());
167  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
168  CPPUNIT_ASSERT_EQUAL(entry2, search->currentItem());
169  //CPPUNIT_ASSERT_EQUAL(false, search->next());
170 
171  /* trigger another callback */
172  cbcalled = false;
173  search->next(searchCallback, NULL);
174  CPPUNIT_ASSERT_EQUAL(true, cbcalled);
175 
176  /* now we should have no error, we should have finished and have no current item */
177  CPPUNIT_ASSERT_EQUAL(false, search->error());
178  CPPUNIT_ASSERT_EQUAL(true, search->isDone());
179  CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
180  //CPPUNIT_ASSERT_EQUAL(false, search->next());
181 
183 }
184 
static bool cbcalled
#define EBIT_CLR(flag, bit)
Definition: defines.h:68
#define INDEXSD(i)
Definition: SquidConfig.h:72
time_t timestamp
Definition: Store.h:225
CPPUNIT_TEST_SUITE_REGISTRATION(testStoreController)
manages a single cache_dir
Definition: Disk.h:22
unsigned char cache_key
Store key.
Definition: forward.h:29
void createMemObject()
Definition: store.cc:1592
void FreeMemory()
undo Init()
Definition: Controller.cc:947
virtual void init() override
Definition: Controller.cc:58
int objectsPerBucket
Definition: SquidConfig.h:269
#define xstrdup
C * getRaw() const
Definition: RefCount.h:80
uint16_t flags
Definition: Store.h:233
time_t expires
Definition: Store.h:227
Store::DiskConfig cacheSwap
Definition: SquidConfig.h:430
@ SWAPOUT_DONE
Definition: enums.h:64
virtual bool error() const =0
char * store_dir_select_algorithm
Definition: SquidConfig.h:517
#define NULL
Definition: types.h:166
static void commonInit()
@ ENTRY_VALIDATED
Definition: enums.h:113
uint16_t refcount
Definition: Store.h:232
sdirno swap_dirn
Definition: Store.h:239
static void searchCallback(void *)
ping_status_t ping_status
Definition: Store.h:243
void free_cachedir(Store::DiskConfig *swap)
Definition: Disks.cc:803
store_status_t store_status
Definition: Store.h:245
char const * termedBuf() const
Definition: SquidString.h:92
YesNoNone memShared
whether the memory cache is shared among workers
Definition: SquidConfig.h:87
void Stats(StoreEntry *output)
Definition: store.cc:124
virtual void next(void(callback)(void *cbdata), void *cbdata)=0
@ NOT_IN_MEMORY
Definition: enums.h:35
virtual bool isDone() const =0
virtual StoreEntry * currentItem()=0
@ PING_NONE
Has not considered whether to send ICP queries to peers yet.
Definition: enums.h:41
void allocate_new_swapdir(Store::DiskConfig &swap)
Definition: Disks.cc:784
static void addSwapDir(TestSwapDirPointer aStore)
StoreSearch * search()
Definition: Controller.cc:211
@ STORE_OK
Definition: enums.h:50
time_t squid_curtime
Definition: stub_time.cc:17
void Init(Controller *root=nullptr)
initialize the storage module; a custom root is used by unit tests only
Definition: Controller.cc:941
void setMemStatus(mem_status_t)
Definition: store.cc:1541
swap_status_t swap_status
Definition: Store.h:247
static StoreEntry * addedEntry(Store::Disk *aStore, String name, String, String)
struct SquidConfig::@109 Store
void Init()
Definition: old_api.cc:426
uint64_t swap_file_sz
Definition: Store.h:231
int64_t maxObjectSize
Definition: SquidConfig.h:271
int64_t avgObjectSize
Definition: SquidConfig.h:270
void hashInsert(const cache_key *)
Definition: store.cc:438
void lastModified(const time_t when)
Definition: Store.h:177
void defaultTo(bool beSet)
enables or disables the option; updating to 'implicit' state
Definition: YesNoNone.h:59
time_t lastref
Definition: Store.h:226
class SquidConfig Config
Definition: SquidConfig.cc:12
@ STORE_PENDING
Definition: enums.h:51
sfileno swap_filen
unique ID inside a cache_dir for swapped out entries; -1 for others
Definition: Store.h:237
Controller & Root()
safely access controller singleton
Definition: Controller.cc:934
RefCount< SwapDir > * swapDirs
Definition: SquidConfig.h:66

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors