1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 13:21:16 +01:00
amp/test/TimeoutTest.php

90 lines
2.2 KiB
PHP
Raw Normal View History

<?php
2016-07-12 11:20:06 -05:00
namespace Amp\Test;
use Amp;
2016-11-14 13:59:21 -06:00
use Amp\{ Failure, Pause, Success };
use AsyncInterop\{ Loop, Promise };
2016-07-12 11:20:06 -05:00
class TimeoutTest extends \PHPUnit_Framework_TestCase {
2016-11-14 13:59:21 -06:00
public function testSuccessfulPromise() {
2016-07-12 11:20:06 -05:00
Loop::execute(function () {
$value = 1;
2016-11-14 13:59:21 -06:00
$promise = new Success($value);
2016-07-12 11:20:06 -05:00
2016-11-14 13:59:21 -06:00
$promise = Amp\timeout($promise, 100);
$this->assertInstanceOf(Promise::class, $promise);
2016-07-12 11:20:06 -05:00
$callback = function ($exception, $value) use (&$result) {
$result = $value;
};
2016-11-14 13:59:21 -06:00
$promise->when($callback);
2016-07-12 11:20:06 -05:00
$this->assertSame($value, $result);
});
}
2016-11-14 13:59:21 -06:00
public function testFailedPromise() {
2016-07-12 11:20:06 -05:00
Loop::execute(function () {
$exception = new \Exception;
2016-11-14 13:59:21 -06:00
$promise = new Failure($exception);
2016-07-12 11:20:06 -05:00
2016-11-14 13:59:21 -06:00
$promise = Amp\timeout($promise, 100);
$this->assertInstanceOf(Promise::class, $promise);
2016-07-12 11:20:06 -05:00
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
2016-11-14 13:59:21 -06:00
$promise->when($callback);
2016-07-12 11:20:06 -05:00
$this->assertSame($exception, $reason);
});
}
2016-07-12 11:20:06 -05:00
/**
2016-11-14 13:59:21 -06:00
* @depends testSuccessfulPromise
2016-07-12 11:20:06 -05:00
*/
public function testFastPending() {
$value = 1;
Loop::execute(function () use (&$result, $value) {
2016-11-14 13:59:21 -06:00
$promise = new Pause(50, $value);
2016-07-12 11:20:06 -05:00
2016-11-14 13:59:21 -06:00
$promise = Amp\timeout($promise, 100);
$this->assertInstanceOf(Promise::class, $promise);
2016-07-12 11:20:06 -05:00
$callback = function ($exception, $value) use (&$result) {
$result = $value;
};
2016-11-14 13:59:21 -06:00
$promise->when($callback);
2016-07-12 11:20:06 -05:00
});
$this->assertSame($value, $result);
}
/**
2016-11-14 13:59:21 -06:00
* @depends testSuccessfulPromise
2016-07-12 11:20:06 -05:00
*/
public function testSlowPending() {
Loop::execute(function () use (&$reason) {
2016-11-14 13:59:21 -06:00
$promise = new Pause(200);
2016-07-12 11:20:06 -05:00
2016-11-14 13:59:21 -06:00
$promise = Amp\timeout($promise, 100);
$this->assertInstanceOf(Promise::class, $promise);
2016-07-12 11:20:06 -05:00
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
2016-11-14 13:59:21 -06:00
$promise->when($callback);
2016-07-12 11:20:06 -05:00
});
$this->assertInstanceOf(Amp\TimeoutException::class, $reason);
}
}