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

Fix tests

This commit is contained in:
Daniil Gentili 2023-01-23 00:38:49 +01:00
parent eb05448ce7
commit da08301a91
5 changed files with 24 additions and 20 deletions

View File

@ -43,7 +43,7 @@ abstract class Loop implements Stringable
/** /**
* Resume deferred ID. * Resume deferred ID.
*/ */
private ?string $resumeDeferred = null; private ?string $resumeImmediate = null;
/** /**
* Report pause, can be overriden for logging. * Report pause, can be overriden for logging.
@ -80,8 +80,8 @@ abstract class Loop implements Stringable
if (!$this->running) { if (!$this->running) {
return false; return false;
} }
$this->running = false;
$this->resume(); $this->resume();
$this->running = false;
return true; return true;
} }
abstract protected function loop(): ?float; abstract protected function loop(): ?float;
@ -109,7 +109,7 @@ abstract class Loop implements Stringable
if ($timeout === self::PAUSE) { if ($timeout === self::PAUSE) {
$this->reportPause(0.0); $this->reportPause(0.0);
} else { } else {
if (!$this->resumeDeferred) { if (!$this->resumeImmediate) {
\assert($this->resumeTimer === null); \assert($this->resumeTimer === null);
$this->resumeTimer = EventLoop::delay($timeout, function (): void { $this->resumeTimer = EventLoop::delay($timeout, function (): void {
$this->resumeTimer = null; $this->resumeTimer = null;
@ -128,8 +128,8 @@ abstract class Loop implements Stringable
EventLoop::cancel($storedWatcherId); EventLoop::cancel($storedWatcherId);
$this->resumeTimer = null; $this->resumeTimer = null;
} }
if ($this->resumeDeferred) { if ($this->resumeImmediate) {
$storedWatcherId = $this->resumeDeferred; $storedWatcherId = $this->resumeImmediate;
EventLoop::cancel($storedWatcherId); EventLoop::cancel($storedWatcherId);
$this->resumeTimer = null; $this->resumeTimer = null;
} }
@ -159,19 +159,23 @@ abstract class Loop implements Stringable
/** /**
* Resume the loop. * 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) { if ($this->resumeTimer) {
$timer = $this->resumeTimer; $timer = $this->resumeTimer;
$this->resumeTimer = null; $this->resumeTimer = null;
EventLoop::cancel($timer); EventLoop::cancel($timer);
} }
$this->resumeDeferred = EventLoop::defer(function (): void { $this->resumeImmediate = EventLoop::defer(function (): void {
$this->resumeDeferred = null; $this->resumeImmediate = null;
$this->loopInternal(); $this->loopInternal();
}); });
return true;
} }
return false;
} }
} }

View File

@ -141,7 +141,7 @@ class GenericTest extends AsyncTestCase
$this->assertEquals(0, $loop->getLastPause()); $this->assertEquals(0, $loop->getLastPause());
$pauseTime = 0.1; $pauseTime = 0.1;
$loop->resume(); $this->assertTrue($loop->resume());
delay(0.002); delay(0.002);
$this->fixtureStarted($loop); $this->fixtureStarted($loop);
@ -163,7 +163,7 @@ class GenericTest extends AsyncTestCase
$this->assertEquals(3, $loop->getPauseCount()); $this->assertEquals(3, $loop->getPauseCount());
$this->assertEquals(0.1, $loop->getLastPause()); $this->assertEquals(0.1, $loop->getLastPause());
$loop->resume(); $this->assertTrue($loop->resume());
delay(0.002); delay(0.002);
$this->assertEquals(4, $runCount); $this->assertEquals(4, $runCount);
@ -171,10 +171,10 @@ class GenericTest extends AsyncTestCase
$this->assertEquals(0.1, $loop->getLastPause()); $this->assertEquals(0.1, $loop->getLastPause());
if ($stopSig) { if ($stopSig) {
$loop->stop(); $this->assertTrue($loop->stop());
} else { } else {
$pauseTime = GenericLoop::STOP; $pauseTime = GenericLoop::STOP;
$loop->resume(); $this->assertTrue($loop->resume());
} }
delay(0.002); delay(0.002);
$this->assertEquals($stopSig ? 4 : 5, $runCount); $this->assertEquals($stopSig ? 4 : 5, $runCount);

View File

@ -9,6 +9,7 @@
namespace danog\Loop\Test; namespace danog\Loop\Test;
use Amp\PHPUnit\UnhandledException;
use danog\Loop\Loop; use danog\Loop\Loop;
use danog\Loop\Test\Interfaces\BasicInterface; use danog\Loop\Test\Interfaces\BasicInterface;
use danog\Loop\Test\Traits\Basic; use danog\Loop\Test\Traits\Basic;
@ -40,9 +41,7 @@ class LoopTest extends Fixtures
*/ */
public function testException(Loop&BasicInterface $loop): void public function testException(Loop&BasicInterface $loop): void
{ {
$this->markTestSkipped(); $this->expectException(UnhandledException::class);
$this->expectException(\RuntimeException::class);
$this->assertPreStart($loop); $this->assertPreStart($loop);
$this->assertTrue($loop->start()); $this->assertTrue($loop->start());

View File

@ -147,16 +147,16 @@ class PeriodicTest extends AsyncTestCase
$this->assertEquals(2, $runCount); $this->assertEquals(2, $runCount);
$loop->resume(); $this->assertTrue($loop->resume());
delay(0.002); delay(0.002);
$this->assertEquals(3, $runCount); $this->assertEquals(3, $runCount);
if ($stopSig) { if ($stopSig) {
$loop->stop(); $this->assertTrue($loop->stop());
} else { } else {
$retValue = true; $retValue = true;
$loop->resume(); $this->assertTrue($loop->resume());
} }
delay(0.002); delay(0.002);
$this->assertEquals($stopSig ? 3 : 4, $runCount); $this->assertEquals($stopSig ? 3 : 4, $runCount);

View File

@ -9,6 +9,7 @@
namespace danog\Loop\Test\Traits; namespace danog\Loop\Test\Traits;
use danog\Loop\Loop;
use danog\Loop\Test\LoopTest; use danog\Loop\Test\LoopTest;
use function Amp\delay; use function Amp\delay;
@ -50,7 +51,7 @@ trait Basic
$this->inited = true; $this->inited = true;
delay(0.1); delay(0.1);
$this->ran = true; $this->ran = true;
return null; return Loop::STOP;
} }
/** /**
* Get loop name. * Get loop name.