summaryrefslogtreecommitdiffstats
path: root/src/test/common/test_async_completion.cc
blob: 4cf4394e1cc08ce0d62bc718c78e23c7e2debd54 (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2018 Red Hat
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation. See file COPYING.
 *
 */

#include "common/async/completion.h"
#include <optional>
#include <boost/intrusive/list.hpp>
#include <gtest/gtest.h>

namespace ceph::async {

using boost::system::error_code;

struct move_only {
  move_only() = default;
  move_only(move_only&&) = default;
  move_only& operator=(move_only&&) = default;
  move_only(const move_only&) = delete;
  move_only& operator=(const move_only&) = delete;
};

TEST(AsyncCompletion, BindHandler)
{
  auto h1 = [] (int i, char c) {};
  auto b1 = bind_handler(std::move(h1), 5, 'a');
  b1();
  const auto& c1 = b1;
  c1();
  std::move(b1)();

  // move-only types can be forwarded with 'operator() &&'
  auto h2 = [] (move_only&& m) {};
  auto b2 = bind_handler(std::move(h2), move_only{});
  std::move(b2)();

  // references bound with std::ref() can be passed to all operator() overloads
  auto h3 = [] (int& c) { c++; };
  int count = 0;
  auto b3 = bind_handler(std::move(h3), std::ref(count));
  EXPECT_EQ(0, count);
  b3();
  EXPECT_EQ(1, count);
  const auto& c3 = b3;
  c3();
  EXPECT_EQ(2, count);
  std::move(b3)();
  EXPECT_EQ(3, count);
}

TEST(AsyncCompletion, ForwardHandler)
{
  // move-only types can be forwarded with 'operator() &'
  auto h = [] (move_only&& m) {};
  auto b = bind_handler(std::move(h), move_only{});
  auto f = forward_handler(std::move(b));
  f();
}

TEST(AsyncCompletion, MoveOnly)
{
  boost::asio::io_context context;
  auto ex1 = context.get_executor();

  std::optional<error_code> ec1, ec2, ec3;
  std::optional<move_only> arg3;
  {
    // move-only user data
    using Completion = Completion<void(error_code), move_only>;
    auto c = Completion::create(ex1, [&ec1] (error_code ec) { ec1 = ec; });
    Completion::post(std::move(c), boost::asio::error::operation_aborted);
    EXPECT_FALSE(ec1);
  }
  {
    // move-only handler
    using Completion = Completion<void(error_code)>;
    auto c = Completion::create(ex1, [&ec2, m=move_only{}] (error_code ec) {
				       static_cast<void>(m);
				       ec2 = ec; });
    Completion::post(std::move(c), boost::asio::error::operation_aborted);
    EXPECT_FALSE(ec2);
  }
  {
    // move-only arg in signature
    using Completion = Completion<void(error_code, move_only)>;
    auto c = Completion::create(ex1, [&] (error_code ec, move_only m) {
        ec3 = ec;
        arg3 = std::move(m);
      });
    Completion::post(std::move(c), boost::asio::error::operation_aborted, move_only{});
    EXPECT_FALSE(ec3);
  }

  context.poll();
  EXPECT_TRUE(context.stopped());

  ASSERT_TRUE(ec1);
  EXPECT_EQ(boost::asio::error::operation_aborted, *ec1);
  ASSERT_TRUE(ec2);
  EXPECT_EQ(boost::asio::error::operation_aborted, *ec2);
  ASSERT_TRUE(ec3);
  EXPECT_EQ(boost::asio::error::operation_aborted, *ec3);
  ASSERT_TRUE(arg3);
}

TEST(AsyncCompletion, VoidCompletion)
{
  boost::asio::io_context context;
  auto ex1 = context.get_executor();

  using Completion = Completion<void(error_code)>;
  std::optional<error_code> ec1;

  auto c = Completion::create(ex1, [&ec1] (error_code ec) { ec1 = ec; });
  Completion::post(std::move(c), boost::asio::error::operation_aborted);

  EXPECT_FALSE(ec1);

  context.poll();
  EXPECT_TRUE(context.stopped());

  ASSERT_TRUE(ec1);
  EXPECT_EQ(boost::asio::error::operation_aborted, *ec1);
}

TEST(AsyncCompletion, CompletionList)
{
  boost::asio::io_context context;
  auto ex1 = context.get_executor();

  using T = AsBase<boost::intrusive::list_base_hook<>>;
  using Completion = Completion<void(), T>;
  boost::intrusive::list<Completion> completions;
  int completed = 0;
  for (int i = 0; i < 3; i++) {
    auto c = Completion::create(ex1, [&] { completed++; });
    completions.push_back(*c.release());
  }
  completions.clear_and_dispose([] (Completion *c) {
      Completion::post(std::unique_ptr<Completion>{c});
    });

  EXPECT_EQ(0, completed);

  context.poll();
  EXPECT_TRUE(context.stopped());

  EXPECT_EQ(3, completed);
}

TEST(AsyncCompletion, CompletionPair)
{
  boost::asio::io_context context;
  auto ex1 = context.get_executor();

  using T = std::pair<int, std::string>;
  using Completion = Completion<void(int, std::string), T>;

  std::optional<T> t;
  auto c = Completion::create(ex1, [&] (int first, std::string second) {
      t = T{first, std::move(second)};
    }, 2, "hello");

  auto data = std::move(c->user_data);
  Completion::post(std::move(c), data.first, std::move(data.second));

  EXPECT_FALSE(t);

  context.poll();
  EXPECT_TRUE(context.stopped());

  ASSERT_TRUE(t);
  EXPECT_EQ(2, t->first);
  EXPECT_EQ("hello", t->second);
}

TEST(AsyncCompletion, CompletionReference)
{
  boost::asio::io_context context;
  auto ex1 = context.get_executor();

  using Completion = Completion<void(int&)>;

  auto c = Completion::create(ex1, [] (int& i) { ++i; });

  int i = 42;
  Completion::post(std::move(c), std::ref(i));

  EXPECT_EQ(42, i);

  context.poll();
  EXPECT_TRUE(context.stopped());

  EXPECT_EQ(43, i);
}

struct throws_on_move {
  throws_on_move() = default;
  throws_on_move(throws_on_move&&) {
    throw std::runtime_error("oops");
  }
  throws_on_move& operator=(throws_on_move&&) {
    throw std::runtime_error("oops");
  }
  throws_on_move(const throws_on_move&) = default;
  throws_on_move& operator=(const throws_on_move&) = default;
};

TEST(AsyncCompletion, ThrowOnCtor)
{
  boost::asio::io_context context;
  auto ex1 = context.get_executor();
  {
    using Completion = Completion<void(int&)>;

    // throw on Handler move construction
    EXPECT_THROW(Completion::create(ex1, [t=throws_on_move{}] (int& i) {
					   static_cast<void>(t);
					   ++i; }),
                 std::runtime_error);
  }
  {
    using T = throws_on_move;
    using Completion = Completion<void(int&), T>;

    // throw on UserData construction
    EXPECT_THROW(Completion::create(ex1, [] (int& i) { ++i; }, throws_on_move{}),
                 std::runtime_error);
  }
}

TEST(AsyncCompletion, FreeFunctions)
{
  boost::asio::io_context context;
  auto ex1 = context.get_executor();

  auto c1 = create_completion<void(), void>(ex1, [] {});
  post(std::move(c1));

  auto c2 = create_completion<void(int), int>(ex1, [] (int) {}, 5);
  defer(std::move(c2), c2->user_data);

  context.poll();
  EXPECT_TRUE(context.stopped());
}

} // namespace ceph::async