ConditionTest.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // ConditionTest.cpp
  3. //
  4. // $Id: //poco/Main/Foundation/testsuite/src/ConditionTest.cpp#1 $
  5. //
  6. // Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
  7. // and Contributors.
  8. //
  9. // Permission is hereby granted, free of charge, to any person or organization
  10. // obtaining a copy of the software and accompanying documentation covered by
  11. // this license (the "Software") to use, reproduce, display, distribute,
  12. // execute, and transmit the Software, and to prepare derivative works of the
  13. // Software, and to permit third-parties to whom the Software is furnished to
  14. // do so, all subject to the following:
  15. //
  16. // The copyright notices in the Software and this entire statement, including
  17. // the above license grant, this restriction and the following disclaimer,
  18. // must be included in all copies of the Software, in whole or in part, and
  19. // all derivative works of the Software, unless such copies or derivative
  20. // works are solely in the form of machine-executable object code generated by
  21. // a source language processor.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  26. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  27. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  28. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. // DEALINGS IN THE SOFTWARE.
  30. //
  31. #include "ConditionTest.h"
  32. #include "CppUnit/TestCaller.h"
  33. #include "CppUnit/TestSuite.h"
  34. #include "Poco/Thread.h"
  35. #include "Poco/Runnable.h"
  36. #include "Poco/Condition.h"
  37. #include "Poco/Mutex.h"
  38. #include "Poco/Exception.h"
  39. using Poco::Thread;
  40. using Poco::Runnable;
  41. using Poco::Condition;
  42. using Poco::Mutex;
  43. using Poco::TimeoutException;
  44. namespace
  45. {
  46. class WaitRunnable: public Runnable
  47. {
  48. public:
  49. WaitRunnable(Condition& cond, Mutex& mutex):
  50. _ran(false),
  51. _cond(cond),
  52. _mutex(mutex)
  53. {
  54. }
  55. void run()
  56. {
  57. _mutex.lock();
  58. _cond.wait(_mutex);
  59. _mutex.unlock();
  60. _ran = true;
  61. }
  62. bool ran() const
  63. {
  64. return _ran;
  65. }
  66. private:
  67. bool _ran;
  68. Condition& _cond;
  69. Mutex& _mutex;
  70. };
  71. class TryWaitRunnable: public Runnable
  72. {
  73. public:
  74. TryWaitRunnable(Condition& cond, Mutex& mutex):
  75. _ran(false),
  76. _cond(cond),
  77. _mutex(mutex)
  78. {
  79. }
  80. void run()
  81. {
  82. _mutex.lock();
  83. if (_cond.tryWait(_mutex, 10000))
  84. {
  85. _ran = true;
  86. }
  87. _mutex.unlock();
  88. }
  89. bool ran() const
  90. {
  91. return _ran;
  92. }
  93. private:
  94. bool _ran;
  95. Condition& _cond;
  96. Mutex& _mutex;
  97. };
  98. }
  99. ConditionTest::ConditionTest(const std::string& name): CppUnit::TestCase(name)
  100. {
  101. }
  102. ConditionTest::~ConditionTest()
  103. {
  104. }
  105. void ConditionTest::testSignal()
  106. {
  107. Condition cond;
  108. Mutex mtx;
  109. WaitRunnable r1(cond, mtx);
  110. WaitRunnable r2(cond, mtx);
  111. Thread t1;
  112. Thread t2;
  113. t1.start(r1);
  114. Thread::sleep(200);
  115. t2.start(r2);
  116. assert (!r1.ran());
  117. assert (!r2.ran());
  118. cond.signal();
  119. t1.join();
  120. assert (r1.ran());
  121. assert (!t2.tryJoin(200));
  122. cond.signal();
  123. t2.join();
  124. assert (r2.ran());
  125. }
  126. void ConditionTest::testBroadcast()
  127. {
  128. Condition cond;
  129. Mutex mtx;
  130. WaitRunnable r1(cond, mtx);
  131. WaitRunnable r2(cond, mtx);
  132. TryWaitRunnable r3(cond, mtx);
  133. Thread t1;
  134. Thread t2;
  135. Thread t3;
  136. t1.start(r1);
  137. Thread::sleep(200);
  138. t2.start(r2);
  139. Thread::sleep(200);
  140. t3.start(r3);
  141. assert (!r1.ran());
  142. assert (!r2.ran());
  143. assert (!r3.ran());
  144. cond.signal();
  145. t1.join();
  146. assert (r1.ran());
  147. assert (!t2.tryJoin(500));
  148. assert (!t3.tryJoin(500));
  149. cond.broadcast();
  150. t2.join();
  151. t3.join();
  152. assert (r2.ran());
  153. assert (r3.ran());
  154. }
  155. void ConditionTest::setUp()
  156. {
  157. }
  158. void ConditionTest::tearDown()
  159. {
  160. }
  161. CppUnit::Test* ConditionTest::suite()
  162. {
  163. CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ConditionTest");
  164. CppUnit_addTest(pSuite, ConditionTest, testSignal);
  165. CppUnit_addTest(pSuite, ConditionTest, testBroadcast);
  166. return pSuite;
  167. }