]>
Commit | Line | Data |
---|---|---|
4e0938ef | 1 | /* |
ef57eb7b | 2 | * Copyright (C) 1996-2016 The Squid Software Foundation and contributors |
4e0938ef AJ |
3 | * |
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. | |
7 | */ | |
8 | ||
582c2af2 | 9 | #include "squid.h" |
cb868059 | 10 | #include "MemObject.h" |
4d5904f7 | 11 | #include "SquidConfig.h" |
985c86bc | 12 | #include "SquidTime.h" |
602d9612 | 13 | #include "Store.h" |
2745fea5 | 14 | #include "store/Disks.h" |
c8f4eac4 | 15 | #include "StoreSearch.h" |
602d9612 A |
16 | #include "testStoreController.h" |
17 | #include "TestSwapDir.h" | |
c8f4eac4 | 18 | |
19 | CPPUNIT_TEST_SUITE_REGISTRATION( testStoreController ); | |
20 | ||
21 | static void | |
22 | addSwapDir(TestSwapDirPointer aStore) | |
23 | { | |
24 | allocate_new_swapdir(&Config.cacheSwap); | |
25 | Config.cacheSwap.swapDirs[Config.cacheSwap.n_configured] = aStore.getRaw(); | |
26 | ++Config.cacheSwap.n_configured; | |
27 | } | |
28 | ||
c8f4eac4 | 29 | void |
30 | testStoreController::testStats() | |
31 | { | |
2745fea5 | 32 | Store::Init(); |
cb868059 AR |
33 | StoreEntry *logEntry = new StoreEntry; |
34 | logEntry->makeMemObject(); | |
35 | logEntry->mem_obj->setUris("dummy_storeId", NULL, HttpRequestMethod()); | |
c8f4eac4 | 36 | logEntry->store_status = STORE_PENDING; |
c8f4eac4 | 37 | TestSwapDirPointer aStore (new TestSwapDir); |
38 | TestSwapDirPointer aStore2 (new TestSwapDir); | |
39 | addSwapDir(aStore); | |
40 | addSwapDir(aStore2); | |
5d1dff27 FC |
41 | CPPUNIT_ASSERT_EQUAL(false, aStore->statsCalled); |
42 | CPPUNIT_ASSERT_EQUAL(false, aStore2->statsCalled); | |
c8f4eac4 | 43 | Store::Stats(logEntry); |
44 | free_cachedir(&Config.cacheSwap); | |
5d1dff27 FC |
45 | CPPUNIT_ASSERT_EQUAL(true, aStore->statsCalled); |
46 | CPPUNIT_ASSERT_EQUAL(true, aStore2->statsCalled); | |
2745fea5 | 47 | Store::FreeMemory(); |
c8f4eac4 | 48 | } |
49 | ||
50 | static void | |
51 | commonInit() | |
52 | { | |
53 | static bool inited = false; | |
54 | ||
55 | if (inited) | |
56 | return; | |
57 | ||
c50b35b5 AJ |
58 | Config.memShared.defaultTo(false); |
59 | ||
c8f4eac4 | 60 | Mem::Init(); |
61 | ||
c8f4eac4 | 62 | Config.Store.avgObjectSize = 1024; |
63 | ||
64 | Config.Store.objectsPerBucket = 20; | |
65 | ||
66 | Config.Store.maxObjectSize = 2048; | |
67 | ||
68 | Config.store_dir_select_algorithm = xstrdup("round-robin"); | |
69 | } | |
70 | ||
71 | void | |
72 | testStoreController::testMaxSize() | |
73 | { | |
74 | commonInit(); | |
cb868059 AR |
75 | StoreEntry *logEntry = new StoreEntry; |
76 | logEntry->makeMemObject(); | |
77 | logEntry->mem_obj->setUris("dummy_storeId", NULL, HttpRequestMethod()); | |
c8f4eac4 | 78 | logEntry->store_status = STORE_PENDING; |
2745fea5 | 79 | Store::Init(); |
c8f4eac4 | 80 | TestSwapDirPointer aStore (new TestSwapDir); |
81 | TestSwapDirPointer aStore2 (new TestSwapDir); | |
82 | addSwapDir(aStore); | |
83 | addSwapDir(aStore2); | |
5d1dff27 | 84 | CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize()); |
c8f4eac4 | 85 | free_cachedir(&Config.cacheSwap); |
2745fea5 | 86 | Store::FreeMemory(); |
c8f4eac4 | 87 | } |
88 | ||
89 | static StoreEntry * | |
2745fea5 | 90 | addedEntry(Store::Disk *aStore, |
30abd221 | 91 | String name, |
92 | String varySpec, | |
93 | String varyKey | |
94 | ||
c8f4eac4 | 95 | ) |
96 | { | |
97 | StoreEntry *e = new StoreEntry(); | |
98 | e->store_status = STORE_OK; | |
3900307b | 99 | e->setMemStatus(NOT_IN_MEMORY); |
c8f4eac4 | 100 | e->swap_status = SWAPOUT_DONE; /* bogus haha */ |
101 | e->swap_filen = 0; /* garh - lower level*/ | |
102 | e->swap_dirn = -1; | |
103 | ||
d7ae3534 | 104 | for (int i=0; i < Config.cacheSwap.n_configured; ++i) { |
2745fea5 | 105 | if (INDEXSD(i) == aStore) |
c8f4eac4 | 106 | e->swap_dirn = i; |
107 | } | |
108 | ||
109 | CPPUNIT_ASSERT (e->swap_dirn != -1); | |
110 | e->swap_file_sz = 0; /* garh lower level */ | |
c8f4eac4 | 111 | e->lastref = squid_curtime; |
112 | e->timestamp = squid_curtime; | |
113 | e->expires = squid_curtime; | |
114 | e->lastmod = squid_curtime; | |
115 | e->refcount = 1; | |
c8f4eac4 | 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); | |
f53969cc | 120 | e->hashInsert((const cache_key *)name.termedBuf()); /* do it after we clear KEY_PRIVATE */ |
c8f4eac4 | 121 | return e; |
122 | } | |
123 | ||
124 | /* TODO make this a cbdata class */ | |
125 | ||
126 | static bool cbcalled; | |
127 | ||
128 | static void | |
129 | searchCallback(void *cbdata) | |
130 | { | |
131 | cbcalled = true; | |
132 | } | |
133 | ||
134 | void | |
135 | testStoreController::testSearch() | |
136 | { | |
137 | commonInit(); | |
2745fea5 | 138 | Store::Init(); |
c8f4eac4 | 139 | TestSwapDirPointer aStore (new TestSwapDir); |
140 | TestSwapDirPointer aStore2 (new TestSwapDir); | |
141 | addSwapDir(aStore); | |
142 | addSwapDir(aStore2); | |
143 | Store::Root().init(); | |
2745fea5 AR |
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 */ | |
c8f4eac4 | 147 | |
148 | /* nothing should be immediately available */ | |
5d1dff27 FC |
149 | CPPUNIT_ASSERT_EQUAL(false, search->error()); |
150 | CPPUNIT_ASSERT_EQUAL(false, search->isDone()); | |
151 | CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem()); | |
c8f4eac4 | 152 | #if 0 |
153 | ||
5d1dff27 | 154 | CPPUNIT_ASSERT_EQUAL(false, search->next()); |
c8f4eac4 | 155 | #endif |
156 | ||
157 | /* trigger a callback */ | |
158 | cbcalled = false; | |
159 | search->next(searchCallback, NULL); | |
5d1dff27 | 160 | CPPUNIT_ASSERT_EQUAL(true, cbcalled); |
c8f4eac4 | 161 | |
162 | /* we should have access to a entry now, that matches the entry we had before */ | |
5d1dff27 FC |
163 | CPPUNIT_ASSERT_EQUAL(false, search->error()); |
164 | CPPUNIT_ASSERT_EQUAL(false, search->isDone()); | |
c8f4eac4 | 165 | /* note the hash order is random - the test happens to be in a nice order */ |
5d1dff27 FC |
166 | CPPUNIT_ASSERT_EQUAL(entry1, search->currentItem()); |
167 | //CPPUNIT_ASSERT_EQUAL(false, search->next()); | |
c8f4eac4 | 168 | |
169 | /* trigger another callback */ | |
170 | cbcalled = false; | |
171 | search->next(searchCallback, NULL); | |
5d1dff27 | 172 | CPPUNIT_ASSERT_EQUAL(true, cbcalled); |
c8f4eac4 | 173 | |
174 | /* we should have access to a entry now, that matches the entry we had before */ | |
5d1dff27 FC |
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()); | |
c8f4eac4 | 179 | |
180 | /* trigger another callback */ | |
181 | cbcalled = false; | |
182 | search->next(searchCallback, NULL); | |
5d1dff27 | 183 | CPPUNIT_ASSERT_EQUAL(true, cbcalled); |
c8f4eac4 | 184 | |
185 | /* now we should have no error, we should have finished and have no current item */ | |
5d1dff27 FC |
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()); | |
c8f4eac4 | 190 | |
2745fea5 | 191 | Store::FreeMemory(); |
c8f4eac4 | 192 | } |
f53969cc | 193 |