1//
2// TimerTest.cpp
3//
4// Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "TimerTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Util/Timer.h"
15#include "Poco/Util/TimerTaskAdapter.h"
16
17
18using Poco::Util::Timer;
19using Poco::Util::TimerTask;
20using Poco::Util::TimerTaskAdapter;
21using Poco::Timestamp;
22using Poco::Clock;
23
24
25TimerTest::TimerTest(const std::string& name): CppUnit::TestCase(name)
26{
27}
28
29
30TimerTest::~TimerTest()
31{
32}
33
34
35void TimerTest::testScheduleTimestamp()
36{
37 Timer timer;
38
39 Timestamp time;
40 time += 1000000;
41
42 TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
43
44 assertTrue (pTask->lastExecution() == 0);
45
46 timer.schedule(pTask, time);
47
48 _event.wait();
49 assertTrue (pTask->lastExecution() >= time);
50}
51
52
53void TimerTest::testScheduleClock()
54{
55 Timer timer;
56
57 // As reference
58 Timestamp time;
59 time += 1000000;
60
61 Clock clock;
62 clock += 1000000;
63
64 TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
65
66 assertTrue (pTask->lastExecution() == 0);
67
68 timer.schedule(pTask, clock);
69
70 _event.wait();
71 assertTrue (pTask->lastExecution() >= time);
72}
73
74
75void TimerTest::testScheduleInterval()
76{
77 Timer timer;
78
79 Timestamp time;
80
81 TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
82
83 assertTrue (pTask->lastExecution() == 0);
84
85 timer.schedule(pTask, 500, 500);
86
87 _event.wait();
88 assertTrue (time.elapsed() >= 590000);
89 assertTrue (pTask->lastExecution().elapsed() < 130000);
90
91 _event.wait();
92 assertTrue (time.elapsed() >= 1190000);
93 assertTrue (pTask->lastExecution().elapsed() < 130000);
94
95 _event.wait();
96 assertTrue (time.elapsed() >= 1790000);
97 assertTrue (pTask->lastExecution().elapsed() < 130000);
98
99 pTask->cancel();
100 assertTrue (pTask->isCancelled());
101}
102
103
104void TimerTest::testScheduleIntervalTimestamp()
105{
106 Timer timer;
107
108 Timestamp time;
109
110 TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
111
112 assertTrue (pTask->lastExecution() == 0);
113
114 Timestamp scheduleTime;
115 scheduleTime += 500 * 1000;
116
117 timer.schedule(pTask, scheduleTime, 500);
118
119 _event.wait();
120 assertTrue (time.elapsed() >= 590000);
121 assertTrue (pTask->lastExecution().elapsed() < 130000);
122
123 _event.wait();
124 assertTrue (time.elapsed() >= 1190000);
125 assertTrue (pTask->lastExecution().elapsed() < 130000);
126
127 _event.wait();
128 assertTrue (time.elapsed() >= 1790000);
129 assertTrue (pTask->lastExecution().elapsed() < 130000);
130
131 pTask->cancel();
132 assertTrue (pTask->isCancelled());
133}
134
135
136void TimerTest::testScheduleIntervalClock()
137{
138 Timer timer;
139
140 Timestamp time;
141
142 TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
143
144 assertTrue (pTask->lastExecution() == 0);
145
146 Clock scheduleClock;
147 scheduleClock += 500 * 1000;
148
149 timer.schedule(pTask, scheduleClock, 500);
150
151 _event.wait();
152 assertTrue (time.elapsed() >= 590000);
153 assertTrue (pTask->lastExecution().elapsed() < 130000);
154
155 _event.wait();
156 assertTrue (time.elapsed() >= 1190000);
157 assertTrue (pTask->lastExecution().elapsed() < 130000);
158
159 _event.wait();
160 assertTrue (time.elapsed() >= 1790000);
161 assertTrue (pTask->lastExecution().elapsed() < 130000);
162
163 pTask->cancel();
164 assertTrue (pTask->isCancelled());
165}
166
167
168void TimerTest::testScheduleAtFixedRate()
169{
170 Timer timer;
171
172 Timestamp time;
173
174 TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
175
176 assertTrue (pTask->lastExecution() == 0);
177
178 timer.scheduleAtFixedRate(pTask, 500, 500);
179
180 _event.wait();
181 assertTrue (time.elapsed() >= 500000);
182 assertTrue (pTask->lastExecution().elapsed() < 130000);
183
184 _event.wait();
185 assertTrue (time.elapsed() >= 1000000);
186 assertTrue (pTask->lastExecution().elapsed() < 130000);
187
188 _event.wait();
189 assertTrue (time.elapsed() >= 1500000);
190 assertTrue (pTask->lastExecution().elapsed() < 130000);
191
192 pTask->cancel();
193 assertTrue (pTask->isCancelled());
194}
195
196
197void TimerTest::testCancel()
198{
199 Timer timer;
200
201 Timestamp time;
202
203 TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
204
205 assertTrue (pTask->lastExecution() == 0);
206
207 timer.scheduleAtFixedRate(pTask, 5000, 5000);
208
209 pTask->cancel();
210 assertTrue (pTask->isCancelled());
211
212 try
213 {
214 timer.scheduleAtFixedRate(pTask, 5000, 5000);
215 fail("must not reschedule a cancelled task");
216 }
217 catch (Poco::IllegalStateException&)
218 {
219 }
220 catch (Poco::Exception&)
221 {
222 fail("bad exception thrown");
223 }
224}
225
226
227void TimerTest::testCancelAllStop()
228{
229 {
230 Timer timer;
231
232 TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
233
234 timer.scheduleAtFixedRate(pTask, 5000, 5000);
235
236 Poco::Thread::sleep(100);
237
238 timer.cancel(false);
239 }
240
241 assertTrue (true); // don't hang
242}
243
244
245void TimerTest::testCancelAllWaitStop()
246{
247 {
248 Timer timer;
249
250 TimerTask::Ptr pTask = new TimerTaskAdapter<TimerTest>(*this, &TimerTest::onTimer);
251
252 timer.scheduleAtFixedRate(pTask, 5000, 5000);
253
254 Poco::Thread::sleep(100);
255
256 timer.cancel(true);
257 }
258
259 assertTrue (true); // don't hang
260}
261
262
263void TimerTest::setUp()
264{
265}
266
267
268void TimerTest::tearDown()
269{
270}
271
272
273void TimerTest::onTimer(TimerTask& task)
274{
275 Poco::Thread::sleep(100);
276 _event.set();
277}
278
279
280CppUnit::Test* TimerTest::suite()
281{
282 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TimerTest");
283
284 CppUnit_addTest(pSuite, TimerTest, testScheduleTimestamp);
285 CppUnit_addTest(pSuite, TimerTest, testScheduleClock);
286 CppUnit_addTest(pSuite, TimerTest, testScheduleInterval);
287 CppUnit_addTest(pSuite, TimerTest, testScheduleIntervalTimestamp);
288 CppUnit_addTest(pSuite, TimerTest, testScheduleIntervalClock);
289 CppUnit_addTest(pSuite, TimerTest, testScheduleAtFixedRate);
290 CppUnit_addTest(pSuite, TimerTest, testCancel);
291 CppUnit_addTest(pSuite, TimerTest, testCancelAllStop);
292 CppUnit_addTest(pSuite, TimerTest, testCancelAllWaitStop);
293
294 return pSuite;
295}
296