1
0
mirror of https://github.com/danog/amp.git synced 2024-12-03 18:07:57 +01:00
amp/test/WaitTest.php

112 lines
2.3 KiB
PHP
Raw Normal View History

<?php
2016-07-12 18:20:06 +02:00
namespace Amp\Test;
use Amp\Deferred;
use Amp\Delayed;
use Amp\Failure;
2017-04-23 14:39:19 +02:00
use Amp\Loop;
2020-09-28 05:19:52 +02:00
use Amp\PHPUnit\AsyncTestCase;
use Amp\PHPUnit\TestException;
use Amp\Promise;
use Amp\Success;
2020-04-15 22:46:31 +02:00
use function Amp\call;
use function Amp\delay;
use function React\Promise\resolve;
2016-07-12 18:20:06 +02:00
2020-09-28 05:19:52 +02:00
class WaitTest extends AsyncTestCase
2018-06-18 20:00:01 +02:00
{
2020-09-28 05:19:52 +02:00
public function testWaitOnSuccessfulPromise(): void
2018-06-18 20:00:01 +02:00
{
2016-07-12 18:20:06 +02:00
$value = 1;
2016-11-14 20:59:21 +01:00
$promise = new Success($value);
2016-07-12 18:20:06 +02:00
$result = Promise\wait($promise);
2016-07-12 18:20:06 +02:00
$this->assertSame($value, $result);
}
2020-09-28 05:19:52 +02:00
public function testWaitOnFailedPromise(): void
2018-06-18 20:00:01 +02:00
{
2016-07-12 18:20:06 +02:00
$exception = new \Exception();
2016-11-14 20:59:21 +01:00
$promise = new Failure($exception);
2016-07-12 18:20:06 +02:00
try {
$result = Promise\wait($promise);
2016-07-12 18:20:06 +02:00
} catch (\Exception $e) {
$this->assertSame($exception, $e);
return;
}
$this->fail('Rejection exception should be thrown from wait().');
}
/**
2016-11-14 20:59:21 +01:00
* @depends testWaitOnSuccessfulPromise
2016-07-12 18:20:06 +02:00
*/
2020-09-28 05:19:52 +02:00
public function testWaitOnPendingPromise(): void
2018-06-18 20:00:01 +02:00
{
2020-09-28 05:19:52 +02:00
$value = 1;
2016-07-12 18:20:06 +02:00
2020-09-28 05:19:52 +02:00
$promise = new Delayed(100, $value);
2016-07-12 18:20:06 +02:00
2020-09-28 05:19:52 +02:00
$result = Promise\wait($promise);
2016-07-12 18:20:06 +02:00
2020-09-28 05:19:52 +02:00
$this->assertSame($value, $result);
2016-07-12 18:20:06 +02:00
}
2020-09-28 05:19:52 +02:00
public function testPromiseWithNoResolutionPathThrowsException(): void
2018-06-18 20:00:01 +02:00
{
2020-09-28 05:19:52 +02:00
$this->expectException(\FiberError::class);
$this->expectExceptionMessage("Scheduler ended");
Promise\wait((new Deferred)->promise());
}
/**
* @depends testWaitOnSuccessfulPromise
*/
2020-09-28 05:19:52 +02:00
public function testReactPromise(): void
2018-06-18 20:00:01 +02:00
{
$value = 1;
$promise = resolve($value);
$result = Promise\wait($promise);
$this->assertSame($value, $result);
}
2017-03-14 22:15:36 +01:00
2020-09-28 05:19:52 +02:00
public function testWaitNested(): void
2020-04-15 22:46:31 +02:00
{
$promise = call(static function () {
yield delay(10);
return Promise\wait(new Delayed(10, 1));
});
$result = Promise\wait($promise);
$this->assertSame(1, $result);
}
2020-09-28 05:19:52 +02:00
public function testWaitNestedDelayed(): void
2020-04-15 22:46:31 +02:00
{
$promise = call(static function () {
yield delay(10);
$result = Promise\wait(new Delayed(10, 1));
yield delay(0);
return $result;
});
$result = Promise\wait($promise);
$this->assertSame(1, $result);
}
2016-07-12 18:20:06 +02:00
}