1//
2// ActiveDispatcherTest.cpp
3//
4// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
5// All rights reserved.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "ActiveDispatcherTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/ActiveDispatcher.h"
15#include "Poco/ActiveMethod.h"
16#include "Poco/Thread.h"
17#include "Poco/Event.h"
18#include "Poco/Exception.h"
19
20
21using Poco::ActiveDispatcher;
22using Poco::ActiveMethod;
23using Poco::ActiveResult;
24using Poco::ActiveStarter;
25using Poco::Thread;
26using Poco::Event;
27using Poco::Exception;
28
29
30namespace
31{
32 class ActiveObject: public ActiveDispatcher
33 {
34 public:
35 ActiveObject():
36 testMethod(this, &ActiveObject::testMethodImpl),
37 testVoid(this, &ActiveObject::testVoidImpl),
38 testVoidInOut(this, &ActiveObject::testVoidInOutImpl),
39 testVoidIn(this, &ActiveObject::testVoidInImpl)
40 {
41 }
42
43 ~ActiveObject()
44 {
45 }
46
47 ActiveMethod<int, int, ActiveObject, ActiveStarter<ActiveDispatcher> > testMethod;
48
49 ActiveMethod<void, int, ActiveObject, ActiveStarter<ActiveDispatcher> > testVoid;
50
51 ActiveMethod<void, void, ActiveObject, ActiveStarter<ActiveDispatcher> > testVoidInOut;
52
53 ActiveMethod<int, void, ActiveObject, ActiveStarter<ActiveDispatcher> > testVoidIn;
54
55 void cont()
56 {
57 _continue.set();
58 }
59
60 protected:
61 int testMethodImpl(const int& n)
62 {
63 if (n == 100) throw Exception("n == 100");
64 _continue.wait();
65 return n;
66 }
67
68 void testVoidImpl(const int& n)
69 {
70 if (n == 100) throw Exception("n == 100");
71 _continue.wait();
72 }
73
74 void testVoidInOutImpl()
75 {
76 _continue.wait();
77 }
78
79 int testVoidInImpl()
80 {
81 _continue.wait();
82 return 123;
83 }
84
85 private:
86 Event _continue;
87 };
88}
89
90
91ActiveDispatcherTest::ActiveDispatcherTest(const std::string& rName): CppUnit::TestCase(rName)
92{
93}
94
95
96ActiveDispatcherTest::~ActiveDispatcherTest()
97{
98}
99
100
101void ActiveDispatcherTest::testWait()
102{
103 ActiveObject activeObj;
104 ActiveResult<int> result = activeObj.testMethod(123);
105 assertTrue (!result.available());
106 activeObj.cont();
107 result.wait();
108 assertTrue (result.available());
109 assertTrue (result.data() == 123);
110 assertTrue (!result.failed());
111}
112
113
114void ActiveDispatcherTest::testWaitInterval()
115{
116 ActiveObject activeObj;
117 ActiveResult<int> result = activeObj.testMethod(123);
118 assertTrue (!result.available());
119 try
120 {
121 result.wait(100);
122 fail("wait must fail");
123 }
124 catch (Exception&)
125 {
126 }
127 activeObj.cont();
128 result.wait(10000);
129 assertTrue (result.available());
130 assertTrue (result.data() == 123);
131 assertTrue (!result.failed());
132}
133
134
135void ActiveDispatcherTest::testTryWait()
136{
137 ActiveObject activeObj;
138 ActiveResult<int> result = activeObj.testMethod(123);
139 assertTrue (!result.available());
140 assertTrue (!result.tryWait(200));
141 activeObj.cont();
142 assertTrue (result.tryWait(10000));
143 assertTrue (result.available());
144 assertTrue (result.data() == 123);
145 assertTrue (!result.failed());
146}
147
148
149void ActiveDispatcherTest::testFailure()
150{
151 ActiveObject activeObj;
152 ActiveResult<int> result = activeObj.testMethod(100);
153 result.wait();
154 assertTrue (result.available());
155 assertTrue (result.failed());
156 std::string msg = result.error();
157 assertTrue (msg.find("n == 100") != std::string::npos);
158}
159
160
161void ActiveDispatcherTest::testVoid()
162{
163 ActiveObject activeObj;
164 ActiveResult<void> result = activeObj.testVoid(123);
165 assertTrue (!result.available());
166 activeObj.cont();
167 result.wait();
168 assertTrue (result.available());
169 assertTrue (!result.failed());
170}
171
172
173void ActiveDispatcherTest::testVoidInOut()
174{
175 ActiveObject activeObj;
176 ActiveResult<void> result = activeObj.testVoidInOut();
177 assertTrue (!result.available());
178 activeObj.cont();
179 result.wait();
180 assertTrue (result.available());
181 assertTrue (!result.failed());
182}
183
184
185void ActiveDispatcherTest::testVoidIn()
186{
187 ActiveObject activeObj;
188 ActiveResult<int> result = activeObj.testVoidIn();
189 assertTrue (!result.available());
190 activeObj.cont();
191 result.wait();
192 assertTrue (result.available());
193 assertTrue (!result.failed());
194 assertTrue (result.data() == 123);
195}
196
197
198void ActiveDispatcherTest::setUp()
199{
200}
201
202
203void ActiveDispatcherTest::tearDown()
204{
205}
206
207
208CppUnit::Test* ActiveDispatcherTest::suite()
209{
210 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ActiveDispatcherTest");
211
212 CppUnit_addTest(pSuite, ActiveDispatcherTest, testWait);
213 CppUnit_addTest(pSuite, ActiveDispatcherTest, testWaitInterval);
214 CppUnit_addTest(pSuite, ActiveDispatcherTest, testTryWait);
215 CppUnit_addTest(pSuite, ActiveDispatcherTest, testFailure);
216 CppUnit_addTest(pSuite, ActiveDispatcherTest, testVoid);
217 CppUnit_addTest(pSuite, ActiveDispatcherTest, testVoidIn);
218 CppUnit_addTest(pSuite, ActiveDispatcherTest, testVoidInOut);
219
220 return pSuite;
221}
222