summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/asio/src/tests/unit/experimental/coro/cancel.cpp
blob: 38cf106790826fa9aa28650e9cab30aa61c95e96 (plain) (blame)
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
//
// experimental/coro/cancel.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2021-2023 Klemens D. Morgenstern
//                         (klemens dot morgenstern at gmx dot net)
//
// 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)
//

// Disable autolinking for unit tests.
#if !defined(BOOST_ALL_NO_LIB)
#define BOOST_ALL_NO_LIB 1
#endif // !defined(BOOST_ALL_NO_LIB)

// Test that header file is self-contained.
#include "asio/experimental/coro.hpp"
#include <iostream>
#include "asio/bind_cancellation_slot.hpp"
#include "asio/io_context.hpp"
#include "asio/steady_timer.hpp"
#include "asio/this_coro.hpp"
#include "../../unit_test.hpp"

using namespace asio::experimental;
namespace this_coro = asio::this_coro;

namespace coro {


auto coro_simple_cancel_impl(asio::io_context& ) noexcept
  -> asio::experimental::coro<void() noexcept, asio::error_code>
{
    ASIO_CHECK(
        !(co_await this_coro::cancellation_state).cancelled());

    asio::steady_timer timer{
        co_await this_coro::executor,
        std::chrono::seconds(1)};

    ASIO_CHECK(
        !(co_await this_coro::cancellation_state).cancelled());

    auto ec = co_await timer;

    ASIO_CHECK(
        (co_await this_coro::cancellation_state).cancelled());

    co_return ec;
}

void coro_simple_cancel()
{
  asio::io_context ctx;
  asio::cancellation_signal sig;

  auto k = coro_simple_cancel_impl(ctx);

  asio::error_code res_ec;
  k.async_resume(
      asio::bind_cancellation_slot(sig.slot(),
        [&](asio::error_code ec) {res_ec = ec;}));
  asio::post(ctx, [&]{sig.emit(asio::cancellation_type::all);});

  ASIO_CHECK(!res_ec);

  ctx.run();

  ASIO_CHECK(res_ec == asio::error::operation_aborted);
}

auto coro_throw_cancel_impl(asio::io_context& )
  -> asio::experimental::coro<void() , void>
{
    asio::steady_timer timer{
        co_await this_coro::executor,
        std::chrono::seconds(1)};
    co_await timer;
}

void coro_throw_cancel()
{
  asio::io_context ctx;
  asio::cancellation_signal sig;

  auto k = coro_throw_cancel_impl(ctx);

  std::exception_ptr res_ex;
  k.async_resume(
      asio::bind_cancellation_slot(sig.slot(),
        [&](std::exception_ptr ex) {res_ex = ex;}));
  asio::post(ctx, [&]{sig.emit(asio::cancellation_type::all);});

  ASIO_CHECK(!res_ex);

  ctx.run();

  ASIO_CHECK(res_ex);
  try
  {
    if (res_ex)
      std::rethrow_exception(res_ex);
  }
  catch (asio::system_error& se)
  {
    ASIO_CHECK(se.code() == asio::error::operation_aborted);
  }
}

auto coro_simple_cancel_nested_k(asio::io_context&, int& cnt) noexcept
  -> asio::experimental::coro<
      void() noexcept,
      asio::error_code>
{
  asio::steady_timer timer{
          co_await this_coro::executor,
          std::chrono::milliseconds(100)};

  ASIO_CHECK(!(co_await this_coro::cancellation_state).cancelled());
  auto ec = co_await timer;
  cnt++;
  ASIO_CHECK((co_await this_coro::cancellation_state).cancelled());

  co_return ec;
}

auto coro_simple_cancel_nested_kouter(
    asio::io_context& ctx, int& cnt) noexcept
  -> asio::experimental::coro<
      asio::error_code() noexcept,
      asio::error_code>
{
    ASIO_CHECK(cnt == 0);
    co_yield co_await coro_simple_cancel_nested_k(ctx, cnt);
    ASIO_CHECK(cnt == 1);
    auto ec = co_await coro_simple_cancel_nested_k(ctx, cnt);
    ASIO_CHECK(cnt == 2);
    co_return ec;
}

void coro_simple_cancel_nested()
{
  asio::io_context ctx;
  asio::cancellation_signal sig;

  int cnt = 0;
  auto kouter = coro_simple_cancel_nested_kouter(ctx, cnt);

  asio::error_code res_ec;
  kouter.async_resume(
      asio::bind_cancellation_slot(sig.slot(),
        [&](asio::error_code ec) {res_ec = ec;}));
  asio::post(ctx, [&]{sig.emit(asio::cancellation_type::all);});
  ASIO_CHECK(!res_ec);
  ctx.run();
  ASIO_CHECK(res_ec == asio::error::operation_aborted);

  ctx.restart();
  res_ec = {};
  kouter.async_resume(
      asio::bind_cancellation_slot(sig.slot(),
        [&](asio::error_code ec) {res_ec = ec;}));
  asio::post(ctx, [&]{sig.emit(asio::cancellation_type::all);});
  ASIO_CHECK(!res_ec);
  ctx.run();
  ASIO_CHECK(res_ec == asio::error::operation_aborted);
  ASIO_CHECK(cnt == 2);
}

} // namespace coro

ASIO_TEST_SUITE
(
  "coro/cancel",
  ASIO_TEST_CASE(::coro::coro_simple_cancel)
  ASIO_TEST_CASE(::coro::coro_throw_cancel)
  ASIO_TEST_CASE(::coro::coro_simple_cancel_nested)
)