1//
2// TimedNotificationQueueTest.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 "TimedNotificationQueueTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/TimedNotificationQueue.h"
15#include "Poco/Notification.h"
16#include "Poco/Timestamp.h"
17#include <iostream>
18
19using Poco::TimedNotificationQueue;
20using Poco::Notification;
21using Poco::Timestamp;
22
23
24namespace
25{
26 class QTestNotification: public Notification
27 {
28 public:
29 QTestNotification(const std::string& data): _data(data)
30 {
31 }
32 ~QTestNotification()
33 {
34 }
35 const std::string& data() const
36 {
37 return _data;
38 }
39
40 private:
41 std::string _data;
42 };
43}
44
45
46TimedNotificationQueueTest::TimedNotificationQueueTest(const std::string& rName): CppUnit::TestCase(rName)
47{
48}
49
50
51TimedNotificationQueueTest::~TimedNotificationQueueTest()
52{
53}
54
55
56void TimedNotificationQueueTest::testDequeue()
57{
58 TimedNotificationQueue queue;
59 assertTrue (queue.empty());
60 assertTrue (queue.size() == 0);
61 Notification* pNf = queue.dequeueNotification();
62 assertNullPtr(pNf);
63 queue.enqueueNotification(new Notification, Timestamp());
64 assertTrue (!queue.empty());
65 assertTrue (queue.size() == 1);
66 pNf = queue.dequeueNotification();
67 assertNotNullPtr(pNf);
68 assertTrue (queue.empty());
69 assertTrue (queue.size() == 0);
70 pNf->release();
71
72 Poco::Clock ts1;
73 ts1 += 100000;
74 Poco::Clock ts2;
75 ts2 += 200000;
76 Poco::Clock ts3;
77 ts3 += 300000;
78 Poco::Clock ts4;
79 ts4 += 400000;
80
81 queue.enqueueNotification(new QTestNotification("first"), ts1);
82 queue.enqueueNotification(new QTestNotification("fourth"), ts4);
83 queue.enqueueNotification(new QTestNotification("third"), ts3);
84 queue.enqueueNotification(new QTestNotification("second"), ts2);
85 assertTrue (!queue.empty());
86 assertTrue (queue.size() == 4);
87 QTestNotification* pTNf = 0;
88 while (!pTNf)
89 {
90 pTNf = dynamic_cast<QTestNotification*>(queue.dequeueNotification());
91 }
92 assertNotNullPtr(pTNf);
93 assertTrue (pTNf->data() == "first");
94 pTNf->release();
95 assertTrue (ts1.elapsed() >= 0);
96 assertTrue (!queue.empty());
97 assertTrue (queue.size() == 3);
98
99 pTNf = 0;
100 while (!pTNf)
101 {
102 pTNf = dynamic_cast<QTestNotification*>(queue.dequeueNotification());
103 }
104 assertNotNullPtr(pTNf);
105 assertTrue (pTNf->data() == "second");
106 pTNf->release();
107 assertTrue (ts2.elapsed() >= 0);
108 assertTrue (!queue.empty());
109 assertTrue (queue.size() == 2);
110
111 pTNf = 0;
112 while (!pTNf)
113 {
114 pTNf = dynamic_cast<QTestNotification*>(queue.dequeueNotification());
115 }
116 assertNotNullPtr(pTNf);
117 assertTrue (pTNf->data() == "third");
118 pTNf->release();
119 assertTrue (ts3.elapsed() >= 0);
120 assertTrue (!queue.empty());
121 assertTrue (queue.size() == 1);
122
123 pTNf = 0;
124 while (!pTNf)
125 {
126 pTNf = dynamic_cast<QTestNotification*>(queue.dequeueNotification());
127 }
128 assertNotNullPtr(pTNf);
129 assertTrue (pTNf->data() == "fourth");
130 pTNf->release();
131 assertTrue (ts4.elapsed() >= 0);
132 assertTrue (queue.empty());
133 assertTrue (queue.size() == 0);
134
135 pNf = queue.dequeueNotification();
136 assertNullPtr(pNf);
137}
138
139
140void TimedNotificationQueueTest::testWaitDequeue()
141{
142 TimedNotificationQueue queue;
143
144 Poco::Timestamp ts1;
145 ts1 += 100000;
146 Poco::Timestamp ts2;
147 ts2 += 200000;
148 Poco::Timestamp ts3;
149 ts3 += 300000;
150 Poco::Timestamp ts4;
151 ts4 += 400000;
152
153 queue.enqueueNotification(new QTestNotification("first"), ts1);
154 queue.enqueueNotification(new QTestNotification("fourth"), ts4);
155 queue.enqueueNotification(new QTestNotification("third"), ts3);
156 queue.enqueueNotification(new QTestNotification("second"), ts2);
157 assertTrue (!queue.empty());
158 assertTrue (queue.size() == 4);
159 QTestNotification* pTNf = dynamic_cast<QTestNotification*>(queue.waitDequeueNotification());
160 assertNotNullPtr(pTNf);
161 assertTrue (pTNf->data() == "first");
162 pTNf->release();
163 assertTrue (ts1.elapsed() >= 0);
164 assertTrue (!queue.empty());
165 assertTrue (queue.size() == 3);
166
167 pTNf = dynamic_cast<QTestNotification*>(queue.waitDequeueNotification());
168 assertNotNullPtr(pTNf);
169 assertTrue (pTNf->data() == "second");
170 pTNf->release();
171 assertTrue (ts2.elapsed() >= 0);
172 assertTrue (!queue.empty());
173 assertTrue (queue.size() == 2);
174
175 pTNf = dynamic_cast<QTestNotification*>(queue.waitDequeueNotification());
176 assertNotNullPtr(pTNf);
177 assertTrue (pTNf->data() == "third");
178 pTNf->release();
179 assertTrue (ts3.elapsed() >= 0);
180 assertTrue (!queue.empty());
181 assertTrue (queue.size() == 1);
182
183 pTNf = dynamic_cast<QTestNotification*>(queue.waitDequeueNotification());
184 assertNotNullPtr(pTNf);
185 assertTrue (pTNf->data() == "fourth");
186 pTNf->release();
187 assertTrue (ts4.elapsed() >= 0);
188 assertTrue (queue.empty());
189 assertTrue (queue.size() == 0);
190}
191
192
193void TimedNotificationQueueTest::testWaitDequeueTimeout()
194{
195 TimedNotificationQueue queue;
196
197 Poco::Timestamp ts1;
198 ts1 += 200000;
199 Poco::Timestamp ts2;
200 ts2 += 400000;
201 Poco::Timestamp ts3;
202 ts3 += 600000;
203 Poco::Timestamp ts4;
204 ts4 += 800000;
205
206 queue.enqueueNotification(new QTestNotification("first"), ts1);
207 queue.enqueueNotification(new QTestNotification("fourth"), ts4);
208 queue.enqueueNotification(new QTestNotification("third"), ts3);
209 queue.enqueueNotification(new QTestNotification("second"), ts2);
210 assertTrue (!queue.empty());
211 assertTrue (queue.size() == 4);
212 QTestNotification* pTNf = dynamic_cast<QTestNotification*>(queue.waitDequeueNotification(10));
213 assertNullPtr(pTNf);
214 pTNf = dynamic_cast<QTestNotification*>(queue.waitDequeueNotification(20));
215 assertNullPtr(pTNf);
216 pTNf = dynamic_cast<QTestNotification*>(queue.waitDequeueNotification(200));
217 assertNotNullPtr(pTNf);
218 assertTrue (pTNf->data() == "first");
219 pTNf->release();
220 assertTrue (ts1.elapsed() >= 0);
221 assertTrue (!queue.empty());
222 assertTrue (queue.size() == 3);
223
224 pTNf = dynamic_cast<QTestNotification*>(queue.waitDequeueNotification(220));
225 assertNotNullPtr(pTNf);
226 assertTrue (pTNf->data() == "second");
227 pTNf->release();
228 assertTrue (ts2.elapsed() >= 0);
229 assertTrue (!queue.empty());
230 assertTrue (queue.size() == 2);
231
232 pTNf = dynamic_cast<QTestNotification*>(queue.waitDequeueNotification(220));
233 assertNotNullPtr(pTNf);
234 assertTrue (pTNf->data() == "third");
235 pTNf->release();
236 assertTrue (ts3.elapsed() >= 0);
237 assertTrue (!queue.empty());
238 assertTrue (queue.size() == 1);
239
240 pTNf = dynamic_cast<QTestNotification*>(queue.waitDequeueNotification(220));
241 assertNotNullPtr(pTNf);
242 assertTrue (pTNf->data() == "fourth");
243 pTNf->release();
244 assertTrue (ts1.elapsed() >= 0);
245 assertTrue (queue.empty());
246 assertTrue (queue.size() == 0);
247}
248
249
250void TimedNotificationQueueTest::setUp()
251{
252}
253
254
255void TimedNotificationQueueTest::tearDown()
256{
257}
258
259
260CppUnit::Test* TimedNotificationQueueTest::suite()
261{
262 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TimedNotificationQueueTest");
263
264 CppUnit_addTest(pSuite, TimedNotificationQueueTest, testDequeue);
265 CppUnit_addTest(pSuite, TimedNotificationQueueTest, testWaitDequeue);
266 CppUnit_addTest(pSuite, TimedNotificationQueueTest, testWaitDequeueTimeout);
267
268 return pSuite;
269}
270