| 1 | // |
| 2 | // SemaphoreTest.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 "SemaphoreTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "Poco/Thread.h" |
| 15 | #include "Poco/Runnable.h" |
| 16 | #include "Poco/Semaphore.h" |
| 17 | #include "Poco/Exception.h" |
| 18 | |
| 19 | |
| 20 | using Poco::Thread; |
| 21 | using Poco::Runnable; |
| 22 | using Poco::Semaphore; |
| 23 | using Poco::TimeoutException; |
| 24 | |
| 25 | |
| 26 | class SemaRunnable: public Runnable |
| 27 | { |
| 28 | public: |
| 29 | SemaRunnable(int n, int max): _ran(false), _sema(n, max) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | void run() |
| 34 | { |
| 35 | _sema.wait(); |
| 36 | _ran = true; |
| 37 | } |
| 38 | |
| 39 | bool ran() const |
| 40 | { |
| 41 | return _ran; |
| 42 | } |
| 43 | |
| 44 | void set() |
| 45 | { |
| 46 | _sema.set(); |
| 47 | } |
| 48 | |
| 49 | void wait() |
| 50 | { |
| 51 | _sema.wait(); |
| 52 | } |
| 53 | |
| 54 | void wait(long milliseconds) |
| 55 | { |
| 56 | _sema.wait(milliseconds); |
| 57 | } |
| 58 | |
| 59 | bool tryWait(long milliseconds) |
| 60 | { |
| 61 | return _sema.tryWait(milliseconds); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | bool _ran; |
| 66 | Semaphore _sema; |
| 67 | }; |
| 68 | |
| 69 | |
| 70 | SemaphoreTest::SemaphoreTest(const std::string& rName): CppUnit::TestCase(rName) |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | |
| 75 | SemaphoreTest::~SemaphoreTest() |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | |
| 80 | void SemaphoreTest::testInitZero() |
| 81 | { |
| 82 | SemaRunnable r(0, 3); |
| 83 | assertTrue (!r.tryWait(10)); |
| 84 | r.set(); |
| 85 | r.wait(); |
| 86 | try |
| 87 | { |
| 88 | r.wait(100); |
| 89 | failmsg("must timeout" ); |
| 90 | } |
| 91 | catch (TimeoutException&) |
| 92 | { |
| 93 | } |
| 94 | catch (...) |
| 95 | { |
| 96 | failmsg("wrong exception" ); |
| 97 | } |
| 98 | r.set(); |
| 99 | r.set(); |
| 100 | assertTrue (r.tryWait(0)); |
| 101 | r.wait(); |
| 102 | assertTrue (!r.tryWait(10)); |
| 103 | |
| 104 | Thread t; |
| 105 | t.start(r); |
| 106 | Thread::sleep(100); |
| 107 | assertTrue (!r.ran()); |
| 108 | r.set(); |
| 109 | t.join(); |
| 110 | assertTrue (r.ran()); |
| 111 | assertTrue (!r.tryWait(10)); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | void SemaphoreTest::testInitNonZero() |
| 116 | { |
| 117 | SemaRunnable r(2, 2); |
| 118 | r.wait(); |
| 119 | assertTrue (r.tryWait(10)); |
| 120 | assertTrue (!r.tryWait(10)); |
| 121 | r.set(); |
| 122 | assertTrue (r.tryWait(10)); |
| 123 | assertTrue (!r.tryWait(10)); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | void SemaphoreTest::setUp() |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | |
| 132 | void SemaphoreTest::tearDown() |
| 133 | { |
| 134 | } |
| 135 | |
| 136 | |
| 137 | CppUnit::Test* SemaphoreTest::suite() |
| 138 | { |
| 139 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SemaphoreTest" ); |
| 140 | |
| 141 | CppUnit_addTest(pSuite, SemaphoreTest, testInitZero); |
| 142 | CppUnit_addTest(pSuite, SemaphoreTest, testInitNonZero); |
| 143 | |
| 144 | return pSuite; |
| 145 | } |
| 146 | |