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-24 12:31:57 +01:00
parent e3cbe834d6
commit 54d524b388
4 changed files with 38 additions and 7 deletions

View File

@ -10,6 +10,7 @@
namespace danog\Loop; namespace danog\Loop;
use AssertionError;
use Revolt\EventLoop; use Revolt\EventLoop;
use Stringable; use Stringable;
@ -65,8 +66,9 @@ abstract class Loop implements Stringable
return false; return false;
} }
$this->running = true; $this->running = true;
$this->paused = true; if (!$this->resume()) {
\assert($this->resume()); throw new AssertionError("Could not resume!");
}
$this->startedLoop(); $this->startedLoop();
return true; return true;
} }
@ -101,8 +103,12 @@ abstract class Loop implements Stringable
private bool $paused = true; private bool $paused = true;
private function loopInternal(): void private function loopInternal(): void
{ {
\assert($this->running); if (!$this->running) {
\assert($this->paused); throw new AssertionError("Already running!");
}
if (!$this->paused) {
throw new AssertionError("Already paused!");
}
$this->paused = false; $this->paused = false;
try { try {
$timeout = $this->loop(); $timeout = $this->loop();
@ -125,7 +131,9 @@ abstract class Loop implements Stringable
$this->reportPause(0.0); $this->reportPause(0.0);
} else { } else {
if (!$this->resumeImmediate) { if (!$this->resumeImmediate) {
\assert($this->resumeTimer === null); if ($this->resumeTimer !== null) {
throw new AssertionError("Already have a resume timer!");
}
$this->resumeTimer = EventLoop::delay($timeout, function (): void { $this->resumeTimer = EventLoop::delay($timeout, function (): void {
$this->resumeTimer = null; $this->resumeTimer = null;
$this->loopInternal(); $this->loopInternal();
@ -138,8 +146,13 @@ abstract class Loop implements Stringable
private function exitedLoopInternal(): void private function exitedLoopInternal(): void
{ {
$this->running = false; $this->running = false;
\assert($this->resumeTimer === null); $this->paused = true;
\assert($this->resumeImmediate === null); if ($this->resumeTimer !== null) {
throw new AssertionError("Already have a resume timer!");
}
if ($this->resumeTimer !== null) {
throw new AssertionError("Already have a resume immediate timer!");
}
$this->exitedLoop(); $this->exitedLoop();
} }
/** /**

View File

@ -149,6 +149,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount); $this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(1, $loop->getPauseCount()); $this->assertEquals(1, $loop->getPauseCount());
$this->assertEquals(0, $loop->getLastPause()); $this->assertEquals(0, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
$pauseTime = 0.1; $pauseTime = 0.1;
$this->assertTrue($loop->resume()); $this->assertTrue($loop->resume());
@ -159,6 +160,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount); $this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(2, $loop->getPauseCount()); $this->assertEquals(2, $loop->getPauseCount());
$this->assertEquals(0.1, $loop->getLastPause()); $this->assertEquals(0.1, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
delay(0.048); delay(0.048);
$this->fixtureStarted($loop); $this->fixtureStarted($loop);
@ -166,6 +168,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount); $this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(2, $loop->getPauseCount()); $this->assertEquals(2, $loop->getPauseCount());
$this->assertEquals(0.1, $loop->getLastPause()); $this->assertEquals(0.1, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
delay(0.060); delay(0.060);
$this->fixtureStarted($loop); $this->fixtureStarted($loop);
@ -174,6 +177,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount); $this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(3, $loop->getPauseCount()); $this->assertEquals(3, $loop->getPauseCount());
$this->assertEquals(0.1, $loop->getLastPause()); $this->assertEquals(0.1, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
$this->assertTrue($loop->resume()); $this->assertTrue($loop->resume());
self::waitTick(); self::waitTick();
@ -182,6 +186,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount); $this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(4, $loop->getPauseCount()); $this->assertEquals(4, $loop->getPauseCount());
$this->assertEquals(0.1, $loop->getLastPause()); $this->assertEquals(0.1, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
if ($stopSig) { if ($stopSig) {
$this->assertTrue($loop->stop()); $this->assertTrue($loop->stop());
@ -194,6 +199,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount); $this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(4, $loop->getPauseCount()); $this->assertEquals(4, $loop->getPauseCount());
$this->assertEquals(0.1, $loop->getLastPause()); $this->assertEquals(0.1, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
$this->assertEquals(1, $loop->startCounter()); $this->assertEquals(1, $loop->startCounter());
$this->assertEquals(1, $loop->endCounter()); $this->assertEquals(1, $loop->endCounter());
@ -212,6 +218,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount); $this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(5, $loop->getPauseCount()); $this->assertEquals(5, $loop->getPauseCount());
$this->assertEquals(0.0, $loop->getLastPause()); $this->assertEquals(0.0, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
if ($stopSig) { if ($stopSig) {
$this->assertTrue($loop->stop()); $this->assertTrue($loop->stop());
@ -224,6 +231,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount); $this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(5, $loop->getPauseCount()); $this->assertEquals(5, $loop->getPauseCount());
$this->assertEquals(0.0, $loop->getLastPause()); $this->assertEquals(0.0, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
$this->assertEquals(2, $loop->startCounter()); $this->assertEquals(2, $loop->startCounter());
$this->assertEquals(2, $loop->endCounter()); $this->assertEquals(2, $loop->endCounter());
@ -241,6 +249,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount); $this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(5, $loop->getPauseCount()); $this->assertEquals(5, $loop->getPauseCount());
$this->assertEquals(0.0, $loop->getLastPause()); $this->assertEquals(0.0, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
$this->assertEquals(3, $loop->startCounter()); $this->assertEquals(3, $loop->startCounter());
$this->assertEquals(3, $loop->endCounter()); $this->assertEquals(3, $loop->endCounter());
@ -258,6 +267,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount); $this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(5, $loop->getPauseCount()); $this->assertEquals(5, $loop->getPauseCount());
$this->assertEquals(0.0, $loop->getLastPause()); $this->assertEquals(0.0, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
$this->assertEquals(4, $loop->startCounter()); $this->assertEquals(4, $loop->startCounter());
$this->assertEquals(4, $loop->endCounter()); $this->assertEquals(4, $loop->endCounter());

View File

@ -58,6 +58,7 @@ class LoopTest extends Fixtures
$this->assertEquals($running ? 0 : 1, $loop->endCounter()); $this->assertEquals($running ? 0 : 1, $loop->endCounter());
$this->assertEquals($running, !$loop->start()); $this->assertEquals($running, !$loop->start());
$this->assertEquals($running, !$loop->isPaused());
} }
/** /**
* Execute final assertions. * Execute final assertions.
@ -140,5 +141,7 @@ class LoopTest extends Fixtures
$this->assertEquals(1, $loop->endCounter()); $this->assertEquals(1, $loop->endCounter());
$this->assertInstanceOf(RuntimeException::class, $e_thrown); $this->assertInstanceOf(RuntimeException::class, $e_thrown);
EventLoop::setErrorHandler(null);
} }
} }

View File

@ -141,6 +141,7 @@ class PeriodicTest extends Fixtures
$this->fixtureStarted($loop); $this->fixtureStarted($loop);
self::waitTick(); self::waitTick();
$this->assertTrue($loop->isPaused());
$this->assertEquals(1, $runCount); $this->assertEquals(1, $runCount);
$this->assertEquals($loop, $l); $this->assertEquals($loop, $l);
@ -148,16 +149,19 @@ class PeriodicTest extends Fixtures
delay(0.048); delay(0.048);
$this->fixtureStarted($loop); $this->fixtureStarted($loop);
$this->assertTrue($loop->isPaused());
$this->assertEquals(1, $runCount); $this->assertEquals(1, $runCount);
delay(0.060); delay(0.060);
$this->fixtureStarted($loop); $this->fixtureStarted($loop);
$this->assertTrue($loop->isPaused());
$this->assertEquals(2, $runCount); $this->assertEquals(2, $runCount);
$this->assertTrue($loop->resume()); $this->assertTrue($loop->resume());
self::waitTick(); self::waitTick();
$this->assertTrue($loop->isPaused());
$this->assertEquals(3, $runCount); $this->assertEquals(3, $runCount);
if ($stopSig) { if ($stopSig) {
@ -169,6 +173,7 @@ class PeriodicTest extends Fixtures
self::waitTick(); self::waitTick();
$this->assertEquals($stopSig ? 3 : 4, $runCount); $this->assertEquals($stopSig ? 3 : 4, $runCount);
$this->assertTrue($loop->isPaused());
$this->assertFalse($loop->isRunning()); $this->assertFalse($loop->isRunning());
$this->assertEquals(1, $loop->startCounter()); $this->assertEquals(1, $loop->startCounter());