-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathtask_tests.cpp
349 lines (279 loc) · 7.21 KB
/
task_tests.cpp
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/task.hpp>
#include <cppcoro/single_consumer_event.hpp>
#include <cppcoro/sync_wait.hpp>
#include <cppcoro/when_all_ready.hpp>
#include <cppcoro/fmap.hpp>
#include "counted.hpp"
#include <ostream>
#include <string>
#include <type_traits>
#include "doctest/doctest.h"
TEST_SUITE_BEGIN("task");
TEST_CASE("task doesn't start until awaited")
{
bool started = false;
auto func = [&]() -> cppcoro::task<>
{
started = true;
co_return;
};
cppcoro::sync_wait([&]() -> cppcoro::task<>
{
auto t = func();
CHECK(!started);
co_await t;
CHECK(started);
}());
}
TEST_CASE("awaiting default-constructed task throws broken_promise")
{
cppcoro::sync_wait([&]() -> cppcoro::task<>
{
cppcoro::task<> t;
CHECK_THROWS_AS(co_await t, const cppcoro::broken_promise&);
}());
}
TEST_CASE("awaiting task that completes asynchronously")
{
bool reachedBeforeEvent = false;
bool reachedAfterEvent = false;
cppcoro::single_consumer_event event;
auto f = [&]() -> cppcoro::task<>
{
reachedBeforeEvent = true;
co_await event;
reachedAfterEvent = true;
};
cppcoro::sync_wait([&]() -> cppcoro::task<>
{
auto t = f();
CHECK(!reachedBeforeEvent);
(void)co_await cppcoro::when_all_ready(
[&]() -> cppcoro::task<>
{
co_await t;
CHECK(reachedBeforeEvent);
CHECK(reachedAfterEvent);
}(),
[&]() -> cppcoro::task<>
{
CHECK(reachedBeforeEvent);
CHECK(!reachedAfterEvent);
event.set();
CHECK(reachedAfterEvent);
co_return;
}());
}());
}
TEST_CASE("destroying task that was never awaited destroys captured args")
{
counted::reset_counts();
auto f = [](counted c) -> cppcoro::task<counted>
{
co_return c;
};
CHECK(counted::active_count() == 0);
{
auto t = f(counted{});
CHECK(counted::active_count() == 1);
}
CHECK(counted::active_count() == 0);
}
TEST_CASE("task destructor destroys result")
{
counted::reset_counts();
auto f = []() -> cppcoro::task<counted>
{
co_return counted{};
};
{
auto t = f();
CHECK(counted::active_count() == 0);
auto& result = cppcoro::sync_wait(t);
CHECK(counted::active_count() == 1);
CHECK(result.id == 0);
}
CHECK(counted::active_count() == 0);
}
TEST_CASE("task of reference type")
{
int value = 3;
auto f = [&]() -> cppcoro::task<int&>
{
co_return value;
};
cppcoro::sync_wait([&]() -> cppcoro::task<>
{
SUBCASE("awaiting rvalue task")
{
decltype(auto) result = co_await f();
static_assert(
std::is_same<decltype(result), int&>::value,
"co_await r-value reference of task<int&> should result in an int&");
CHECK(&result == &value);
}
SUBCASE("awaiting lvalue task")
{
auto t = f();
decltype(auto) result = co_await t;
static_assert(
std::is_same<decltype(result), int&>::value,
"co_await l-value reference of task<int&> should result in an int&");
CHECK(&result == &value);
}
}());
}
TEST_CASE("passing parameter by value to task coroutine calls move-constructor exactly once")
{
counted::reset_counts();
auto f = [](counted arg) -> cppcoro::task<>
{
co_return;
};
counted c;
CHECK(counted::active_count() == 1);
CHECK(counted::default_construction_count == 1);
CHECK(counted::copy_construction_count == 0);
CHECK(counted::move_construction_count == 0);
CHECK(counted::destruction_count == 0);
{
auto t = f(c);
// Should have called copy-constructor to pass a copy of 'c' into f by value.
CHECK(counted::copy_construction_count == 1);
// Inside f it should have move-constructed parameter into coroutine frame variable
//WARN_MESSAGE(counted::move_construction_count == 1,
// "Known bug in MSVC 2017.1, not critical if it performs multiple moves");
// Active counts should be the instance 'c' and the instance captured in coroutine frame of 't'.
CHECK(counted::active_count() == 2);
}
CHECK(counted::active_count() == 1);
}
TEST_CASE("task<void> fmap pipe operator")
{
using cppcoro::fmap;
cppcoro::single_consumer_event event;
auto f = [&]() -> cppcoro::task<>
{
co_await event;
co_return;
};
auto t = f() | fmap([] { return 123; });
cppcoro::sync_wait(cppcoro::when_all_ready(
[&]() -> cppcoro::task<>
{
CHECK(co_await t == 123);
}(),
[&]() -> cppcoro::task<>
{
event.set();
co_return;
}()));
}
TEST_CASE("task<int> fmap pipe operator")
{
using cppcoro::task;
using cppcoro::fmap;
using cppcoro::sync_wait;
using cppcoro::make_task;
auto one = [&]() -> task<int>
{
co_return 1;
};
SUBCASE("r-value fmap / r-value lambda")
{
auto t = one()
| fmap([delta = 1](auto i) { return i + delta; });
CHECK(sync_wait(t) == 2);
}
SUBCASE("r-value fmap / l-value lambda")
{
using namespace std::string_literals;
auto t = [&]
{
auto f = [prefix = "pfx"s](int x)
{
return prefix + std::to_string(x);
};
// Want to make sure that the resulting awaitable has taken
// a copy of the lambda passed to fmap().
return one() | fmap(f);
}();
CHECK(sync_wait(t) == "pfx1");
}
SUBCASE("l-value fmap / r-value lambda")
{
using namespace std::string_literals;
auto t = [&]
{
auto addprefix = fmap([prefix = "a really really long prefix that prevents small string optimisation"s](int x)
{
return prefix + std::to_string(x);
});
// Want to make sure that the resulting awaitable has taken
// a copy of the lambda passed to fmap().
return one() | addprefix;
}();
CHECK(sync_wait(t) == "a really really long prefix that prevents small string optimisation1");
}
SUBCASE("l-value fmap / l-value lambda")
{
using namespace std::string_literals;
task<std::string> t;
{
auto lambda = [prefix = "a really really long prefix that prevents small string optimisation"s](int x)
{
return prefix + std::to_string(x);
};
auto addprefix = fmap(lambda);
// Want to make sure that the resulting task has taken
// a copy of the lambda passed to fmap().
t = make_task(one() | addprefix);
}
CHECK(!t.is_ready());
CHECK(sync_wait(t) == "a really really long prefix that prevents small string optimisation1");
}
}
TEST_CASE("chained fmap pipe operations")
{
using namespace std::string_literals;
using cppcoro::task;
using cppcoro::sync_wait;
auto prepend = [](std::string s)
{
using cppcoro::fmap;
return fmap([s = std::move(s)](const std::string& value) { return s + value; });
};
auto append = [](std::string s)
{
using cppcoro::fmap;
return fmap([s = std::move(s)](const std::string& value){ return value + s; });
};
auto asyncString = [](std::string s) -> task<std::string>
{
co_return std::move(s);
};
auto t = asyncString("base"s) | prepend("pre_"s) | append("_post"s);
CHECK(sync_wait(t) == "pre_base_post");
}
TEST_CASE("lots of synchronous completions doesn't result in stack-overflow")
{
auto completesSynchronously = []() -> cppcoro::task<int>
{
co_return 1;
};
auto run = [&]() -> cppcoro::task<>
{
int sum = 0;
for (int i = 0; i < 1'000'000; ++i)
{
sum += co_await completesSynchronously();
}
CHECK(sum == 1'000'000);
};
cppcoro::sync_wait(run());
}
TEST_SUITE_END();