1 | // |
---|---|
2 | // NamedMutexTest.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 "NamedMutexTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/NamedMutex.h" |
15 | #include "Poco/Thread.h" |
16 | #include "Poco/Runnable.h" |
17 | #include "Poco/Timestamp.h" |
18 | |
19 | |
20 | using Poco::NamedMutex; |
21 | using Poco::Thread; |
22 | using Poco::Runnable; |
23 | using Poco::Timestamp; |
24 | |
25 | #if POCO != POCO_OS_CYGWIN |
26 | |
27 | static NamedMutex testMutex("TestMutex"); |
28 | |
29 | namespace |
30 | { |
31 | class TestLock: public Runnable |
32 | { |
33 | public: |
34 | void run() |
35 | { |
36 | |
37 | testMutex.lock(); |
38 | _timestamp.update(); |
39 | testMutex.unlock(); |
40 | } |
41 | |
42 | const Timestamp& timestamp() const |
43 | { |
44 | return _timestamp; |
45 | } |
46 | |
47 | private: |
48 | Timestamp _timestamp; |
49 | }; |
50 | |
51 | class TestTryLock: public Runnable |
52 | { |
53 | public: |
54 | TestTryLock(): _locked(false) |
55 | { |
56 | } |
57 | |
58 | void run() |
59 | { |
60 | if (testMutex.tryLock()) |
61 | { |
62 | _locked = true; |
63 | testMutex.unlock(); |
64 | } |
65 | } |
66 | |
67 | bool locked() const |
68 | { |
69 | return _locked; |
70 | } |
71 | |
72 | private: |
73 | bool _locked; |
74 | }; |
75 | } |
76 | |
77 | |
78 | NamedMutexTest::NamedMutexTest(const std::string& rName): CppUnit::TestCase(rName) |
79 | { |
80 | } |
81 | |
82 | |
83 | NamedMutexTest::~NamedMutexTest() |
84 | { |
85 | } |
86 | |
87 | |
88 | void NamedMutexTest::testLock() |
89 | { |
90 | try { |
91 | testMutex.lock(); |
92 | Thread thr; |
93 | TestLock tl; |
94 | thr.start(tl); |
95 | Timestamp now; |
96 | Thread::sleep(2000); |
97 | testMutex.unlock(); |
98 | thr.join(); |
99 | assertTrue (tl.timestamp() > now); |
100 | } |
101 | catch(Poco::NotImplementedException e) |
102 | { |
103 | #if POCO_OS != POCO_OS_ANDROID |
104 | throw e; |
105 | #endif |
106 | } |
107 | } |
108 | |
109 | |
110 | void NamedMutexTest::testTryLock() |
111 | { |
112 | Thread thr1; |
113 | TestTryLock ttl1; |
114 | thr1.start(ttl1); |
115 | thr1.join(); |
116 | #if POCO_OS != POCO_OS_ANDROID |
117 | assertTrue (ttl1.locked()); |
118 | #endif |
119 | try { |
120 | testMutex.lock(); |
121 | Thread thr2; |
122 | TestTryLock ttl2; |
123 | thr2.start(ttl2); |
124 | thr2.join(); |
125 | testMutex.unlock(); |
126 | assertTrue (!ttl2.locked()); |
127 | } |
128 | catch(Poco::NotImplementedException e) |
129 | { |
130 | #if POCO_OS != POCO_OS_ANDROID |
131 | throw e; |
132 | #endif |
133 | } |
134 | } |
135 | #endif |
136 | |
137 | void NamedMutexTest::setUp() |
138 | { |
139 | } |
140 | |
141 | |
142 | void NamedMutexTest::tearDown() |
143 | { |
144 | } |
145 | |
146 | |
147 | CppUnit::Test* NamedMutexTest::suite() |
148 | { |
149 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NamedMutexTest"); |
150 | |
151 | #if POCO != POCO_OS_CYGWIN |
152 | CppUnit_addTest(pSuite, NamedMutexTest, testLock); |
153 | CppUnit_addTest(pSuite, NamedMutexTest, testTryLock); |
154 | #endif |
155 | |
156 | return pSuite; |
157 | } |
158 |