Skip to content
Navigation Menu
{{ message }}
forked from lewissbaker/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcancellation_token_tests.cpp
More file actions
342 lines (283 loc) · 10.1 KB
/
Copy pathcancellation_token_tests.cpp
File metadata and controls
342 lines (283 loc) · 10.1 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
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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/cancellation_token.hpp>
#include <cppcoro/cancellation_source.hpp>
#include <cppcoro/cancellation_registration.hpp>
#include <cppcoro/operation_cancelled.hpp>
#include <thread>
#include <ostream>
#include "doctest/doctest.h"
TEST_SUITE_BEGIN("cancellation_token tests");
TEST_CASE("default cancellation_token is not cancellable")
{
cppcoro::cancellation_token t;
CHECK(!t.is_cancellation_requested());
CHECK(!t.can_be_cancelled());
}
TEST_CASE("calling request_cancellation on cancellation_source updates cancellation_token")
{
cppcoro::cancellation_source s;
cppcoro::cancellation_token t = s.token();
CHECK(t.can_be_cancelled());
CHECK(!t.is_cancellation_requested());
s.request_cancellation();
CHECK(t.is_cancellation_requested());
CHECK(t.can_be_cancelled());
}
TEST_CASE("cancellation_token can't be cancelled when last cancellation_source destructed")
{
cppcoro::cancellation_token t;
{
cppcoro::cancellation_source s;
t = s.token();
CHECK(t.can_be_cancelled());
}
CHECK(!t.can_be_cancelled());
}
TEST_CASE("cancelation_token can be cancelled when last cancellation_source destructed if cancellation already requested")
{
cppcoro::cancellation_token t;
{
cppcoro::cancellation_source s;
t = s.token();
CHECK(t.can_be_cancelled());
s.request_cancellation();
}
CHECK(t.can_be_cancelled());
CHECK(t.is_cancellation_requested());
}
TEST_CASE("cancellation_registration when cancellation not yet requested")
{
cppcoro::cancellation_source s;
bool callbackExecuted = false;
{
cppcoro::cancellation_registration callbackRegistration(
s.token(),
[&] { callbackExecuted = true; });
}
CHECK(!callbackExecuted);
{
cppcoro::cancellation_registration callbackRegistration(
s.token(),
[&] { callbackExecuted = true; });
CHECK(!callbackExecuted);
s.request_cancellation();
CHECK(callbackExecuted);
}
}
TEST_CASE("throw_if_cancellation_requested")
{
cppcoro::cancellation_source s;
cppcoro::cancellation_token t = s.token();
CHECK_NOTHROW(t.throw_if_cancellation_requested());
s.request_cancellation();
CHECK_THROWS_AS(t.throw_if_cancellation_requested(), const cppcoro::operation_cancelled&);
}
TEST_CASE("cancellation_registration called immediately when cancellation already requested")
{
cppcoro::cancellation_source s;
s.request_cancellation();
bool executed = false;
cppcoro::cancellation_registration r{ s.token(), [&] { executed = true; } };
CHECK(executed);
}
TEST_CASE("register many callbacks"
* doctest::description{
"this checks the code-path that allocates the next chunk of entries "
"in the internal data-structres, which occurs on 17th callback" })
{
cppcoro::cancellation_source s;
auto t = s.token();
int callbackExecutionCount = 0;
auto callback = [&] { ++callbackExecutionCount; };
// Allocate enough to require a second chunk to be allocated.
cppcoro::cancellation_registration r1{ t, callback };
cppcoro::cancellation_registration r2{ t, callback };
cppcoro::cancellation_registration r3{ t, callback };
cppcoro::cancellation_registration r4{ t, callback };
cppcoro::cancellation_registration r5{ t, callback };
cppcoro::cancellation_registration r6{ t, callback };
cppcoro::cancellation_registration r7{ t, callback };
cppcoro::cancellation_registration r8{ t, callback };
cppcoro::cancellation_registration r9{ t, callback };
cppcoro::cancellation_registration r10{ t, callback };
cppcoro::cancellation_registration r11{ t, callback };
cppcoro::cancellation_registration r12{ t, callback };
cppcoro::cancellation_registration r13{ t, callback };
cppcoro::cancellation_registration r14{ t, callback };
cppcoro::cancellation_registration r15{ t, callback };
cppcoro::cancellation_registration r16{ t, callback };
cppcoro::cancellation_registration r17{ t, callback };
cppcoro::cancellation_registration r18{ t, callback };
s.request_cancellation();
CHECK(callbackExecutionCount == 18);
}
TEST_CASE("concurrent registration and cancellation")
{
// Just check this runs and terminates without crashing.
for (int i = 0; i < 100; ++i)
{
cppcoro::cancellation_source source;
std::thread waiter1{ [token = source.token()]
{
std::atomic<bool> cancelled = false;
while (!cancelled)
{
cppcoro::cancellation_registration registration{ token, [&]
{
cancelled = true;
} };
cppcoro::cancellation_registration reg0{ token, [] {} };
cppcoro::cancellation_registration reg1{ token, [] {} };
cppcoro::cancellation_registration reg2{ token, [] {} };
cppcoro::cancellation_registration reg3{ token, [] {} };
cppcoro::cancellation_registration reg4{ token, [] {} };
cppcoro::cancellation_registration reg5{ token, [] {} };
cppcoro::cancellation_registration reg6{ token, [] {} };
cppcoro::cancellation_registration reg7{ token, [] {} };
cppcoro::cancellation_registration reg8{ token, [] {} };
cppcoro::cancellation_registration reg9{ token, [] {} };
cppcoro::cancellation_registration reg10{ token, [] {} };
cppcoro::cancellation_registration reg11{ token, [] {} };
cppcoro::cancellation_registration reg12{ token, [] {} };
cppcoro::cancellation_registration reg13{ token, [] {} };
cppcoro::cancellation_registration reg14{ token, [] {} };
cppcoro::cancellation_registration reg15{ token, [] {} };
cppcoro::cancellation_registration reg17{ token, [] {} };
std::this_thread::yield();
}
} };
std::thread waiter2{ [token = source.token()]
{
std::atomic<bool> cancelled = false;
while (!cancelled)
{
cppcoro::cancellation_registration registration{ token, [&]
{
cancelled = true;
} };
cppcoro::cancellation_registration reg0{ token, [] {} };
cppcoro::cancellation_registration reg1{ token, [] {} };
cppcoro::cancellation_registration reg2{ token, [] {} };
cppcoro::cancellation_registration reg3{ token, [] {} };
cppcoro::cancellation_registration reg4{ token, [] {} };
cppcoro::cancellation_registration reg5{ token, [] {} };
cppcoro::cancellation_registration reg6{ token, [] {} };
cppcoro::cancellation_registration reg7{ token, [] {} };
cppcoro::cancellation_registration reg8{ token, [] {} };
cppcoro::cancellation_registration reg9{ token, [] {} };
cppcoro::cancellation_registration reg10{ token, [] {} };
cppcoro::cancellation_registration reg11{ token, [] {} };
cppcoro::cancellation_registration reg12{ token, [] {} };
cppcoro::cancellation_registration reg13{ token, [] {} };
cppcoro::cancellation_registration reg14{ token, [] {} };
cppcoro::cancellation_registration reg15{ token, [] {} };
cppcoro::cancellation_registration reg16{ token, [] {} };
std::this_thread::yield();
}
} };
std::thread waiter3{ [token = source.token()]
{
std::atomic<bool> cancelled = false;
while (!cancelled)
{
cppcoro::cancellation_registration registration{ token, [&]
{
cancelled = true;
} };
cppcoro::cancellation_registration reg0{ token, [] {} };
cppcoro::cancellation_registration reg1{ token, [] {} };
cppcoro::cancellation_registration reg2{ token, [] {} };
cppcoro::cancellation_registration reg3{ token, [] {} };
cppcoro::cancellation_registration reg4{ token, [] {} };
cppcoro::cancellation_registration reg5{ token, [] {} };
cppcoro::cancellation_registration reg6{ token, [] {} };
cppcoro::cancellation_registration reg7{ token, [] {} };
cppcoro::cancellation_registration reg8{ token, [] {} };
cppcoro::cancellation_registration reg9{ token, [] {} };
cppcoro::cancellation_registration reg10{ token, [] {} };
cppcoro::cancellation_registration reg11{ token, [] {} };
cppcoro::cancellation_registration reg12{ token, [] {} };
cppcoro::cancellation_registration reg13{ token, [] {} };
cppcoro::cancellation_registration reg14{ token, [] {} };
cppcoro::cancellation_registration reg15{ token, [] {} };
cppcoro::cancellation_registration reg16{ token, [] {} };
std::this_thread::yield();
}
} };
std::thread canceller{ [&source]
{
source.request_cancellation();
} };
canceller.join();
waiter1.join();
waiter2.join();
waiter3.join();
}
}
TEST_CASE("cancellation registration single-threaded performance")
{
struct batch
{
batch(cppcoro::cancellation_token t)
: r0(t, [] {})
, r1(t, [] {})
, r2(t, [] {})
, r3(t, [] {})
, r4(t, [] {})
, r5(t, [] {})
, r6(t, [] {})
, r7(t, [] {})
, r8(t, [] {})
, r9(t, [] {})
{}
cppcoro::cancellation_registration r0;
cppcoro::cancellation_registration r1;
cppcoro::cancellation_registration r2;
cppcoro::cancellation_registration r3;
cppcoro::cancellation_registration r4;
cppcoro::cancellation_registration r5;
cppcoro::cancellation_registration r6;
cppcoro::cancellation_registration r7;
cppcoro::cancellation_registration r8;
cppcoro::cancellation_registration r9;
};
cppcoro::cancellation_source s;
constexpr int iterationCount = 100'000;
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < iterationCount; ++i)
{
cppcoro::cancellation_registration r{ s.token(), [] {} };
}
auto end = std::chrono::high_resolution_clock::now();
auto time1 = end - start;
start = end;
for (int i = 0; i < iterationCount; ++i)
{
batch b{ s.token() };
}
end = std::chrono::high_resolution_clock::now();
auto time2 = end - start;
start = end;
for (int i = 0; i < iterationCount; ++i)
{
batch b0{ s.token() };
batch b1{ s.token() };
batch b2{ s.token() };
batch b3{ s.token() };
batch b4{ s.token() };
}
end = std::chrono::high_resolution_clock::now();
auto time3 = end - start;
auto report = [](const char* label, auto time, std::uint64_t count)
{
auto us = std::chrono::duration_cast<std::chrono::microseconds>(time).count();
MESSAGE(label << " took " << us << "us (" << (1000.0 * us / count) << " ns/item)");
};
report("Individual", time1, iterationCount);
report("Batch10", time2, 10 * iterationCount);
report("Batch50", time3, 50 * iterationCount);
}
TEST_SUITE_END();
You can’t perform that action at this time.
