1 #define SQUID_UNIT_TEST 1
5 #include "testStoreHashIndex.h"
8 #include "TestSwapDir.h"
9 #include "StoreHashIndex.h"
11 #include "StoreSearch.h"
12 #include "SquidTime.h"
14 CPPUNIT_TEST_SUITE_REGISTRATION( testStoreHashIndex
);
17 addSwapDir(TestSwapDirPointer aStore
)
19 allocate_new_swapdir(&Config
.cacheSwap
);
20 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
21 ++Config
.cacheSwap
.n_configured
;
26 testStoreHashIndex::testStats()
28 StoreEntry
* logEntry
= new StoreEntry("dummy_url", "dummy_log_url");
29 logEntry
->store_status
= STORE_PENDING
;
30 StorePointer
aRoot (new StoreHashIndex());
32 TestSwapDirPointer
aStore (new TestSwapDir
);
33 TestSwapDirPointer
aStore2 (new TestSwapDir
);
36 CPPUNIT_ASSERT(aStore
->statsCalled
== false);
37 CPPUNIT_ASSERT(aStore2
->statsCalled
== false);
38 Store::Stats(logEntry
);
39 free_cachedir(&Config
.cacheSwap
);
40 CPPUNIT_ASSERT(aStore
->statsCalled
== true);
41 CPPUNIT_ASSERT(aStore2
->statsCalled
== true);
46 testStoreHashIndex::testMaxSize()
48 StoreEntry
* logEntry
= new StoreEntry("dummy_url", "dummy_log_url");
49 logEntry
->store_status
= STORE_PENDING
;
50 StorePointer
aRoot (new StoreHashIndex());
52 TestSwapDirPointer
aStore (new TestSwapDir
);
53 TestSwapDirPointer
aStore2 (new TestSwapDir
);
56 CPPUNIT_ASSERT(Store::Root().maxSize() == 6);
57 free_cachedir(&Config
.cacheSwap
);
62 addedEntry(StorePointer hashStore
,
70 StoreEntry
*e
= new StoreEntry();
71 e
->store_status
= STORE_OK
;
72 e
->setMemStatus(NOT_IN_MEMORY
);
73 e
->swap_status
= SWAPOUT_DONE
; /* bogus haha */
74 e
->swap_filen
= 0; /* garh - lower level*/
77 for (int i
=0; i
< Config
.cacheSwap
.n_configured
; i
++) {
78 if (INDEXSD (i
) == aStore
.getRaw())
82 CPPUNIT_ASSERT (e
->swap_dirn
!= -1);
83 e
->swap_file_sz
= 0; /* garh lower level */
85 e
->lastref
= squid_curtime
;
86 e
->timestamp
= squid_curtime
;
87 e
->expires
= squid_curtime
;
88 e
->lastmod
= squid_curtime
;
90 EBIT_SET(e
->flags
, ENTRY_CACHABLE
);
91 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
92 EBIT_CLR(e
->flags
, KEY_PRIVATE
);
93 e
->ping_status
= PING_NONE
;
94 EBIT_CLR(e
->flags
, ENTRY_VALIDATED
);
95 e
->hashInsert((const cache_key
*)name
.buf()); /* do it after we clear KEY_PRIVATE */
101 static bool inited
= false;
108 Config
.Store
.avgObjectSize
= 1024;
110 Config
.Store
.objectsPerBucket
= 20;
112 Config
.Store
.maxObjectSize
= 2048;
115 /* TODO make this a cbdata class */
117 static bool cbcalled
;
120 searchCallback(void *cbdata
)
126 testStoreHashIndex::testSearch()
129 StorePointer
aRoot (new StoreHashIndex());
131 TestSwapDirPointer
aStore (new TestSwapDir
);
132 TestSwapDirPointer
aStore2 (new TestSwapDir
);
135 Store::Root().init();
136 StoreEntry
* entry1
= addedEntry (&Store::Root(), aStore
.getRaw(), "name", NULL
, NULL
);
137 StoreEntry
* entry2
= addedEntry (&Store::Root(), aStore2
.getRaw(), "name2", NULL
, NULL
);
138 StoreSearchPointer search
= aRoot
->search (NULL
, NULL
); /* search for everything in the store */
140 /* nothing should be immediately available */
141 CPPUNIT_ASSERT(search
->error() == false);
142 CPPUNIT_ASSERT(search
->isDone() == false);
143 CPPUNIT_ASSERT(search
->currentItem() == NULL
);
146 CPPUNIT_ASSERT(search
->next() == false);
149 /* trigger a callback */
151 search
->next(searchCallback
, NULL
);
152 CPPUNIT_ASSERT(cbcalled
== true);
154 /* we should have access to a entry now, that matches the entry we had before */
155 CPPUNIT_ASSERT(search
->error() == false);
156 CPPUNIT_ASSERT(search
->isDone() == false);
157 /* note the hash order is random - the test happens to be in a nice order */
158 CPPUNIT_ASSERT(search
->currentItem() == entry1
);
159 //CPPUNIT_ASSERT(search->next() == false);
161 /* trigger another callback */
163 search
->next(searchCallback
, NULL
);
164 CPPUNIT_ASSERT(cbcalled
== true);
166 /* we should have access to a entry now, that matches the entry we had before */
167 CPPUNIT_ASSERT(search
->error() == false);
168 CPPUNIT_ASSERT(search
->isDone() == false);
169 CPPUNIT_ASSERT(search
->currentItem() == entry2
);
170 //CPPUNIT_ASSERT(search->next() == false);
172 /* trigger another callback */
174 search
->next(searchCallback
, NULL
);
175 CPPUNIT_ASSERT(cbcalled
== true);
177 /* now we should have no error, we should have finished and have no current item */
178 CPPUNIT_ASSERT(search
->error() == false);
179 CPPUNIT_ASSERT(search
->isDone() == true);
180 CPPUNIT_ASSERT(search
->currentItem() == NULL
);
181 //CPPUNIT_ASSERT(search->next() == false);