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