-
Notifications
You must be signed in to change notification settings - Fork 483
/
Copy pathscheduling_operator_tests.cpp
290 lines (222 loc) · 7.25 KB
/
scheduling_operator_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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/schedule_on.hpp>
#include <cppcoro/resume_on.hpp>
#include <cppcoro/io_service.hpp>
#include <cppcoro/sync_wait.hpp>
#include <cppcoro/when_all_ready.hpp>
#include <cppcoro/on_scope_exit.hpp>
#include <cppcoro/fmap.hpp>
#include "io_service_fixture.hpp"
#include <ostream>
#include "doctest/doctest.h"
TEST_SUITE_BEGIN("schedule/resume_on");
TEST_CASE_FIXTURE(io_service_fixture, "schedule_on task<> function")
{
auto mainThreadId = std::this_thread::get_id();
std::thread::id ioThreadId;
auto start = [&]() -> cppcoro::task<>
{
ioThreadId = std::this_thread::get_id();
CHECK(ioThreadId != mainThreadId);
co_return;
};
cppcoro::sync_wait([&]() -> cppcoro::task<>
{
CHECK(std::this_thread::get_id() == mainThreadId);
co_await schedule_on(io_service(), start());
// TODO: Uncomment this check once the implementation of task<T>
// guarantees that the continuation will resume on the same thread
// that the task completed on. Currently it's possible to resume on
// the thread that launched the task if it completes on another thread
// before the current thread could attach the continuation after it
// suspended. See cppcoro issue #79.
//
// The long-term solution here is to use the symmetric-transfer capability
// to avoid the use of atomics and races, but we're still waiting for MSVC to
// implement this (doesn't seem to be implemented as of VS 2017.8 Preview 5)
//CHECK(std::this_thread::get_id() == ioThreadId);
}());
}
TEST_CASE_FIXTURE(io_service_fixture, "schedule_on async_generator<> function")
{
auto mainThreadId = std::this_thread::get_id();
std::thread::id ioThreadId;
auto makeSequence = [&]() -> cppcoro::async_generator<int>
{
ioThreadId = std::this_thread::get_id();
CHECK(ioThreadId != mainThreadId);
co_yield 1;
CHECK(std::this_thread::get_id() == ioThreadId);
co_yield 2;
CHECK(std::this_thread::get_id() == ioThreadId);
co_yield 3;
CHECK(std::this_thread::get_id() == ioThreadId);
co_return;
};
cppcoro::io_service otherIoService;
cppcoro::sync_wait(cppcoro::when_all_ready(
[&]() -> cppcoro::task<>
{
CHECK(std::this_thread::get_id() == mainThreadId);
auto seq = schedule_on(io_service(), makeSequence());
int expected = 1;
for (auto iter = co_await seq.begin(); iter != seq.end(); co_await ++iter)
{
int value = *iter;
CHECK(value == expected++);
// Transfer exection back to main thread before
// awaiting next item in the loop to chck that
// the generator is resumed on io_service() thread.
co_await otherIoService.schedule();
}
otherIoService.stop();
}(),
[&]() -> cppcoro::task<>
{
otherIoService.process_events();
co_return;
}()));
}
TEST_CASE_FIXTURE(io_service_fixture, "resume_on task<> function")
{
auto mainThreadId = std::this_thread::get_id();
auto start = [&]() -> cppcoro::task<>
{
CHECK(std::this_thread::get_id() == mainThreadId);
co_return;
};
cppcoro::sync_wait([&]() -> cppcoro::task<>
{
CHECK(std::this_thread::get_id() == mainThreadId);
co_await resume_on(io_service(), start());
// NOTE: This check could potentially spuriously fail with the current
// implementation of task<T>. See cppcoro issue #79.
CHECK(std::this_thread::get_id() != mainThreadId);
}());
}
constexpr bool isMsvc15_4X86Optimised =
#if defined(_MSC_VER) && _MSC_VER == 1911 && defined(_M_IX86) && !defined(_DEBUG)
true;
#else
false;
#endif
// Disable under MSVC 15.4 X86 Optimised due to presumed compiler bug that causes
// an access violation. Seems to be fixed under MSVC 15.5.
TEST_CASE_FIXTURE(io_service_fixture, "resume_on async_generator<> function"
* doctest::skip{ isMsvc15_4X86Optimised })
{
auto mainThreadId = std::this_thread::get_id();
std::thread::id ioThreadId;
auto makeSequence = [&]() -> cppcoro::async_generator<int>
{
co_await io_service().schedule();
ioThreadId = std::this_thread::get_id();
CHECK(ioThreadId != mainThreadId);
co_yield 1;
co_yield 2;
co_await io_service().schedule();
co_yield 3;
co_await io_service().schedule();
co_return;
};
cppcoro::io_service otherIoService;
cppcoro::sync_wait(cppcoro::when_all_ready(
[&]() -> cppcoro::task<>
{
auto stopOnExit = cppcoro::on_scope_exit([&] { otherIoService.stop(); });
CHECK(std::this_thread::get_id() == mainThreadId);
auto seq = resume_on(otherIoService, makeSequence());
int expected = 1;
for (auto iter = co_await seq.begin(); iter != seq.end(); co_await ++iter)
{
int value = *iter;
// Every time we receive a value it should be on our requested
// scheduler (ie. main thread)
CHECK(std::this_thread::get_id() == mainThreadId);
CHECK(value == expected++);
// Occasionally transfer execution to a different thread before
// awaiting next element.
if (value == 2)
{
co_await io_service().schedule();
}
}
otherIoService.stop();
}(),
[&]() -> cppcoro::task<>
{
otherIoService.process_events();
co_return;
}()));
}
TEST_CASE_FIXTURE(io_service_fixture, "schedule_on task<> pipe syntax")
{
auto mainThreadId = std::this_thread::get_id();
auto makeTask = [&]() -> cppcoro::task<int>
{
CHECK(std::this_thread::get_id() != mainThreadId);
co_return 123;
};
auto triple = [&](int x)
{
CHECK(std::this_thread::get_id() != mainThreadId);
return x * 3;
};
CHECK(cppcoro::sync_wait(makeTask() | schedule_on(io_service())) == 123);
// Shouldn't matter where in sequence schedule_on() appears since it applies
// at the start of the pipeline (ie. before first task starts).
CHECK(cppcoro::sync_wait(makeTask() | schedule_on(io_service()) | cppcoro::fmap(triple)) == 369);
CHECK(cppcoro::sync_wait(makeTask() | cppcoro::fmap(triple) | schedule_on(io_service())) == 369);
}
TEST_CASE_FIXTURE(io_service_fixture, "resume_on task<> pipe syntax")
{
auto mainThreadId = std::this_thread::get_id();
auto makeTask = [&]() -> cppcoro::task<int>
{
CHECK(std::this_thread::get_id() == mainThreadId);
co_return 123;
};
cppcoro::sync_wait([&]() -> cppcoro::task<>
{
cppcoro::task<int> t = makeTask() | cppcoro::resume_on(io_service());
CHECK(co_await t == 123);
CHECK(std::this_thread::get_id() != mainThreadId);
}());
}
TEST_CASE_FIXTURE(io_service_fixture, "resume_on task<> pipe syntax multiple uses")
{
auto mainThreadId = std::this_thread::get_id();
auto makeTask = [&]() -> cppcoro::task<int>
{
CHECK(std::this_thread::get_id() == mainThreadId);
co_return 123;
};
auto triple = [&](int x)
{
CHECK(std::this_thread::get_id() != mainThreadId);
return x * 3;
};
cppcoro::io_service otherIoService;
cppcoro::sync_wait(cppcoro::when_all_ready(
[&]() -> cppcoro::task<>
{
auto stopOnExit = cppcoro::on_scope_exit([&] { otherIoService.stop(); });
CHECK(std::this_thread::get_id() == mainThreadId);
cppcoro::task<int> t =
makeTask()
| cppcoro::resume_on(io_service())
| cppcoro::fmap(triple)
| cppcoro::resume_on(otherIoService);
CHECK(co_await t == 369);
CHECK(std::this_thread::get_id() == mainThreadId);
}(),
[&]() -> cppcoro::task<>
{
otherIoService.process_events();
co_return;
}()));
}
TEST_SUITE_END();