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