mirror of
https://github.com/danog/amp.git
synced 2025-01-23 05:41:25 +01:00
90 lines
2.1 KiB
PHP
90 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace Amp\Test;
|
|
|
|
use Amp\Deferred;
|
|
use Amp\Loop;
|
|
use Amp\PHPUnit\AsyncTestCase;
|
|
use function Amp\await;
|
|
|
|
class LoopTest extends AsyncTestCase
|
|
{
|
|
public function testDelayWithNegativeDelay(): void
|
|
{
|
|
$this->expectException(\Error::class);
|
|
|
|
Loop::delay(-1, function () {
|
|
});
|
|
}
|
|
|
|
public function testRepeatWithNegativeInterval(): void
|
|
{
|
|
$this->expectException(\Error::class);
|
|
|
|
Loop::repeat(-1, function () {
|
|
});
|
|
}
|
|
|
|
public function testOnReadable(): void
|
|
{
|
|
$ends = \stream_socket_pair(
|
|
\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX,
|
|
STREAM_SOCK_STREAM,
|
|
STREAM_IPPROTO_IP
|
|
);
|
|
\fwrite($ends[0], "trigger readability watcher");
|
|
|
|
$deferred = new Deferred;
|
|
|
|
Loop::onReadable($ends[1], function ($watcher) use ($deferred): void {
|
|
$this->assertTrue(true);
|
|
Loop::cancel($watcher);
|
|
$deferred->resolve();
|
|
});
|
|
|
|
await($deferred->promise());
|
|
}
|
|
|
|
public function testOnWritable()
|
|
{
|
|
$deferred = new Deferred;
|
|
|
|
Loop::onWritable(STDOUT, function ($watcher) use ($deferred): void {
|
|
$this->assertTrue(true);
|
|
Loop::cancel($watcher);
|
|
$deferred->resolve();
|
|
});
|
|
|
|
await($deferred->promise());
|
|
}
|
|
|
|
public function testNow(): void
|
|
{
|
|
$deferred = new Deferred;
|
|
|
|
$now = Loop::now();
|
|
Loop::delay(100, function () use ($now, $deferred): void {
|
|
$now += 100;
|
|
$new = Loop::now();
|
|
|
|
// Allow a few milliseconds of inaccuracy.
|
|
$this->assertGreaterThanOrEqual($now - 1, $new);
|
|
$this->assertLessThanOrEqual($now + 100, $new);
|
|
|
|
$deferred->resolve();
|
|
});
|
|
|
|
await($deferred->promise());
|
|
}
|
|
|
|
public function testGet(): void
|
|
{
|
|
$this->assertInstanceOf(Loop\Driver::class, Loop::get());
|
|
}
|
|
|
|
public function testGetInfo(): void
|
|
{
|
|
$this->assertSame(Loop::get()->getInfo(), Loop::getInfo());
|
|
}
|
|
}
|