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