1
0
mirror of https://github.com/danog/amp.git synced 2024-11-30 04:29:08 +01:00
amp/test/DelayedTest.php
2017-11-29 13:36:50 +01:00

74 lines
2.0 KiB
PHP

<?php
namespace Amp\Test;
use Amp\Delayed;
use Amp\Loop;
class DelayedTest extends \PHPUnit\Framework\TestCase {
public function testDelayed() {
$time = 100;
$value = "test";
$start = \microtime(true);
Loop::run(function () use (&$result, $time, $value) {
$promise = new Delayed($time, $value);
$callback = function ($exception, $value) use (&$result) {
$result = $value;
};
$promise->onResolve($callback);
});
$this->assertGreaterThanOrEqual($time - 1 /* 1ms grace period */, (\microtime(true) - $start) * 1000);
$this->assertSame($value, $result);
}
public function testUnreference() {
$time = 100;
$value = "test";
$start = \microtime(true);
$invoked = false;
Loop::run(function () use (&$invoked, $time, $value) {
$promise = new Delayed($time, $value);
$promise->unreference();
$callback = function ($exception, $value) use (&$invoked) {
$invoked = true;
};
$promise->onResolve($callback);
});
$this->assertLessThanOrEqual($time - 1 /* 1ms grace period */, (\microtime(true) - $start) * 1000);
$this->assertFalse($invoked);
}
/**
* @depends testUnreference
*/
public function testReference() {
$time = 100;
$value = "test";
$start = \microtime(true);
$invoked = false;
Loop::run(function () use (&$invoked, $time, $value) {
$promise = new Delayed($time, $value);
$promise->unreference();
$promise->reference();
$callback = function ($exception, $value) use (&$invoked) {
$invoked = true;
};
$promise->onResolve($callback);
});
$this->assertGreaterThanOrEqual($time - 1 /* 1ms grace period */, (\microtime(true) - $start) * 1000);
$this->assertTrue($invoked);
}
}