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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
// Copyright Oliver Kowalke 2013.
// 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)
//
// This test is based on the tests of Boost.Thread
#include <chrono>
#include <mutex>
#include <sstream>
#include <string>
#include <boost/assert.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/fiber/all.hpp>
int value1 = 0;
std::string value2 = "";
struct X {
int value;
void foo( int i) {
value = i;
}
};
class copyable {
public:
bool state;
int value;
copyable() :
state( false),
value( -1) {
}
copyable( int v) :
state( true),
value( v) {
}
void operator()() {
value1 = value;
}
};
class moveable {
public:
bool state;
int value;
moveable() :
state( false),
value( -1) {
}
moveable( int v) :
state( true),
value( v) {
}
moveable( moveable && other) :
state( other.state),
value( other.value) {
other.state = false;
other.value = -1;
}
moveable & operator=( moveable && other) {
if ( this == & other) return * this;
state = other.state;
value = other.value;
other.state = false;
other.value = -1;
return * this;
}
moveable( moveable const& other) = delete;
moveable & operator=( moveable const& other) = delete;
void operator()() {
value1 = value;
}
};
class detachable {
private:
int alive_count_;
public:
static int alive_count;
static bool was_running;
detachable() :
alive_count_( 1) {
++alive_count;
}
detachable( detachable const& g) :
alive_count_( g.alive_count_) {
++alive_count;
}
~detachable() {
alive_count_ = 0;
--alive_count;
}
void operator()() {
BOOST_CHECK_EQUAL(1, alive_count_);
was_running = true;
}
};
int detachable::alive_count = 0;
bool detachable::was_running = false;
void fn1() {
value1 = 1;
}
void fn2( int i, std::string const& s) {
value1 = i;
value2 = s;
}
void fn3( int & i) {
i = 1;
boost::this_fiber::yield();
i = 1;
boost::this_fiber::yield();
i = 2;
boost::this_fiber::yield();
i = 3;
boost::this_fiber::yield();
i = 5;
boost::this_fiber::yield();
i = 8;
}
void fn4() {
boost::this_fiber::yield();
}
void fn5() {
boost::fibers::fiber f( boost::fibers::launch::post, fn4);
BOOST_CHECK( f.joinable() );
f.join();
BOOST_CHECK( ! f.joinable() );
}
void test_scheduler_dtor() {
boost::fibers::context * ctx(
boost::fibers::context::active() );
(void)ctx;
}
void test_join_fn() {
{
value1 = 0;
boost::fibers::fiber f( boost::fibers::launch::post, fn1);
f.join();
BOOST_CHECK_EQUAL( value1, 1);
}
{
value1 = 0;
value2 = "";
boost::fibers::fiber f( boost::fibers::launch::post, fn2, 3, "abc");
f.join();
BOOST_CHECK_EQUAL( value1, 3);
BOOST_CHECK_EQUAL( value2, "abc");
}
}
void test_join_memfn() {
X x = {0};
BOOST_CHECK_EQUAL( x.value, 0);
boost::fibers::fiber( boost::fibers::launch::post, & X::foo, & x, 3).join();
BOOST_CHECK_EQUAL( x.value, 3);
}
void test_join_copyable() {
value1 = 0;
copyable cp( 3);
BOOST_CHECK( cp.state);
BOOST_CHECK_EQUAL( value1, 0);
boost::fibers::fiber f( boost::fibers::launch::post, cp);
f.join();
BOOST_CHECK( cp.state);
BOOST_CHECK_EQUAL( value1, 3);
}
void test_join_moveable() {
value1 = 0;
moveable mv( 7);
BOOST_CHECK( mv.state);
BOOST_CHECK_EQUAL( value1, 0);
boost::fibers::fiber f( boost::fibers::launch::post, std::move( mv) );
f.join();
BOOST_CHECK( ! mv.state);
BOOST_CHECK_EQUAL( value1, 7);
}
void test_join_lambda() {
{
value1 = 0;
value2 = "";
int i = 3;
std::string abc("abc");
boost::fibers::fiber f(
boost::fibers::launch::post, [i,abc]() {
value1 = i;
value2 = abc;
});
f.join();
BOOST_CHECK_EQUAL( value1, 3);
BOOST_CHECK_EQUAL( value2, "abc");
}
{
value1 = 0;
value2 = "";
int i = 3;
std::string abc("abc");
boost::fibers::fiber f(
boost::fibers::launch::post, [](int i, std::string const& abc) {
value1 = i;
value2 = abc;
},
i, abc);
f.join();
BOOST_CHECK_EQUAL( value1, 3);
BOOST_CHECK_EQUAL( value2, "abc");
}
}
void test_join_bind() {
{
value1 = 0;
value2 = "";
int i = 3;
std::string abc("abc");
boost::fibers::fiber f(
boost::fibers::launch::post, std::bind(
[i,abc]() {
value1 = i;
value2 = abc;
}
));
f.join();
BOOST_CHECK_EQUAL( value1, 3);
BOOST_CHECK_EQUAL( value2, "abc");
}
{
value1 = 0;
value2 = "";
std::string abc("abc");
boost::fibers::fiber f(
boost::fibers::launch::post, std::bind(
[](std::string & str) {
value1 = 3;
value2 = str;
},
abc
));
f.join();
BOOST_CHECK_EQUAL( value1, 3);
BOOST_CHECK_EQUAL( value2, "abc");
}
{
value1 = 0;
value2 = "";
std::string abc("abc");
boost::fibers::fiber f(
boost::fibers::launch::post, std::bind(
[]( std::string & str) {
value1 = 3;
value2 = str;
},
std::placeholders::_1
),
std::ref( abc) );
f.join();
BOOST_CHECK_EQUAL( value1, 3);
BOOST_CHECK_EQUAL( value2, "abc");
}
}
void test_join_in_fiber() {
// spawn fiber f
// f spawns an new fiber f' in its fiber-fn
// f' yields in its fiber-fn
// f joins s' and gets suspended (waiting on s')
boost::fibers::fiber f( boost::fibers::launch::post, fn5);
BOOST_CHECK( f.joinable() );
// join() resumes f + f' which completes
f.join();
BOOST_CHECK( ! f.joinable() );
}
void test_move_fiber() {
boost::fibers::fiber f1;
BOOST_CHECK( ! f1.joinable() );
boost::fibers::fiber f2( boost::fibers::launch::post, fn1);
BOOST_CHECK( f2.joinable() );
f1 = std::move( f2);
BOOST_CHECK( f1.joinable() );
BOOST_CHECK( ! f2.joinable() );
f1.join();
BOOST_CHECK( ! f1.joinable() );
BOOST_CHECK( ! f2.joinable() );
}
void test_id() {
boost::fibers::fiber f1;
boost::fibers::fiber f2( boost::fibers::launch::post, fn1);
BOOST_CHECK( ! f1.joinable() );
BOOST_CHECK( f2.joinable() );
BOOST_CHECK_EQUAL( boost::fibers::fiber::id(), f1.get_id() );
BOOST_CHECK( boost::fibers::fiber::id() != f2.get_id() );
boost::fibers::fiber f3( boost::fibers::launch::post, fn1);
BOOST_CHECK( f2.get_id() != f3.get_id() );
f1 = std::move( f2);
BOOST_CHECK( f1.joinable() );
BOOST_CHECK( ! f2.joinable() );
BOOST_CHECK( boost::fibers::fiber::id() != f1.get_id() );
BOOST_CHECK_EQUAL( boost::fibers::fiber::id(), f2.get_id() );
BOOST_CHECK( ! f2.joinable() );
f1.join();
f3.join();
}
void test_yield() {
int v1 = 0, v2 = 0;
BOOST_CHECK_EQUAL( 0, v1);
BOOST_CHECK_EQUAL( 0, v2);
boost::fibers::fiber f1( boost::fibers::launch::post, fn3, std::ref( v1) );
boost::fibers::fiber f2( boost::fibers::launch::post, fn3, std::ref( v2) );
f1.join();
f2.join();
BOOST_CHECK( ! f1.joinable() );
BOOST_CHECK( ! f2.joinable() );
BOOST_CHECK_EQUAL( 8, v1);
BOOST_CHECK_EQUAL( 8, v2);
}
void test_sleep_for() {
typedef std::chrono::system_clock Clock;
typedef Clock::time_point time_point;
std::chrono::milliseconds ms(500);
time_point t0 = Clock::now();
boost::this_fiber::sleep_for(ms);
time_point t1 = Clock::now();
std::chrono::nanoseconds ns = (t1 - t0) - ms;
std::chrono::nanoseconds err = ms / 10;
// This test is spurious as it depends on the time the fiber system switches the fiber
BOOST_CHECK((std::max)(ns.count(), -ns.count()) < (err+std::chrono::milliseconds(1000)).count());
}
void test_sleep_until() {
{
typedef std::chrono::steady_clock Clock;
typedef Clock::time_point time_point;
std::chrono::milliseconds ms(500);
time_point t0 = Clock::now();
boost::this_fiber::sleep_until(t0 + ms);
time_point t1 = Clock::now();
std::chrono::nanoseconds ns = (t1 - t0) - ms;
std::chrono::nanoseconds err = ms / 10;
// This test is spurious as it depends on the time the thread system switches the threads
BOOST_CHECK((std::max)(ns.count(), -ns.count()) < (err+std::chrono::milliseconds(1000)).count());
}
{
typedef std::chrono::system_clock Clock;
typedef Clock::time_point time_point;
std::chrono::milliseconds ms(500);
time_point t0 = Clock::now();
boost::this_fiber::sleep_until(t0 + ms);
time_point t1 = Clock::now();
std::chrono::nanoseconds ns = (t1 - t0) - ms;
std::chrono::nanoseconds err = ms / 10;
// This test is spurious as it depends on the time the thread system switches the threads
BOOST_CHECK((std::max)(ns.count(), -ns.count()) < (err+std::chrono::milliseconds(1000)).count());
}
}
void do_wait( boost::fibers::barrier* b) {
b->wait();
}
void test_detach() {
{
boost::fibers::fiber f( boost::fibers::launch::post, (detachable()) );
BOOST_CHECK( f.joinable() );
f.detach();
BOOST_CHECK( ! f.joinable() );
boost::this_fiber::sleep_for( std::chrono::milliseconds(250) );
BOOST_CHECK( detachable::was_running);
BOOST_CHECK_EQUAL( 0, detachable::alive_count);
}
{
boost::fibers::fiber f( boost::fibers::launch::post, (detachable()) );
BOOST_CHECK( f.joinable() );
boost::this_fiber::yield();
f.detach();
BOOST_CHECK( ! f.joinable() );
boost::this_fiber::sleep_for( std::chrono::milliseconds(250) );
BOOST_CHECK( detachable::was_running);
BOOST_CHECK_EQUAL( 0, detachable::alive_count);
}
}
boost::unit_test::test_suite * init_unit_test_suite( int, char* []) {
boost::unit_test::test_suite * test =
BOOST_TEST_SUITE("Boost.Fiber: fiber test suite");
test->add( BOOST_TEST_CASE( & test_scheduler_dtor) );
test->add( BOOST_TEST_CASE( & test_join_fn) );
test->add( BOOST_TEST_CASE( & test_join_memfn) );
test->add( BOOST_TEST_CASE( & test_join_copyable) );
test->add( BOOST_TEST_CASE( & test_join_moveable) );
test->add( BOOST_TEST_CASE( & test_join_lambda) );
test->add( BOOST_TEST_CASE( & test_join_bind) );
test->add( BOOST_TEST_CASE( & test_join_in_fiber) );
test->add( BOOST_TEST_CASE( & test_move_fiber) );
test->add( BOOST_TEST_CASE( & test_yield) );
test->add( BOOST_TEST_CASE( & test_sleep_for) );
test->add( BOOST_TEST_CASE( & test_sleep_until) );
test->add( BOOST_TEST_CASE( & test_detach) );
return test;
}
|