| 1 | // |
|---|---|
| 2 | // Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net) |
| 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/cobalt/wait_group.hpp> |
| 9 | #include <boost/cobalt/promise.hpp> |
| 10 | |
| 11 | #include <boost/asio/any_io_executor.hpp> |
| 12 | #include <boost/asio/steady_timer.hpp> |
| 13 | |
| 14 | #include <boost/test/unit_test.hpp> |
| 15 | #include "test.hpp" |
| 16 | |
| 17 | using namespace boost; |
| 18 | |
| 19 | cobalt::promise<void> gdelay(asio::any_io_executor exec, |
| 20 | std::chrono::milliseconds ms = std::chrono::milliseconds(25)) |
| 21 | { |
| 22 | if (ms.count() == 0u) |
| 23 | co_return; |
| 24 | if (ms == std::chrono::milliseconds ::max()) |
| 25 | throw std::runtime_error("wdummy_throw"); |
| 26 | |
| 27 | asio::steady_timer tim{exec, ms}; |
| 28 | co_await tim.async_wait(token: cobalt::use_op); |
| 29 | } |
| 30 | |
| 31 | |
| 32 | BOOST_AUTO_TEST_SUITE(wait_group); |
| 33 | |
| 34 | CO_TEST_CASE(grp) |
| 35 | { |
| 36 | auto e = co_await cobalt::this_coro::executor; |
| 37 | |
| 38 | using namespace std; |
| 39 | |
| 40 | cobalt::wait_group wg; |
| 41 | wg.push_back(p: gdelay(exec: e)); |
| 42 | wg.push_back(p: gdelay(exec: e, ms: 0ms)); |
| 43 | wg.push_back(p: gdelay(exec: e, ms: 10ms)); |
| 44 | wg.push_back(p: gdelay(exec: e, ms: 20ms)); |
| 45 | |
| 46 | co_await asio::post(ex: e, token: cobalt::use_op); |
| 47 | BOOST_CHECK(wg.size() == 4u); |
| 48 | BOOST_CHECK(wg.reap() == 1u); |
| 49 | BOOST_CHECK(wg.size() == 3u); |
| 50 | |
| 51 | co_await wg.wait_one(); |
| 52 | BOOST_CHECK(wg.size() == 2u); |
| 53 | |
| 54 | wg.cancel(); |
| 55 | co_await wg; |
| 56 | |
| 57 | BOOST_CHECK(wg.size() == 0u); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | BOOST_AUTO_TEST_SUITE_END(); |
| 62 | |
| 63 |
