]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // |
2 | // experimental/coro/co_spawn.cpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
5 | // Copyright (c) 2021-2022 Klemens D. Morgenstern | |
6 | // (klemens dot morgenstern at gmx dot net) | |
7 | // | |
8 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
9 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
10 | // | |
11 | ||
12 | // Disable autolinking for unit tests. | |
13 | #if !defined(BOOST_ALL_NO_LIB) | |
14 | #define BOOST_ALL_NO_LIB 1 | |
15 | #endif // !defined(BOOST_ALL_NO_LIB) | |
16 | ||
17 | // Test that header file is self-contained. | |
18 | #include <boost/asio/experimental/co_spawn.hpp> | |
19 | ||
20 | #include <iostream> | |
21 | #include <boost/asio/io_context.hpp> | |
22 | #include <boost/asio/steady_timer.hpp> | |
23 | #include <boost/asio/this_coro.hpp> | |
24 | #include "../../unit_test.hpp" | |
25 | ||
26 | using namespace boost::asio::experimental; | |
27 | namespace this_coro = boost::asio::this_coro; | |
28 | ||
29 | namespace coro { | |
30 | ||
31 | auto coro_simple_co_spawn_impl(boost::asio::io_context& , bool &done) noexcept | |
32 | -> boost::asio::experimental::coro<void() noexcept, int> | |
33 | { | |
34 | boost::asio::steady_timer timer( | |
35 | co_await this_coro::executor, | |
36 | std::chrono::milliseconds(10)); | |
37 | ||
38 | done = true; | |
39 | ||
40 | co_return 42; | |
41 | } | |
42 | ||
43 | void coro_co_spawn() | |
44 | { | |
45 | boost::asio::io_context ctx; | |
46 | ||
47 | bool done1 = false; | |
48 | bool done2 = false; | |
49 | int res = 0; | |
50 | ||
51 | co_spawn(coro_simple_co_spawn_impl(ctx, done1), | |
52 | [&](int r){done2= true; res = r;}); | |
53 | ||
54 | ctx.run(); | |
55 | ||
56 | BOOST_ASIO_CHECK(done1); | |
57 | BOOST_ASIO_CHECK(done2); | |
58 | BOOST_ASIO_CHECK(res == 42); | |
59 | } | |
60 | ||
61 | } // namespace coro | |
62 | ||
63 | BOOST_ASIO_TEST_SUITE | |
64 | ( | |
65 | "coro/co_spawn", | |
66 | BOOST_ASIO_TEST_CASE(::coro::coro_co_spawn) | |
67 | ) |