1 #define SQUID_UNIT_TEST 1
6 #include "SquidConfig.h"
9 #include "StoreHashIndex.h"
10 #include "StoreSearch.h"
12 #include "testStoreHashIndex.h"
13 #include "TestSwapDir.h"
15 CPPUNIT_TEST_SUITE_REGISTRATION( testStoreHashIndex
);
18 addSwapDir(TestSwapDirPointer aStore
)
20 allocate_new_swapdir(&Config
.cacheSwap
);
21 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
22 ++Config
.cacheSwap
.n_configured
;
26 testStoreHashIndex::testStats()
28 StoreEntry
*logEntry
= new StoreEntry
;
29 logEntry
->makeMemObject();
30 logEntry
->mem_obj
->setUris("dummy_storeId", NULL
, HttpRequestMethod());
31 logEntry
->store_status
= STORE_PENDING
;
32 StorePointer
aRoot (new StoreHashIndex());
34 TestSwapDirPointer
aStore (new TestSwapDir
);
35 TestSwapDirPointer
aStore2 (new TestSwapDir
);
38 CPPUNIT_ASSERT_EQUAL(false, aStore
->statsCalled
);
39 CPPUNIT_ASSERT_EQUAL(false, aStore2
->statsCalled
);
40 Store::Stats(logEntry
);
41 free_cachedir(&Config
.cacheSwap
);
42 CPPUNIT_ASSERT_EQUAL(true, aStore
->statsCalled
);
43 CPPUNIT_ASSERT_EQUAL(true, aStore2
->statsCalled
);
48 testStoreHashIndex::testMaxSize()
50 StoreEntry
*logEntry
= new StoreEntry
;
51 logEntry
->makeMemObject();
52 logEntry
->mem_obj
->setUris("dummy_storeId", NULL
, HttpRequestMethod());
53 logEntry
->store_status
= STORE_PENDING
;
54 StorePointer
aRoot (new StoreHashIndex());
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(StorePointer hashStore
,
74 StoreEntry
*e
= new StoreEntry();
75 e
->store_status
= STORE_OK
;
76 e
->setMemStatus(NOT_IN_MEMORY
);
77 e
->swap_status
= SWAPOUT_DONE
; /* bogus haha */
78 e
->swap_filen
= 0; /* garh - lower level*/
81 for (int i
=0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
82 if (INDEXSD (i
) == aStore
.getRaw())
86 CPPUNIT_ASSERT (e
->swap_dirn
!= -1);
87 e
->swap_file_sz
= 0; /* garh lower level */
88 e
->lastref
= squid_curtime
;
89 e
->timestamp
= squid_curtime
;
90 e
->expires
= squid_curtime
;
91 e
->lastmod
= squid_curtime
;
93 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
94 EBIT_CLR(e
->flags
, KEY_PRIVATE
);
95 e
->ping_status
= PING_NONE
;
96 EBIT_CLR(e
->flags
, ENTRY_VALIDATED
);
97 e
->hashInsert((const cache_key
*)name
.termedBuf()); /* do it after we clear KEY_PRIVATE */
103 static bool inited
= false;
110 Config
.Store
.avgObjectSize
= 1024;
112 Config
.Store
.objectsPerBucket
= 20;
114 Config
.Store
.maxObjectSize
= 2048;
117 /* TODO make this a cbdata class */
119 static bool cbcalled
;
122 searchCallback(void *cbdata
)
128 testStoreHashIndex::testSearch()
131 StorePointer
aRoot (new StoreHashIndex());
133 TestSwapDirPointer
aStore (new TestSwapDir
);
134 TestSwapDirPointer
aStore2 (new TestSwapDir
);
137 Store::Root().init();
138 StoreEntry
* entry1
= addedEntry (&Store::Root(), aStore
.getRaw(), "name", NULL
, NULL
);
139 StoreEntry
* entry2
= addedEntry (&Store::Root(), aStore2
.getRaw(), "name2", NULL
, NULL
);
140 StoreSearchPointer search
= aRoot
->search (NULL
, NULL
); /* 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());