2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 23:39:25 +02:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
namespace Amp\Test;
|
|
|
|
|
|
|
|
use Amp;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Failure;
|
|
|
|
use Amp\Loop;
|
|
|
|
use Amp\Pause;
|
|
|
|
use Amp\Success;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
2017-03-13 05:27:43 +01:00
|
|
|
use function React\Promise\resolve;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2017-03-11 14:57:03 +01:00
|
|
|
class TimeoutTest extends \PHPUnit\Framework\TestCase {
|
2016-11-14 20:59:21 +01:00
|
|
|
public function testSuccessfulPromise() {
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () {
|
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
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = Amp\timeout($promise, 100);
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$callback = function ($exception, $value) use (&$result) {
|
|
|
|
$result = $value;
|
|
|
|
};
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise->when($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
public function testFailedPromise() {
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () {
|
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
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = Amp\timeout($promise, 100);
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$callback = function ($exception, $value) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
};
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise->when($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
});
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testSuccessfulPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
|
|
|
public function testFastPending() {
|
|
|
|
$value = 1;
|
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$result, $value) {
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = new Pause(50, $value);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = Amp\timeout($promise, 100);
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$callback = function ($exception, $value) use (&$result) {
|
|
|
|
$result = $value;
|
|
|
|
};
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise->when($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testSuccessfulPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
|
|
|
public function testSlowPending() {
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$reason) {
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = new Pause(200);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = Amp\timeout($promise, 100);
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$callback = function ($exception, $value) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
};
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise->when($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Amp\TimeoutException::class, $reason);
|
|
|
|
}
|
2017-03-13 05:27:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSuccessfulPromise
|
|
|
|
*/
|
|
|
|
public function testReactPromise() {
|
|
|
|
Loop::run(function () {
|
|
|
|
$value = 1;
|
|
|
|
|
|
|
|
$promise = resolve($value);
|
|
|
|
|
|
|
|
$promise = Amp\timeout($promise, 100);
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
|
|
|
|
|
|
|
$callback = function ($exception, $value) use (&$result) {
|
|
|
|
$result = $value;
|
|
|
|
};
|
|
|
|
|
|
|
|
$promise->when($callback);
|
|
|
|
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
});
|
|
|
|
}
|
2017-03-14 22:15:36 +01:00
|
|
|
|
|
|
|
public function testNonPromise() {
|
|
|
|
$this->expectException(Amp\UnionTypeError::class);
|
|
|
|
Amp\timeout(42, 42);
|
|
|
|
}
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|