123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- // Copyright (C) 2014 Ian Forbed
- // Copyright (C) 2014 Vicente J. Botet Escriba
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- #include <boost/config.hpp>
- #if ! defined BOOST_NO_CXX11_DECLTYPE
- #define BOOST_RESULT_OF_USE_DECLTYPE
- #endif
- #define BOOST_THREAD_VERSION 4
- #define BOOST_THREAD_PROVIDES_EXECUTORS
- #include <boost/thread.hpp>
- #include <boost/chrono.hpp>
- #include <boost/function.hpp>
- #include <boost/thread/concurrent_queues/sync_timed_queue.hpp>
- #include <boost/thread/executors/work.hpp>
- #include <boost/core/lightweight_test.hpp>
- using namespace boost::chrono;
- typedef boost::concurrent::sync_timed_queue<int> sync_tq;
- void test_all()
- {
- sync_tq pq;
- BOOST_TEST(pq.empty());
- BOOST_TEST(!pq.closed());
- BOOST_TEST_EQ(pq.size(), std::size_t(0));
- for(int i = 1; i <= 5; i++){
- pq.push(i, milliseconds(i*100));
- BOOST_TEST(!pq.empty());
- BOOST_TEST_EQ(pq.size(), std::size_t(i));
- }
- for(int i = 6; i <= 10; i++){
- pq.push(i,steady_clock::now() + milliseconds(i*100));
- BOOST_TEST(!pq.empty());
- BOOST_TEST_EQ(pq.size(), std::size_t(i));
- }
- for(int i = 1; i <= 10; i++){
- int val = pq.pull();
- BOOST_TEST_EQ(val, i);
- }
- int val;
- boost::queue_op_status st = pq.nonblocking_pull(val);
- BOOST_TEST(boost::queue_op_status::empty == st);
- BOOST_TEST(pq.empty());
- pq.close();
- BOOST_TEST(pq.closed());
- }
- void test_all_with_try()
- {
- sync_tq pq;
- BOOST_TEST(pq.empty());
- BOOST_TEST(!pq.closed());
- BOOST_TEST_EQ(pq.size(), std::size_t(0));
- for(int i = 1; i <= 5; i++){
- boost::queue_op_status st = pq.try_push(i, milliseconds(i*100));
- BOOST_TEST(st == boost::queue_op_status::success );
- BOOST_TEST(!pq.empty());
- BOOST_TEST_EQ(pq.size(), std::size_t(i));
- }
- for(int i = 6; i <= 10; i++){
- boost::queue_op_status st = pq.try_push(i,steady_clock::now() + milliseconds(i*100));
- BOOST_TEST(st == boost::queue_op_status::success );
- BOOST_TEST(!pq.empty());
- BOOST_TEST_EQ(pq.size(), std::size_t(i));
- }
- for(int i = 1; i <= 10; i++){
- int val=0;
- boost::queue_op_status st = pq.wait_pull(val);
- BOOST_TEST(st == boost::queue_op_status::success );
- BOOST_TEST_EQ(val, i);
- }
- int val;
- boost::queue_op_status st = pq.nonblocking_pull(val);
- BOOST_TEST(st == boost::queue_op_status::empty );
- BOOST_TEST(pq.empty());
- pq.close();
- BOOST_TEST(pq.closed());
- }
- void func(steady_clock::time_point pushed, steady_clock::duration dur)
- {
- BOOST_TEST(pushed + dur <= steady_clock::now());
- }
- void func2()
- {
- BOOST_TEST(false);
- }
- /**
- * This test ensures that when items come of the front of the queue
- * that at least $dur has elapsed.
- */
- void test_deque_times()
- {
- boost::concurrent::sync_timed_queue<boost::function<void()> > tq;
- for(int i = 0; i < 10; i++)
- {
- steady_clock::duration d = milliseconds(i*100);
- boost::function<void()> fn = boost::bind(func, steady_clock::now(), d);
- tq.push(fn, d);
- }
- while(!tq.empty())
- {
- boost::function<void()> fn = tq.pull();
- fn();
- }
- }
- /**
- * This test ensures that when items come of the front of the queue
- * that at least $dur has elapsed.
- */
- #if 0
- void test_deque_times2()
- {
- boost::concurrent::sync_timed_queue<boost::executors::work> tq;
- for(int i = 0; i < 10; i++)
- {
- steady_clock::duration d = milliseconds(i*100);
- tq.push(func2, d);
- }
- while(!tq.empty())
- {
- boost::executors::work fn = tq.pull();
- fn();
- }
- }
- #endif
- int main()
- {
- test_all();
- test_all_with_try();
- test_deque_times();
- //test_deque_times2(); // rt fails
- return boost::report_errors();
- }
|