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