test_future_dispatch.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // (C) Copyright 2008-10 Anthony Williams
  2. // 2015 Oliver Kowalke
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <chrono>
  8. #include <memory>
  9. #include <stdexcept>
  10. #include <string>
  11. #include <utility>
  12. #include <boost/test/unit_test.hpp>
  13. #include <boost/fiber/all.hpp>
  14. typedef std::chrono::milliseconds ms;
  15. typedef std::chrono::high_resolution_clock Clock;
  16. int gi = 7;
  17. struct my_exception : public std::runtime_error {
  18. my_exception() :
  19. std::runtime_error("my_exception") {
  20. }
  21. };
  22. struct A {
  23. A() = default;
  24. A( A const&) = delete;
  25. A( A &&) = default;
  26. A & operator=( A const&) = delete;
  27. A & operator=( A &&) = default;
  28. int value;
  29. };
  30. void fn1( boost::fibers::promise< int > * p, int i) {
  31. boost::this_fiber::yield();
  32. p->set_value( i);
  33. }
  34. void fn2() {
  35. boost::fibers::promise< int > p;
  36. boost::fibers::future< int > f( p.get_future() );
  37. boost::this_fiber::yield();
  38. boost::fibers::fiber( boost::fibers::launch::dispatch, fn1, & p, 7).detach();
  39. boost::this_fiber::yield();
  40. BOOST_CHECK( 7 == f.get() );
  41. }
  42. int fn3() {
  43. return 3;
  44. }
  45. void fn4() {
  46. }
  47. int fn5() {
  48. boost::throw_exception( my_exception() );
  49. return 3;
  50. }
  51. void fn6() {
  52. boost::throw_exception( my_exception() );
  53. }
  54. int & fn7() {
  55. return gi;
  56. }
  57. int fn8( int i) {
  58. return i;
  59. }
  60. A fn9() {
  61. A a;
  62. a.value = 3;
  63. return a;
  64. }
  65. A fn10() {
  66. boost::throw_exception( my_exception() );
  67. return A();
  68. }
  69. void fn11( boost::fibers::promise< int > p) {
  70. boost::this_fiber::sleep_for( ms(500) );
  71. p.set_value(3);
  72. }
  73. void fn12( boost::fibers::promise< int& > p) {
  74. boost::this_fiber::sleep_for( ms(500) );
  75. gi = 5;
  76. p.set_value( gi);
  77. }
  78. void fn13( boost::fibers::promise< void > p) {
  79. boost::this_fiber::sleep_for( ms(400) );
  80. p.set_value();
  81. }
  82. // future
  83. void test_future_create() {
  84. // default constructed future is not valid
  85. boost::fibers::future< int > f1;
  86. BOOST_CHECK( ! f1.valid() );
  87. // future retrieved from promise is valid (if it is the first)
  88. boost::fibers::promise< int > p2;
  89. boost::fibers::future< int > f2 = p2.get_future();
  90. BOOST_CHECK( f2.valid() );
  91. }
  92. void test_future_create_ref() {
  93. // default constructed future is not valid
  94. boost::fibers::future< int& > f1;
  95. BOOST_CHECK( ! f1.valid() );
  96. // future retrieved from promise is valid (if it is the first)
  97. boost::fibers::promise< int& > p2;
  98. boost::fibers::future< int& > f2 = p2.get_future();
  99. BOOST_CHECK( f2.valid() );
  100. }
  101. void test_future_create_void() {
  102. // default constructed future is not valid
  103. boost::fibers::future< void > f1;
  104. BOOST_CHECK( ! f1.valid() );
  105. // future retrieved from promise is valid (if it is the first)
  106. boost::fibers::promise< void > p2;
  107. boost::fibers::future< void > f2 = p2.get_future();
  108. BOOST_CHECK( f2.valid() );
  109. }
  110. void test_future_move() {
  111. // future retrieved from promise is valid (if it is the first)
  112. boost::fibers::promise< int > p1;
  113. boost::fibers::future< int > f1 = p1.get_future();
  114. BOOST_CHECK( f1.valid() );
  115. // move construction
  116. boost::fibers::future< int > f2( std::move( f1) );
  117. BOOST_CHECK( ! f1.valid() );
  118. BOOST_CHECK( f2.valid() );
  119. // move assignment
  120. f1 = std::move( f2);
  121. BOOST_CHECK( f1.valid() );
  122. BOOST_CHECK( ! f2.valid() );
  123. }
  124. void test_future_move_ref() {
  125. // future retrieved from promise is valid (if it is the first)
  126. boost::fibers::promise< int& > p1;
  127. boost::fibers::future< int& > f1 = p1.get_future();
  128. BOOST_CHECK( f1.valid() );
  129. // move construction
  130. boost::fibers::future< int& > f2( std::move( f1) );
  131. BOOST_CHECK( ! f1.valid() );
  132. BOOST_CHECK( f2.valid() );
  133. // move assignment
  134. f1 = std::move( f2);
  135. BOOST_CHECK( f1.valid() );
  136. BOOST_CHECK( ! f2.valid() );
  137. }
  138. void test_future_move_void() {
  139. // future retrieved from promise is valid (if it is the first)
  140. boost::fibers::promise< void > p1;
  141. boost::fibers::future< void > f1 = p1.get_future();
  142. BOOST_CHECK( f1.valid() );
  143. // move construction
  144. boost::fibers::future< void > f2( std::move( f1) );
  145. BOOST_CHECK( ! f1.valid() );
  146. BOOST_CHECK( f2.valid() );
  147. // move assignment
  148. f1 = std::move( f2);
  149. BOOST_CHECK( f1.valid() );
  150. BOOST_CHECK( ! f2.valid() );
  151. }
  152. void test_future_get() {
  153. // future retrieved from promise is valid (if it is the first)
  154. boost::fibers::promise< int > p1;
  155. p1.set_value( 7);
  156. boost::fibers::future< int > f1 = p1.get_future();
  157. BOOST_CHECK( f1.valid() );
  158. // get
  159. BOOST_CHECK( ! f1.get_exception_ptr() );
  160. BOOST_CHECK( 7 == f1.get() );
  161. BOOST_CHECK( ! f1.valid() );
  162. // throw broken_promise if promise is destroyed without set
  163. {
  164. boost::fibers::promise< int > p2;
  165. f1 = p2.get_future();
  166. }
  167. bool thrown = false;
  168. try {
  169. f1.get();
  170. } catch ( boost::fibers::broken_promise const&) {
  171. thrown = true;
  172. }
  173. BOOST_CHECK( ! f1.valid() );
  174. BOOST_CHECK( thrown);
  175. }
  176. void test_future_get_move() {
  177. // future retrieved from promise is valid (if it is the first)
  178. boost::fibers::promise< A > p1;
  179. A a; a.value = 7;
  180. p1.set_value( std::move( a) );
  181. boost::fibers::future< A > f1 = p1.get_future();
  182. BOOST_CHECK( f1.valid() );
  183. // get
  184. BOOST_CHECK( ! f1.get_exception_ptr() );
  185. BOOST_CHECK( 7 == f1.get().value);
  186. BOOST_CHECK( ! f1.valid() );
  187. // throw broken_promise if promise is destroyed without set
  188. {
  189. boost::fibers::promise< A > p2;
  190. f1 = p2.get_future();
  191. }
  192. bool thrown = false;
  193. try {
  194. f1.get();
  195. } catch ( boost::fibers::broken_promise const&) {
  196. thrown = true;
  197. }
  198. BOOST_CHECK( ! f1.valid() );
  199. BOOST_CHECK( thrown);
  200. }
  201. void test_future_get_ref() {
  202. // future retrieved from promise is valid (if it is the first)
  203. boost::fibers::promise< int& > p1;
  204. int i = 7;
  205. p1.set_value( i);
  206. boost::fibers::future< int& > f1 = p1.get_future();
  207. BOOST_CHECK( f1.valid() );
  208. // get
  209. BOOST_CHECK( ! f1.get_exception_ptr() );
  210. int & j = f1.get();
  211. BOOST_CHECK( &i == &j);
  212. BOOST_CHECK( ! f1.valid() );
  213. // throw broken_promise if promise is destroyed without set
  214. {
  215. boost::fibers::promise< int& > p2;
  216. f1 = p2.get_future();
  217. }
  218. bool thrown = false;
  219. try {
  220. f1.get();
  221. } catch ( boost::fibers::broken_promise const&) {
  222. thrown = true;
  223. }
  224. BOOST_CHECK( ! f1.valid() );
  225. BOOST_CHECK( thrown);
  226. }
  227. void test_future_get_void() {
  228. // future retrieved from promise is valid (if it is the first)
  229. boost::fibers::promise< void > p1;
  230. p1.set_value();
  231. boost::fibers::future< void > f1 = p1.get_future();
  232. BOOST_CHECK( f1.valid() );
  233. // get
  234. BOOST_CHECK( ! f1.get_exception_ptr() );
  235. f1.get();
  236. BOOST_CHECK( ! f1.valid() );
  237. // throw broken_promise if promise is destroyed without set
  238. {
  239. boost::fibers::promise< void > p2;
  240. f1 = p2.get_future();
  241. }
  242. bool thrown = false;
  243. try {
  244. f1.get();
  245. } catch ( boost::fibers::broken_promise const&) {
  246. thrown = true;
  247. }
  248. BOOST_CHECK( ! f1.valid() );
  249. BOOST_CHECK( thrown);
  250. }
  251. void test_future_share() {
  252. // future retrieved from promise is valid (if it is the first)
  253. boost::fibers::promise< int > p1;
  254. int i = 7;
  255. p1.set_value( i);
  256. boost::fibers::future< int > f1 = p1.get_future();
  257. BOOST_CHECK( f1.valid() );
  258. // share
  259. boost::fibers::shared_future< int > sf1 = f1.share();
  260. BOOST_CHECK( sf1.valid() );
  261. BOOST_CHECK( ! f1.valid() );
  262. // get
  263. BOOST_CHECK( ! sf1.get_exception_ptr() );
  264. int j = sf1.get();
  265. BOOST_CHECK_EQUAL( i, j);
  266. BOOST_CHECK( sf1.valid() );
  267. }
  268. void test_future_share_ref() {
  269. // future retrieved from promise is valid (if it is the first)
  270. boost::fibers::promise< int& > p1;
  271. int i = 7;
  272. p1.set_value( i);
  273. boost::fibers::future< int& > f1 = p1.get_future();
  274. BOOST_CHECK( f1.valid() );
  275. // share
  276. boost::fibers::shared_future< int& > sf1 = f1.share();
  277. BOOST_CHECK( sf1.valid() );
  278. BOOST_CHECK( ! f1.valid() );
  279. // get
  280. BOOST_CHECK( ! sf1.get_exception_ptr() );
  281. int & j = sf1.get();
  282. BOOST_CHECK( &i == &j);
  283. BOOST_CHECK( sf1.valid() );
  284. }
  285. void test_future_share_void() {
  286. // future retrieved from promise is valid (if it is the first)
  287. boost::fibers::promise< void > p1;
  288. p1.set_value();
  289. boost::fibers::future< void > f1 = p1.get_future();
  290. BOOST_CHECK( f1.valid() );
  291. // share
  292. boost::fibers::shared_future< void > sf1 = f1.share();
  293. BOOST_CHECK( sf1.valid() );
  294. BOOST_CHECK( ! f1.valid() );
  295. // get
  296. BOOST_CHECK( ! sf1.get_exception_ptr() );
  297. sf1.get();
  298. BOOST_CHECK( sf1.valid() );
  299. }
  300. void test_future_wait() {
  301. // future retrieved from promise is valid (if it is the first)
  302. boost::fibers::promise< int > p1;
  303. boost::fibers::future< int > f1 = p1.get_future();
  304. // wait on future
  305. p1.set_value( 7);
  306. f1.wait();
  307. BOOST_CHECK( 7 == f1.get() );
  308. }
  309. void test_future_wait_ref() {
  310. // future retrieved from promise is valid (if it is the first)
  311. boost::fibers::promise< int& > p1;
  312. boost::fibers::future< int& > f1 = p1.get_future();
  313. // wait on future
  314. int i = 7;
  315. p1.set_value( i);
  316. f1.wait();
  317. int & j = f1.get();
  318. BOOST_CHECK( &i == &j);
  319. }
  320. void test_future_wait_void() {
  321. // future retrieved from promise is valid (if it is the first)
  322. boost::fibers::promise< void > p1;
  323. boost::fibers::future< void > f1 = p1.get_future();
  324. // wait on future
  325. p1.set_value();
  326. f1.wait();
  327. f1.get();
  328. BOOST_CHECK( ! f1.valid() );
  329. }
  330. void test_future_wait_for() {
  331. // future retrieved from promise is valid (if it is the first)
  332. boost::fibers::promise< int > p1;
  333. boost::fibers::future< int > f1 = p1.get_future();
  334. boost::fibers::fiber( boost::fibers::launch::dispatch, fn11, std::move( p1) ).detach();
  335. // wait on future
  336. BOOST_CHECK( f1.valid() );
  337. boost::fibers::future_status status = f1.wait_for( ms(300) );
  338. BOOST_CHECK( boost::fibers::future_status::timeout == status);
  339. BOOST_CHECK( f1.valid() );
  340. status = f1.wait_for( ms(400) );
  341. BOOST_CHECK( boost::fibers::future_status::ready == status);
  342. BOOST_CHECK( f1.valid() );
  343. f1.wait();
  344. }
  345. void test_future_wait_for_ref() {
  346. // future retrieved from promise is valid (if it is the first)
  347. boost::fibers::promise< int& > p1;
  348. boost::fibers::future< int& > f1 = p1.get_future();
  349. boost::fibers::fiber( boost::fibers::launch::dispatch, fn12, std::move( p1) ).detach();
  350. // wait on future
  351. BOOST_CHECK( f1.valid() );
  352. boost::fibers::future_status status = f1.wait_for( ms(300) );
  353. BOOST_CHECK( boost::fibers::future_status::timeout == status);
  354. BOOST_CHECK( f1.valid() );
  355. status = f1.wait_for( ms(400) );
  356. BOOST_CHECK( boost::fibers::future_status::ready == status);
  357. BOOST_CHECK( f1.valid() );
  358. f1.wait();
  359. }
  360. void test_future_wait_for_void() {
  361. // future retrieved from promise is valid (if it is the first)
  362. boost::fibers::promise< void > p1;
  363. boost::fibers::future< void > f1 = p1.get_future();
  364. boost::fibers::fiber( boost::fibers::launch::dispatch, fn13, std::move( p1) ).detach();
  365. // wait on future
  366. BOOST_CHECK( f1.valid() );
  367. boost::fibers::future_status status = f1.wait_for( ms(300) );
  368. BOOST_CHECK( boost::fibers::future_status::timeout == status);
  369. BOOST_CHECK( f1.valid() );
  370. status = f1.wait_for( ms(400) );
  371. BOOST_CHECK( boost::fibers::future_status::ready == status);
  372. BOOST_CHECK( f1.valid() );
  373. f1.wait();
  374. }
  375. void test_future_wait_until() {
  376. // future retrieved from promise is valid (if it is the first)
  377. boost::fibers::promise< int > p1;
  378. boost::fibers::future< int > f1 = p1.get_future();
  379. boost::fibers::fiber( boost::fibers::launch::dispatch, fn11, std::move( p1) ).detach();
  380. // wait on future
  381. BOOST_CHECK( f1.valid() );
  382. boost::fibers::future_status status = f1.wait_until( Clock::now() + ms(300) );
  383. BOOST_CHECK( boost::fibers::future_status::timeout == status);
  384. BOOST_CHECK( f1.valid() );
  385. status = f1.wait_until( Clock::now() + ms(400) );
  386. BOOST_CHECK( boost::fibers::future_status::ready == status);
  387. BOOST_CHECK( f1.valid() );
  388. f1.wait();
  389. }
  390. void test_future_wait_until_ref() {
  391. // future retrieved from promise is valid (if it is the first)
  392. boost::fibers::promise< int& > p1;
  393. boost::fibers::future< int& > f1 = p1.get_future();
  394. boost::fibers::fiber( boost::fibers::launch::dispatch, fn12, std::move( p1) ).detach();
  395. // wait on future
  396. BOOST_CHECK( f1.valid() );
  397. boost::fibers::future_status status = f1.wait_until( Clock::now() + ms(300) );
  398. BOOST_CHECK( boost::fibers::future_status::timeout == status);
  399. BOOST_CHECK( f1.valid() );
  400. status = f1.wait_until( Clock::now() + ms(400) );
  401. BOOST_CHECK( boost::fibers::future_status::ready == status);
  402. BOOST_CHECK( f1.valid() );
  403. f1.wait();
  404. }
  405. void test_future_wait_until_void() {
  406. // future retrieved from promise is valid (if it is the first)
  407. boost::fibers::promise< void > p1;
  408. boost::fibers::future< void > f1 = p1.get_future();
  409. boost::fibers::fiber( boost::fibers::launch::dispatch, fn13, std::move( p1) ).detach();
  410. // wait on future
  411. BOOST_CHECK( f1.valid() );
  412. boost::fibers::future_status status = f1.wait_until( Clock::now() + ms(300) );
  413. BOOST_CHECK( boost::fibers::future_status::timeout == status);
  414. BOOST_CHECK( f1.valid() );
  415. status = f1.wait_until( Clock::now() + ms(400) );
  416. BOOST_CHECK( boost::fibers::future_status::ready == status);
  417. BOOST_CHECK( f1.valid() );
  418. f1.wait();
  419. }
  420. void test_future_wait_with_fiber_1() {
  421. boost::fibers::promise< int > p1;
  422. boost::fibers::fiber( boost::fibers::launch::dispatch, fn1, & p1, 7).detach();
  423. boost::fibers::future< int > f1 = p1.get_future();
  424. // wait on future
  425. BOOST_CHECK( 7 == f1.get() );
  426. }
  427. void test_future_wait_with_fiber_2() {
  428. boost::fibers::fiber( boost::fibers::launch::dispatch, fn2).join();
  429. }
  430. boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) {
  431. boost::unit_test_framework::test_suite* test =
  432. BOOST_TEST_SUITE("Boost.Fiber: future test suite");
  433. test->add(BOOST_TEST_CASE(test_future_create));
  434. test->add(BOOST_TEST_CASE(test_future_create_ref));
  435. test->add(BOOST_TEST_CASE(test_future_create_void));
  436. test->add(BOOST_TEST_CASE(test_future_move));
  437. test->add(BOOST_TEST_CASE(test_future_move_ref));
  438. test->add(BOOST_TEST_CASE(test_future_move_void));
  439. test->add(BOOST_TEST_CASE(test_future_get));
  440. test->add(BOOST_TEST_CASE(test_future_get_move));
  441. test->add(BOOST_TEST_CASE(test_future_get_ref));
  442. test->add(BOOST_TEST_CASE(test_future_get_void));
  443. test->add(BOOST_TEST_CASE(test_future_share));
  444. test->add(BOOST_TEST_CASE(test_future_share_ref));
  445. test->add(BOOST_TEST_CASE(test_future_share_void));
  446. test->add(BOOST_TEST_CASE(test_future_wait));
  447. test->add(BOOST_TEST_CASE(test_future_wait_ref));
  448. test->add(BOOST_TEST_CASE(test_future_wait_void));
  449. test->add(BOOST_TEST_CASE(test_future_wait_for));
  450. test->add(BOOST_TEST_CASE(test_future_wait_for_ref));
  451. test->add(BOOST_TEST_CASE(test_future_wait_for_void));
  452. test->add(BOOST_TEST_CASE(test_future_wait_until));
  453. test->add(BOOST_TEST_CASE(test_future_wait_until_ref));
  454. test->add(BOOST_TEST_CASE(test_future_wait_until_void));
  455. test->add(BOOST_TEST_CASE(test_future_wait_with_fiber_1));
  456. test->add(BOOST_TEST_CASE(test_future_wait_with_fiber_2));
  457. return test;
  458. }