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 "CppUnit/TestCaller.h"
13 #include "CppUnit/TestSuite.h"
14 #include "Poco/Activity.h"
15 #include "Poco/Thread.h"
16 
17 
18 using Poco::Activity;
19 using Poco::Thread;
20 
21 
22 namespace
23 {
24 	class ActiveObject
25 	{
26 	public:
ActiveObject()27 		ActiveObject():
28 			_activity(this, &ActiveObject::run),
29 			_count(0)
30 		{
31 		}
32 
~ActiveObject()33 		~ActiveObject()
34 		{
35 		}
36 
activity()37 		Activity<ActiveObject>& activity()
38 		{
39 			return _activity;
40 		}
41 
count() const42 		Poco::UInt64 count() const
43 		{
44 			return _count;
45 		}
46 
47 	protected:
run()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 
ActivityTest(const std::string & name)61 ActivityTest::ActivityTest(const std::string& name): CppUnit::TestCase(name)
62 {
63 }
64 
65 
~ActivityTest()66 ActivityTest::~ActivityTest()
67 {
68 }
69 
70 
testActivity()71 void 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 
setUp()85 void ActivityTest::setUp()
86 {
87 }
88 
89 
tearDown()90 void ActivityTest::tearDown()
91 {
92 }
93 
94 
suite()95 CppUnit::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