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.
10 #include "MemObject.h"
11 #include "SquidConfig.h"
12 #include "SquidTime.h"
14 #include "StoreHashIndex.h"
15 #include "StoreSearch.h"
17 #include "testStoreHashIndex.h"
18 #include "TestSwapDir.h"
20 CPPUNIT_TEST_SUITE_REGISTRATION( testStoreHashIndex
);
23 addSwapDir(TestSwapDirPointer aStore
)
25 allocate_new_swapdir(&Config
.cacheSwap
);
26 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
27 ++Config
.cacheSwap
.n_configured
;
31 testStoreHashIndex::testStats()
33 StoreEntry
*logEntry
= new StoreEntry
;
34 logEntry
->makeMemObject();
35 logEntry
->mem_obj
->setUris("dummy_storeId", NULL
, HttpRequestMethod());
36 logEntry
->store_status
= STORE_PENDING
;
37 StorePointer
aRoot (new StoreHashIndex());
39 TestSwapDirPointer
aStore (new TestSwapDir
);
40 TestSwapDirPointer
aStore2 (new TestSwapDir
);
43 CPPUNIT_ASSERT_EQUAL(false, aStore
->statsCalled
);
44 CPPUNIT_ASSERT_EQUAL(false, aStore2
->statsCalled
);
45 Store::Stats(logEntry
);
46 free_cachedir(&Config
.cacheSwap
);
47 CPPUNIT_ASSERT_EQUAL(true, aStore
->statsCalled
);
48 CPPUNIT_ASSERT_EQUAL(true, aStore2
->statsCalled
);
53 testStoreHashIndex::testMaxSize()
55 StoreEntry
*logEntry
= new StoreEntry
;
56 logEntry
->makeMemObject();
57 logEntry
->mem_obj
->setUris("dummy_storeId", NULL
, HttpRequestMethod());
58 logEntry
->store_status
= STORE_PENDING
;
59 StorePointer
aRoot (new StoreHashIndex());
61 TestSwapDirPointer
aStore (new TestSwapDir
);
62 TestSwapDirPointer
aStore2 (new TestSwapDir
);
65 CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize());
66 free_cachedir(&Config
.cacheSwap
);
71 addedEntry(StorePointer hashStore
,
79 StoreEntry
*e
= new StoreEntry();
80 e
->store_status
= STORE_OK
;
81 e
->setMemStatus(NOT_IN_MEMORY
);
82 e
->swap_status
= SWAPOUT_DONE
; /* bogus haha */
83 e
->swap_filen
= 0; /* garh - lower level*/
86 for (int i
=0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
87 if (INDEXSD (i
) == aStore
.getRaw())
91 CPPUNIT_ASSERT (e
->swap_dirn
!= -1);
92 e
->swap_file_sz
= 0; /* garh lower level */
93 e
->lastref
= squid_curtime
;
94 e
->timestamp
= squid_curtime
;
95 e
->expires
= squid_curtime
;
96 e
->lastmod
= squid_curtime
;
98 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
99 EBIT_CLR(e
->flags
, KEY_PRIVATE
);
100 e
->ping_status
= PING_NONE
;
101 EBIT_CLR(e
->flags
, ENTRY_VALIDATED
);
102 e
->hashInsert((const cache_key
*)name
.termedBuf()); /* do it after we clear KEY_PRIVATE */
108 static bool inited
= false;
115 Config
.Store
.avgObjectSize
= 1024;
117 Config
.Store
.objectsPerBucket
= 20;
119 Config
.Store
.maxObjectSize
= 2048;
122 /* TODO make this a cbdata class */
124 static bool cbcalled
;
127 searchCallback(void *cbdata
)
133 testStoreHashIndex::testSearch()
136 StorePointer
aRoot (new StoreHashIndex());
138 TestSwapDirPointer
aStore (new TestSwapDir
);
139 TestSwapDirPointer
aStore2 (new TestSwapDir
);
142 Store::Root().init();
143 StoreEntry
* entry1
= addedEntry (&Store::Root(), aStore
.getRaw(), "name", NULL
, NULL
);
144 StoreEntry
* entry2
= addedEntry (&Store::Root(), aStore2
.getRaw(), "name2", NULL
, NULL
);
145 StoreSearchPointer search
= aRoot
->search (NULL
, NULL
); /* search for everything in the store */
147 /* nothing should be immediately available */
148 CPPUNIT_ASSERT_EQUAL(false, search
->error());
149 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
150 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(NULL
), search
->currentItem());
153 CPPUNIT_ASSERT_EQUAL(false, search
->next());
156 /* trigger a callback */
158 search
->next(searchCallback
, NULL
);
159 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
161 /* we should have access to a entry now, that matches the entry we had before */
162 CPPUNIT_ASSERT_EQUAL(false, search
->error());
163 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
164 /* note the hash order is random - the test happens to be in a nice order */
165 CPPUNIT_ASSERT_EQUAL(entry1
, search
->currentItem());
166 //CPPUNIT_ASSERT_EQUAL(false, search->next());
168 /* trigger another callback */
170 search
->next(searchCallback
, NULL
);
171 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
173 /* we should have access to a entry now, that matches the entry we had before */
174 CPPUNIT_ASSERT_EQUAL(false, search
->error());
175 CPPUNIT_ASSERT_EQUAL(false, search
->isDone());
176 CPPUNIT_ASSERT_EQUAL(entry2
, search
->currentItem());
177 //CPPUNIT_ASSERT_EQUAL(false, search->next());
179 /* trigger another callback */
181 search
->next(searchCallback
, NULL
);
182 CPPUNIT_ASSERT_EQUAL(true, cbcalled
);
184 /* now we should have no error, we should have finished and have no current item */
185 CPPUNIT_ASSERT_EQUAL(false, search
->error());
186 CPPUNIT_ASSERT_EQUAL(true, search
->isDone());
187 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(NULL
), search
->currentItem());
188 //CPPUNIT_ASSERT_EQUAL(false, search->next());