2 * Copyright (C) 1996-2014 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.
11 #include "MemObject.h"
12 #include "SquidConfig.h"
13 #include "SquidTime.h"
15 #include "StoreHashIndex.h"
16 #include "StoreSearch.h"
18 #include "testStoreHashIndex.h"
19 #include "TestSwapDir.h"
21 CPPUNIT_TEST_SUITE_REGISTRATION( testStoreHashIndex
);
24 addSwapDir(TestSwapDirPointer aStore
)
26 allocate_new_swapdir(&Config
.cacheSwap
);
27 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
28 ++Config
.cacheSwap
.n_configured
;
32 testStoreHashIndex::testStats()
34 StoreEntry
*logEntry
= new StoreEntry
;
35 logEntry
->makeMemObject();
36 logEntry
->mem_obj
->setUris("dummy_storeId", NULL
, HttpRequestMethod());
37 logEntry
->store_status
= STORE_PENDING
;
38 StorePointer
aRoot (new StoreHashIndex());
40 TestSwapDirPointer
aStore (new TestSwapDir
);
41 TestSwapDirPointer
aStore2 (new TestSwapDir
);
44 CPPUNIT_ASSERT_EQUAL(false, aStore
->statsCalled
);
45 CPPUNIT_ASSERT_EQUAL(false, aStore2
->statsCalled
);
46 Store::Stats(logEntry
);
47 free_cachedir(&Config
.cacheSwap
);
48 CPPUNIT_ASSERT_EQUAL(true, aStore
->statsCalled
);
49 CPPUNIT_ASSERT_EQUAL(true, aStore2
->statsCalled
);
54 testStoreHashIndex::testMaxSize()
56 StoreEntry
*logEntry
= new StoreEntry
;
57 logEntry
->makeMemObject();
58 logEntry
->mem_obj
->setUris("dummy_storeId", NULL
, HttpRequestMethod());
59 logEntry
->store_status
= STORE_PENDING
;
60 StorePointer
aRoot (new StoreHashIndex());
62 TestSwapDirPointer
aStore (new TestSwapDir
);
63 TestSwapDirPointer
aStore2 (new TestSwapDir
);
66 CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize());
67 free_cachedir(&Config
.cacheSwap
);
72 addedEntry(StorePointer hashStore
,
80 StoreEntry
*e
= new StoreEntry();
81 e
->store_status
= STORE_OK
;
82 e
->setMemStatus(NOT_IN_MEMORY
);
83 e
->swap_status
= SWAPOUT_DONE
; /* bogus haha */
84 e
->swap_filen
= 0; /* garh - lower level*/
87 for (int i
=0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
88 if (INDEXSD (i
) == aStore
.getRaw())
92 CPPUNIT_ASSERT (e
->swap_dirn
!= -1);
93 e
->swap_file_sz
= 0; /* garh lower level */
94 e
->lastref
= squid_curtime
;
95 e
->timestamp
= squid_curtime
;
96 e
->expires
= squid_curtime
;
97 e
->lastmod
= squid_curtime
;
99 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
100 EBIT_CLR(e
->flags
, KEY_PRIVATE
);
101 e
->ping_status
= PING_NONE
;
102 EBIT_CLR(e
->flags
, ENTRY_VALIDATED
);
103 e
->hashInsert((const cache_key
*)name
.termedBuf()); /* do it after we clear KEY_PRIVATE */
109 static bool inited
= false;
116 Config
.Store
.avgObjectSize
= 1024;
118 Config
.Store
.objectsPerBucket
= 20;
120 Config
.Store
.maxObjectSize
= 2048;
123 /* TODO make this a cbdata class */
125 static bool cbcalled
;
128 searchCallback(void *cbdata
)
134 testStoreHashIndex::testSearch()
137 StorePointer
aRoot (new StoreHashIndex());
139 TestSwapDirPointer
aStore (new TestSwapDir
);
140 TestSwapDirPointer
aStore2 (new TestSwapDir
);
143 Store::Root().init();
144 StoreEntry
* entry1
= addedEntry (&Store::Root(), aStore
.getRaw(), "name", NULL
, NULL
);
145 StoreEntry
* entry2
= addedEntry (&Store::Root(), aStore2
.getRaw(), "name2", NULL
, NULL
);
146 StoreSearchPointer search
= aRoot
->search (NULL
, NULL
); /* search for everything in the store */
148 /* nothing should be immediately available */
149 CPPUNIT_ASSERT_EQUAL(false, search
->error());
150 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
151 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(NULL
), search
->currentItem());
154 CPPUNIT_ASSERT_EQUAL(false, search
->next());
157 /* trigger a callback */
159 search
->next(searchCallback
, NULL
);
160 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
162 /* we should have access to a entry now, that matches the entry we had before */
163 CPPUNIT_ASSERT_EQUAL(false, search
->error());
164 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
165 /* note the hash order is random - the test happens to be in a nice order */
166 CPPUNIT_ASSERT_EQUAL(entry1
, search
->currentItem());
167 //CPPUNIT_ASSERT_EQUAL(false, search->next());
169 /* trigger another callback */
171 search
->next(searchCallback
, NULL
);
172 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
174 /* we should have access to a entry now, that matches the entry we had before */
175 CPPUNIT_ASSERT_EQUAL(false, search
->error());
176 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
177 CPPUNIT_ASSERT_EQUAL(entry2
, search
->currentItem());
178 //CPPUNIT_ASSERT_EQUAL(false, search->next());
180 /* trigger another callback */
182 search
->next(searchCallback
, NULL
);
183 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
185 /* now we should have no error, we should have finished and have no current item */
186 CPPUNIT_ASSERT_EQUAL(false, search
->error());
187 CPPUNIT_ASSERT_EQUAL(true, search
->isDone());
188 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(NULL
), search
->currentItem());
189 //CPPUNIT_ASSERT_EQUAL(false, search->next());