testStoreHashIndex.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 "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 static StoreEntry *
67  String name,
68  String,
69  String
70  )
71 {
72  StoreEntry *e = new StoreEntry();
75  e->swap_status = SWAPOUT_DONE; /* bogus haha */
76  e->swap_filen = 0; /* garh - lower level*/
77  e->swap_dirn = -1;
78 
79  for (int i=0; i < Config.cacheSwap.n_configured; ++i) {
80  if (INDEXSD(i) == aStore)
81  e->swap_dirn = i;
82  }
83 
84  CPPUNIT_ASSERT (e->swap_dirn != -1);
85  e->swap_file_sz = 0; /* garh lower level */
90  e->refcount = 1;
93  e->hashInsert((const cache_key *)name.termedBuf()); /* do it after we clear KEY_PRIVATE */
94  return e;
95 }
96 
97 static void commonInit()
98 {
99  static bool inited = false;
100 
101  if (inited)
102  return;
103 
104  Mem::Init();
105 
106  Config.Store.avgObjectSize = 1024;
107 
109 
110  Config.Store.maxObjectSize = 2048;
111 }
112 
113 /* TODO make this a cbdata class */
114 
115 static bool cbcalled;
116 
117 static void
119 {
120  cbcalled = true;
121 }
122 
123 void
125 {
126  commonInit();
127  Store::Init();
128  TestSwapDirPointer aStore (new TestSwapDir);
129  TestSwapDirPointer aStore2 (new TestSwapDir);
130  addSwapDir(aStore);
131  addSwapDir(aStore2);
132  Store::Root().init();
133  StoreEntry * entry1 = addedEntry(aStore.getRaw(), "name", NULL, NULL);
134  StoreEntry * entry2 = addedEntry(aStore2.getRaw(), "name2", NULL, NULL);
135  StoreSearchPointer search = Store::Root().search(); /* search for everything in the store */
136 
137  /* nothing should be immediately available */
138  CPPUNIT_ASSERT_EQUAL(false, search->error());
139  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
140  CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
141 
142  /* trigger a callback */
143  cbcalled = false;
144  search->next(searchCallback, NULL);
145  CPPUNIT_ASSERT_EQUAL(true, cbcalled);
146 
147  /* we should have access to a entry now, that matches the entry we had before */
148  CPPUNIT_ASSERT_EQUAL(false, search->error());
149  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
150  /* note the hash order is random - the test happens to be in a nice order */
151  CPPUNIT_ASSERT_EQUAL(entry1, search->currentItem());
152  //CPPUNIT_ASSERT_EQUAL(false, search->next());
153 
154  /* trigger another callback */
155  cbcalled = false;
156  search->next(searchCallback, NULL);
157  CPPUNIT_ASSERT_EQUAL(true, cbcalled);
158 
159  /* we should have access to a entry now, that matches the entry we had before */
160  CPPUNIT_ASSERT_EQUAL(false, search->error());
161  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
162  CPPUNIT_ASSERT_EQUAL(entry2, 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  /* now we should have no error, we should have finished and have no current item */
171  CPPUNIT_ASSERT_EQUAL(false, search->error());
172  CPPUNIT_ASSERT_EQUAL(true, search->isDone());
173  CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
174  //CPPUNIT_ASSERT_EQUAL(false, search->next());
175 
177 }
178 
#define EBIT_CLR(flag, bit)
Definition: defines.h:68
static void commonInit()
#define INDEXSD(i)
Definition: SquidConfig.h:72
time_t timestamp
Definition: Store.h:225
static bool cbcalled
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
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
#define NULL
Definition: types.h:166
@ ENTRY_VALIDATED
Definition: enums.h:113
static void addSwapDir(TestSwapDirPointer aStore)
uint16_t refcount
Definition: Store.h:232
CPPUNIT_TEST_SUITE_REGISTRATION(testStoreHashIndex)
sdirno swap_dirn
Definition: Store.h:239
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
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
static void searchCallback(void *)
void allocate_new_swapdir(Store::DiskConfig &swap)
Definition: Disks.cc:784
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
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