From b7242c6d06ac4d883360c2f363ffd59a232431ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Fri, 4 Mar 2022 11:50:32 +0100 Subject: [PATCH] Skip cancellation of promise within fiber without cancellation support --- src/FiberMap.php | 2 +- tests/AwaitTest.php | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/FiberMap.php b/src/FiberMap.php index 7ee07d1..5c7c7bf 100644 --- a/src/FiberMap.php +++ b/src/FiberMap.php @@ -25,7 +25,7 @@ public static function cancel(\Fiber $fiber): void public static function isCancelled(\Fiber $fiber): bool { - return self::$status[\spl_object_id($fiber)]; + return self::$status[\spl_object_id($fiber)] ?? false; } public static function setPromise(\Fiber $fiber, PromiseInterface $promise): void diff --git a/tests/AwaitTest.php b/tests/AwaitTest.php index f2faceb..93a69f8 100644 --- a/tests/AwaitTest.php +++ b/tests/AwaitTest.php @@ -97,6 +97,27 @@ public function testAwaitAsyncThrowsExceptionImmediatelyWhenPromiseIsRejected(ca } } + /** + * @dataProvider provideAwaiters + */ + public function testAwaitThrowsExceptionImmediatelyInCustomFiberWhenPromiseIsRejected(callable $await) + { + $fiber = new \Fiber(function () use ($await) { + $promise = new Promise(function ($resolve) { + throw new \RuntimeException('Test'); + }); + + return $await($promise); + }); + + try { + $fiber->start(); + } catch (\RuntimeException $e) { + $this->assertTrue($fiber->isTerminated()); + $this->assertEquals('Test', $e->getMessage()); + } + } + /** * @dataProvider provideAwaiters */ @@ -230,6 +251,25 @@ public function testAwaitAsyncReturnsValueImmediatelyWhenPromiseIsFulfilled(call $this->assertEquals(1, $ticks); } + /** + * @dataProvider provideAwaiters + */ + public function testAwaitReturnsValueImmediatelyInCustomFiberWhenPromiseIsFulfilled(callable $await) + { + $fiber = new \Fiber(function () use ($await) { + $promise = new Promise(function ($resolve) { + $resolve(42); + }); + + return $await($promise); + }); + + $fiber->start(); + + $this->assertTrue($fiber->isTerminated()); + $this->assertEquals(42, $fiber->getReturn()); + } + /** * @dataProvider provideAwaiters */