]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testStoreHashIndex.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / tests / testStoreHashIndex.cc
1 #define SQUID_UNIT_TEST 1
2
3 #include "squid.h"
4 #include "testStoreHashIndex.h"
5 #include "Store.h"
6 #include "SwapDir.h"
7 #include "TestSwapDir.h"
8 #include "StoreHashIndex.h"
9 #include "Mem.h"
10 #include "StoreSearch.h"
11 #include "SquidTime.h"
12
13 CPPUNIT_TEST_SUITE_REGISTRATION( testStoreHashIndex );
14
15 static void
16 addSwapDir(TestSwapDirPointer aStore)
17 {
18 allocate_new_swapdir(&Config.cacheSwap);
19 Config.cacheSwap.swapDirs[Config.cacheSwap.n_configured] = aStore.getRaw();
20 ++Config.cacheSwap.n_configured;
21 }
22
23 void
24 testStoreHashIndex::testStats()
25 {
26 StoreEntry * logEntry = new StoreEntry("dummy_url", "dummy_log_url");
27 logEntry->store_status = STORE_PENDING;
28 StorePointer aRoot (new StoreHashIndex());
29 Store::Root(aRoot);
30 TestSwapDirPointer aStore (new TestSwapDir);
31 TestSwapDirPointer aStore2 (new TestSwapDir);
32 addSwapDir(aStore);
33 addSwapDir(aStore2);
34 CPPUNIT_ASSERT(aStore->statsCalled == false);
35 CPPUNIT_ASSERT(aStore2->statsCalled == false);
36 Store::Stats(logEntry);
37 free_cachedir(&Config.cacheSwap);
38 CPPUNIT_ASSERT(aStore->statsCalled == true);
39 CPPUNIT_ASSERT(aStore2->statsCalled == true);
40 Store::Root(NULL);
41 }
42
43 void
44 testStoreHashIndex::testMaxSize()
45 {
46 StoreEntry * logEntry = new StoreEntry("dummy_url", "dummy_log_url");
47 logEntry->store_status = STORE_PENDING;
48 StorePointer aRoot (new StoreHashIndex());
49 Store::Root(aRoot);
50 TestSwapDirPointer aStore (new TestSwapDir);
51 TestSwapDirPointer aStore2 (new TestSwapDir);
52 addSwapDir(aStore);
53 addSwapDir(aStore2);
54 CPPUNIT_ASSERT(Store::Root().maxSize() == 6);
55 free_cachedir(&Config.cacheSwap);
56 Store::Root(NULL);
57 }
58
59 StoreEntry *
60 addedEntry(StorePointer hashStore,
61 StorePointer aStore,
62 String name,
63 String varySpec,
64 String varyKey
65
66 )
67 {
68 StoreEntry *e = new StoreEntry();
69 e->store_status = STORE_OK;
70 e->setMemStatus(NOT_IN_MEMORY);
71 e->swap_status = SWAPOUT_DONE; /* bogus haha */
72 e->swap_filen = 0; /* garh - lower level*/
73 e->swap_dirn = -1;
74
75 for (int i=0; i < Config.cacheSwap.n_configured; ++i) {
76 if (INDEXSD (i) == aStore.getRaw())
77 e->swap_dirn = i;
78 }
79
80 CPPUNIT_ASSERT (e->swap_dirn != -1);
81 e->swap_file_sz = 0; /* garh lower level */
82 e->lock_count = 0;
83 e->lastref = squid_curtime;
84 e->timestamp = squid_curtime;
85 e->expires = squid_curtime;
86 e->lastmod = squid_curtime;
87 e->refcount = 1;
88 EBIT_SET(e->flags, ENTRY_CACHABLE);
89 EBIT_CLR(e->flags, RELEASE_REQUEST);
90 EBIT_CLR(e->flags, KEY_PRIVATE);
91 e->ping_status = PING_NONE;
92 EBIT_CLR(e->flags, ENTRY_VALIDATED);
93 e->hashInsert((const cache_key *)name.termedBuf()); /* do it after we clear KEY_PRIVATE */
94 return e;
95 }
96
97 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
108 Config.Store.objectsPerBucket = 20;
109
110 Config.Store.maxObjectSize = 2048;
111 }
112
113 /* TODO make this a cbdata class */
114
115 static bool cbcalled;
116
117 static void
118 searchCallback(void *cbdata)
119 {
120 cbcalled = true;
121 }
122
123 void
124 testStoreHashIndex::testSearch()
125 {
126 commonInit();
127 StorePointer aRoot (new StoreHashIndex());
128 Store::Root(aRoot);
129 TestSwapDirPointer aStore (new TestSwapDir);
130 TestSwapDirPointer aStore2 (new TestSwapDir);
131 addSwapDir(aStore);
132 addSwapDir(aStore2);
133 Store::Root().init();
134 StoreEntry * entry1 = addedEntry (&Store::Root(), aStore.getRaw(), "name", NULL, NULL);
135 StoreEntry * entry2 = addedEntry (&Store::Root(), aStore2.getRaw(), "name2", NULL, NULL);
136 StoreSearchPointer search = aRoot->search (NULL, NULL); /* search for everything in the store */
137
138 /* nothing should be immediately available */
139 CPPUNIT_ASSERT(search->error() == false);
140 CPPUNIT_ASSERT(search->isDone() == false);
141 CPPUNIT_ASSERT(search->currentItem() == NULL);
142 #if 0
143
144 CPPUNIT_ASSERT(search->next() == false);
145 #endif
146
147 /* trigger a callback */
148 cbcalled = false;
149 search->next(searchCallback, NULL);
150 CPPUNIT_ASSERT(cbcalled == true);
151
152 /* we should have access to a entry now, that matches the entry we had before */
153 CPPUNIT_ASSERT(search->error() == false);
154 CPPUNIT_ASSERT(search->isDone() == false);
155 /* note the hash order is random - the test happens to be in a nice order */
156 CPPUNIT_ASSERT(search->currentItem() == entry1);
157 //CPPUNIT_ASSERT(search->next() == false);
158
159 /* trigger another callback */
160 cbcalled = false;
161 search->next(searchCallback, NULL);
162 CPPUNIT_ASSERT(cbcalled == true);
163
164 /* we should have access to a entry now, that matches the entry we had before */
165 CPPUNIT_ASSERT(search->error() == false);
166 CPPUNIT_ASSERT(search->isDone() == false);
167 CPPUNIT_ASSERT(search->currentItem() == entry2);
168 //CPPUNIT_ASSERT(search->next() == false);
169
170 /* trigger another callback */
171 cbcalled = false;
172 search->next(searchCallback, NULL);
173 CPPUNIT_ASSERT(cbcalled == true);
174
175 /* now we should have no error, we should have finished and have no current item */
176 CPPUNIT_ASSERT(search->error() == false);
177 CPPUNIT_ASSERT(search->isDone() == true);
178 CPPUNIT_ASSERT(search->currentItem() == NULL);
179 //CPPUNIT_ASSERT(search->next() == false);
180
181 Store::Root(NULL);
182 }