Skip to content

Skip cancellation of promise within Fiber without cancellation support #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/FiberMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions tests/AwaitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down