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