threadSafeDequeTest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifdef TORQUE_TESTS_ENABLED
  23. #include "testing/unitTesting.h"
  24. #include "platform/threads/threadSafeDeque.h"
  25. #include "platform/threads/thread.h"
  26. #include "core/util/tVector.h"
  27. #include "console/console.h"
  28. FIXTURE(ThreadSafeDeque)
  29. {
  30. public:
  31. // Used by the concurrent test.
  32. struct Value : public ThreadSafeRefCount<Value>
  33. {
  34. U32 mIndex;
  35. U32 mTick;
  36. Value() {}
  37. Value(U32 index, U32 tick)
  38. : mIndex(index), mTick(tick) {}
  39. };
  40. typedef ThreadSafeRef<Value> ValueRef;
  41. struct Deque : public ThreadSafeDeque<ValueRef>
  42. {
  43. typedef ThreadSafeDeque<ValueRef> Parent;
  44. U32 mPushIndex;
  45. U32 mPopIndex;
  46. Deque()
  47. : mPushIndex(0), mPopIndex(0) {}
  48. void pushBack(const ValueRef& value)
  49. {
  50. EXPECT_EQ(value->mIndex, mPushIndex) << "index out of line";
  51. mPushIndex++;
  52. Parent::pushBack(value);
  53. }
  54. bool tryPopFront(ValueRef& outValue)
  55. {
  56. if(Parent::tryPopFront(outValue))
  57. {
  58. EXPECT_EQ(outValue->mIndex, mPopIndex) << "index out of line";
  59. mPopIndex++;
  60. return true;
  61. }
  62. else
  63. return false;
  64. }
  65. };
  66. struct ProducerThread : public Thread
  67. {
  68. Vector<U32>& mValues;
  69. Deque& mDeque;
  70. ProducerThread(Vector<U32>& values, Deque& deque)
  71. : mValues(values), mDeque(deque) {}
  72. virtual void run(void*)
  73. {
  74. for(U32 i = 0; i < mValues.size(); i++)
  75. {
  76. U32 tick = Platform::getRealMilliseconds();
  77. mValues[i] = tick;
  78. ValueRef val = new Value(i, tick);
  79. mDeque.pushBack(val);
  80. }
  81. // WORKAROUND: due to a bug in the Deque, we lose an item, and thus the test will loop forever. We currently
  82. // don't have a timeout solution, so instead push som extra elements just to make sure Consumer
  83. // doesn't get stuck.
  84. for(U32 i = mValues.size(); i < mValues.size() + 5; i++)
  85. {
  86. U32 tick = Platform::getRealMilliseconds();
  87. ValueRef val = new Value(i, tick);
  88. mDeque.pushBack(val);
  89. }
  90. }
  91. };
  92. struct ConsumerThread : public Thread
  93. {
  94. Vector<U32>& mValues;
  95. Deque& mDeque;
  96. ConsumerThread(Vector<U32>& values, Deque& deque)
  97. : mValues(values), mDeque(deque) {}
  98. virtual void run(void*)
  99. {
  100. for(U32 i = 0; i < mValues.size(); i++)
  101. {
  102. ValueRef value;
  103. while(!mDeque.tryPopFront(value));
  104. EXPECT_EQ(i, value->mIndex);
  105. EXPECT_EQ(value->mTick, mValues[i]);
  106. }
  107. }
  108. };
  109. };
  110. // Test deque without concurrency.
  111. TEST_FIX(ThreadSafeDeque, PopFront)
  112. {
  113. ThreadSafeDeque<char> deque;
  114. String str = "teststring";
  115. for(U32 i = 0; i < str.length(); i++)
  116. deque.pushBack(str[i]);
  117. EXPECT_FALSE(deque.isEmpty());
  118. char ch;
  119. for(U32 i = 0; i < str.length(); i++)
  120. {
  121. EXPECT_TRUE(deque.tryPopFront(ch));
  122. EXPECT_EQ(str[i], ch);
  123. }
  124. ASSERT_TRUE(deque.isEmpty());
  125. }
  126. TEST_FIX(ThreadSafeDeque, PopBack)
  127. {
  128. ThreadSafeDeque<char> deque;
  129. String str = "teststring";
  130. const char* p1 = str.c_str() + 4;
  131. const char* p2 = p1 + 1;
  132. while(*p2)
  133. {
  134. deque.pushFront(*p1);
  135. deque.pushBack(*p2);
  136. --p1;
  137. ++p2;
  138. }
  139. char ch;
  140. for(S32 i = str.length()-1; i >= 0; i--)
  141. {
  142. EXPECT_TRUE(deque.tryPopBack(ch));
  143. EXPECT_EQ(str[i], ch);
  144. }
  145. ASSERT_TRUE(deque.isEmpty());
  146. }
  147. // Test deque in a concurrent setting.
  148. // Test many items in a row
  149. TEST_FIX(ThreadSafeDeque, Concurrent1)
  150. {
  151. const U32 NumValues = 100;
  152. Deque mDeque;
  153. Vector<U32> mValues;
  154. mValues.setSize(NumValues);
  155. ProducerThread pThread(mValues, mDeque);
  156. ConsumerThread cThread(mValues, mDeque);
  157. pThread.start();
  158. cThread.start();
  159. pThread.join();
  160. cThread.join();
  161. mValues.clear();
  162. };
  163. // Test a few items many times to catch any race-condition in start-up
  164. TEST_FIX(ThreadSafeDeque, Concurrent2)
  165. {
  166. for (int i = 0; i < 10000; ++i)
  167. {
  168. Deque mDeque;
  169. Vector<U32> mValues;
  170. mValues.setSize(5);
  171. ProducerThread pThread(mValues, mDeque);
  172. ConsumerThread cThread(mValues, mDeque);
  173. cThread.start();
  174. pThread.start();
  175. pThread.join();
  176. cThread.join();
  177. mValues.clear();
  178. if (::testing::Test::HasFailure()) break;
  179. }
  180. };
  181. #endif