testStoreHashIndex.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 "testStoreHashIndex.h"
17 #include "TestSwapDir.h"
18 
20 
21 static void
23 {
27 }
28 
29 void
31 {
32  StoreEntry *logEntry = new StoreEntry;
33  logEntry->createMemObject("dummy_storeId", NULL, HttpRequestMethod());
34  logEntry->store_status = STORE_PENDING;
35  Store::Init();
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 void
51 {
52  StoreEntry *logEntry = new StoreEntry;
53  logEntry->createMemObject("dummy_storeId", NULL, HttpRequestMethod());
54  logEntry->store_status = STORE_PENDING;
55  Store::Init();
56  TestSwapDirPointer aStore (new TestSwapDir);
57  TestSwapDirPointer aStore2 (new TestSwapDir);
58  addSwapDir(aStore);
59  addSwapDir(aStore2);
60  CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize());
63 }
64 
65 StoreEntry *
67  String name,
68  String varySpec,
69  String varyKey
70 
71  )
72 {
73  StoreEntry *e = new StoreEntry();
76  e->swap_status = SWAPOUT_DONE; /* bogus haha */
77  e->swap_filen = 0; /* garh - lower level*/
78  e->swap_dirn = -1;
79 
80  for (int i=0; i < Config.cacheSwap.n_configured; ++i) {
81  if (INDEXSD(i) == aStore)
82  e->swap_dirn = i;
83  }
84 
85  CPPUNIT_ASSERT (e->swap_dirn != -1);
86  e->swap_file_sz = 0; /* garh lower level */
91  e->refcount = 1;
93  e->clearPrivate();
96  e->hashInsert((const cache_key *)name.termedBuf()); /* do it after we clear KEY_PRIVATE */
97  return e;
98 }
99 
101 {
102  static bool inited = false;
103 
104  if (inited)
105  return;
106 
107  Mem::Init();
108 
109  Config.Store.avgObjectSize = 1024;
110 
112 
113  Config.Store.maxObjectSize = 2048;
114 }
115 
116 /* TODO make this a cbdata class */
117 
118 static bool cbcalled;
119 
120 static void
122 {
123  cbcalled = true;
124 }
125 
126 void
128 {
129  commonInit();
130  Store::Init();
131  TestSwapDirPointer aStore (new TestSwapDir);
132  TestSwapDirPointer aStore2 (new TestSwapDir);
133  addSwapDir(aStore);
134  addSwapDir(aStore2);
135  Store::Root().init();
136  StoreEntry * entry1 = addedEntry(aStore.getRaw(), "name", NULL, NULL);
137  StoreEntry * entry2 = addedEntry(aStore2.getRaw(), "name2", NULL, NULL);
138  StoreSearchPointer search = Store::Root().search(); /* search for everything in the store */
139 
140  /* nothing should be immediately available */
141  CPPUNIT_ASSERT_EQUAL(false, search->error());
142  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
143  CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
144 #if 0
145 
146  CPPUNIT_ASSERT_EQUAL(false, search->next());
147 #endif
148 
149  /* trigger a callback */
150  cbcalled = false;
151  search->next(searchCallback, NULL);
152  CPPUNIT_ASSERT_EQUAL(true, cbcalled);
153 
154  /* we should have access to a entry now, that matches the entry we had before */
155  CPPUNIT_ASSERT_EQUAL(false, search->error());
156  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
157  /* note the hash order is random - the test happens to be in a nice order */
158  CPPUNIT_ASSERT_EQUAL(entry1, search->currentItem());
159  //CPPUNIT_ASSERT_EQUAL(false, search->next());
160 
161  /* trigger another callback */
162  cbcalled = false;
163  search->next(searchCallback, NULL);
164  CPPUNIT_ASSERT_EQUAL(true, cbcalled);
165 
166  /* we should have access to a entry now, that matches the entry we had before */
167  CPPUNIT_ASSERT_EQUAL(false, search->error());
168  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
169  CPPUNIT_ASSERT_EQUAL(entry2, search->currentItem());
170  //CPPUNIT_ASSERT_EQUAL(false, search->next());
171 
172  /* trigger another callback */
173  cbcalled = false;
174  search->next(searchCallback, NULL);
175  CPPUNIT_ASSERT_EQUAL(true, cbcalled);
176 
177  /* now we should have no error, we should have finished and have no current item */
178  CPPUNIT_ASSERT_EQUAL(false, search->error());
179  CPPUNIT_ASSERT_EQUAL(true, search->isDone());
180  CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
181  //CPPUNIT_ASSERT_EQUAL(false, search->next());
182 
184 }
185 
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
Definition: cbdata.cc:60
CPPUNIT_TEST_SUITE_REGISTRATION(testStoreHashIndex)
unsigned char cache_key
Store key.
Definition: forward.h:29
int i
Definition: membanger.c:49
void lastModified(const time_t when)
Definition: Store.h:142
void Stats(StoreEntry *output)
Definition: store.cc:115
static void searchCallback(void *cbdata)
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
void commonInit()
char const * termedBuf() const
Definition: SquidString.h:90
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
StoreEntry * addedEntry(Store::Disk *aStore, String name, String varySpec, String varyKey)
uint16_t refcount
Definition: Store.h:172
static bool cbcalled
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
swap_status_t swap_status
Definition: Store.h:187
static void addSwapDir(TestSwapDirPointer aStore)
virtual void init() override
Definition: Controller.cc:58
void clearPrivate()
Definition: store.cc:158
time_t lastref
Definition: Store.h:166
C * getRaw() const
Definition: RefCount.h:74
class SquidConfig Config
Definition: SquidConfig.cc:12
#define NULL
Definition: types.h:166
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