1//
2// ActivityTest.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 "ActivityTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Activity.h"
15#include "Poco/Thread.h"
16
17
18using Poco::Activity;
19using Poco::Thread;
20
21
22namespace
23{
24 class ActiveObject
25 {
26 public:
27 ActiveObject():
28 _activity(this, &ActiveObject::run),
29 _count(0)
30 {
31 }
32
33 ~ActiveObject()
34 {
35 }
36
37 Activity<ActiveObject>& activity()
38 {
39 return _activity;
40 }
41
42 Poco::UInt64 count() const
43 {
44 return _count;
45 }
46
47 protected:
48 void run()
49 {
50 while (!_activity.isStopped())
51 ++_count;
52 }
53
54 private:
55 Activity<ActiveObject> _activity;
56 Poco::UInt64 _count;
57 };
58}
59
60
61ActivityTest::ActivityTest(const std::string& rName): CppUnit::TestCase(rName)
62{
63}
64
65
66ActivityTest::~ActivityTest()
67{
68}
69
70
71void ActivityTest::testActivity()
72{
73 ActiveObject activeObj;
74 assertTrue (activeObj.activity().isStopped());
75 activeObj.activity().start();
76 assertTrue (!activeObj.activity().isStopped());
77 Thread::sleep(1000);
78 assertTrue (activeObj.activity().isRunning());
79 activeObj.activity().stop();
80 activeObj.activity().wait();
81 assertTrue (activeObj.count() > 0);
82}
83
84
85void ActivityTest::setUp()
86{
87}
88
89
90void ActivityTest::tearDown()
91{
92}
93
94
95CppUnit::Test* ActivityTest::suite()
96{
97 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ActivityTest");
98
99 CppUnit_addTest(pSuite, ActivityTest, testActivity);
100
101 return pSuite;
102}
103