tq_single_thread_pass.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (C) 2014 Ian Forbed
  2. // Copyright (C) 2014 Vicente J. Botet Escriba
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #include <boost/config.hpp>
  8. #if ! defined BOOST_NO_CXX11_DECLTYPE
  9. #define BOOST_RESULT_OF_USE_DECLTYPE
  10. #endif
  11. #define BOOST_THREAD_VERSION 4
  12. #define BOOST_THREAD_PROVIDES_EXECUTORS
  13. #include <boost/thread.hpp>
  14. #include <boost/chrono.hpp>
  15. #include <boost/function.hpp>
  16. #include <boost/thread/concurrent_queues/sync_timed_queue.hpp>
  17. #include <boost/thread/executors/work.hpp>
  18. #include <boost/core/lightweight_test.hpp>
  19. using namespace boost::chrono;
  20. typedef boost::concurrent::sync_timed_queue<int> sync_tq;
  21. void test_all()
  22. {
  23. sync_tq pq;
  24. BOOST_TEST(pq.empty());
  25. BOOST_TEST(!pq.closed());
  26. BOOST_TEST_EQ(pq.size(), std::size_t(0));
  27. for(int i = 1; i <= 5; i++){
  28. pq.push(i, milliseconds(i*100));
  29. BOOST_TEST(!pq.empty());
  30. BOOST_TEST_EQ(pq.size(), std::size_t(i));
  31. }
  32. for(int i = 6; i <= 10; i++){
  33. pq.push(i,steady_clock::now() + milliseconds(i*100));
  34. BOOST_TEST(!pq.empty());
  35. BOOST_TEST_EQ(pq.size(), std::size_t(i));
  36. }
  37. for(int i = 1; i <= 10; i++){
  38. int val = pq.pull();
  39. BOOST_TEST_EQ(val, i);
  40. }
  41. int val;
  42. boost::queue_op_status st = pq.nonblocking_pull(val);
  43. BOOST_TEST(boost::queue_op_status::empty == st);
  44. BOOST_TEST(pq.empty());
  45. pq.close();
  46. BOOST_TEST(pq.closed());
  47. }
  48. void test_all_with_try()
  49. {
  50. sync_tq pq;
  51. BOOST_TEST(pq.empty());
  52. BOOST_TEST(!pq.closed());
  53. BOOST_TEST_EQ(pq.size(), std::size_t(0));
  54. for(int i = 1; i <= 5; i++){
  55. boost::queue_op_status st = pq.try_push(i, milliseconds(i*100));
  56. BOOST_TEST(st == boost::queue_op_status::success );
  57. BOOST_TEST(!pq.empty());
  58. BOOST_TEST_EQ(pq.size(), std::size_t(i));
  59. }
  60. for(int i = 6; i <= 10; i++){
  61. boost::queue_op_status st = pq.try_push(i,steady_clock::now() + milliseconds(i*100));
  62. BOOST_TEST(st == boost::queue_op_status::success );
  63. BOOST_TEST(!pq.empty());
  64. BOOST_TEST_EQ(pq.size(), std::size_t(i));
  65. }
  66. for(int i = 1; i <= 10; i++){
  67. int val=0;
  68. boost::queue_op_status st = pq.wait_pull(val);
  69. BOOST_TEST(st == boost::queue_op_status::success );
  70. BOOST_TEST_EQ(val, i);
  71. }
  72. int val;
  73. boost::queue_op_status st = pq.nonblocking_pull(val);
  74. BOOST_TEST(st == boost::queue_op_status::empty );
  75. BOOST_TEST(pq.empty());
  76. pq.close();
  77. BOOST_TEST(pq.closed());
  78. }
  79. void func(steady_clock::time_point pushed, steady_clock::duration dur)
  80. {
  81. BOOST_TEST(pushed + dur <= steady_clock::now());
  82. }
  83. void func2()
  84. {
  85. BOOST_TEST(false);
  86. }
  87. /**
  88. * This test ensures that when items come of the front of the queue
  89. * that at least $dur has elapsed.
  90. */
  91. void test_deque_times()
  92. {
  93. boost::concurrent::sync_timed_queue<boost::function<void()> > tq;
  94. for(int i = 0; i < 10; i++)
  95. {
  96. steady_clock::duration d = milliseconds(i*100);
  97. boost::function<void()> fn = boost::bind(func, steady_clock::now(), d);
  98. tq.push(fn, d);
  99. }
  100. while(!tq.empty())
  101. {
  102. boost::function<void()> fn = tq.pull();
  103. fn();
  104. }
  105. }
  106. /**
  107. * This test ensures that when items come of the front of the queue
  108. * that at least $dur has elapsed.
  109. */
  110. #if 0
  111. void test_deque_times2()
  112. {
  113. boost::concurrent::sync_timed_queue<boost::executors::work> tq;
  114. for(int i = 0; i < 10; i++)
  115. {
  116. steady_clock::duration d = milliseconds(i*100);
  117. tq.push(func2, d);
  118. }
  119. while(!tq.empty())
  120. {
  121. boost::executors::work fn = tq.pull();
  122. fn();
  123. }
  124. }
  125. #endif
  126. int main()
  127. {
  128. test_all();
  129. test_all_with_try();
  130. test_deque_times();
  131. //test_deque_times2(); // rt fails
  132. return boost::report_errors();
  133. }