Debian Codegraph @

src/kj/async-coroutine-test.c++

raw
1 // Copyright (c) 2020 Cloudflare, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21
22 #include <kj/async.h>
23 #include <kj/array.h>
24 #include <kj/compat/http.h>
25 #include <kj/debug.h>
26 #include <kj/test.h>
27
28 namespace kj {
29 namespace {
30
31 #ifdef KJ_HAS_COROUTINE
32
33 template <typename T>
34 Promise<kj::Decay<T>> identity(T&& value) {
35 co_return kj::fwd<T>(value);
36 }
37 // Work around a bonkers MSVC ICE with a separate overload.
38 Promise<const char*> identity(const char* value) {
39 co_return value;
40 }
41
42 KJ_TEST("Identity coroutine") {
43 EventLoop loop;
44 WaitScope waitScope(loop);
45
46 KJ_EXPECT(identity(123).wait(waitScope) == 123);
47 KJ_EXPECT(*identity(kj::heap(456)).wait(waitScope) == 456);
48
49 {
50 auto p = identity("we can cancel the coroutine");
51 }
52 }
53
54 template <typename T>
55 Promise<T> simpleCoroutine(kj::Promise<T> result, kj::Promise<bool> dontThrow = true) {
56 KJ_ASSERT(co_await dontThrow);
57 co_return co_await result;
58 }
59
60 KJ_TEST("Simple coroutine test") {
61 EventLoop loop;
62 WaitScope waitScope(loop);
63
64 simpleCoroutine(kj::Promise<void>(kj::READY_NOW)).wait(waitScope);
65
66 KJ_EXPECT(simpleCoroutine(kj::Promise<int>(123)).wait(waitScope) == 123);
67 }
68
69 struct Counter {
70 size_t& wind;
71 size_t& unwind;
72 Counter(size_t& wind, size_t& unwind): wind(wind), unwind(unwind) { ++wind; }
73 ~Counter() { ++unwind; }
74 KJ_DISALLOW_COPY_AND_MOVE(Counter);
75 };
76
77 kj::Promise<void> countAroundAwait(size_t& wind, size_t& unwind, kj::Promise<void> promise) {
78 Counter counter1(wind, unwind);
79 co_await promise;
80 Counter counter2(wind, unwind);
81 co_return;
82 };
83
84 KJ_TEST("co_awaiting initial immediate promises suspends even if event loop is empty and running") {
85 // The coroutine PromiseNode implementation contains an optimization which allows us to avoid
86 // suspending the coroutine and instead immediately call PromiseNode::get() and proceed with
87 // execution, but only if the coroutine has suspended at least once. This test verifies that the
88 // optimization is disabled for this initial suspension.
89
90 EventLoop loop;
91 WaitScope waitScope(loop);
92
93 // The immediate-execution optimization is only enabled when the event loop is running, so use an
94 // eagerly-evaluated evalLater() to perform the test from within the event loop. (If we didn't
95 // eagerly-evaluate the promise, the result would be extracted after the loop finished, which
96 // would disable the optimization anyway.)
97 kj::evalLater([&]() {
98 size_t wind = 0, unwind = 0;
99
100 auto promise = kj::Promise<void>(kj::READY_NOW);
101 auto coroPromise = countAroundAwait(wind, unwind, kj::READY_NOW);
102
103 // `coro` has not completed.
104 KJ_EXPECT(wind == 1);
105 KJ_EXPECT(unwind == 0);
106 }).eagerlyEvaluate(nullptr).wait(waitScope);
107
108 kj::evalLater([&]() {
109 // If there are no background tasks in the queue, coroutines execute through an evalLater()
110 // without suspending.
111
112 size_t wind = 0, unwind = 0;
113 bool evalLaterRan = false;
114
115 auto promise = kj::evalLater([&]() { evalLaterRan = true; });
116 auto coroPromise = countAroundAwait(wind, unwind, kj::mv(promise));
117
118 KJ_EXPECT(evalLaterRan == false);
119 KJ_EXPECT(wind == 1);
120 KJ_EXPECT(unwind == 0);
121 }).eagerlyEvaluate(nullptr).wait(waitScope);
122 }
123
124 KJ_TEST("co_awaiting an immediate promise suspends if the event loop is not running") {
125 // We only want to enable the immediate-execution optimization if the event loop is running, or
126 // else a whole bunch of RPC tests break, because some .then()s get evaluated on promise
127 // construction, before any .wait() call.
128
129 EventLoop loop;
130 WaitScope waitScope(loop);
131
132 size_t wind = 0, unwind = 0;
133
134 auto promise = kj::Promise<void>(kj::READY_NOW);
135 auto coroPromise = countAroundAwait(wind, unwind, kj::READY_NOW);
136
137 // In the previous test, this exact same code executed immediately because the event loop was
138 // running.
139 KJ_EXPECT(wind == 1);
140 KJ_EXPECT(unwind == 0);
141 }
142
143 KJ_TEST("co_awaiting immediate promises suspends if the event loop is not empty") {
144 // We want to make sure that we can still return to the event loop when we need to.
145
146 EventLoop loop;
147 WaitScope waitScope(loop);
148
149 // The immediate-execution optimization is only enabled when the event loop is running, so use an
150 // eagerly-evaluated evalLater() to perform the test from within the event loop. (If we didn't
151 // eagerly-evaluate the promise, the result would be extracted after the loop finished.)
152 kj::evalLater([&]() {
153 size_t wind = 0, unwind = 0;
154
155 // We need to enqueue an Event on the event loop to inhibit the immediate-execution
156 // optimization. Creating and then immediately fulfilling an EagerPromiseNode is a convenient
157 // way to do so.
158 auto paf = newPromiseAndFulfiller<void>();
159 paf.promise = paf.promise.eagerlyEvaluate(nullptr);
160 paf.fulfiller->fulfill();
161
162 auto promise = kj::Promise<void>(kj::READY_NOW);
163 auto coroPromise = countAroundAwait(wind, unwind, kj::READY_NOW);
164
165 // We didn't immediately extract the READY_NOW.
166 KJ_EXPECT(wind == 1);
167 KJ_EXPECT(unwind == 0);
168 }).eagerlyEvaluate(nullptr).wait(waitScope);
169
170 kj::evalLater([&]() {
171 size_t wind = 0, unwind = 0;
172 bool evalLaterRan = false;
173
174 // We need to enqueue an Event on the event loop to inhibit the immediate-execution
175 // optimization. Creating and then immediately fulfilling an EagerPromiseNode is a convenient
176 // way to do so.
177 auto paf = newPromiseAndFulfiller<void>();
178 paf.promise = paf.promise.eagerlyEvaluate(nullptr);
179 paf.fulfiller->fulfill();
180
181 auto promise = kj::evalLater([&]() { evalLaterRan = true; });
182 auto coroPromise = countAroundAwait(wind, unwind, kj::mv(promise));
183
184 // We didn't continue through the evalLater() promise, because the background promise's
185 // continuation was next in the event loop's queue.
186 KJ_EXPECT(evalLaterRan == false);
187 // No Counter destructor has run.
188 KJ_EXPECT(wind == 1);
189 KJ_EXPECT(unwind == 0);
190 }).eagerlyEvaluate(nullptr).wait(waitScope);
191 }
192
193 KJ_TEST("Exceptions propagate through layered coroutines") {
194 EventLoop loop;
195 WaitScope waitScope(loop);
196
197 auto throwy = simpleCoroutine(kj::Promise<int>(kj::NEVER_DONE), false);
198
199 KJ_EXPECT_THROW_RECOVERABLE(FAILED, simpleCoroutine(kj::mv(throwy)).wait(waitScope));
200 }
201
202 KJ_TEST("Exceptions before the first co_await don't escape, but reject the promise") {
203 EventLoop loop;
204 WaitScope waitScope(loop);
205
206 auto throwEarly = []() -> Promise<void> {
207 KJ_FAIL_ASSERT("test exception");
208 #ifdef __GNUC__
209 // Yes, this `co_return` is unreachable. But without it, this function is no longer a coroutine.
210 #pragma GCC diagnostic push
211 #pragma GCC diagnostic ignored "-Wunreachable-code"
212 #endif // __GNUC__
213 co_return;
214 #ifdef __GNUC__
215 #pragma GCC diagnostic pop
216 #endif // __GNUC__
217 };
218
219 auto throwy = throwEarly();
220
221 KJ_EXPECT_THROW_RECOVERABLE(FAILED, throwy.wait(waitScope));
222 }
223
224 KJ_TEST("Coroutines can catch exceptions from co_await") {
225 EventLoop loop;
226 WaitScope waitScope(loop);
227
228 kj::String description;
229
230 auto tryCatch = [&](kj::Promise<void> promise) -> kj::Promise<kj::String> {
231 try {
232 co_await promise;
233 } catch (const kj::Exception& exception) {
234 co_return kj::str(exception.getDescription());
235 }
236 KJ_FAIL_EXPECT("should have thrown");
237 KJ_UNREACHABLE;
238 };
239
240 {
241 // Immediately ready case.
242 auto promise = kj::Promise<void>(KJ_EXCEPTION(FAILED, "catch me"));
243 KJ_EXPECT(tryCatch(kj::mv(promise)).wait(waitScope) == "catch me");
244 }
245
246 {
247 // Ready later case.
248 auto promise = kj::evalLater([]() -> kj::Promise<void> {
249 return KJ_EXCEPTION(FAILED, "catch me");
250 });
251 KJ_EXPECT(tryCatch(kj::mv(promise)).wait(waitScope) == "catch me");
252 }
253 }
254
255 KJ_TEST("Coroutines can be canceled while suspended") {
256 EventLoop loop;
257 WaitScope waitScope(loop);
258
259 size_t wind = 0, unwind = 0;
260
261 auto coro = [&](kj::Promise<int> promise) -> kj::Promise<void> {
262 Counter counter1(wind, unwind);
263 co_await kj::evalLater([](){});
264 Counter counter2(wind, unwind);
265 co_await promise;
266 };
267
268 {
269 auto neverDone = kj::Promise<int>(kj::NEVER_DONE);
270 neverDone = neverDone.attach(kj::heap<Counter>(wind, unwind));
271 auto promise = coro(kj::mv(neverDone));
272 KJ_EXPECT(!promise.poll(waitScope));
273 }
274
275 // Stack variables on both sides of a co_await, plus coroutine arguments are destroyed.
276 KJ_EXPECT(wind == 3);
277 KJ_EXPECT(unwind == 3);
278 }
279
280 kj::Promise<void> deferredThrowCoroutine(kj::Promise<void> awaitMe) {
281 KJ_DEFER(kj::throwFatalException(KJ_EXCEPTION(FAILED, "thrown during unwind")));
282 co_await awaitMe;
283 co_return;
284 };
285
286 KJ_TEST("Exceptions during suspended coroutine frame-unwind propagate via destructor") {
287 EventLoop loop;
288 WaitScope waitScope(loop);
289
290 auto exception = KJ_ASSERT_NONNULL(kj::runCatchingExceptions([&]() {
291 deferredThrowCoroutine(kj::NEVER_DONE);
292 }));
293
294 KJ_EXPECT(exception.getDescription() == "thrown during unwind");
295 };
296
297 KJ_TEST("Exceptions during suspended coroutine frame-unwind do not cause a memory leak") {
298 EventLoop loop;
299 WaitScope waitScope(loop);
300
301 // We can't easily test for memory leaks without hooking operator new and delete. However, we can
302 // arrange for the test to crash on failure, by having the coroutine suspend at a promise that we
303 // later fulfill, thus arming the Coroutine's Event. If we fail to destroy the coroutine in this
304 // state, EventLoop will throw on destruction because it can still see the Event in its list.
305
306 auto exception = KJ_ASSERT_NONNULL(kj::runCatchingExceptions([&]() {
307 auto paf = kj::newPromiseAndFulfiller<void>();
308
309 auto coroPromise = deferredThrowCoroutine(kj::mv(paf.promise));
310
311 // Arm the Coroutine's Event.
312 paf.fulfiller->fulfill();
313
314 // If destroying `coroPromise` does not run ~Event(), then ~EventLoop() will crash later.
315 }));
316
317 KJ_EXPECT(exception.getDescription() == "thrown during unwind");
318 };
319
320 KJ_TEST("Exceptions during completed coroutine frame-unwind propagate via returned Promise") {
321 EventLoop loop;
322 WaitScope waitScope(loop);
323
324 {
325 // First, prove that exceptions don't escape the destructor of a completed coroutine.
326 auto promise = deferredThrowCoroutine(kj::READY_NOW);
327 KJ_EXPECT(promise.poll(waitScope));
328 }
329
330 {
331 // Next, prove that they show up via the returned Promise.
332 auto promise = deferredThrowCoroutine(kj::READY_NOW);
333 KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("thrown during unwind", promise.wait(waitScope));
334 }
335 }
336
337 KJ_TEST("Coroutine destruction exceptions are ignored if there is another exception in flight") {
338 EventLoop loop;
339 WaitScope waitScope(loop);
340
341 auto exception = KJ_ASSERT_NONNULL(kj::runCatchingExceptions([&]() {
342 auto promise = deferredThrowCoroutine(kj::NEVER_DONE);
343 kj::throwFatalException(KJ_EXCEPTION(FAILED, "thrown before destroying throwy promise"));
344 }));
345
346 KJ_EXPECT(exception.getDescription() == "thrown before destroying throwy promise");
347 }
348
349 KJ_TEST("co_await only sees coroutine destruction exceptions if promise was not rejected") {
350 EventLoop loop;
351 WaitScope waitScope(loop);
352
353 // throwyDtorPromise is an immediate void promise that will throw when it's destroyed, which
354 // we expect to be able to catch from a coroutine which co_awaits it.
355 auto throwyDtorPromise = kj::Promise<void>(kj::READY_NOW)
356 .attach(kj::defer([]() {
357 kj::throwFatalException(KJ_EXCEPTION(FAILED, "thrown during unwind"));
358 }));
359
360 // rejectedThrowyDtorPromise is a rejected promise. When co_awaited in a coroutine,
361 // Awaiter::await_resume() will throw that exception for us to catch, but before we can catch it,
362 // the temporary promise will be destroyed. The exception it throws during unwind will be ignored,
363 // and the caller of the coroutine will see only the "thrown during execution" exception.
364 auto rejectedThrowyDtorPromise = kj::evalNow([&]() -> kj::Promise<void> {
365 kj::throwFatalException(KJ_EXCEPTION(FAILED, "thrown during execution"));
366 }).attach(kj::defer([]() {
367 kj::throwFatalException(KJ_EXCEPTION(FAILED, "thrown during unwind"));
368 }));
369
370 auto awaitPromise = [](kj::Promise<void> promise) -> kj::Promise<void> {
371 co_await promise;
372 };
373
374 KJ_EXPECT_THROW_MESSAGE("thrown during unwind",
375 awaitPromise(kj::mv(throwyDtorPromise)).wait(waitScope));
376
377 KJ_EXPECT_THROW_MESSAGE("thrown during execution",
378 awaitPromise(kj::mv(rejectedThrowyDtorPromise)).wait(waitScope));
379 }
380
381 #if !_MSC_VER && !__aarch64__
382 uint countLines(StringPtr s) {
383 uint lines = 0;
384 for (char c: s) {
385 lines += c == '\n';
386 }
387 return lines;
388 }
389
390 // TODO(msvc): This test relies on GetFunctorStartAddress, which is not supported on MSVC currently,
391 // so skip the test.
392 // TODO(someday): Test is flakey on arm64, depending on how it's compiled. I haven't had a chance to
393 // investigate much, but noticed that it failed in a debug build, but passed in a local opt build.
394 KJ_TEST("Can trace through coroutines") {
395 // This verifies that async traces, generated either from promises or from events, can see through
396 // coroutines.
397 //
398 // This test may be a bit brittle because it depends on specific trace counts.
399
400 // Enable stack traces, even in release mode.
401 class EnableFullStackTrace: public ExceptionCallback {
402 public:
403 StackTraceMode stackTraceMode() override { return StackTraceMode::FULL; }
404 };
405 EnableFullStackTrace exceptionCallback;
406
407 EventLoop loop;
408 WaitScope waitScope(loop);
409
410 auto paf = newPromiseAndFulfiller<void>();
411
412 // Get an async trace when the promise is fulfilled. We eagerlyEvaluate() to make sure the
413 // continuation executes while the event loop is running.
414 paf.promise = paf.promise.then([]() {
415 auto trace = getAsyncTrace();
416 // We expect one entry for waitImpl(), one for the coroutine, and one for this continuation.
417 // When building in debug mode with CMake, I observed this count can be 2. The missing frame is
418 // probably this continuation. Let's just expect a range.
419 auto count = countLines(trace);
420 KJ_EXPECT(0 < count && count <= 3);
421 }).eagerlyEvaluate(nullptr);
422
423 auto coroPromise = [&]() -> kj::Promise<void> {
424 co_await paf.promise;
425 }();
426
427 {
428 auto trace = coroPromise.trace();
429 // One for the Coroutine PromiseNode, one for paf.promise.
430 KJ_EXPECT(countLines(trace) >= 2);
431 }
432
433 paf.fulfiller->fulfill();
434
435 coroPromise.wait(waitScope);
436 }
437 #endif // !_MSC_VER || defined(__clang__)
438
439 Promise<void> sendData(Promise<Own<NetworkAddress>> addressPromise) {
440 auto address = co_await addressPromise;
441 auto client = co_await address->connect();
442 co_await client->write("foo", 3);
443 }
444
445 Promise<String> receiveDataCoroutine(Own<ConnectionReceiver> listener) {
446 auto server = co_await listener->accept();
447 char buffer[4];
448 auto n = co_await server->read(buffer, 3, 4);
449 KJ_EXPECT(3u == n);
450 co_return heapString(buffer, n);
451 }
452
453 KJ_TEST("Simple network test with coroutine") {
454 auto io = setupAsyncIo();
455 auto& network = io.provider->getNetwork();
456
457 Own<NetworkAddress> serverAddress = network.parseAddress("*", 0).wait(io.waitScope);
458 Own<ConnectionReceiver> listener = serverAddress->listen();
459
460 sendData(network.parseAddress("localhost", listener->getPort()))
461 .detach([](Exception&& exception) {
462 KJ_FAIL_EXPECT(exception);
463 });
464
465 String result = receiveDataCoroutine(kj::mv(listener)).wait(io.waitScope);
466
467 KJ_EXPECT("foo" == result);
468 }
469
470 Promise<Own<AsyncIoStream>> httpClientConnect(AsyncIoContext& io) {
471 auto addr = co_await io.provider->getNetwork().parseAddress("capnproto.org", 80);
472 co_return co_await addr->connect();
473 }
474
475 Promise<void> httpClient(Own<AsyncIoStream> connection) {
476 // Borrowed and rewritten from compat/http-test.c++.
477
478 HttpHeaderTable table;
479 auto client = newHttpClient(table, *connection);
480
481 HttpHeaders headers(table);
482 headers.set(HttpHeaderId::HOST, "capnproto.org");
483
484 auto response = co_await client->request(HttpMethod::GET, "/", headers).response;
485 KJ_EXPECT(response.statusCode / 100 == 3);
486 auto location = KJ_ASSERT_NONNULL(response.headers->get(HttpHeaderId::LOCATION));
487 KJ_EXPECT(location == "https://capnproto.org/");
488
489 auto body = co_await response.body->readAllText();
490 }
491
492 KJ_TEST("HttpClient to capnproto.org with a coroutine") {
493 auto io = setupAsyncIo();
494
495 auto promise = httpClientConnect(io).then([](Own<AsyncIoStream> connection) {
496 return httpClient(kj::mv(connection));
497 }, [](Exception&&) {
498 KJ_LOG(WARNING, "skipping test because couldn't connect to capnproto.org");
499 });
500
501 promise.wait(io.waitScope);
502 }
503
504 // =======================================================================================
505 // coCapture() tests
506
507 KJ_TEST("Verify coCapture() functors can only be run once") {
508 auto io = kj::setupAsyncIo();
509
510 auto functor = coCapture([](kj::Timer& timer) -> kj::Promise<void> {
511 co_await timer.afterDelay(1 * kj::MILLISECONDS);
512 });
513
514 auto promise = functor(io.lowLevelProvider->getTimer());
515 KJ_EXPECT_THROW(FAILED, functor(io.lowLevelProvider->getTimer()));
516
517 promise.wait(io.waitScope);
518 }
519
520 auto makeDelayedIntegerFunctor(size_t i) {
521 return [i](kj::Timer& timer) -> kj::Promise<size_t> {
522 co_await timer.afterDelay(1 * kj::MILLISECONDS);
523 co_return i;
524 };
525 }
526
527 KJ_TEST("Verify coCapture() with local scoped functors") {
528 auto io = kj::setupAsyncIo();
529
530 constexpr size_t COUNT = 100;
531 kj::Vector<kj::Promise<size_t>> promises;
532 for (size_t i = 0; i < COUNT; ++i) {
533 auto functor = coCapture(makeDelayedIntegerFunctor(i));
534 promises.add(functor(io.lowLevelProvider->getTimer()));
535 }
536
537 for (size_t i = COUNT; i > 0 ; --i) {
538 auto j = i-1;
539 auto result = promises[j].wait(io.waitScope);
540 KJ_REQUIRE(result == j);
541 }
542 }
543
544 auto makeCheckThenDelayedIntegerFunctor(kj::Timer& timer, size_t i) {
545 return [&timer, i](size_t val) -> kj::Promise<size_t> {
546 KJ_REQUIRE(val == i);
547 co_await timer.afterDelay(1 * kj::MILLISECONDS);
548 co_return i;
549 };
550 }
551
552 KJ_TEST("Verify coCapture() with continuation functors") {
553 // This test usually works locally without `coCapture()()`. It does however, fail in
554 // ASAN.
555 auto io = kj::setupAsyncIo();
556
557 constexpr size_t COUNT = 100;
558 kj::Vector<kj::Promise<size_t>> promises;
559 for (size_t i = 0; i < COUNT; ++i) {
560 auto promise = io.lowLevelProvider->getTimer().afterDelay(1 * kj::MILLISECONDS).then([i]() {
561 return i;
562 });
563 promise = promise.then(coCapture(
564 makeCheckThenDelayedIntegerFunctor(io.lowLevelProvider->getTimer(), i)));
565 promises.add(kj::mv(promise));
566 }
567
568 for (size_t i = COUNT; i > 0 ; --i) {
569 auto j = i-1;
570 auto result = promises[j].wait(io.waitScope);
571 KJ_REQUIRE(result == j);
572 }
573 }
574
575 #endif // KJ_HAS_COROUTINE
576
577 } // namespace
578 } // namespace kj