1//
2// NamedEventTest.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 "NamedEventTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/NamedEvent.h"
15#include "Poco/Thread.h"
16#include "Poco/Runnable.h"
17#include "Poco/Timestamp.h"
18
19
20using Poco::NamedEvent;
21using Poco::Thread;
22using Poco::Runnable;
23using Poco::Timestamp;
24
25
26static NamedEvent testEvent("TestEvent");
27
28
29namespace
30{
31 class TestEvent: public Runnable
32 {
33 public:
34 void run()
35 {
36
37 testEvent.wait();
38 _timestamp.update();
39 }
40
41 const Timestamp& timestamp() const
42 {
43 return _timestamp;
44 }
45
46 private:
47 Timestamp _timestamp;
48 };
49}
50
51
52NamedEventTest::NamedEventTest(const std::string& rName): CppUnit::TestCase(rName)
53{
54}
55
56
57NamedEventTest::~NamedEventTest()
58{
59}
60
61
62void NamedEventTest::testNamedEvent()
63{
64 Thread thr1;
65 TestEvent te;
66 thr1.start(te);
67 Timestamp now;
68 Thread::sleep(2000);
69 try {
70 testEvent.set();
71 }
72 catch(Poco::NotImplementedException e)
73 {
74#if POCO_OS != POCO_OS_ANDROID
75 throw e;
76#endif
77 }
78 thr1.join();
79#if POCO_OS != POCO_OS_ANDROID
80 assertTrue (te.timestamp() > now);
81#endif
82 Thread thr2;
83 thr2.start(te);
84 now.update();
85 Thread::sleep(2000);
86 try {
87 testEvent.set();
88 }
89 catch(Poco::NotImplementedException e)
90 {
91#if POCO_OS != POCO_OS_ANDROID
92 throw e;
93#endif
94 }
95 thr2.join();
96#if POCO_OS != POCO_OS_ANDROID
97 assertTrue (te.timestamp() > now);
98#endif
99}
100
101
102void NamedEventTest::setUp()
103{
104}
105
106
107void NamedEventTest::tearDown()
108{
109}
110
111
112CppUnit::Test* NamedEventTest::suite()
113{
114 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NamedEventTest");
115
116 CppUnit_addTest(pSuite, NamedEventTest, testNamedEvent);
117
118 return pSuite;
119}
120