| 1 | // Copyright (c) 2022 Klemens D. Morgenstern |
|---|---|
| 2 | // |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #include <boost/cobalt/detail/wrapper.hpp> |
| 7 | |
| 8 | #include <boost/asio/detached.hpp> |
| 9 | #include <boost/asio/io_context.hpp> |
| 10 | #include <boost/asio/bind_allocator.hpp> |
| 11 | |
| 12 | |
| 13 | #include <boost/test/unit_test.hpp> |
| 14 | |
| 15 | BOOST_AUTO_TEST_SUITE(wrappers); |
| 16 | |
| 17 | BOOST_AUTO_TEST_CASE(regular) |
| 18 | { |
| 19 | boost::asio::io_context ctx; |
| 20 | boost::cobalt::this_thread::set_executor(ctx.get_executor()); |
| 21 | bool ran = false; |
| 22 | |
| 23 | #if !defined(BOOST_COBALT_NO_PMR) |
| 24 | char buf[512]; |
| 25 | boost::cobalt::pmr::monotonic_buffer_resource res{buf, 512}; |
| 26 | struct completion |
| 27 | { |
| 28 | bool & ran; |
| 29 | using allocator_type = boost::cobalt::pmr::polymorphic_allocator<void>; |
| 30 | allocator_type get_allocator() const { return alloc; } |
| 31 | boost::cobalt::pmr::polymorphic_allocator<void> alloc; |
| 32 | void operator()() |
| 33 | { |
| 34 | ran = true; |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | auto p = boost::cobalt::detail::post_coroutine(exec: ctx.get_executor(), |
| 39 | token: completion{.ran: ran, .alloc: boost::cobalt::pmr::polymorphic_allocator<void>(&res)} |
| 40 | ); |
| 41 | #else |
| 42 | auto p = boost::cobalt::detail::post_coroutine(ctx.get_executor(), [&]{ran = true;}); |
| 43 | #endif |
| 44 | BOOST_CHECK(p); |
| 45 | BOOST_CHECK(!ran); |
| 46 | p.resume(); |
| 47 | BOOST_CHECK(!ran); |
| 48 | ctx.run(); |
| 49 | BOOST_CHECK(ran); |
| 50 | } |
| 51 | |
| 52 | BOOST_AUTO_TEST_CASE(expire) |
| 53 | { |
| 54 | |
| 55 | boost::asio::io_context ct2; |
| 56 | boost::cobalt::this_thread::set_executor(ct2.get_executor()); |
| 57 | auto h = boost::cobalt::detail::post_coroutine(exec: ct2.get_executor(), token: boost::asio::detached); |
| 58 | boost::cobalt::detail::self_destroy(h); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | BOOST_AUTO_TEST_SUITE_END(); |
