1
0
mirror of https://github.com/danog/amp.git synced 2025-01-23 05:41:25 +01:00
amp/test/DelayedTest.php
2020-10-02 13:40:29 -05:00

58 lines
1.3 KiB
PHP

<?php
namespace Amp\Test;
use Amp\Delayed;
use Amp\PHPUnit\AsyncTestCase;
use function Amp\await;
class DelayedTest extends AsyncTestCase
{
public function testDelayed(): void
{
$time = 100;
$value = "test";
$start = \microtime(true);
$promise = new Delayed($time, $value);
$this->assertSame($value, await($promise));
$this->assertGreaterThanOrEqual($time - 1 /* 1ms grace period */, (\microtime(true) - $start) * 1000);
}
public function testUnreference(): void
{
$time = 100;
$value = "test";
$promise = new Delayed($time, $value);
$promise->unreference();
$this->ignoreLoopWatchers();
$this->expectException(\FiberError::class);
$this->expectExceptionMessage("Scheduler ended");
await($promise);
}
/**
* @depends testUnreference
*/
public function testReference(): void
{
$time = 100;
$value = "test";
$start = \microtime(true);
$promise = new Delayed($time, $value);
$promise->unreference();
$promise->reference();
await($promise);
$this->assertGreaterThanOrEqual($time - 1 /* 1ms grace period */, (\microtime(true) - $start) * 1000);
}
}