2 * Copyright (C) 1996-2015 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
->makeMemObject();
34 logEntry
->mem_obj
->setUris("dummy_storeId", NULL
, HttpRequestMethod());
35 logEntry
->store_status
= STORE_PENDING
;
37 TestSwapDirPointer
aStore (new TestSwapDir
);
38 TestSwapDirPointer
aStore2 (new TestSwapDir
);
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
);
51 testStoreHashIndex::testMaxSize()
53 StoreEntry
*logEntry
= new StoreEntry
;
54 logEntry
->makeMemObject();
55 logEntry
->mem_obj
->setUris("dummy_storeId", NULL
, HttpRequestMethod());
56 logEntry
->store_status
= STORE_PENDING
;
58 TestSwapDirPointer
aStore (new TestSwapDir
);
59 TestSwapDirPointer
aStore2 (new TestSwapDir
);
62 CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize());
63 free_cachedir(&Config
.cacheSwap
);
68 addedEntry(Store::Disk
*aStore
,
75 StoreEntry
*e
= new StoreEntry();
76 e
->store_status
= STORE_OK
;
77 e
->setMemStatus(NOT_IN_MEMORY
);
78 e
->swap_status
= SWAPOUT_DONE
; /* bogus haha */
79 e
->swap_filen
= 0; /* garh - lower level*/
82 for (int i
=0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
83 if (INDEXSD(i
) == aStore
)
87 CPPUNIT_ASSERT (e
->swap_dirn
!= -1);
88 e
->swap_file_sz
= 0; /* garh lower level */
89 e
->lastref
= squid_curtime
;
90 e
->timestamp
= squid_curtime
;
91 e
->expires
= squid_curtime
;
92 e
->lastmod
= squid_curtime
;
94 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
95 EBIT_CLR(e
->flags
, KEY_PRIVATE
);
96 e
->ping_status
= PING_NONE
;
97 EBIT_CLR(e
->flags
, ENTRY_VALIDATED
);
98 e
->hashInsert((const cache_key
*)name
.termedBuf()); /* do it after we clear KEY_PRIVATE */
104 static bool inited
= false;
111 Config
.Store
.avgObjectSize
= 1024;
113 Config
.Store
.objectsPerBucket
= 20;
115 Config
.Store
.maxObjectSize
= 2048;
118 /* TODO make this a cbdata class */
120 static bool cbcalled
;
123 searchCallback(void *cbdata
)
129 testStoreHashIndex::testSearch()
133 TestSwapDirPointer
aStore (new TestSwapDir
);
134 TestSwapDirPointer
aStore2 (new TestSwapDir
);
137 Store::Root().init();
138 StoreEntry
* entry1
= addedEntry(aStore
.getRaw(), "name", NULL
, NULL
);
139 StoreEntry
* entry2
= addedEntry(aStore2
.getRaw(), "name2", NULL
, NULL
);
140 StoreSearchPointer search
= Store::Root().search(); /* search for everything in the store */
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
*>(NULL
), search
->currentItem());
148 CPPUNIT_ASSERT_EQUAL(false, search
->next());
151 /* trigger a callback */
153 search
->next(searchCallback
, NULL
);
154 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
156 /* we should have access to a entry now, that matches the entry we had before */
157 CPPUNIT_ASSERT_EQUAL(false, search
->error());
158 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
159 /* note the hash order is random - the test happens to be in a nice order */
160 CPPUNIT_ASSERT_EQUAL(entry1
, search
->currentItem());
161 //CPPUNIT_ASSERT_EQUAL(false, search->next());
163 /* trigger another callback */
165 search
->next(searchCallback
, NULL
);
166 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
168 /* we should have access to a entry now, that matches the entry we had before */
169 CPPUNIT_ASSERT_EQUAL(false, search
->error());
170 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
171 CPPUNIT_ASSERT_EQUAL(entry2
, search
->currentItem());
172 //CPPUNIT_ASSERT_EQUAL(false, search->next());
174 /* trigger another callback */
176 search
->next(searchCallback
, NULL
);
177 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
179 /* now we should have no error, we should have finished and have no current item */
180 CPPUNIT_ASSERT_EQUAL(false, search
->error());
181 CPPUNIT_ASSERT_EQUAL(true, search
->isDone());
182 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(NULL
), search
->currentItem());
183 //CPPUNIT_ASSERT_EQUAL(false, search->next());