2017-03-12 21:02:26 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
|
|
|
use Amp\Loop;
|
|
|
|
|
2019-05-14 21:37:08 +02:00
|
|
|
class LoopTest extends BaseTest
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
|
|
|
public function testDelayWithNegativeDelay()
|
|
|
|
{
|
2017-03-14 18:44:44 +01:00
|
|
|
$this->expectException(\Error::class);
|
2017-03-12 21:02:26 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
Loop::delay(-1, function () {
|
|
|
|
});
|
2017-03-12 21:02:26 +01:00
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testRepeatWithNegativeInterval()
|
|
|
|
{
|
2017-03-14 18:44:44 +01:00
|
|
|
$this->expectException(\Error::class);
|
2017-03-12 21:02:26 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
Loop::repeat(-1, function () {
|
|
|
|
});
|
2017-03-12 21:02:26 +01:00
|
|
|
}
|
2017-03-15 08:40:58 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testOnReadable()
|
|
|
|
{
|
2017-03-15 08:40:58 +01:00
|
|
|
Loop::run(function () {
|
2019-09-21 23:17:45 +02:00
|
|
|
$ends = \stream_socket_pair(
|
|
|
|
\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX,
|
|
|
|
STREAM_SOCK_STREAM,
|
|
|
|
STREAM_IPPROTO_IP
|
|
|
|
);
|
2017-11-29 13:36:50 +01:00
|
|
|
\fwrite($ends[0], "trigger readability watcher");
|
2017-03-15 08:40:58 +01:00
|
|
|
|
2019-05-14 21:37:08 +02:00
|
|
|
Loop::onReadable($ends[1], function ($watcher) {
|
2017-03-15 08:40:58 +01:00
|
|
|
$this->assertTrue(true);
|
2019-05-14 21:37:08 +02:00
|
|
|
Loop::cancel($watcher);
|
2017-03-15 08:40:58 +01:00
|
|
|
Loop::stop();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testOnWritable()
|
|
|
|
{
|
2017-03-15 08:40:58 +01:00
|
|
|
Loop::run(function () {
|
2019-05-14 21:37:08 +02:00
|
|
|
Loop::onWritable(STDOUT, function ($watcher) {
|
2017-03-15 08:40:58 +01:00
|
|
|
$this->assertTrue(true);
|
2019-05-14 21:37:08 +02:00
|
|
|
Loop::cancel($watcher);
|
2017-03-15 08:40:58 +01:00
|
|
|
Loop::stop();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-06 03:32:57 +01:00
|
|
|
public function testNow()
|
|
|
|
{
|
|
|
|
Loop::run(function () {
|
|
|
|
$now = Loop::now();
|
|
|
|
Loop::delay(100, function () use ($now) {
|
|
|
|
$now += 100;
|
|
|
|
$new = Loop::now();
|
|
|
|
|
|
|
|
// Allow a few milliseconds of inaccuracy.
|
2018-12-11 11:31:37 +01:00
|
|
|
$this->assertGreaterThanOrEqual($now - 1, $new);
|
2019-09-21 23:17:45 +02:00
|
|
|
$this->assertLessThanOrEqual($now + 100, $new);
|
2018-01-06 03:32:57 +01:00
|
|
|
|
|
|
|
// Same time should be returned from later call.
|
|
|
|
$this->assertSame($new, Loop::now());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testGet()
|
|
|
|
{
|
2017-03-15 08:40:58 +01:00
|
|
|
$this->assertInstanceOf(Loop\Driver::class, Loop::get());
|
|
|
|
}
|
|
|
|
|
2018-01-06 03:32:57 +01:00
|
|
|
public function testGetInfo()
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2017-03-15 08:40:58 +01:00
|
|
|
$this->assertSame(Loop::get()->getInfo(), Loop::getInfo());
|
|
|
|
}
|
2017-04-24 15:39:08 +02:00
|
|
|
}
|