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