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.
*/
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;
}
}

View File

@ -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);

View File

@ -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());

View File

@ -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);

View File

@ -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.