concurrent_queue_test.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2018 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: vitus@google.com (Michael Vitus)
  30. #include "ceres/concurrent_queue.h"
  31. #include <chrono>
  32. #include <thread>
  33. #include "ceres/internal/config.h"
  34. #include "gmock/gmock.h"
  35. #include "gtest/gtest.h"
  36. namespace ceres::internal {
  37. // A basic test of push and pop.
  38. TEST(ConcurrentQueue, PushPop) {
  39. ConcurrentQueue<int> queue;
  40. const int num_to_add = 10;
  41. for (int i = 0; i < num_to_add; ++i) {
  42. queue.Push(i);
  43. }
  44. for (int i = 0; i < num_to_add; ++i) {
  45. int value;
  46. ASSERT_TRUE(queue.Pop(&value));
  47. EXPECT_EQ(i, value);
  48. }
  49. }
  50. // Push and pop elements from the queue after StopWaiters has been called.
  51. TEST(ConcurrentQueue, PushPopAfterStopWaiters) {
  52. ConcurrentQueue<int> queue;
  53. const int num_to_add = 10;
  54. int value;
  55. // Pop should return immediately with false with an empty queue.
  56. ASSERT_FALSE(queue.Pop(&value));
  57. for (int i = 0; i < num_to_add; ++i) {
  58. queue.Push(i);
  59. }
  60. // Call stop waiters to ensure we can still Push and Pop from the queue.
  61. queue.StopWaiters();
  62. for (int i = 0; i < num_to_add; ++i) {
  63. ASSERT_TRUE(queue.Pop(&value));
  64. EXPECT_EQ(i, value);
  65. }
  66. // Pop should return immediately with false with an empty queue.
  67. ASSERT_FALSE(queue.Pop(&value));
  68. // Ensure we can still push onto the queue after StopWaiters has been called.
  69. const int offset = 123;
  70. for (int i = 0; i < num_to_add; ++i) {
  71. queue.Push(i + offset);
  72. }
  73. for (int i = 0; i < num_to_add; ++i) {
  74. int value;
  75. ASSERT_TRUE(queue.Pop(&value));
  76. EXPECT_EQ(i + offset, value);
  77. }
  78. // Pop should return immediately with false with an empty queue.
  79. ASSERT_FALSE(queue.Pop(&value));
  80. // Try calling StopWaiters again to ensure nothing changes.
  81. queue.StopWaiters();
  82. queue.Push(13456);
  83. ASSERT_TRUE(queue.Pop(&value));
  84. EXPECT_EQ(13456, value);
  85. }
  86. // Push and pop elements after StopWaiters and EnableWaiters has been called.
  87. TEST(ConcurrentQueue, PushPopStopAndStart) {
  88. ConcurrentQueue<int> queue;
  89. int value;
  90. queue.Push(13456);
  91. queue.Push(256);
  92. queue.StopWaiters();
  93. ASSERT_TRUE(queue.Pop(&value));
  94. EXPECT_EQ(13456, value);
  95. queue.EnableWaiters();
  96. // Try adding another entry after enable has been called.
  97. queue.Push(989);
  98. // Ensure we can pop both elements off.
  99. ASSERT_TRUE(queue.Pop(&value));
  100. EXPECT_EQ(256, value);
  101. ASSERT_TRUE(queue.Pop(&value));
  102. EXPECT_EQ(989, value);
  103. // Re-enable waiting.
  104. queue.EnableWaiters();
  105. // Pop should return immediately with false with an empty queue.
  106. ASSERT_FALSE(queue.Pop(&value));
  107. }
  108. // A basic test for Wait.
  109. TEST(ConcurrentQueue, Wait) {
  110. ConcurrentQueue<int> queue;
  111. int value;
  112. queue.Push(13456);
  113. ASSERT_TRUE(queue.Wait(&value));
  114. EXPECT_EQ(13456, value);
  115. queue.StopWaiters();
  116. // Ensure waiting returns immediately after StopWaiters.
  117. EXPECT_FALSE(queue.Wait(&value));
  118. EXPECT_FALSE(queue.Wait(&value));
  119. EXPECT_FALSE(queue.Pop(&value));
  120. // Calling StopWaiters multiple times does not change anything.
  121. queue.StopWaiters();
  122. EXPECT_FALSE(queue.Wait(&value));
  123. EXPECT_FALSE(queue.Wait(&value));
  124. queue.Push(989);
  125. queue.Push(789);
  126. ASSERT_TRUE(queue.Wait(&value));
  127. EXPECT_EQ(989, value);
  128. ASSERT_TRUE(queue.Wait(&value));
  129. EXPECT_EQ(789, value);
  130. }
  131. // Ensure wait blocks until an element is pushed. Also ensure wait does not
  132. // block after StopWaiters is called and there is no value in the queue.
  133. // Finally, ensures EnableWaiters re-enables waiting.
  134. TEST(ConcurrentQueue, EnsureWaitBlocks) {
  135. ConcurrentQueue<int> queue;
  136. int value = 0;
  137. bool valid_value = false;
  138. bool waiting = false;
  139. std::mutex mutex;
  140. std::thread thread([&]() {
  141. {
  142. std::lock_guard<std::mutex> lock(mutex);
  143. waiting = true;
  144. }
  145. int element = 87987;
  146. bool valid = queue.Wait(&element);
  147. {
  148. std::lock_guard<std::mutex> lock(mutex);
  149. waiting = false;
  150. value = element;
  151. valid_value = valid;
  152. }
  153. });
  154. // Give the thread time to start and wait.
  155. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  156. // Ensure nothing is has been popped off the queue
  157. {
  158. std::lock_guard<std::mutex> lock(mutex);
  159. EXPECT_TRUE(waiting);
  160. ASSERT_FALSE(valid_value);
  161. ASSERT_EQ(0, value);
  162. }
  163. queue.Push(13456);
  164. // Wait for the thread to pop the value.
  165. thread.join();
  166. EXPECT_TRUE(valid_value);
  167. EXPECT_EQ(13456, value);
  168. }
  169. TEST(ConcurrentQueue, StopAndEnableWaiters) {
  170. ConcurrentQueue<int> queue;
  171. int value = 0;
  172. bool valid_value = false;
  173. bool waiting = false;
  174. std::mutex mutex;
  175. auto task = [&]() {
  176. {
  177. std::lock_guard<std::mutex> lock(mutex);
  178. waiting = true;
  179. }
  180. int element = 87987;
  181. bool valid = queue.Wait(&element);
  182. {
  183. std::lock_guard<std::mutex> lock(mutex);
  184. waiting = false;
  185. value = element;
  186. valid_value = valid;
  187. }
  188. };
  189. std::thread thread_1(task);
  190. // Give the thread time to start and wait.
  191. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  192. // Ensure the thread is waiting.
  193. {
  194. std::lock_guard<std::mutex> lock(mutex);
  195. EXPECT_TRUE(waiting);
  196. }
  197. // Unblock the thread.
  198. queue.StopWaiters();
  199. thread_1.join();
  200. // Ensure nothing has been popped off the queue.
  201. EXPECT_FALSE(valid_value);
  202. EXPECT_EQ(87987, value);
  203. // Ensure another call to Wait returns immediately.
  204. EXPECT_FALSE(queue.Wait(&value));
  205. queue.EnableWaiters();
  206. value = 0;
  207. valid_value = false;
  208. waiting = false;
  209. // Start another task waiting for an element to be pushed.
  210. std::thread thread_2(task);
  211. // Give the thread time to start and wait.
  212. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  213. // Ensure nothing is popped off the queue.
  214. {
  215. std::lock_guard<std::mutex> lock(mutex);
  216. EXPECT_TRUE(waiting);
  217. ASSERT_FALSE(valid_value);
  218. ASSERT_EQ(0, value);
  219. }
  220. queue.Push(13456);
  221. // Wait for the thread to pop the value.
  222. thread_2.join();
  223. EXPECT_TRUE(valid_value);
  224. EXPECT_EQ(13456, value);
  225. }
  226. } // namespace ceres::internal