2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 23:39:25 +02:00
|
|
|
|
2015-07-21 04:24:51 +02:00
|
|
|
namespace Amp\Test;
|
|
|
|
|
2017-04-28 14:42:02 +02:00
|
|
|
use Amp\Delayed;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Loop;
|
2015-07-21 04:24:51 +02:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
class DelayedTest extends \PHPUnit\Framework\TestCase
|
|
|
|
{
|
|
|
|
public function testDelayed()
|
|
|
|
{
|
2016-07-19 07:05:40 +02:00
|
|
|
$time = 100;
|
|
|
|
$value = "test";
|
2017-11-29 13:36:50 +01:00
|
|
|
$start = \microtime(true);
|
2016-07-19 07:05:40 +02:00
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$result, $time, $value) {
|
2017-04-28 14:42:02 +02:00
|
|
|
$promise = new Delayed($time, $value);
|
2016-07-19 07:05:40 +02:00
|
|
|
|
|
|
|
$callback = function ($exception, $value) use (&$result) {
|
|
|
|
$result = $value;
|
|
|
|
};
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve($callback);
|
2015-07-21 04:28:51 +02:00
|
|
|
});
|
2016-07-19 07:05:40 +02:00
|
|
|
|
2017-11-29 13:36:50 +01:00
|
|
|
$this->assertGreaterThanOrEqual($time - 1 /* 1ms grace period */, (\microtime(true) - $start) * 1000);
|
2016-07-19 07:05:40 +02:00
|
|
|
$this->assertSame($value, $result);
|
2015-07-21 04:28:51 +02:00
|
|
|
}
|
2017-01-08 08:15:57 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testUnreference()
|
|
|
|
{
|
2017-01-08 08:15:57 +01:00
|
|
|
$time = 100;
|
|
|
|
$value = "test";
|
2017-11-29 13:36:50 +01:00
|
|
|
$start = \microtime(true);
|
2017-01-08 08:15:57 +01:00
|
|
|
|
|
|
|
$invoked = false;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$invoked, $time, $value) {
|
2017-04-28 14:42:02 +02:00
|
|
|
$promise = new Delayed($time, $value);
|
2017-01-08 08:15:57 +01:00
|
|
|
$promise->unreference();
|
|
|
|
|
|
|
|
$callback = function ($exception, $value) use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
};
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve($callback);
|
2017-01-08 08:15:57 +01:00
|
|
|
});
|
|
|
|
|
2017-11-29 13:36:50 +01:00
|
|
|
$this->assertLessThanOrEqual($time - 1 /* 1ms grace period */, (\microtime(true) - $start) * 1000);
|
2017-01-08 08:15:57 +01:00
|
|
|
$this->assertFalse($invoked);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testUnreference
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testReference()
|
|
|
|
{
|
2017-01-08 08:15:57 +01:00
|
|
|
$time = 100;
|
|
|
|
$value = "test";
|
2017-11-29 13:36:50 +01:00
|
|
|
$start = \microtime(true);
|
2017-01-08 08:15:57 +01:00
|
|
|
|
|
|
|
$invoked = false;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$invoked, $time, $value) {
|
2017-04-28 14:42:02 +02:00
|
|
|
$promise = new Delayed($time, $value);
|
2017-01-08 08:15:57 +01:00
|
|
|
$promise->unreference();
|
|
|
|
$promise->reference();
|
|
|
|
|
|
|
|
$callback = function ($exception, $value) use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
};
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve($callback);
|
2017-01-08 08:15:57 +01:00
|
|
|
});
|
|
|
|
|
2017-11-29 13:36:50 +01:00
|
|
|
$this->assertGreaterThanOrEqual($time - 1 /* 1ms grace period */, (\microtime(true) - $start) * 1000);
|
2017-01-08 08:15:57 +01:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
2015-07-21 04:24:51 +02:00
|
|
|
}
|