1
0
mirror of https://github.com/danog/loop.git synced 2024-11-30 04:19:04 +01:00

Improve coverage

This commit is contained in:
Daniil Gentili 2023-01-22 10:20:28 +01:00
parent 2556e4dcb0
commit 93e38171ee
4 changed files with 110 additions and 10 deletions

View File

@ -0,0 +1,31 @@
<?php declare(strict_types=1);
/**
* Signal loop test interface.
*
* @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\Interfaces;
use danog\Loop\Interfaces\SignalLoopInterface;
/**
* Signal loop test interface.
*
* @author Daniil Gentili <daniil@daniil.it>
*/
interface SimpleSignalInterface extends BasicInterface, SignalLoopInterface
{
/**
* Get signaled payload.
*
*/
public function getPayload();
/**
* Get signaled exception.
*/
public function getException(): ?\Throwable;
}

View File

@ -13,7 +13,9 @@ use Amp\Loop;
use danog\Loop\ResumableSignalLoop;
use danog\Loop\SignalLoop;
use danog\Loop\Test\Interfaces\SignalInterface;
use danog\Loop\Test\Interfaces\SimpleSignalInterface;
use danog\Loop\Test\Traits\Signal;
use danog\Loop\Test\Traits\SignalSimple;
use function Amp\delay;
@ -22,15 +24,13 @@ class SignalTest extends Fixtures
/**
* Test signaling loop.
*
* @param SignalInterface $loop Loop
*
*
*
* @dataProvider provideSignal
*/
public function testSignal(SignalInterface $loop): void
public function testSignal(SignalInterface|SimpleSignalInterface $loop): void
{
if ($loop instanceof SignalInterface) {
$loop->setInterval(500); // Wait 0.5 seconds before returning null
}
$this->assertPreStart($loop);
$this->assertTrue($loop->start());
@ -62,10 +62,12 @@ class SignalTest extends Fixtures
$this->assertEquals($obj, $loop->getPayload());
$this->assertAfterStart($loop);
if ($loop instanceof SignalInterface) {
$loop->setInterval(100); // Wait 0.1 seconds before returning null
$loop->signal(true); // Move along loop to apply new interval
delay(0.110);
$this->assertNull($loop->getPayload()); // Result of sleep
}
$loop->signal($e = new \RuntimeException('Test'));
delay(0.001);
@ -81,6 +83,9 @@ class SignalTest extends Fixtures
public function provideSignal(): array
{
return [
[new class() extends SignalLoop implements SimpleSignalInterface {
use SignalSimple;
}],
[new class() extends SignalLoop implements SignalInterface {
use Signal;
}],

View File

@ -26,6 +26,7 @@ trait Logging
*/
final protected function startedLoop(): void
{
parent::startedLoop();
$this->startCounter++;
}
/**
@ -34,6 +35,7 @@ trait Logging
*/
final protected function exitedLoop(): void
{
parent::exitedLoop();
$this->endCounter++;
}

View File

@ -0,0 +1,62 @@
<?php declare(strict_types=1);
/**
* Resumable test trait.
*
* @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\Traits;
use danog\Loop\Interfaces\ResumableLoopInterface;
use function Amp\async;
use function Amp\delay;
trait SignalSimple
{
use Basic;
/**
* Signaled payload.
*
*/
private $payload;
/**
* Signaled exception.
*
* @var ?\Throwable
*/
private $exception;
/**
* Get signaled payload.
*
*/
public function getPayload()
{
return $this->payload;
}
/**
* Get signaled exception.
*/
public function getException(): ?\Throwable
{
return $this->exception;
}
/**
* Loop implementation.
*
*/
public function loop(): void
{
$this->inited = true;
try {
while (true) {
$this->payload = $this->waitSignal();
}
} catch (\Throwable $e) {
$this->exception = $e;
}
$this->ran = true;
}
}