From 93e38171ee7b40d6ec4b84f3346974829f2e46c1 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 22 Jan 2023 10:20:28 +0100 Subject: [PATCH] Improve coverage --- test/Interfaces/SimpleSignalInterface.php | 31 ++++++++++++ test/SignalTest.php | 25 +++++---- test/Traits/Logging.php | 2 + test/Traits/SignalSimple.php | 62 +++++++++++++++++++++++ 4 files changed, 110 insertions(+), 10 deletions(-) create mode 100644 test/Interfaces/SimpleSignalInterface.php create mode 100644 test/Traits/SignalSimple.php diff --git a/test/Interfaces/SimpleSignalInterface.php b/test/Interfaces/SimpleSignalInterface.php new file mode 100644 index 0000000..6966eeb --- /dev/null +++ b/test/Interfaces/SimpleSignalInterface.php @@ -0,0 +1,31 @@ + + * @copyright 2016-2020 Daniil Gentili + * @license https://opensource.org/licenses/MIT MIT + */ + +namespace danog\Loop\Test\Interfaces; + +use danog\Loop\Interfaces\SignalLoopInterface; + +/** + * Signal loop test interface. + * + * @author Daniil Gentili + */ +interface SimpleSignalInterface extends BasicInterface, SignalLoopInterface +{ + /** + * Get signaled payload. + * + */ + public function getPayload(); + /** + * Get signaled exception. + */ + public function getException(): ?\Throwable; +} diff --git a/test/SignalTest.php b/test/SignalTest.php index fa54b6c..629c7c2 100644 --- a/test/SignalTest.php +++ b/test/SignalTest.php @@ -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 { - $loop->setInterval(500); // Wait 0.5 seconds before returning null + 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); - $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 + 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; }], diff --git a/test/Traits/Logging.php b/test/Traits/Logging.php index df17a59..8004861 100644 --- a/test/Traits/Logging.php +++ b/test/Traits/Logging.php @@ -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++; } diff --git a/test/Traits/SignalSimple.php b/test/Traits/SignalSimple.php new file mode 100644 index 0000000..51c5932 --- /dev/null +++ b/test/Traits/SignalSimple.php @@ -0,0 +1,62 @@ + + * @copyright 2016-2020 Daniil Gentili + * @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; + } +}