]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testStoreController.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / tests / testStoreController.cc
1 #define SQUID_UNIT_TEST 1
2
3 #include "squid.h"
4 #include "testStoreController.h"
5 #include "Store.h"
6 #include "SwapDir.h"
7 #include "TestSwapDir.h"
8 #include "Mem.h"
9 #include "SquidTime.h"
10 #include "StoreSearch.h"
11
12 CPPUNIT_TEST_SUITE_REGISTRATION( testStoreController );
13
14 static void
15 addSwapDir(TestSwapDirPointer aStore)
16 {
17 allocate_new_swapdir(&Config.cacheSwap);
18 Config.cacheSwap.swapDirs[Config.cacheSwap.n_configured] = aStore.getRaw();
19 ++Config.cacheSwap.n_configured;
20 }
21
22 void
23 testStoreController::testStats()
24 {
25 StoreEntry * logEntry = new StoreEntry("dummy_url", "dummy_log_url");
26 logEntry->store_status = STORE_PENDING;
27 StorePointer aRoot (new StoreController);
28 Store::Root(aRoot);
29 TestSwapDirPointer aStore (new TestSwapDir);
30 TestSwapDirPointer aStore2 (new TestSwapDir);
31 addSwapDir(aStore);
32 addSwapDir(aStore2);
33 CPPUNIT_ASSERT(aStore->statsCalled == false);
34 CPPUNIT_ASSERT(aStore2->statsCalled == false);
35 Store::Stats(logEntry);
36 free_cachedir(&Config.cacheSwap);
37 CPPUNIT_ASSERT(aStore->statsCalled == true);
38 CPPUNIT_ASSERT(aStore2->statsCalled == true);
39 Store::Root(NULL);
40 }
41
42 static void
43 commonInit()
44 {
45 static bool inited = false;
46
47 if (inited)
48 return;
49
50 Mem::Init();
51
52 Config.Store.avgObjectSize = 1024;
53
54 Config.Store.objectsPerBucket = 20;
55
56 Config.Store.maxObjectSize = 2048;
57
58 Config.store_dir_select_algorithm = xstrdup("round-robin");
59 }
60
61 void
62 testStoreController::testMaxSize()
63 {
64 commonInit();
65 StoreEntry * logEntry = new StoreEntry("dummy_url", "dummy_log_url");
66 logEntry->store_status = STORE_PENDING;
67 StorePointer aRoot (new StoreController);
68 Store::Root(aRoot);
69 TestSwapDirPointer aStore (new TestSwapDir);
70 TestSwapDirPointer aStore2 (new TestSwapDir);
71 addSwapDir(aStore);
72 addSwapDir(aStore2);
73 CPPUNIT_ASSERT(Store::Root().maxSize() == 6);
74 free_cachedir(&Config.cacheSwap);
75 Store::Root(NULL);
76 }
77
78 static StoreEntry *
79 addedEntry(StorePointer hashStore,
80 StorePointer aStore,
81 String name,
82 String varySpec,
83 String varyKey
84
85 )
86 {
87 StoreEntry *e = new StoreEntry();
88 e->store_status = STORE_OK;
89 e->setMemStatus(NOT_IN_MEMORY);
90 e->swap_status = SWAPOUT_DONE; /* bogus haha */
91 e->swap_filen = 0; /* garh - lower level*/
92 e->swap_dirn = -1;
93
94 for (int i=0; i < Config.cacheSwap.n_configured; ++i) {
95 if (INDEXSD (i) == aStore.getRaw())
96 e->swap_dirn = i;
97 }
98
99 CPPUNIT_ASSERT (e->swap_dirn != -1);
100 e->swap_file_sz = 0; /* garh lower level */
101 e->lock_count = 0;
102 e->lastref = squid_curtime;
103 e->timestamp = squid_curtime;
104 e->expires = squid_curtime;
105 e->lastmod = squid_curtime;
106 e->refcount = 1;
107 EBIT_SET(e->flags, ENTRY_CACHABLE);
108 EBIT_CLR(e->flags, RELEASE_REQUEST);
109 EBIT_CLR(e->flags, KEY_PRIVATE);
110 e->ping_status = PING_NONE;
111 EBIT_CLR(e->flags, ENTRY_VALIDATED);
112 e->hashInsert((const cache_key *)name.termedBuf()); /* do it after we clear KEY_PRIVATE */
113 return e;
114 }
115
116 /* TODO make this a cbdata class */
117
118 static bool cbcalled;
119
120 static void
121 searchCallback(void *cbdata)
122 {
123 cbcalled = true;
124 }
125
126 void
127 testStoreController::testSearch()
128 {
129 commonInit();
130 StorePointer aRoot (new StoreController());
131 Store::Root(aRoot);
132 TestSwapDirPointer aStore (new TestSwapDir);
133 TestSwapDirPointer aStore2 (new TestSwapDir);
134 addSwapDir(aStore);
135 addSwapDir(aStore2);
136 Store::Root().init();
137 StoreEntry * entry1 = addedEntry (&Store::Root(), aStore.getRaw(), "name", NULL, NULL);
138 StoreEntry * entry2 = addedEntry (&Store::Root(), aStore2.getRaw(), "name2", NULL, NULL);
139 StoreSearchPointer search = aRoot->search (NULL, NULL); /* search for everything in the store */
140
141 /* nothing should be immediately available */
142 CPPUNIT_ASSERT(search->error() == false);
143 CPPUNIT_ASSERT(search->isDone() == false);
144 CPPUNIT_ASSERT(search->currentItem() == NULL);
145 #if 0
146
147 CPPUNIT_ASSERT(search->next() == false);
148 #endif
149
150 /* trigger a callback */
151 cbcalled = false;
152 search->next(searchCallback, NULL);
153 CPPUNIT_ASSERT(cbcalled == true);
154
155 /* we should have access to a entry now, that matches the entry we had before */
156 CPPUNIT_ASSERT(search->error() == false);
157 CPPUNIT_ASSERT(search->isDone() == false);
158 /* note the hash order is random - the test happens to be in a nice order */
159 CPPUNIT_ASSERT(search->currentItem() == entry1);
160 //CPPUNIT_ASSERT(search->next() == false);
161
162 /* trigger another callback */
163 cbcalled = false;
164 search->next(searchCallback, NULL);
165 CPPUNIT_ASSERT(cbcalled == true);
166
167 /* we should have access to a entry now, that matches the entry we had before */
168 CPPUNIT_ASSERT(search->error() == false);
169 CPPUNIT_ASSERT(search->isDone() == false);
170 CPPUNIT_ASSERT(search->currentItem() == entry2);
171 //CPPUNIT_ASSERT(search->next() == false);
172
173 /* trigger another callback */
174 cbcalled = false;
175 search->next(searchCallback, NULL);
176 CPPUNIT_ASSERT(cbcalled == true);
177
178 /* now we should have no error, we should have finished and have no current item */
179 CPPUNIT_ASSERT(search->error() == false);
180 CPPUNIT_ASSERT(search->isDone() == true);
181 CPPUNIT_ASSERT(search->currentItem() == NULL);
182 //CPPUNIT_ASSERT(search->next() == false);
183
184 Store::Root(NULL);
185 }