2 * Copyright (C) 1996-2016 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 "store/Disks.h"
15 #include "StoreSearch.h"
16 #include "testStoreController.h"
17 #include "TestSwapDir.h"
19 CPPUNIT_TEST_SUITE_REGISTRATION( testStoreController
);
22 addSwapDir(TestSwapDirPointer aStore
)
24 allocate_new_swapdir(&Config
.cacheSwap
);
25 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
26 ++Config
.cacheSwap
.n_configured
;
30 testStoreController::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 TestSwapDirPointer
aStore (new TestSwapDir
);
38 TestSwapDirPointer
aStore2 (new TestSwapDir
);
41 CPPUNIT_ASSERT_EQUAL(false, aStore
->statsCalled
);
42 CPPUNIT_ASSERT_EQUAL(false, aStore2
->statsCalled
);
43 Store::Stats(logEntry
);
44 free_cachedir(&Config
.cacheSwap
);
45 CPPUNIT_ASSERT_EQUAL(true, aStore
->statsCalled
);
46 CPPUNIT_ASSERT_EQUAL(true, aStore2
->statsCalled
);
53 static bool inited
= false;
58 Config
.memShared
.defaultTo(false);
62 Config
.Store
.avgObjectSize
= 1024;
64 Config
.Store
.objectsPerBucket
= 20;
66 Config
.Store
.maxObjectSize
= 2048;
68 Config
.store_dir_select_algorithm
= xstrdup("round-robin");
72 testStoreController::testMaxSize()
75 StoreEntry
*logEntry
= new StoreEntry
;
76 logEntry
->makeMemObject();
77 logEntry
->mem_obj
->setUris("dummy_storeId", NULL
, HttpRequestMethod());
78 logEntry
->store_status
= STORE_PENDING
;
80 TestSwapDirPointer
aStore (new TestSwapDir
);
81 TestSwapDirPointer
aStore2 (new TestSwapDir
);
84 CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize());
85 free_cachedir(&Config
.cacheSwap
);
90 addedEntry(Store::Disk
*aStore
,
97 StoreEntry
*e
= new StoreEntry();
98 e
->store_status
= STORE_OK
;
99 e
->setMemStatus(NOT_IN_MEMORY
);
100 e
->swap_status
= SWAPOUT_DONE
; /* bogus haha */
101 e
->swap_filen
= 0; /* garh - lower level*/
104 for (int i
=0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
105 if (INDEXSD(i
) == aStore
)
109 CPPUNIT_ASSERT (e
->swap_dirn
!= -1);
110 e
->swap_file_sz
= 0; /* garh lower level */
111 e
->lastref
= squid_curtime
;
112 e
->timestamp
= squid_curtime
;
113 e
->expires
= squid_curtime
;
114 e
->lastModified(squid_curtime
);
116 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
117 EBIT_CLR(e
->flags
, KEY_PRIVATE
);
118 e
->ping_status
= PING_NONE
;
119 EBIT_CLR(e
->flags
, ENTRY_VALIDATED
);
120 e
->hashInsert((const cache_key
*)name
.termedBuf()); /* do it after we clear KEY_PRIVATE */
124 /* TODO make this a cbdata class */
126 static bool cbcalled
;
129 searchCallback(void *cbdata
)
135 testStoreController::testSearch()
139 TestSwapDirPointer
aStore (new TestSwapDir
);
140 TestSwapDirPointer
aStore2 (new TestSwapDir
);
143 Store::Root().init();
144 StoreEntry
* entry1
= addedEntry(aStore
.getRaw(), "name", NULL
, NULL
);
145 StoreEntry
* entry2
= addedEntry(aStore2
.getRaw(), "name2", NULL
, NULL
);
146 StoreSearchPointer search
= Store::Root().search(); /* 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());