2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
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.
10 #include "compat/cppunit.h"
11 #include "MemObject.h"
12 #include "SquidConfig.h"
14 #include "store/Disks.h"
15 #include "StoreSearch.h"
16 #include "TestSwapDir.h"
19 * test the store framework
22 class TestStoreHashIndex
: public CPPUNIT_NS::TestFixture
24 CPPUNIT_TEST_SUITE(TestStoreHashIndex
);
25 CPPUNIT_TEST(testStats
);
26 CPPUNIT_TEST(testMaxSize
);
27 CPPUNIT_TEST(testSearch
);
28 CPPUNIT_TEST_SUITE_END();
36 CPPUNIT_TEST_SUITE_REGISTRATION(TestStoreHashIndex
);
39 addSwapDir(TestSwapDirPointer aStore
)
41 allocate_new_swapdir(Config
.cacheSwap
);
42 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
43 ++Config
.cacheSwap
.n_configured
;
47 TestStoreHashIndex::testStats()
49 StoreEntry
*logEntry
= new StoreEntry
;
50 logEntry
->createMemObject("dummy_storeId", nullptr, HttpRequestMethod());
51 logEntry
->store_status
= STORE_PENDING
;
52 TestSwapDirPointer
aStore (new TestSwapDir
);
53 TestSwapDirPointer
aStore2 (new TestSwapDir
);
56 CPPUNIT_ASSERT_EQUAL(false, aStore
->statsCalled
);
57 CPPUNIT_ASSERT_EQUAL(false, aStore2
->statsCalled
);
58 Store::Stats(logEntry
);
59 free_cachedir(&Config
.cacheSwap
);
60 CPPUNIT_ASSERT_EQUAL(true, aStore
->statsCalled
);
61 CPPUNIT_ASSERT_EQUAL(true, aStore2
->statsCalled
);
65 TestStoreHashIndex::testMaxSize()
67 StoreEntry
*logEntry
= new StoreEntry
;
68 logEntry
->createMemObject("dummy_storeId", nullptr, HttpRequestMethod());
69 logEntry
->store_status
= STORE_PENDING
;
70 TestSwapDirPointer
aStore (new TestSwapDir
);
71 TestSwapDirPointer
aStore2 (new TestSwapDir
);
74 CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize());
75 free_cachedir(&Config
.cacheSwap
);
79 addedEntry(Store::Disk
*aStore
,
85 StoreEntry
*e
= new StoreEntry();
86 e
->store_status
= STORE_OK
;
87 e
->setMemStatus(NOT_IN_MEMORY
);
88 e
->swap_status
= SWAPOUT_DONE
; /* bogus haha */
89 e
->swap_filen
= 0; /* garh - lower level*/
92 for (int i
=0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
93 if (INDEXSD(i
) == aStore
)
97 CPPUNIT_ASSERT (e
->swap_dirn
!= -1);
98 e
->swap_file_sz
= 0; /* garh lower level */
99 e
->lastref
= squid_curtime
;
100 e
->timestamp
= squid_curtime
;
101 e
->expires
= squid_curtime
;
102 e
->lastModified(squid_curtime
);
104 e
->ping_status
= PING_NONE
;
105 EBIT_CLR(e
->flags
, ENTRY_VALIDATED
);
106 e
->hashInsert((const cache_key
*)name
.termedBuf()); /* do it after we clear KEY_PRIVATE */
110 static void commonInit()
112 static bool inited
= false;
121 Config
.Store
.avgObjectSize
= 1024;
123 Config
.Store
.objectsPerBucket
= 20;
125 Config
.Store
.maxObjectSize
= 2048;
127 Config
.memShared
.defaultTo(false);
129 Config
.store_dir_select_algorithm
= xstrdup("round-robin");
132 /* TODO make this a cbdata class */
134 static bool cbcalled
;
137 searchCallback(void *)
143 TestStoreHashIndex::testSearch()
146 TestSwapDirPointer
aStore (new TestSwapDir
);
147 TestSwapDirPointer
aStore2 (new TestSwapDir
);
150 Store::Root().init();
151 StoreEntry
* entry1
= addedEntry(aStore
.getRaw(), "name", nullptr, nullptr);
152 StoreEntry
* entry2
= addedEntry(aStore2
.getRaw(), "name2", nullptr, nullptr);
153 StoreSearchPointer search
= Store::Root().search(); /* search for everything in the store */
155 /* nothing should be immediately available */
156 CPPUNIT_ASSERT_EQUAL(false, search
->error());
157 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
158 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(nullptr), search
->currentItem());
160 /* trigger a callback */
162 search
->next(searchCallback
, nullptr);
163 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
165 /* we should have access to a entry now, that matches the entry we had before */
166 CPPUNIT_ASSERT_EQUAL(false, search
->error());
167 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
168 /* note the hash order is random - the test happens to be in a nice order */
169 CPPUNIT_ASSERT_EQUAL(entry1
, search
->currentItem());
170 //CPPUNIT_ASSERT_EQUAL(false, search->next());
172 /* trigger another callback */
174 search
->next(searchCallback
, nullptr);
175 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
177 /* we should have access to a entry now, that matches the entry we had before */
178 CPPUNIT_ASSERT_EQUAL(false, search
->error());
179 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
180 CPPUNIT_ASSERT_EQUAL(entry2
, search
->currentItem());
181 //CPPUNIT_ASSERT_EQUAL(false, search->next());
183 /* trigger another callback */
185 search
->next(searchCallback
, nullptr);
186 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
188 /* now we should have no error, we should have finished and have no current item */
189 CPPUNIT_ASSERT_EQUAL(false, search
->error());
190 CPPUNIT_ASSERT_EQUAL(true, search
->isDone());
191 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(nullptr), search
->currentItem());
192 //CPPUNIT_ASSERT_EQUAL(false, search->next());
195 // This test uses main() from ./testStore.cc.