]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testStoreController.cc
90bc4d57f0d8377b2afa5f56d16822bfcf3059a2
[thirdparty/squid.git] / src / tests / testStoreController.cc
1 /*
2 * Copyright (C) 1996-2022 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 "Store.h"
13 #include "store/Disks.h"
14 #include "StoreSearch.h"
15 #include "testStoreController.h"
16 #include "TestSwapDir.h"
17
18 CPPUNIT_TEST_SUITE_REGISTRATION( testStoreController );
19
20 static void
21 addSwapDir(TestSwapDirPointer aStore)
22 {
23 allocate_new_swapdir(Config.cacheSwap);
24 Config.cacheSwap.swapDirs[Config.cacheSwap.n_configured] = aStore.getRaw();
25 ++Config.cacheSwap.n_configured;
26 }
27
28 void
29 testStoreController::testStats()
30 {
31 Store::Init();
32 StoreEntry *logEntry = new StoreEntry;
33 logEntry->createMemObject("dummy_storeId", nullptr, HttpRequestMethod());
34 logEntry->store_status = STORE_PENDING;
35 TestSwapDirPointer aStore (new TestSwapDir);
36 TestSwapDirPointer aStore2 (new TestSwapDir);
37 addSwapDir(aStore);
38 addSwapDir(aStore2);
39 CPPUNIT_ASSERT_EQUAL(false, aStore->statsCalled);
40 CPPUNIT_ASSERT_EQUAL(false, aStore2->statsCalled);
41 Store::Stats(logEntry);
42 free_cachedir(&Config.cacheSwap);
43 CPPUNIT_ASSERT_EQUAL(true, aStore->statsCalled);
44 CPPUNIT_ASSERT_EQUAL(true, aStore2->statsCalled);
45 Store::FreeMemory();
46 }
47
48 static void
49 commonInit()
50 {
51 static bool inited = false;
52
53 if (inited)
54 return;
55
56 Config.memShared.defaultTo(false);
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->createMemObject("dummy_storeId", nullptr, HttpRequestMethod());
75 logEntry->store_status = STORE_PENDING;
76 Store::Init();
77 TestSwapDirPointer aStore (new TestSwapDir);
78 TestSwapDirPointer aStore2 (new TestSwapDir);
79 addSwapDir(aStore);
80 addSwapDir(aStore2);
81 CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize());
82 free_cachedir(&Config.cacheSwap);
83 Store::FreeMemory();
84 }
85
86 static StoreEntry *
87 addedEntry(Store::Disk *aStore,
88 String name,
89 String,
90 String
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)
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->lastModified(squid_curtime);
111 e->refcount = 1;
112 e->ping_status = PING_NONE;
113 EBIT_CLR(e->flags, ENTRY_VALIDATED);
114 e->hashInsert((const cache_key *)name.termedBuf()); /* do it after we clear KEY_PRIVATE */
115 return e;
116 }
117
118 /* TODO make this a cbdata class */
119
120 static bool cbcalled;
121
122 static void
123 searchCallback(void *)
124 {
125 cbcalled = true;
126 }
127
128 void
129 testStoreController::testSearch()
130 {
131 commonInit();
132 Store::Init();
133 TestSwapDirPointer aStore (new TestSwapDir);
134 TestSwapDirPointer aStore2 (new TestSwapDir);
135 addSwapDir(aStore);
136 addSwapDir(aStore2);
137 Store::Root().init();
138 StoreEntry * entry1 = addedEntry(aStore.getRaw(), "name", nullptr, nullptr);
139 StoreEntry * entry2 = addedEntry(aStore2.getRaw(), "name2", nullptr, nullptr);
140 StoreSearchPointer search = Store::Root().search(); /* search for everything in the store */
141
142 /* nothing should be immediately available */
143 CPPUNIT_ASSERT_EQUAL(false, search->error());
144 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
145 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(nullptr), search->currentItem());
146
147 /* trigger a callback */
148 cbcalled = false;
149 search->next(searchCallback, nullptr);
150 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
151
152 /* we should have access to a entry now, that matches the entry we had before */
153 CPPUNIT_ASSERT_EQUAL(false, search->error());
154 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
155 /* note the hash order is random - the test happens to be in a nice order */
156 CPPUNIT_ASSERT_EQUAL(entry1, search->currentItem());
157 //CPPUNIT_ASSERT_EQUAL(false, search->next());
158
159 /* trigger another callback */
160 cbcalled = false;
161 search->next(searchCallback, nullptr);
162 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
163
164 /* we should have access to a entry now, that matches the entry we had before */
165 CPPUNIT_ASSERT_EQUAL(false, search->error());
166 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
167 CPPUNIT_ASSERT_EQUAL(entry2, search->currentItem());
168 //CPPUNIT_ASSERT_EQUAL(false, search->next());
169
170 /* trigger another callback */
171 cbcalled = false;
172 search->next(searchCallback, nullptr);
173 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
174
175 /* now we should have no error, we should have finished and have no current item */
176 CPPUNIT_ASSERT_EQUAL(false, search->error());
177 CPPUNIT_ASSERT_EQUAL(true, search->isDone());
178 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(nullptr), search->currentItem());
179 //CPPUNIT_ASSERT_EQUAL(false, search->next());
180
181 Store::FreeMemory();
182 }
183