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-03-10 21:31:57 +01:00
|
|
|
use Amp\Loop;
|
2017-04-23 14:39:19 +02:00
|
|
|
use Amp\Pause;
|
2015-07-21 04:24:51 +02:00
|
|
|
|
2017-03-11 14:57:03 +01:00
|
|
|
class PauseTest extends \PHPUnit\Framework\TestCase {
|
2016-07-19 07:05:40 +02:00
|
|
|
public function testPause() {
|
|
|
|
$time = 100;
|
|
|
|
$value = "test";
|
|
|
|
$start = microtime(true);
|
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$result, $time, $value) {
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = new Pause($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-03-14 22:18:47 +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
|
|
|
|
|
|
|
public function testUnreference() {
|
|
|
|
$time = 100;
|
|
|
|
$value = "test";
|
|
|
|
$start = microtime(true);
|
|
|
|
|
|
|
|
$invoked = false;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$invoked, $time, $value) {
|
2017-01-08 08:15:57 +01:00
|
|
|
$promise = new Pause($time, $value);
|
|
|
|
$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-03-14 22:18:47 +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
|
|
|
|
*/
|
|
|
|
public function testReference() {
|
|
|
|
$time = 100;
|
|
|
|
$value = "test";
|
|
|
|
$start = microtime(true);
|
|
|
|
|
|
|
|
$invoked = false;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$invoked, $time, $value) {
|
2017-01-08 08:15:57 +01:00
|
|
|
$promise = new Pause($time, $value);
|
|
|
|
$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-04-26 21:37:09 +02: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
|
|
|
}
|