testStoreHashIndex.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2022 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 "Store.h"
13 #include "store/Disks.h"
14 #include "StoreSearch.h"
15 #include "testStoreHashIndex.h"
16 #include "TestSwapDir.h"
17 
19 
20 static void
22 {
26 }
27 
28 void
30 {
31  StoreEntry *logEntry = new StoreEntry;
32  logEntry->createMemObject("dummy_storeId", NULL, HttpRequestMethod());
33  logEntry->store_status = STORE_PENDING;
34  Store::Init();
35  TestSwapDirPointer aStore (new TestSwapDir);
36  TestSwapDirPointer aStore2 (new TestSwapDir);
37  addSwapDir(aStore);
38  addSwapDir(aStore2);
39  CPPUNIT_ASSERT_EQUAL(false, aStore->statsCalled);
40  CPPUNIT_ASSERT_EQUAL(false, aStore2->statsCalled);
41  Store::Stats(logEntry);
43  CPPUNIT_ASSERT_EQUAL(true, aStore->statsCalled);
44  CPPUNIT_ASSERT_EQUAL(true, aStore2->statsCalled);
46 }
47 
48 void
50 {
51  StoreEntry *logEntry = new StoreEntry;
52  logEntry->createMemObject("dummy_storeId", NULL, HttpRequestMethod());
53  logEntry->store_status = STORE_PENDING;
54  Store::Init();
55  TestSwapDirPointer aStore (new TestSwapDir);
56  TestSwapDirPointer aStore2 (new TestSwapDir);
57  addSwapDir(aStore);
58  addSwapDir(aStore2);
59  CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize());
62 }
63 
64 static StoreEntry *
66  String name,
67  String,
68  String
69  )
70 {
71  StoreEntry *e = new StoreEntry();
74  e->swap_status = SWAPOUT_DONE; /* bogus haha */
75  e->swap_filen = 0; /* garh - lower level*/
76  e->swap_dirn = -1;
77 
78  for (int i=0; i < Config.cacheSwap.n_configured; ++i) {
79  if (INDEXSD(i) == aStore)
80  e->swap_dirn = i;
81  }
82 
83  CPPUNIT_ASSERT (e->swap_dirn != -1);
84  e->swap_file_sz = 0; /* garh lower level */
89  e->refcount = 1;
92  e->hashInsert((const cache_key *)name.termedBuf()); /* do it after we clear KEY_PRIVATE */
93  return e;
94 }
95 
96 static void commonInit()
97 {
98  static bool inited = false;
99 
100  if (inited)
101  return;
102 
103  Mem::Init();
104 
105  Config.Store.avgObjectSize = 1024;
106 
108 
109  Config.Store.maxObjectSize = 2048;
110 }
111 
112 /* TODO make this a cbdata class */
113 
114 static bool cbcalled;
115 
116 static void
118 {
119  cbcalled = true;
120 }
121 
122 void
124 {
125  commonInit();
126  Store::Init();
127  TestSwapDirPointer aStore (new TestSwapDir);
128  TestSwapDirPointer aStore2 (new TestSwapDir);
129  addSwapDir(aStore);
130  addSwapDir(aStore2);
131  Store::Root().init();
132  StoreEntry * entry1 = addedEntry(aStore.getRaw(), "name", NULL, NULL);
133  StoreEntry * entry2 = addedEntry(aStore2.getRaw(), "name2", NULL, NULL);
134  StoreSearchPointer search = Store::Root().search(); /* search for everything in the store */
135 
136  /* nothing should be immediately available */
137  CPPUNIT_ASSERT_EQUAL(false, search->error());
138  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
139  CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
140 
141  /* trigger a callback */
142  cbcalled = false;
143  search->next(searchCallback, NULL);
144  CPPUNIT_ASSERT_EQUAL(true, cbcalled);
145 
146  /* we should have access to a entry now, that matches the entry we had before */
147  CPPUNIT_ASSERT_EQUAL(false, search->error());
148  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
149  /* note the hash order is random - the test happens to be in a nice order */
150  CPPUNIT_ASSERT_EQUAL(entry1, search->currentItem());
151  //CPPUNIT_ASSERT_EQUAL(false, search->next());
152 
153  /* trigger another 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  CPPUNIT_ASSERT_EQUAL(entry2, search->currentItem());
162  //CPPUNIT_ASSERT_EQUAL(false, search->next());
163 
164  /* trigger another callback */
165  cbcalled = false;
166  search->next(searchCallback, NULL);
167  CPPUNIT_ASSERT_EQUAL(true, cbcalled);
168 
169  /* now we should have no error, we should have finished and have no current item */
170  CPPUNIT_ASSERT_EQUAL(false, search->error());
171  CPPUNIT_ASSERT_EQUAL(true, search->isDone());
172  CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
173  //CPPUNIT_ASSERT_EQUAL(false, search->next());
174 
176 }
177 
#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:222
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:1553
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:230
time_t expires
Definition: Store.h:224
Store::DiskConfig cacheSwap
Definition: SquidConfig.h:429
@ 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:229
CPPUNIT_TEST_SUITE_REGISTRATION(testStoreHashIndex)
sdirno swap_dirn
Definition: Store.h:236
ping_status_t ping_status
Definition: Store.h:240
void free_cachedir(Store::DiskConfig *swap)
Definition: Disks.cc:802
store_status_t store_status
Definition: Store.h:242
char const * termedBuf() const
Definition: SquidString.h:92
void Stats(StoreEntry *output)
Definition: store.cc:123
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:783
StoreSearch * search()
Definition: Controller.cc:211
@ STORE_OK
Definition: enums.h:50
time_t squid_curtime
Definition: stub_libtime.cc:20
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:1502
swap_status_t swap_status
Definition: Store.h:244
static StoreEntry * addedEntry(Store::Disk *aStore, String name, String, String)
void Init()
Definition: old_api.cc:425
uint64_t swap_file_sz
Definition: Store.h:228
int64_t maxObjectSize
Definition: SquidConfig.h:271
int64_t avgObjectSize
Definition: SquidConfig.h:270
void hashInsert(const cache_key *)
Definition: store.cc:399
void lastModified(const time_t when)
Definition: Store.h:174
time_t lastref
Definition: Store.h:223
class SquidConfig Config
Definition: SquidConfig.cc:12
struct SquidConfig::@108 Store
@ 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:234
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