1
0
mirror of https://github.com/danog/amp.git synced 2024-12-11 17:09:40 +01:00
amp/test/TimeoutTest.php

127 lines
3.0 KiB
PHP
Raw Normal View History

<?php
2016-07-12 18:20:06 +02:00
namespace Amp\Test;
use Amp\Delayed;
use Amp\Failure;
use Amp\Loop;
use Amp\Promise;
2017-04-23 14:39:19 +02:00
use Amp\Success;
use function React\Promise\resolve;
2016-07-12 18:20:06 +02:00
class TimeoutTest extends BaseTest
2018-06-18 20:00:01 +02:00
{
public function testSuccessfulPromise()
{
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
$promise = Promise\timeout($promise, 100);
2016-11-14 20:59:21 +01:00
$this->assertInstanceOf(Promise::class, $promise);
2016-07-12 18:20:06 +02:00
$callback = function ($exception, $value) use (&$result) {
$result = $value;
};
$promise->onResolve($callback);
2016-07-12 18:20:06 +02:00
$this->assertSame($value, $result);
});
}
2018-06-18 20:00:01 +02:00
public function testFailedPromise()
{
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
$promise = Promise\timeout($promise, 100);
2016-11-14 20:59:21 +01:00
$this->assertInstanceOf(Promise::class, $promise);
2016-07-12 18:20:06 +02:00
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
$promise->onResolve($callback);
2016-07-12 18:20:06 +02:00
$this->assertSame($exception, $reason);
});
}
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
*/
2018-06-18 20:00:01 +02:00
public function testFastPending()
{
2016-07-12 18:20:06 +02:00
$value = 1;
Loop::run(function () use (&$result, $value) {
$promise = new Delayed(50, $value);
2016-07-12 18:20:06 +02:00
$promise = Promise\timeout($promise, 100);
2016-11-14 20:59:21 +01:00
$this->assertInstanceOf(Promise::class, $promise);
2016-07-12 18:20:06 +02:00
$callback = function ($exception, $value) use (&$result) {
$result = $value;
};
$promise->onResolve($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
*/
2018-06-18 20:00:01 +02:00
public function testSlowPending()
{
Loop::run(function () use (&$reason) {
$promise = new Delayed(200);
2016-07-12 18:20:06 +02:00
$promise = Promise\timeout($promise, 100);
2016-11-14 20:59:21 +01:00
$this->assertInstanceOf(Promise::class, $promise);
2016-07-12 18:20:06 +02:00
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
$promise->onResolve($callback);
2016-07-12 18:20:06 +02:00
});
$this->assertInstanceOf(\Amp\TimeoutException::class, $reason);
2016-07-12 18:20:06 +02:00
}
/**
* @depends testSuccessfulPromise
*/
2018-06-18 20:00:01 +02:00
public function testReactPromise()
{
Loop::run(function () {
$value = 1;
$promise = resolve($value);
$promise = Promise\timeout($promise, 100);
$this->assertInstanceOf(Promise::class, $promise);
$callback = function ($exception, $value) use (&$result) {
$result = $value;
};
$promise->onResolve($callback);
$this->assertSame($value, $result);
});
}
2017-03-14 22:15:36 +01:00
2018-06-18 20:00:01 +02:00
public function testNonPromise()
{
2017-04-23 19:08:40 +02:00
$this->expectException(\TypeError::class);
Promise\timeout(42, 42);
2017-03-14 22:15:36 +01:00
}
2016-07-12 18:20:06 +02:00
}