-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathwhen_all_ready_tests.cpp
More file actions
265 lines (215 loc) · 5.39 KB
/
when_all_ready_tests.cpp
File metadata and controls
265 lines (215 loc) · 5.39 KB
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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/when_all.hpp>
#include <cppcoro/task.hpp>
#include <cppcoro/shared_task.hpp>
#include <cppcoro/sync_wait.hpp>
#include <cppcoro/async_manual_reset_event.hpp>
#include "counted.hpp"
#include <functional>
#include <string>
#include <vector>
#include <ostream>
#include "doctest/cppcoro_doctest.h"
TEST_SUITE_BEGIN("when_all_ready");
template<template<typename T> class TASK, typename T>
TASK<T> when_event_set_return(cppcoro::async_manual_reset_event& event, T value)
{
co_await event;
co_return std::move(value);
}
TEST_CASE("when_all_ready() with no args")
{
[[maybe_unused]] std::tuple<> result = cppcoro::sync_wait(cppcoro::when_all_ready());
}
TEST_CASE("when_all_ready() with one task")
{
bool started = false;
auto f = [&](cppcoro::async_manual_reset_event& event) -> cppcoro::task<>
{
started = true;
co_await event;
};
cppcoro::async_manual_reset_event event;
auto whenAllAwaitable = cppcoro::when_all_ready(f(event));
CHECK(!started);
bool finished = false;
cppcoro::sync_wait(cppcoro::when_all_ready(
[&]() -> cppcoro::task<>
{
auto&[t] = co_await whenAllAwaitable;
finished = true;
t.result();
}(),
[&]() -> cppcoro::task<>
{
CHECK(started);
CHECK(!finished);
event.set();
CHECK(finished);
co_return;
}()));
}
TEST_CASE("when_all_ready() with multiple task")
{
auto makeTask = [&](bool& started, cppcoro::async_manual_reset_event& event, int result) -> cppcoro::task<int>
{
started = true;
co_await event;
co_return result;
};
cppcoro::async_manual_reset_event event1;
cppcoro::async_manual_reset_event event2;
bool started1 = false;
bool started2 = false;
auto whenAllAwaitable = cppcoro::when_all_ready(
makeTask(started1, event1, 1),
makeTask(started2, event2, 2));
CHECK(!started1);
CHECK(!started2);
bool whenAllAwaitableFinished = false;
cppcoro::sync_wait(cppcoro::when_all_ready(
[&]() -> cppcoro::task<>
{
auto[t1, t2] = co_await std::move(whenAllAwaitable);
whenAllAwaitableFinished = true;
CHECK(t1.result() == 1);
CHECK(t2.result() == 2);
}(),
[&]() -> cppcoro::task<>
{
CHECK(started1);
CHECK(started2);
event2.set();
CHECK(!whenAllAwaitableFinished);
event1.set();
CHECK(whenAllAwaitableFinished);
co_return;
}()));
}
TEST_CASE("when_all_ready() with all task types")
{
cppcoro::async_manual_reset_event event;
auto t0 = when_event_set_return<cppcoro::task>(event, 1);
auto t1 = when_event_set_return<cppcoro::shared_task>(event, 2);
auto allTask = cppcoro::when_all_ready(std::move(t0), t1);
cppcoro::sync_wait(cppcoro::when_all_ready(
[&]() -> cppcoro::task<>
{
auto [r0, r1] = co_await std::move(allTask);
CHECK(r0.result() == 1);
CHECK(r1.result() == 2);
}(),
[&]() -> cppcoro::task<>
{
event.set();
co_return;
}()));
}
TEST_CASE("when_all_ready() with std::vector<task<T>>")
{
cppcoro::async_manual_reset_event event;
std::uint32_t startedCount = 0;
std::uint32_t finishedCount = 0;
auto makeTask = [&]() -> cppcoro::task<>
{
++startedCount;
co_await event;
++finishedCount;
};
std::vector<cppcoro::task<>> tasks;
for (std::uint32_t i = 0; i < 10; ++i)
{
tasks.emplace_back(makeTask());
}
auto allTask = cppcoro::when_all_ready(std::move(tasks));
// Shouldn't have started any tasks yet.
CHECK(startedCount == 0u);
cppcoro::sync_wait(cppcoro::when_all_ready(
[&]() -> cppcoro::task<>
{
auto resultTasks = co_await std::move(allTask);
CHECK(resultTasks.size() == 10u);
for (auto& t : resultTasks)
{
CHECK_NOTHROW(t.result());
}
}(),
[&]() -> cppcoro::task<>
{
CHECK(startedCount == 10u);
CHECK(finishedCount == 0u);
event.set();
CHECK(finishedCount == 10u);
co_return;
}()));
}
TEST_CASE("when_all_ready() with std::vector<shared_task<T>>")
{
cppcoro::async_manual_reset_event event;
std::uint32_t startedCount = 0;
std::uint32_t finishedCount = 0;
auto makeTask = [&]() -> cppcoro::shared_task<>
{
++startedCount;
co_await event;
++finishedCount;
};
std::vector<cppcoro::shared_task<>> tasks;
for (std::uint32_t i = 0; i < 10; ++i)
{
tasks.emplace_back(makeTask());
}
auto allTask = cppcoro::when_all_ready(std::move(tasks));
// Shouldn't have started any tasks yet.
CHECK(startedCount == 0u);
cppcoro::sync_wait(cppcoro::when_all_ready(
[&]() -> cppcoro::task<>
{
auto resultTasks = co_await std::move(allTask);
CHECK(resultTasks.size() == 10u);
for (auto& t : resultTasks)
{
CHECK_NOTHROW(t.result());
}
}(),
[&]() -> cppcoro::task<>
{
CHECK(startedCount == 10u);
CHECK(finishedCount == 0u);
event.set();
CHECK(finishedCount == 10u);
co_return;
}()));
}
TEST_CASE("when_all_ready() doesn't rethrow exceptions")
{
auto makeTask = [](bool throwException) -> cppcoro::task<int>
{
if (throwException)
{
throw std::exception{};
}
else
{
co_return 123;
}
};
cppcoro::sync_wait([&]() -> cppcoro::task<>
{
try
{
auto[t0, t1] = co_await cppcoro::when_all_ready(makeTask(true), makeTask(false));
// You can obtain the exceptions by re-awaiting the returned tasks.
CHECK_THROWS_AS(t0.result(), const std::exception&);
CHECK(t1.result() == 123);
}
catch (...)
{
FAIL("Shouldn't throw");
}
}());
}
TEST_SUITE_END();