1
0
mirror of https://github.com/danog/loop.git synced 2024-11-26 20:04:44 +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;
use AssertionError;
use Revolt\EventLoop;
use Stringable;
@ -65,8 +66,9 @@ abstract class Loop implements Stringable
return false;
}
$this->running = true;
$this->paused = true;
\assert($this->resume());
if (!$this->resume()) {
throw new AssertionError("Could not resume!");
}
$this->startedLoop();
return true;
}
@ -101,8 +103,12 @@ abstract class Loop implements Stringable
private bool $paused = true;
private function loopInternal(): void
{
\assert($this->running);
\assert($this->paused);
if (!$this->running) {
throw new AssertionError("Already running!");
}
if (!$this->paused) {
throw new AssertionError("Already paused!");
}
$this->paused = false;
try {
$timeout = $this->loop();
@ -125,7 +131,9 @@ abstract class Loop implements Stringable
$this->reportPause(0.0);
} else {
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 = null;
$this->loopInternal();
@ -138,8 +146,13 @@ abstract class Loop implements Stringable
private function exitedLoopInternal(): void
{
$this->running = false;
\assert($this->resumeTimer === null);
\assert($this->resumeImmediate === null);
$this->paused = true;
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();
}
/**

View File

@ -149,6 +149,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(1, $loop->getPauseCount());
$this->assertEquals(0, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
$pauseTime = 0.1;
$this->assertTrue($loop->resume());
@ -159,6 +160,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(2, $loop->getPauseCount());
$this->assertEquals(0.1, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
delay(0.048);
$this->fixtureStarted($loop);
@ -166,6 +168,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(2, $loop->getPauseCount());
$this->assertEquals(0.1, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
delay(0.060);
$this->fixtureStarted($loop);
@ -174,6 +177,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(3, $loop->getPauseCount());
$this->assertEquals(0.1, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
$this->assertTrue($loop->resume());
self::waitTick();
@ -182,6 +186,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(4, $loop->getPauseCount());
$this->assertEquals(0.1, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
if ($stopSig) {
$this->assertTrue($loop->stop());
@ -194,6 +199,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(4, $loop->getPauseCount());
$this->assertEquals(0.1, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
$this->assertEquals(1, $loop->startCounter());
$this->assertEquals(1, $loop->endCounter());
@ -212,6 +218,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(5, $loop->getPauseCount());
$this->assertEquals(0.0, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
if ($stopSig) {
$this->assertTrue($loop->stop());
@ -224,6 +231,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(5, $loop->getPauseCount());
$this->assertEquals(0.0, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
$this->assertEquals(2, $loop->startCounter());
$this->assertEquals(2, $loop->endCounter());
@ -241,6 +249,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(5, $loop->getPauseCount());
$this->assertEquals(0.0, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
$this->assertEquals(3, $loop->startCounter());
$this->assertEquals(3, $loop->endCounter());
@ -258,6 +267,7 @@ class GenericTest extends Fixtures
$this->assertEquals($expectedRunCount, $runCount);
$this->assertEquals(5, $loop->getPauseCount());
$this->assertEquals(0.0, $loop->getLastPause());
$this->assertTrue($loop->isPaused());
$this->assertEquals(4, $loop->startCounter());
$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, !$loop->start());
$this->assertEquals($running, !$loop->isPaused());
}
/**
* Execute final assertions.
@ -140,5 +141,7 @@ class LoopTest extends Fixtures
$this->assertEquals(1, $loop->endCounter());
$this->assertInstanceOf(RuntimeException::class, $e_thrown);
EventLoop::setErrorHandler(null);
}
}

View File

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