1//
2// ExpireCacheTest.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 "ExpireCacheTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Exception.h"
15#include "Poco/ExpireCache.h"
16#include "Poco/AccessExpireCache.h"
17#include "Poco/Bugcheck.h"
18#include "Poco/Thread.h"
19
20
21using namespace Poco;
22
23
24#define DURSLEEP 250
25#define DURHALFSLEEP DURSLEEP / 2
26#define DURWAIT 300
27
28
29ExpireCacheTest::ExpireCacheTest(const std::string& rName): CppUnit::TestCase(rName)
30{
31}
32
33
34ExpireCacheTest::~ExpireCacheTest()
35{
36}
37
38
39void ExpireCacheTest::testClear()
40{
41 ExpireCache<int, int> aCache(DURSLEEP);
42 aCache.add(1, 2);
43 aCache.add(3, 4);
44 aCache.add(5, 6);
45 assertTrue (aCache.has(1));
46 assertTrue (aCache.has(3));
47 assertTrue (aCache.has(5));
48 assertTrue (*aCache.get(1) == 2);
49 assertTrue (*aCache.get(3) == 4);
50 assertTrue (*aCache.get(5) == 6);
51 aCache.clear();
52 assertTrue (!aCache.has(1));
53 assertTrue (!aCache.has(3));
54 assertTrue (!aCache.has(5));
55}
56
57
58void ExpireCacheTest::testExpire0()
59{
60 try
61 {
62 ExpireCache<int, int> aCache(24);
63 failmsg("cache expire lower than 25 is illegal, test should fail");
64 }
65 catch (Poco::InvalidArgumentException&)
66 {
67 }
68}
69
70
71void ExpireCacheTest::testExpireN()
72{
73 // 3-1 represents the cache sorted by age, elements get replaced at the end of the list
74 // 3-1|5 -> 5 gets removed
75 ExpireCache<int, int> aCache(DURSLEEP);
76 aCache.add(1, 2); // 1
77 assertTrue (aCache.has(1));
78 SharedPtr<int> tmp = aCache.get(1);
79 assertTrue (!tmp.isNull());
80 assertTrue (*tmp == 2);
81 assertTrue (aCache.size() == 1);
82 Thread::sleep(DURWAIT);
83 assertTrue (aCache.size() == 0);
84 assertTrue (!aCache.has(1));
85
86 // tmp must still be valid, access it
87 assertTrue (*tmp == 2);
88 tmp = aCache.get(1);
89 assertTrue (!tmp);
90
91 aCache.add(1, 2); // 1
92 Thread::sleep(DURHALFSLEEP);
93 aCache.add(3, 4); // 3-1
94 assertTrue (aCache.has(1));
95 assertTrue (aCache.has(3));
96 tmp = aCache.get(1);
97 SharedPtr<int> tmp2 = aCache.get(3);
98 assertTrue (*tmp == 2);
99 assertTrue (*tmp2 == 4);
100
101 Thread::sleep(DURHALFSLEEP+25); //3|1
102 assertTrue (!aCache.has(1));
103 assertTrue (aCache.has(3));
104 assertTrue (*tmp == 2); // 1-3
105 assertTrue (*tmp2 == 4); // 3-1
106 tmp2 = aCache.get(3);
107 assertTrue (*tmp2 == 4);
108 Thread::sleep(DURHALFSLEEP+25); //3|1
109 assertTrue (!aCache.has(3));
110 assertTrue (*tmp2 == 4);
111 tmp = aCache.get(1);
112 tmp2 = aCache.get(3);
113 assertTrue (!tmp);
114 assertTrue (!tmp2);
115
116 // removing illegal entries should work too
117 aCache.remove(666);
118
119 aCache.clear();
120 assertTrue (!aCache.has(5));
121 assertTrue (!aCache.has(3));
122}
123
124
125void ExpireCacheTest::testDuplicateAdd()
126{
127 ExpireCache<int, int> aCache(DURSLEEP);
128 aCache.add(1, 2); // 1
129 assertTrue (aCache.has(1));
130 assertTrue (*aCache.get(1) == 2);
131 aCache.add(1, 3);
132 assertTrue (aCache.has(1));
133 assertTrue (*aCache.get(1) == 3);
134}
135
136
137
138void ExpireCacheTest::testAccessExpireN()
139{
140 // 3-1 represents the cache sorted by age, elements get replaced at the end of the list
141 // 3-1|5 -> 5 gets removed
142 AccessExpireCache<int, int> aCache(DURSLEEP);
143 aCache.add(1, 2); // 1
144 assertTrue (aCache.has(1));
145 SharedPtr<int> tmp = aCache.get(1);
146 assertTrue (!tmp.isNull());
147 assertTrue (*tmp == 2);
148 assertTrue (aCache.size() == 1);
149 Thread::sleep(DURWAIT);
150 assertTrue (aCache.size() == 0);
151 assertTrue (!aCache.has(1));
152
153 // tmp must still be valid, access it
154 assertTrue (*tmp == 2);
155 tmp = aCache.get(1);
156 assertTrue (!tmp);
157
158 aCache.add(1, 2); // 1
159 Thread::sleep(DURHALFSLEEP);
160 aCache.add(3, 4); // 3-1
161 assertTrue (aCache.has(1));
162 assertTrue (aCache.has(3));
163
164 Thread::sleep(DURHALFSLEEP+50); //3|1
165 assertTrue (!aCache.has(1));
166 assertTrue (*aCache.get(3) == 4);
167 Thread::sleep(DURHALFSLEEP+25); //3|1
168 assertTrue (*aCache.get(3) == 4);
169 // removing illegal entries should work too
170 aCache.remove(666);
171
172 aCache.clear();
173 assertTrue (!aCache.has(5));
174 assertTrue (!aCache.has(3));
175}
176
177
178void ExpireCacheTest::testExpireWithHas()
179{
180 // 3-1 represents the cache sorted by age, elements get replaced at the end of the list
181 // 3-1|5 -> 5 gets removed
182 ExpireCache<int, int> aCache(DURSLEEP);
183 aCache.add(1, 2); // 1
184 assertTrue (aCache.has(1));
185 Thread::sleep(DURWAIT);
186 assertTrue (!aCache.has(1));
187}
188
189
190void ExpireCacheTest::setUp()
191{
192}
193
194
195void ExpireCacheTest::tearDown()
196{
197}
198
199
200CppUnit::Test* ExpireCacheTest::suite()
201{
202 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ExpireCacheTest");
203
204 CppUnit_addTest(pSuite, ExpireCacheTest, testClear);
205 CppUnit_addTest(pSuite, ExpireCacheTest, testExpire0);
206 CppUnit_addTest(pSuite, ExpireCacheTest, testExpireN);
207 CppUnit_addTest(pSuite, ExpireCacheTest, testDuplicateAdd);
208 CppUnit_addTest(pSuite, ExpireCacheTest, testAccessExpireN);
209 CppUnit_addTest(pSuite, ExpireCacheTest, testExpireWithHas);
210
211 return pSuite;
212}
213