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