1 #define SQUID_UNIT_TEST 1
4 #include "testStoreHashIndex.h"
7 #include "TestSwapDir.h"
8 #include "StoreHashIndex.h"
10 #include "StoreSearch.h"
11 #include "SquidConfig.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
;
25 testStoreHashIndex::testStats()
27 StoreEntry
* logEntry
= new StoreEntry("dummy_url", "dummy_log_url");
28 logEntry
->store_status
= STORE_PENDING
;
29 StorePointer
aRoot (new StoreHashIndex());
31 TestSwapDirPointer
aStore (new TestSwapDir
);
32 TestSwapDirPointer
aStore2 (new TestSwapDir
);
35 CPPUNIT_ASSERT(aStore
->statsCalled
== false);
36 CPPUNIT_ASSERT(aStore2
->statsCalled
== false);
37 Store::Stats(logEntry
);
38 free_cachedir(&Config
.cacheSwap
);
39 CPPUNIT_ASSERT(aStore
->statsCalled
== true);
40 CPPUNIT_ASSERT(aStore2
->statsCalled
== true);
45 testStoreHashIndex::testMaxSize()
47 StoreEntry
* logEntry
= new StoreEntry("dummy_url", "dummy_log_url");
48 logEntry
->store_status
= STORE_PENDING
;
49 StorePointer
aRoot (new StoreHashIndex());
51 TestSwapDirPointer
aStore (new TestSwapDir
);
52 TestSwapDirPointer
aStore2 (new TestSwapDir
);
55 CPPUNIT_ASSERT(Store::Root().maxSize() == 6);
56 free_cachedir(&Config
.cacheSwap
);
61 addedEntry(StorePointer hashStore
,
69 StoreEntry
*e
= new StoreEntry();
70 e
->store_status
= STORE_OK
;
71 e
->setMemStatus(NOT_IN_MEMORY
);
72 e
->swap_status
= SWAPOUT_DONE
; /* bogus haha */
73 e
->swap_filen
= 0; /* garh - lower level*/
76 for (int i
=0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
77 if (INDEXSD (i
) == aStore
.getRaw())
81 CPPUNIT_ASSERT (e
->swap_dirn
!= -1);
82 e
->swap_file_sz
= 0; /* garh lower level */
84 e
->lastref
= squid_curtime
;
85 e
->timestamp
= squid_curtime
;
86 e
->expires
= squid_curtime
;
87 e
->lastmod
= squid_curtime
;
89 EBIT_SET(e
->flags
, ENTRY_CACHABLE
);
90 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
91 EBIT_CLR(e
->flags
, KEY_PRIVATE
);
92 e
->ping_status
= PING_NONE
;
93 EBIT_CLR(e
->flags
, ENTRY_VALIDATED
);
94 e
->hashInsert((const cache_key
*)name
.termedBuf()); /* do it after we clear KEY_PRIVATE */
100 static bool inited
= false;
107 Config
.Store
.avgObjectSize
= 1024;
109 Config
.Store
.objectsPerBucket
= 20;
111 Config
.Store
.maxObjectSize
= 2048;
114 /* TODO make this a cbdata class */
116 static bool cbcalled
;
119 searchCallback(void *cbdata
)
125 testStoreHashIndex::testSearch()
128 StorePointer
aRoot (new StoreHashIndex());
130 TestSwapDirPointer
aStore (new TestSwapDir
);
131 TestSwapDirPointer
aStore2 (new TestSwapDir
);
134 Store::Root().init();
135 StoreEntry
* entry1
= addedEntry (&Store::Root(), aStore
.getRaw(), "name", NULL
, NULL
);
136 StoreEntry
* entry2
= addedEntry (&Store::Root(), aStore2
.getRaw(), "name2", NULL
, NULL
);
137 StoreSearchPointer search
= aRoot
->search (NULL
, NULL
); /* search for everything in the store */
139 /* nothing should be immediately available */
140 CPPUNIT_ASSERT(search
->error() == false);
141 CPPUNIT_ASSERT(search
->isDone() == false);
142 CPPUNIT_ASSERT(search
->currentItem() == NULL
);
145 CPPUNIT_ASSERT(search
->next() == false);
148 /* trigger a callback */
150 search
->next(searchCallback
, NULL
);
151 CPPUNIT_ASSERT(cbcalled
== true);
153 /* we should have access to a entry now, that matches the entry we had before */
154 CPPUNIT_ASSERT(search
->error() == false);
155 CPPUNIT_ASSERT(search
->isDone() == false);
156 /* note the hash order is random - the test happens to be in a nice order */
157 CPPUNIT_ASSERT(search
->currentItem() == entry1
);
158 //CPPUNIT_ASSERT(search->next() == false);
160 /* trigger another callback */
162 search
->next(searchCallback
, NULL
);
163 CPPUNIT_ASSERT(cbcalled
== true);
165 /* we should have access to a entry now, that matches the entry we had before */
166 CPPUNIT_ASSERT(search
->error() == false);
167 CPPUNIT_ASSERT(search
->isDone() == false);
168 CPPUNIT_ASSERT(search
->currentItem() == entry2
);
169 //CPPUNIT_ASSERT(search->next() == false);
171 /* trigger another callback */
173 search
->next(searchCallback
, NULL
);
174 CPPUNIT_ASSERT(cbcalled
== true);
176 /* now we should have no error, we should have finished and have no current item */
177 CPPUNIT_ASSERT(search
->error() == false);
178 CPPUNIT_ASSERT(search
->isDone() == true);
179 CPPUNIT_ASSERT(search
->currentItem() == NULL
);
180 //CPPUNIT_ASSERT(search->next() == false);