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