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