2 * Copyright (C) 1996-2018 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 "MemObject.h"
11 #include "SquidConfig.h"
12 #include "SquidTime.h"
14 #include "store/Disks.h"
15 #include "StoreSearch.h"
16 #include "testStoreHashIndex.h"
17 #include "TestSwapDir.h"
19 CPPUNIT_TEST_SUITE_REGISTRATION( testStoreHashIndex
);
22 addSwapDir(TestSwapDirPointer aStore
)
24 allocate_new_swapdir(&Config
.cacheSwap
);
25 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
26 ++Config
.cacheSwap
.n_configured
;
30 testStoreHashIndex::testStats()
32 StoreEntry
*logEntry
= new StoreEntry
;
33 logEntry
->createMemObject("dummy_storeId", NULL
, HttpRequestMethod());
34 logEntry
->store_status
= STORE_PENDING
;
36 TestSwapDirPointer
aStore (new TestSwapDir
);
37 TestSwapDirPointer
aStore2 (new TestSwapDir
);
40 CPPUNIT_ASSERT_EQUAL(false, aStore
->statsCalled
);
41 CPPUNIT_ASSERT_EQUAL(false, aStore2
->statsCalled
);
42 Store::Stats(logEntry
);
43 free_cachedir(&Config
.cacheSwap
);
44 CPPUNIT_ASSERT_EQUAL(true, aStore
->statsCalled
);
45 CPPUNIT_ASSERT_EQUAL(true, aStore2
->statsCalled
);
50 testStoreHashIndex::testMaxSize()
52 StoreEntry
*logEntry
= new StoreEntry
;
53 logEntry
->createMemObject("dummy_storeId", NULL
, HttpRequestMethod());
54 logEntry
->store_status
= STORE_PENDING
;
56 TestSwapDirPointer
aStore (new TestSwapDir
);
57 TestSwapDirPointer
aStore2 (new TestSwapDir
);
60 CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize());
61 free_cachedir(&Config
.cacheSwap
);
66 addedEntry(Store::Disk
*aStore
,
73 StoreEntry
*e
= new StoreEntry();
74 e
->store_status
= STORE_OK
;
75 e
->setMemStatus(NOT_IN_MEMORY
);
76 e
->swap_status
= SWAPOUT_DONE
; /* bogus haha */
77 e
->swap_filen
= 0; /* garh - lower level*/
80 for (int i
=0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
81 if (INDEXSD(i
) == aStore
)
85 CPPUNIT_ASSERT (e
->swap_dirn
!= -1);
86 e
->swap_file_sz
= 0; /* garh lower level */
87 e
->lastref
= squid_curtime
;
88 e
->timestamp
= squid_curtime
;
89 e
->expires
= squid_curtime
;
90 e
->lastModified(squid_curtime
);
92 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
94 e
->ping_status
= PING_NONE
;
95 EBIT_CLR(e
->flags
, ENTRY_VALIDATED
);
96 e
->hashInsert((const cache_key
*)name
.termedBuf()); /* do it after we clear KEY_PRIVATE */
102 static bool inited
= false;
109 Config
.Store
.avgObjectSize
= 1024;
111 Config
.Store
.objectsPerBucket
= 20;
113 Config
.Store
.maxObjectSize
= 2048;
116 /* TODO make this a cbdata class */
118 static bool cbcalled
;
121 searchCallback(void *cbdata
)
127 testStoreHashIndex::testSearch()
131 TestSwapDirPointer
aStore (new TestSwapDir
);
132 TestSwapDirPointer
aStore2 (new TestSwapDir
);
135 Store::Root().init();
136 StoreEntry
* entry1
= addedEntry(aStore
.getRaw(), "name", NULL
, NULL
);
137 StoreEntry
* entry2
= addedEntry(aStore2
.getRaw(), "name2", NULL
, NULL
);
138 StoreSearchPointer search
= Store::Root().search(); /* search for everything in the store */
140 /* nothing should be immediately available */
141 CPPUNIT_ASSERT_EQUAL(false, search
->error());
142 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
143 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(NULL
), search
->currentItem());
146 CPPUNIT_ASSERT_EQUAL(false, search
->next());
149 /* trigger a callback */
151 search
->next(searchCallback
, NULL
);
152 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
154 /* we should have access to a entry now, that matches the entry we had before */
155 CPPUNIT_ASSERT_EQUAL(false, search
->error());
156 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
157 /* note the hash order is random - the test happens to be in a nice order */
158 CPPUNIT_ASSERT_EQUAL(entry1
, search
->currentItem());
159 //CPPUNIT_ASSERT_EQUAL(false, search->next());
161 /* trigger another callback */
163 search
->next(searchCallback
, NULL
);
164 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
166 /* we should have access to a entry now, that matches the entry we had before */
167 CPPUNIT_ASSERT_EQUAL(false, search
->error());
168 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
169 CPPUNIT_ASSERT_EQUAL(entry2
, search
->currentItem());
170 //CPPUNIT_ASSERT_EQUAL(false, search->next());
172 /* trigger another callback */
174 search
->next(searchCallback
, NULL
);
175 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
177 /* now we should have no error, we should have finished and have no current item */
178 CPPUNIT_ASSERT_EQUAL(false, search
->error());
179 CPPUNIT_ASSERT_EQUAL(true, search
->isDone());
180 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(NULL
), search
->currentItem());
181 //CPPUNIT_ASSERT_EQUAL(false, search->next());