From da08301a91568cfe7542c59371f6cf5dbe1f562b Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 23 Jan 2023 00:38:49 +0100 Subject: [PATCH] Fix tests --- lib/Loop.php | 22 +++++++++++++--------- test/GenericTest.php | 8 ++++---- test/LoopTest.php | 5 ++--- test/PeriodicTest.php | 6 +++--- test/Traits/Basic.php | 3 ++- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/lib/Loop.php b/lib/Loop.php index 6612f09..8f5b649 100644 --- a/lib/Loop.php +++ b/lib/Loop.php @@ -43,7 +43,7 @@ abstract class Loop implements Stringable /** * Resume deferred ID. */ - private ?string $resumeDeferred = null; + private ?string $resumeImmediate = null; /** * Report pause, can be overriden for logging. @@ -80,8 +80,8 @@ abstract class Loop implements Stringable if (!$this->running) { return false; } - $this->running = false; $this->resume(); + $this->running = false; return true; } abstract protected function loop(): ?float; @@ -109,7 +109,7 @@ abstract class Loop implements Stringable if ($timeout === self::PAUSE) { $this->reportPause(0.0); } else { - if (!$this->resumeDeferred) { + if (!$this->resumeImmediate) { \assert($this->resumeTimer === null); $this->resumeTimer = EventLoop::delay($timeout, function (): void { $this->resumeTimer = null; @@ -128,8 +128,8 @@ abstract class Loop implements Stringable EventLoop::cancel($storedWatcherId); $this->resumeTimer = null; } - if ($this->resumeDeferred) { - $storedWatcherId = $this->resumeDeferred; + if ($this->resumeImmediate) { + $storedWatcherId = $this->resumeImmediate; EventLoop::cancel($storedWatcherId); $this->resumeTimer = null; } @@ -159,19 +159,23 @@ abstract class Loop implements Stringable /** * Resume the loop. + * + * @return bool Returns false if the loop is not paused. */ - public function resume(): void + public function resume(): bool { - if (!$this->resumeDeferred && $this->running && $this->paused) { + if (!$this->resumeImmediate && $this->running && $this->paused) { if ($this->resumeTimer) { $timer = $this->resumeTimer; $this->resumeTimer = null; EventLoop::cancel($timer); } - $this->resumeDeferred = EventLoop::defer(function (): void { - $this->resumeDeferred = null; + $this->resumeImmediate = EventLoop::defer(function (): void { + $this->resumeImmediate = null; $this->loopInternal(); }); + return true; } + return false; } } diff --git a/test/GenericTest.php b/test/GenericTest.php index 57e65ba..2aadb10 100644 --- a/test/GenericTest.php +++ b/test/GenericTest.php @@ -141,7 +141,7 @@ class GenericTest extends AsyncTestCase $this->assertEquals(0, $loop->getLastPause()); $pauseTime = 0.1; - $loop->resume(); + $this->assertTrue($loop->resume()); delay(0.002); $this->fixtureStarted($loop); @@ -163,7 +163,7 @@ class GenericTest extends AsyncTestCase $this->assertEquals(3, $loop->getPauseCount()); $this->assertEquals(0.1, $loop->getLastPause()); - $loop->resume(); + $this->assertTrue($loop->resume()); delay(0.002); $this->assertEquals(4, $runCount); @@ -171,10 +171,10 @@ class GenericTest extends AsyncTestCase $this->assertEquals(0.1, $loop->getLastPause()); if ($stopSig) { - $loop->stop(); + $this->assertTrue($loop->stop()); } else { $pauseTime = GenericLoop::STOP; - $loop->resume(); + $this->assertTrue($loop->resume()); } delay(0.002); $this->assertEquals($stopSig ? 4 : 5, $runCount); diff --git a/test/LoopTest.php b/test/LoopTest.php index 6f98b94..6e1937c 100644 --- a/test/LoopTest.php +++ b/test/LoopTest.php @@ -9,6 +9,7 @@ namespace danog\Loop\Test; +use Amp\PHPUnit\UnhandledException; use danog\Loop\Loop; use danog\Loop\Test\Interfaces\BasicInterface; use danog\Loop\Test\Traits\Basic; @@ -40,9 +41,7 @@ class LoopTest extends Fixtures */ public function testException(Loop&BasicInterface $loop): void { - $this->markTestSkipped(); - - $this->expectException(\RuntimeException::class); + $this->expectException(UnhandledException::class); $this->assertPreStart($loop); $this->assertTrue($loop->start()); diff --git a/test/PeriodicTest.php b/test/PeriodicTest.php index a821275..00ca5a7 100644 --- a/test/PeriodicTest.php +++ b/test/PeriodicTest.php @@ -147,16 +147,16 @@ class PeriodicTest extends AsyncTestCase $this->assertEquals(2, $runCount); - $loop->resume(); + $this->assertTrue($loop->resume()); delay(0.002); $this->assertEquals(3, $runCount); if ($stopSig) { - $loop->stop(); + $this->assertTrue($loop->stop()); } else { $retValue = true; - $loop->resume(); + $this->assertTrue($loop->resume()); } delay(0.002); $this->assertEquals($stopSig ? 3 : 4, $runCount); diff --git a/test/Traits/Basic.php b/test/Traits/Basic.php index abaf0f8..5a289a3 100644 --- a/test/Traits/Basic.php +++ b/test/Traits/Basic.php @@ -9,6 +9,7 @@ namespace danog\Loop\Test\Traits; +use danog\Loop\Loop; use danog\Loop\Test\LoopTest; use function Amp\delay; @@ -50,7 +51,7 @@ trait Basic $this->inited = true; delay(0.1); $this->ran = true; - return null; + return Loop::STOP; } /** * Get loop name.