From df52bbc7bf082986def6113a7e6950cedbe192a2 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sat, 24 Dec 2022 18:49:23 +0100 Subject: [PATCH] Cleanup tests --- test/Fixtures.php | 16 +++++----------- test/LoopTest.php | 7 ++++--- test/ResumableTest.php | 27 ++++++++++++++++----------- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/test/Fixtures.php b/test/Fixtures.php index 71f1d76..df12244 100644 --- a/test/Fixtures.php +++ b/test/Fixtures.php @@ -9,11 +9,9 @@ namespace danog\Loop\Test; -use Amp\Future; use Amp\PHPUnit\AsyncTestCase; use danog\Loop\Test\Interfaces\BasicInterface; -use function Amp\async; use function Amp\delay; /** @@ -22,13 +20,6 @@ use function Amp\delay; abstract class Fixtures extends AsyncTestCase { const LOOP_NAME = 'TTTT'; - /** - * Check if promise has been resolved afterwards. - */ - protected static function isResolved(Future $promise): bool - { - return $promise->isComplete(); - } /** * Execute pre-start assertions. * @@ -52,11 +43,14 @@ abstract class Fixtures extends AsyncTestCase * * @param BasicInterface $loop Loop * @param bool $running Whether we should expect the loop to be running + * @param bool $running Whether we should actually start the loop by returning control to the event loop * */ - protected function assertAfterStart(BasicInterface $loop, bool $running = true): void + protected function assertAfterStart(BasicInterface $loop, bool $running = true, bool $start = true): void { - delay(0.001); + if ($start) { + delay(0.001); + } $this->assertTrue($loop->inited()); if ($running) { diff --git a/test/LoopTest.php b/test/LoopTest.php index f0de6a8..87c673f 100644 --- a/test/LoopTest.php +++ b/test/LoopTest.php @@ -43,13 +43,14 @@ class LoopTest extends Fixtures /** * Test basic exception in loop. * - * @param BasicInterface $loop Loop * * * @dataProvider provideBasicExceptions */ - /*public function testException(BasicInterface $loop): void + public function testException(BasicInterface $loop): void { + $this->markTestSkipped(); + $this->expectException(\RuntimeException::class); $this->assertPreStart($loop); @@ -61,7 +62,7 @@ class LoopTest extends Fixtures $this->assertEquals(1, $loop->startCounter()); $this->assertEquals(1, $loop->endCounter()); - }*/ + } /** * Provide loop implementations. diff --git a/test/ResumableTest.php b/test/ResumableTest.php index 8c0e856..125586e 100644 --- a/test/ResumableTest.php +++ b/test/ResumableTest.php @@ -14,6 +14,7 @@ use danog\Loop\ResumableLoop; use danog\Loop\ResumableSignalLoop; use danog\Loop\Test\Interfaces\ResumableInterface; use danog\Loop\Test\Traits\Resumable; +use Generator; use function Amp\delay; @@ -37,7 +38,7 @@ class ResumableTest extends Fixtures $this->assertAfterStart($loop); delay(0.010); - $this->assertTrue(self::isResolved($paused)); + $this->assertTrue($paused->isComplete()); delay(0.1); $this->assertFinal($loop); @@ -60,7 +61,7 @@ class ResumableTest extends Fixtures $this->assertTrue($loop->start()); delay(0.001); - $this->assertFalse(self::isResolved($paused)); // Did not pause + $this->assertFalse($paused->isComplete()); // Did not pause // Invert the order as the afterTest assertions will begin the test anew $this->assertFinal($loop); @@ -90,7 +91,7 @@ class ResumableTest extends Fixtures $this->assertAfterStart($loop); delay(0.001); - $this->assertTrue(self::isResolved($paused)); // Did pause + $this->assertTrue($paused->isComplete()); // Did pause $paused = $deferred ? $loop->resumeDefer() : $loop->resume(); if ($deferred) { @@ -100,7 +101,7 @@ class ResumableTest extends Fixtures $this->assertFinal($loop); delay(0.001); - $this->assertFalse(self::isResolved($paused)); // Did not pause again + $this->assertFalse($paused->isComplete()); // Did not pause again } /** @@ -124,18 +125,22 @@ class ResumableTest extends Fixtures $this->assertAfterStart($loop); delay(0.001); - $this->assertTrue(self::isResolved($paused1)); // Did pause - $this->assertTrue(self::isResolved($paused2)); // Did pause + $this->assertTrue($paused1->isComplete()); // Did pause + $this->assertTrue($paused2->isComplete()); // Did pause $paused1 = $loop->resumeDeferOnce(); $paused2 = $loop->resumeDeferOnce(); - $this->assertAfterStart($loop); + + $this->assertFalse($paused1->isComplete()); // Did not pause again + $this->assertFalse($paused2->isComplete()); // Did not pause again + + $this->assertAfterStart($loop, true, false); delay(0.001); $this->assertFinal($loop); delay(0.001); - $this->assertFalse(self::isResolved($paused1)); // Did not pause again - $this->assertFalse(self::isResolved($paused2)); // Did not pause again + $this->assertFalse($paused1->isComplete()); // Did not pause again + $this->assertFalse($paused2->isComplete()); // Did not pause again } /** @@ -160,14 +165,14 @@ class ResumableTest extends Fixtures * * */ - public function provideResumableInterval(): void + public function provideResumableInterval(): Generator { foreach ([true, false] as $deferred) { foreach ([10000, null] as $interval) { foreach ($this->provideResumable() as $params) { $params[] = $interval; $params[] = $deferred; - $params; + yield $params; } } }