This commit is contained in:
2022-10-23 01:39:27 +02:00
parent 8c17aab483
commit 1929b84685
4130 changed files with 479334 additions and 0 deletions

View File

@@ -0,0 +1,97 @@
<?php
namespace React\Tests\EventLoop\Timer;
use React\Tests\EventLoop\TestCase;
abstract class AbstractTimerTest extends TestCase
{
abstract public function createLoop();
public function testAddTimer()
{
// usleep is intentionally high
$loop = $this->createLoop();
$loop->addTimer(0.001, $this->expectCallableOnce());
usleep(1000);
$loop->tick();
}
public function testAddPeriodicTimer()
{
$loop = $this->createLoop();
$loop->addPeriodicTimer(0.001, $this->expectCallableExactly(3));
usleep(1000);
$loop->tick();
usleep(1000);
$loop->tick();
usleep(1000);
$loop->tick();
}
public function testAddPeriodicTimerWithCancel()
{
$loop = $this->createLoop();
$timer = $loop->addPeriodicTimer(0.001, $this->expectCallableExactly(2));
usleep(1000);
$loop->tick();
usleep(1000);
$loop->tick();
$timer->cancel();
usleep(1000);
$loop->tick();
}
public function testAddPeriodicTimerCancelsItself()
{
$i = 0;
$loop = $this->createLoop();
$loop->addPeriodicTimer(0.001, function ($timer) use (&$i) {
$i++;
if ($i == 2) {
$timer->cancel();
}
});
usleep(1000);
$loop->tick();
usleep(1000);
$loop->tick();
usleep(1000);
$loop->tick();
$this->assertSame(2, $i);
}
public function testIsTimerActive()
{
$loop = $this->createLoop();
$timer = $loop->addPeriodicTimer(0.001, function () {});
$this->assertTrue($loop->isTimerActive($timer));
$timer->cancel();
$this->assertFalse($loop->isTimerActive($timer));
}
public function testMinimumIntervalOneMicrosecond()
{
$loop = $this->createLoop();
$timer = $loop->addTimer(0, function () {});
$this->assertEquals(0.000001, $timer->getInterval());
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace React\Tests\EventLoop\Timer;
use React\EventLoop\ExtEventLoop;
class ExtEventTimerTest extends AbstractTimerTest
{
public function createLoop()
{
if (!extension_loaded('event')) {
$this->markTestSkipped('ext-event tests skipped because ext-event is not installed.');
}
return new ExtEventLoop();
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace React\Tests\EventLoop\Timer;
use React\EventLoop\LibEvLoop;
class LibEvTimerTest extends AbstractTimerTest
{
public function createLoop()
{
if (!class_exists('libev\EventLoop')) {
$this->markTestSkipped('libev tests skipped because ext-libev is not installed.');
}
return new LibEvLoop();
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace React\Tests\EventLoop\Timer;
use React\EventLoop\LibEventLoop;
class LibEventTimerTest extends AbstractTimerTest
{
public function createLoop()
{
if (!function_exists('event_base_new')) {
$this->markTestSkipped('libevent tests skipped because ext-libevent is not installed.');
}
return new LibEventLoop();
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace React\Tests\EventLoop\Timer;
use React\EventLoop\StreamSelectLoop;
class StreamSelectTimerTest extends AbstractTimerTest
{
public function createLoop()
{
return new StreamSelectLoop();
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace React\Tests\EventLoop\Timer;
use React\Tests\EventLoop\TestCase;
use React\EventLoop\Timer\Timer;
use React\EventLoop\Timer\Timers;
class TimersTest extends TestCase
{
public function testBlockedTimer()
{
$loop = $this
->getMockBuilder('React\EventLoop\LoopInterface')
->getMock();
$timers = new Timers();
$timers->tick();
// simulate a bunch of processing on stream events,
// part of which schedules a future timer...
sleep(1);
$timers->add(new Timer($loop, 0.5, function () {
$this->fail("Timer shouldn't be called");
}));
$timers->tick();
}
}