1//
2// UniqueExpireCacheTest.cpp
3//
4// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "UniqueExpireCacheTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Exception.h"
15#include "Poco/UniqueExpireCache.h"
16#include "Poco/UniqueAccessExpireCache.h"
17#include "Poco/ExpirationDecorator.h"
18#include "Poco/AccessExpirationDecorator.h"
19#include "Poco/Bugcheck.h"
20#include "Poco/Thread.h"
21
22
23using namespace Poco;
24
25
26struct IntVal
27{
28 int value;
29 Poco::Timestamp validUntil;
30 IntVal(int val, Poco::Timestamp::TimeDiff v):value(val), validUntil()
31 {
32 validUntil += (v*1000);
33 }
34
35 const Poco::Timestamp& getExpiration() const
36 {
37 return validUntil;
38 }
39};
40
41typedef AccessExpirationDecorator<int> DIntVal;
42
43#define DURSLEEP 250
44#define DURHALFSLEEP DURSLEEP / 2
45#define DURWAIT 300
46
47
48UniqueExpireCacheTest::UniqueExpireCacheTest(const std::string& rName): CppUnit::TestCase(rName)
49{
50}
51
52
53UniqueExpireCacheTest::~UniqueExpireCacheTest()
54{
55}
56
57
58void UniqueExpireCacheTest::testClear()
59{
60 UniqueExpireCache<int, IntVal> aCache;
61 aCache.add(1, IntVal(2, DURSLEEP));
62 aCache.add(3, IntVal(4, DURSLEEP));
63 aCache.add(5, IntVal(6, DURSLEEP));
64 assertTrue (aCache.has(1));
65 assertTrue (aCache.has(3));
66 assertTrue (aCache.has(5));
67 assertTrue (aCache.get(1)->value == 2);
68 assertTrue (aCache.get(3)->value == 4);
69 assertTrue (aCache.get(5)->value == 6);
70 aCache.clear();
71 assertTrue (!aCache.has(1));
72 assertTrue (!aCache.has(3));
73 assertTrue (!aCache.has(5));
74}
75
76
77void UniqueExpireCacheTest::testAccessClear()
78{
79 UniqueAccessExpireCache<int, DIntVal> aCache;
80 aCache.add(1, DIntVal(2, DURSLEEP));
81 aCache.add(3, DIntVal(4, DURSLEEP));
82 aCache.add(5, DIntVal(6, DURSLEEP));
83 assertTrue (aCache.has(1));
84 assertTrue (aCache.has(3));
85 assertTrue (aCache.has(5));
86 assertTrue (aCache.get(1)->value() == 2);
87 assertTrue (aCache.get(3)->value() == 4);
88 assertTrue (aCache.get(5)->value() == 6);
89 aCache.clear();
90 assertTrue (!aCache.has(1));
91 assertTrue (!aCache.has(3));
92 assertTrue (!aCache.has(5));
93}
94
95
96void UniqueExpireCacheTest::testAccessUpdate()
97{
98 UniqueAccessExpireCache<int, DIntVal> aCache;
99 aCache.add(1, DIntVal(2, DURSLEEP));
100 aCache.add(3, DIntVal(4, DURSLEEP));
101 aCache.add(5, DIntVal(6, DURSLEEP));
102 assertTrue (aCache.has(1));
103 assertTrue (aCache.has(3));
104 assertTrue (aCache.has(5));
105 assertTrue (aCache.get(1)->value() == 2);
106 Thread::sleep(DURSLEEP/2);
107 assertTrue (aCache.get(1)->value() == 2);
108 Thread::sleep(DURSLEEP/2);
109 assertTrue (aCache.get(1)->value() == 2);
110 Thread::sleep(DURSLEEP/2);
111 assertTrue (aCache.get(1)->value() == 2);
112 assertTrue (!aCache.has(3));
113 assertTrue (!aCache.has(5));
114 Thread::sleep(DURSLEEP*2);
115
116 assertTrue (!aCache.has(1));
117 assertTrue (!aCache.has(3));
118 assertTrue (!aCache.has(5));
119 aCache.remove(666); //must work too
120}
121
122
123void UniqueExpireCacheTest::testExpire0()
124{
125 UniqueExpireCache<int, IntVal> aCache;
126 aCache.add(1, IntVal(2, 0));
127 assertTrue (!aCache.has(1));
128}
129
130
131
132void UniqueExpireCacheTest::testAccessExpire0()
133{
134 UniqueAccessExpireCache<int, DIntVal> aCache;
135 aCache.add(1, DIntVal(2, Timespan(0, 0)));
136 assertTrue (!aCache.has(1));
137}
138
139
140void UniqueExpireCacheTest::testExpireN()
141{
142 // 3-1 represents the cache sorted by age, elements get replaced at the end of the list
143 // 3-1|5 -> 5 gets removed
144 UniqueExpireCache<int, IntVal> aCache;
145 aCache.add(1, IntVal(2, DURSLEEP)); // 1
146 assertTrue (aCache.has(1));
147 SharedPtr<IntVal> tmp = aCache.get(1);
148 assertTrue (!tmp.isNull());
149 assertTrue (tmp->value == 2);
150 Thread::sleep(DURWAIT);
151 assertTrue (!aCache.has(1));
152
153 // tmp must still be valid, access it
154 assertTrue (tmp->value == 2);
155 tmp = aCache.get(1);
156 assertTrue (tmp.isNull());
157
158 aCache.add(1, IntVal(2, DURSLEEP)); // 1
159 Thread::sleep(DURHALFSLEEP);
160 aCache.add(3, IntVal(4, DURSLEEP)); // 3-1
161 assertTrue (aCache.has(1));
162 assertTrue (aCache.has(3));
163 tmp = aCache.get(1);
164 SharedPtr<IntVal> tmp2 = aCache.get(3);
165 assertTrue (tmp->value == 2);
166 assertTrue (tmp2->value == 4);
167
168 Thread::sleep(DURHALFSLEEP+25); //3|1
169 assertTrue (!aCache.has(1));
170 assertTrue (aCache.has(3));
171 assertTrue (tmp->value == 2); // 1-3
172 assertTrue (tmp2->value == 4); // 3-1
173 tmp2 = aCache.get(3);
174 assertTrue (tmp2->value == 4);
175 Thread::sleep(DURHALFSLEEP+25); //3|1
176 assertTrue (!aCache.has(3));
177 assertTrue (tmp2->value == 4);
178 tmp = aCache.get(1);
179 tmp2 = aCache.get(3);
180 assertTrue (!tmp);
181 assertTrue (!tmp2);
182
183 // removing illegal entries should work too
184 aCache.remove(666);
185
186 aCache.clear();
187 assertTrue (!aCache.has(5));
188 assertTrue (!aCache.has(3));
189}
190
191
192void UniqueExpireCacheTest::testDuplicateAdd()
193{
194 UniqueExpireCache<int, IntVal> aCache;
195 aCache.add(1, IntVal(2, DURSLEEP)); // 1
196 assertTrue (aCache.has(1));
197 assertTrue (aCache.get(1)->value == 2);
198 aCache.add(1, IntVal(3, DURSLEEP));
199 assertTrue (aCache.has(1));
200 assertTrue (aCache.get(1)->value == 3);
201}
202
203
204void UniqueExpireCacheTest::testAccessDuplicateAdd()
205{
206 UniqueAccessExpireCache<int, DIntVal> aCache;
207 aCache.add(1, DIntVal(2, DURSLEEP)); // 1
208 assertTrue (aCache.has(1));
209 assertTrue (aCache.get(1)->value() == 2);
210 aCache.add(1, DIntVal(3, DURSLEEP));
211 assertTrue (aCache.has(1));
212 assertTrue (aCache.get(1)->value() == 3);
213}
214
215
216void UniqueExpireCacheTest::testExpirationDecorator()
217{
218 typedef ExpirationDecorator<int> ExpireInt;
219 UniqueExpireCache<int, ExpireInt> aCache;
220 aCache.add(1, ExpireInt(2, DURSLEEP)); // 1
221 assertTrue (aCache.has(1));
222 assertTrue (aCache.get(1)->value() == 2);
223 aCache.add(1, ExpireInt(3, DURSLEEP));
224 assertTrue (aCache.has(1));
225 assertTrue (aCache.get(1)->value() == 3);
226}
227
228
229void UniqueExpireCacheTest::setUp()
230{
231}
232
233
234void UniqueExpireCacheTest::tearDown()
235{
236}
237
238
239CppUnit::Test* UniqueExpireCacheTest::suite()
240{
241 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("UniqueExpireCacheTest");
242
243 CppUnit_addTest(pSuite, UniqueExpireCacheTest, testClear);
244 CppUnit_addTest(pSuite, UniqueExpireCacheTest, testAccessClear);
245 CppUnit_addTest(pSuite, UniqueExpireCacheTest, testAccessUpdate);
246 CppUnit_addTest(pSuite, UniqueExpireCacheTest, testExpire0);
247 CppUnit_addTest(pSuite, UniqueExpireCacheTest, testAccessExpire0);
248 CppUnit_addTest(pSuite, UniqueExpireCacheTest, testExpireN);
249 CppUnit_addTest(pSuite, UniqueExpireCacheTest, testDuplicateAdd);
250 CppUnit_addTest(pSuite, UniqueExpireCacheTest, testAccessDuplicateAdd);
251 CppUnit_addTest(pSuite, UniqueExpireCacheTest, testExpirationDecorator);
252
253 return pSuite;
254}
255