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