1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
// Copyright (C) 2013 Tim Blechmann
//
// 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/test/unit_test.hpp>
#include <boost/bind.hpp>
#include <boost/chrono.hpp>
#include <boost/thread/thread.hpp>
#include <boost/sync/events.hpp>
#include <boost/sync/support/boost_chrono.hpp>
#include <boost/typeof/typeof.hpp>
BOOST_AUTO_TEST_CASE(test_event_post_wait)
{
boost::sync::manual_reset_event ev;
ev.set();
ev.wait();
BOOST_REQUIRE( ev.try_wait() == true );
ev.reset();
BOOST_REQUIRE( ev.try_wait() == false );
}
BOOST_AUTO_TEST_CASE(test_event_post_try_wait)
{
boost::sync::manual_reset_event ev;
BOOST_REQUIRE( ev.try_wait() == false );
ev.set();
BOOST_REQUIRE( ev.try_wait() == true );
}
BOOST_AUTO_TEST_CASE(test_event_post_wait_autoreset)
{
boost::sync::auto_reset_event ev;
ev.post();
BOOST_REQUIRE( ev.try_wait() == true );
BOOST_REQUIRE( ev.try_wait() == false );
}
BOOST_AUTO_TEST_CASE(test_event_reset)
{
boost::sync::manual_reset_event ev;
BOOST_REQUIRE( ev.try_wait() == false );
ev.set();
BOOST_REQUIRE( ev.try_wait() == true );
ev.reset();
BOOST_REQUIRE( ev.try_wait() == false );
}
inline void post_event(boost::sync::manual_reset_event& evt)
{
evt.set();
}
inline void post_event(boost::sync::auto_reset_event& evt)
{
evt.post();
}
template <typename EventType>
struct event_wait_and_post_test
{
void run()
{
boost::thread post_thread(boost::bind(&event_wait_and_post_test::wait_and_post, this));
ev_.wait();
}
void wait_and_post()
{
boost::this_thread::sleep_for(boost::chrono::seconds(1));
(post_event)(ev_);
}
EventType ev_;
boost::thread thread_;
};
BOOST_AUTO_TEST_CASE(event_wait_and_post)
{
event_wait_and_post_test<boost::sync::manual_reset_event> test;
test.run();
}
BOOST_AUTO_TEST_CASE(event_wait_and_post_autoreset)
{
event_wait_and_post_test<boost::sync::auto_reset_event> test;
test.run();
}
BOOST_AUTO_TEST_CASE(test_event_wait_for)
{
using namespace boost;
boost::sync::manual_reset_event ev;
BOOST_AUTO(start, chrono::system_clock::now());
BOOST_REQUIRE( ev.try_wait() == false );
BOOST_REQUIRE(!ev.wait_for(chrono::milliseconds(500)));
BOOST_REQUIRE( ev.try_wait() == false );
BOOST_AUTO(end, chrono::system_clock::now());
BOOST_AUTO(wait_time, end - start);
// guessing!
BOOST_REQUIRE( wait_time > chrono::milliseconds(450) );
BOOST_REQUIRE( wait_time < chrono::milliseconds(1000) );
ev.set();
BOOST_REQUIRE(ev.wait_for(chrono::milliseconds(500)));
}
BOOST_AUTO_TEST_CASE(test_event_wait_until)
{
using namespace boost;
boost::sync::manual_reset_event ev;
{
BOOST_AUTO(now, chrono::system_clock::now());
BOOST_AUTO(timeout, now + chrono::milliseconds(500));
BOOST_REQUIRE(!ev.wait_until(timeout));
BOOST_AUTO(end, chrono::system_clock::now());
BOOST_AUTO(timeout_delta, end - timeout);
// guessing!
BOOST_REQUIRE( timeout_delta > chrono::milliseconds(-400) );
BOOST_REQUIRE( timeout_delta < chrono::milliseconds(400) );
}
ev.set();
{
BOOST_AUTO(start, chrono::system_clock::now());
BOOST_AUTO(timeout, start + chrono::milliseconds(500));
BOOST_REQUIRE(ev.wait_until(timeout));
BOOST_AUTO(end, chrono::system_clock::now());
// guessing!
BOOST_REQUIRE( (end - start) < chrono::milliseconds(100) );
}
}
|