8000 Add new `await()` function (import from clue/reactphp-block) by clue · Pull Request #8 · reactphp/async · GitHub
[go: up one dir, main page]

Skip to content
8000

Add new await() function (import from clue/reactphp-block) #8

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 5 commits into from
Oct 21, 2021
Merged
Changes from 1 commit
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
Prev Previous commit
Clean up test suite for await() function
  • Loading branch information
clue committed Oct 21, 2021
commit 84a2de59c1a2879767af7927d597b9d765260ac2
103 changes: 44 additions & 59 deletions tests/AwaitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,43 @@

use React;
use React\EventLoop\Loop;
use React\Promise;
use React\Promise\Deferred;
use React\Promise\Promise;

class AwaitTest extends TestCase
{
public function testAwaitOneRejected()
public function testAwaitThrowsExceptionWhenPromiseIsRejectedWithException()
{
$promise = $this->createPromiseRejected(new \Exception('test'));
$promise = new Promise(function () {
throw new \Exception('test');
});

$this->setExpectedException('Exception', 'test');
React\Async\await($promise);
}

public function testAwaitOneRejectedWithFalseWillWrapInUnexpectedValueException()
public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWithFalse()
{
if (!interface_exists('React\Promise\CancellablePromiseInterface')) {
$this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3');
}

$promise = Promise\reject(false);
$promise = new Promise(function ($_, $reject) {
$reject(false);
});

$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type bool');
React\Async\await($promise);
}

public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException()
public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWithNull()
{
if (!interface_exists('React\Promise\CancellablePromiseInterface')) {
$this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3');
}

$promise = Promise\reject(null);
$promise = new Promise(function ($_, $reject) {
$reject(null);
});

$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type NULL');
React\Async\await($promise);
Expand All @@ -44,60 +49,67 @@ public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException()
/**
* @requires PHP 7
*/
public function testAwaitRejectedWithPhp7ErrorWillThrowOriginalError()
public function testAwaitThrowsErrorWhenPromiseIsRejectedWithError()
{
$promise = Promise\reject(new \Error('Test', 42));
$promise = new Promise(function ($_, $reject) {
throw new \Error('Test', 42);
});

$this->setExpectedException('Error', 'Test', 42);
React\Async\await($promise);
}

public function testAwaitOneResolved()
{
$promise = $this->createPromiseResolved(2);

$this->assertEquals(2, React\Async\await($promise));
}

public function testAwaitReturnsFulfilledValueWithoutGivingLoop()
public function testAwaitReturnsValueWhenPromiseIsFullfilled()
{
$promise = Promise\resolve(42);
$promise = new Promise(function ($resolve) {
$resolve(42);
});

$this->assertEquals(42, React\Async\await($promise));
}

public function testAwaitOneInterrupted()
public function testAwaitReturnsValueWhenPromiseIsFulfilledEvenWhenOtherTimerStopsLoop()
{
$promise = $this->createPromiseResolved(2, 0.02);
$this->createTimerInterrupt(0.01);
$promise = new Promise(function ($resolve) {
Loop::addTimer(0.02, function () use ($resolve) {
$resolve(2);
});
});
Loop::addTimer(0.01, function () {
Loop::stop();
});

$this->assertEquals(2, React\Async\await($promise));
}

public function testAwaitOneResolvesShouldNotCreateAnyGarbageReferences()
public function testAwaitShouldNotCreateAnyGarbageReferencesForResolvedPromise()
{
if (class_exists('React\Promise\When') && PHP_VERSION_ID >= 50400) {
$this->markTestSkipped('Not supported on legacy Promise v1 API with PHP 5.4+');
}

gc_collect_cycles();

$promise = Promise\resolve(1);
$promise = new Promise(function ($resolve) {
$resolve(42);
});
React\Async\await($promise);
unset($promise);

$this->assertEquals(0, gc_collect_cycles());
}

public function testAwaitOneRejectedShouldNotCreateAnyGarbageReferences()
public function testAwaitShouldNotCreateAnyGarbageReferencesForRejectedPromise()
{
if (class_exists('React\Promise\When') && PHP_VERSION_ID >= 50400) {
$this->markTestSkipped('Not supported on legacy Promise v1 API with PHP 5.4+');
if (class_exists('React\Promise\When')) {
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}

gc_collect_cycles();

$promise = Promise\reject(new \RuntimeException());
$promise = new Promise(function () {
throw new \RuntimeException();
});
try {
React\Async\await($promise);
} catch (\Exception $e) {
Expand All @@ -108,7 +120,7 @@ public function testAwaitOneRejectedShouldNotCreateAnyGarbageReferences()
$this->assertEquals(0, gc_collect_cycles());
}

public function testAwaitNullValueShouldNotCreateAnyGarbageReferences()
public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWithNullValue()
{
if (!interface_exists('React\Promise\CancellablePromiseInterface')) {
$this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3');
Expand AF73 All @@ -120,7 +132,9 @@ public function testAwaitNullValueShouldNotCreateAnyGarbageReferences()

gc_collect_cycles();

$promise = Promise\reject(null);
$promise = new Promise(function ($_, $reject) {
$reject(null);
});
try {
React\Async\await($promise);
} catch (\Exception $e) {
Expand All @@ -131,35 +145,6 @@ public function testAwaitNullValueShouldNotCreateAnyGarbageReferences()
$this->assertEquals(0, gc_collect_cycles());
}

protected function createPromiseResolved($value = null, $delay = 0.01)
{
$deferred = new Deferred();

Loop::addTimer($delay, function () use ($deferred, $value) {
$deferred->resolve($value);
});

return $deferred->promise();
}

protected function createPromiseRejected($value = null, $delay = 0.01)
{
$deferred = new Deferred();

Loop::addTimer($delay, function () use ($deferred, $value) {
$deferred->reject($value);
});

return $deferred->promise();
}

protected function createTimerInterrupt($delay = 0.01)
{
Loop::addTimer($delay, function () {
Loop::stop();
});
}

public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
{
if (method_exists($this, 'expectException')) {
Expand Down
0