1
0
mirror of https://github.com/danog/loop.git synced 2024-12-04 02:07:56 +01:00
loop/test/LoopTest.php

180 lines
4.7 KiB
PHP
Raw Normal View History

2022-12-24 14:36:39 +01:00
<?php declare(strict_types=1);
2020-07-21 18:06:19 +02:00
/**
* Loop test.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/MIT MIT
*/
namespace danog\Loop\Test;
2020-07-21 21:45:22 +02:00
use danog\Loop\Loop;
2020-07-21 19:53:57 +02:00
use danog\Loop\Test\Interfaces\BasicInterface;
2020-07-21 18:06:19 +02:00
use danog\Loop\Test\Traits\Basic;
2020-07-21 19:53:57 +02:00
use danog\Loop\Test\Traits\BasicException;
2023-01-24 12:23:10 +01:00
use Revolt\EventLoop;
use RuntimeException;
2020-07-21 18:06:19 +02:00
use function Amp\delay;
2020-07-21 19:53:57 +02:00
class LoopTest extends Fixtures
2020-07-21 18:06:19 +02:00
{
2023-01-24 12:23:10 +01:00
/**
* Execute pre-start assertions.
*/
2023-01-24 15:49:37 +01:00
private function assertPreStart(BasicInterface&Loop $loop): void
2023-01-24 03:49:03 +01:00
{
2023-01-24 12:23:10 +01:00
$this->assertEquals(self::LOOP_NAME, "$loop");
$this->assertFalse($loop->isRunning());
$this->assertFalse($loop->ran());
$this->assertFalse($loop->inited());
$this->assertEquals(0, $loop->startCounter());
$this->assertEquals(0, $loop->endCounter());
}
/**
* Execute after-start assertions.
*/
2023-01-24 15:49:37 +01:00
private function assertAfterStart(BasicInterface&Loop $loop, int $prevRun = 0): void
2023-01-24 12:23:10 +01:00
{
2023-01-24 15:49:37 +01:00
self::waitTick();
2023-01-24 12:23:10 +01:00
$this->assertTrue($loop->inited());
2023-01-24 15:49:37 +01:00
if ($prevRun === 0) {
2023-01-24 12:23:10 +01:00
$this->assertFalse($loop->ran());
2023-01-24 15:49:37 +01:00
} else {
$this->assertTrue($loop->ran());
2023-01-24 12:23:10 +01:00
}
2023-01-24 15:49:37 +01:00
$this->assertTrue($loop->isRunning());
2023-01-24 12:23:10 +01:00
2023-01-24 15:49:37 +01:00
$this->assertEquals($prevRun+1, $loop->startCounter());
$this->assertEquals($prevRun, $loop->endCounter());
$this->assertFalse($loop->start());
$this->assertFalse($loop->isPaused());
2023-01-24 12:23:10 +01:00
}
/**
* Execute final assertions.
*/
2023-01-24 15:49:37 +01:00
private function assertFinal(BasicInterface&Loop $loop, int $count = 1): void
2023-01-24 12:23:10 +01:00
{
$this->assertTrue($loop->ran());
$this->assertFalse($loop->isRunning());
$this->assertTrue($loop->inited());
2023-01-24 15:49:37 +01:00
$this->assertEquals($count, $loop->startCounter());
$this->assertEquals($count, $loop->endCounter());
2023-01-24 03:49:03 +01:00
}
2020-07-21 18:06:19 +02:00
/**
* Test basic loop.
*/
2023-01-23 16:17:49 +01:00
public function testLoop(): void
2020-07-21 18:06:19 +02:00
{
2023-01-23 16:17:49 +01:00
$loop = new class() extends Loop implements BasicInterface {
use Basic;
};
$this->assertPreStart($loop);
$this->assertTrue($loop->start());
$this->assertAfterStart($loop);
delay(0.110);
$this->assertFinal($loop);
}
/**
* Test basic loop.
*/
2023-01-24 15:49:37 +01:00
public function testLoopStopFromOutside(): void
2023-01-23 16:17:49 +01:00
{
$loop = new class() extends Loop implements BasicInterface {
use Basic;
/**
* Loop implementation.
*/
public function loop(): ?float
{
$this->inited = true;
delay(0.1);
$this->ran = true;
2023-01-24 03:49:03 +01:00
return 1000.0;
2023-01-23 16:17:49 +01:00
}
};
2020-07-21 19:12:11 +02:00
$this->assertPreStart($loop);
$this->assertTrue($loop->start());
$this->assertAfterStart($loop);
2023-01-24 15:49:37 +01:00
$this->assertTrue($loop->stop());
2022-12-24 17:30:32 +01:00
delay(0.110);
2020-07-21 19:12:11 +02:00
$this->assertFinal($loop);
}
2023-01-24 15:49:37 +01:00
/**
* Test basic loop.
*/
public function testLoopStopFromOutsideRestart(): void
{
$loop = new class() extends Loop implements BasicInterface {
use Basic;
/**
* Loop implementation.
*/
public function loop(): ?float
{
$this->inited = true;
delay(0.1);
$this->ran = true;
return 1000.0;
}
};
$this->assertPreStart($loop);
$this->assertTrue($loop->start());
$this->assertAfterStart($loop);
EventLoop::queue(function () use ($loop): void {
$this->assertTrue($loop->stop());
});
self::waitTick();
$this->assertTrue($loop->start());
$this->assertAfterStart($loop, 1);
$this->assertTrue($loop->stop());
delay(0.110);
$this->assertFinal($loop, 2);
}
2020-07-21 19:12:11 +02:00
/**
2020-07-21 19:53:57 +02:00
* Test basic exception in loop.
2020-07-21 19:12:11 +02:00
*/
2023-01-23 16:17:49 +01:00
public function testException(): void
2020-07-21 19:12:11 +02:00
{
2023-01-23 16:17:49 +01:00
$loop = new class() extends Loop implements BasicInterface {
use BasicException;
};
2023-01-24 12:23:10 +01:00
$e_thrown = null;
EventLoop::setErrorHandler(function (\RuntimeException $e) use (&$e_thrown): void {
$e_thrown = $e;
});
2020-07-21 18:06:19 +02:00
2020-07-21 19:53:57 +02:00
$this->assertPreStart($loop);
$this->assertTrue($loop->start());
2023-01-24 03:49:03 +01:00
self::waitTick();
2020-07-21 18:06:19 +02:00
$this->assertFalse($loop->isRunning());
2020-07-21 19:12:11 +02:00
$this->assertTrue($loop->inited());
2020-07-21 21:45:22 +02:00
2020-07-21 19:12:11 +02:00
$this->assertEquals(1, $loop->startCounter());
$this->assertEquals(1, $loop->endCounter());
2023-01-24 12:23:10 +01:00
$this->assertInstanceOf(RuntimeException::class, $e_thrown);
2023-01-24 12:31:57 +01:00
EventLoop::setErrorHandler(null);
2022-12-24 18:49:23 +01:00
}
2020-07-21 18:06:19 +02:00
}