2016-12-29 14:09:49 -06:00
|
|
|
<?php
|
2016-08-16 16:39:25 -05:00
|
|
|
|
2015-07-20 22:24:51 -04:00
|
|
|
namespace Amp\Test;
|
|
|
|
|
2017-04-28 14:42:02 +02:00
|
|
|
use Amp\Delayed;
|
2020-09-27 22:19:52 -05:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
|
|
|
use function Amp\await;
|
2015-07-20 22:24:51 -04:00
|
|
|
|
2020-09-27 22:19:52 -05:00
|
|
|
class DelayedTest extends AsyncTestCase
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-27 22:19:52 -05:00
|
|
|
public function testDelayed(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-07-19 00:05:40 -05:00
|
|
|
$time = 100;
|
|
|
|
$value = "test";
|
2017-11-29 13:36:50 +01:00
|
|
|
$start = \microtime(true);
|
2016-07-19 00:05:40 -05:00
|
|
|
|
2020-09-27 22:19:52 -05:00
|
|
|
$promise = new Delayed($time, $value);
|
2016-07-19 00:05:40 -05:00
|
|
|
|
2020-09-27 22:19:52 -05:00
|
|
|
$this->assertSame($value, await($promise));
|
2016-07-19 00:05:40 -05:00
|
|
|
|
2017-11-29 13:36:50 +01:00
|
|
|
$this->assertGreaterThanOrEqual($time - 1 /* 1ms grace period */, (\microtime(true) - $start) * 1000);
|
2015-07-20 22:28:51 -04:00
|
|
|
}
|
2017-01-08 01:15:57 -06:00
|
|
|
|
2020-09-27 22:19:52 -05:00
|
|
|
public function testReference(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2017-01-08 01:15:57 -06:00
|
|
|
$time = 100;
|
|
|
|
$value = "test";
|
2017-11-29 13:36:50 +01:00
|
|
|
$start = \microtime(true);
|
2017-01-08 01:15:57 -06:00
|
|
|
|
2020-09-27 22:19:52 -05:00
|
|
|
$promise = new Delayed($time, $value);
|
|
|
|
$promise->unreference();
|
|
|
|
$promise->reference();
|
2017-01-08 01:15:57 -06:00
|
|
|
|
2020-09-27 22:19:52 -05:00
|
|
|
await($promise);
|
2017-01-08 01:15:57 -06:00
|
|
|
|
2017-11-29 13:36:50 +01:00
|
|
|
$this->assertGreaterThanOrEqual($time - 1 /* 1ms grace period */, (\microtime(true) - $start) * 1000);
|
2017-01-08 01:15:57 -06:00
|
|
|
}
|
2015-07-20 22:24:51 -04:00
|
|
|
}
|