1
0
mirror of https://github.com/danog/loop.git synced 2024-11-30 04:19:04 +01:00
This commit is contained in:
Daniil Gentili 2022-12-23 20:45:00 +01:00
parent 47d2d51442
commit afcf214f24
18 changed files with 106 additions and 114 deletions

View File

@ -39,7 +39,7 @@ namespace danog\Loop;
abstract class Loop abstract class Loop
{ {
abstract public function loop(): \Generator; abstract public function loop();
abstract public function __toString(): string; abstract public function __toString(): string;
public function start(): bool; public function start(): bool;

View File

@ -19,16 +19,16 @@ Loop::run(function () {
}; };
$loop = new GenericLoop($callable, "Loop number $x"); $loop = new GenericLoop($callable, "Loop number $x");
$loop->start(); $loop->start();
yield delay(100); delay(100);
$loops []= $loop; $loops []= $loop;
} }
yield delay(5000); delay(5000);
echo "Resuming prematurely all loops!".PHP_EOL; echo "Resuming prematurely all loops!".PHP_EOL;
foreach ($loops as $loop) { foreach ($loops as $loop) {
$loop->resume(); $loop->resume();
} }
echo "OK done, waiting 5 more seconds!".PHP_EOL; echo "OK done, waiting 5 more seconds!".PHP_EOL;
yield delay(5000); delay(5000);
echo "Closing all loops!".PHP_EOL; echo "Closing all loops!".PHP_EOL;
yield delay(10); delay(10);
}); });

View File

@ -7,7 +7,6 @@ use danog\Loop\Generic\PeriodicLoop;
use function Amp\delay; use function Amp\delay;
Loop::run(function () {
/** @var PeriodicLoop[] */ /** @var PeriodicLoop[] */
$loops = []; $loops = [];
for ($x = 0; $x < 10; $x++) { for ($x = 0; $x < 10; $x++) {
@ -19,16 +18,15 @@ Loop::run(function () {
}; };
$loop = new PeriodicLoop($callable, "Loop number $x", 1000); $loop = new PeriodicLoop($callable, "Loop number $x", 1000);
$loop->start(); $loop->start();
yield delay(100); delay(100);
$loops []= $loop; $loops []= $loop;
} }
yield delay(5000); delay(5000);
echo "Resuming prematurely all loops!".PHP_EOL; echo "Resuming prematurely all loops!".PHP_EOL;
foreach ($loops as $loop) { foreach ($loops as $loop) {
$loop->resume(); $loop->resume();
} }
echo "OK done, waiting 5 more seconds!".PHP_EOL; echo "OK done, waiting 5 more seconds!".PHP_EOL;
yield delay(5000); delay(5000);
echo "Closing all loops!".PHP_EOL; echo "Closing all loops!".PHP_EOL;
yield delay(10); delay(10);
});

View File

@ -37,13 +37,13 @@ class MyLoop extends Loop
* *
* @return \Generator * @return \Generator
*/ */
public function loop(): \Generator public function loop()
{ {
$callable = $this->callable; $callable = $this->callable;
$number = 0; $number = 0;
while (true) { while (true) {
$number = yield from $callable($number); $number = $callable($number);
echo "$this: $number".PHP_EOL; echo "$this: $number".PHP_EOL;
} }
} }
@ -82,16 +82,15 @@ class MyLoop extends Loop
} }
} }
AmpLoop::run(function () { $function = function (int $number) {
$function = function (int $number): \Generator { delay(1000);
yield delay(1000);
return $number + 1; return $number + 1;
}; };
$loops = []; $loops = [];
for ($x = 0; $x < 10; $x++) { for ($x = 0; $x < 10; $x++) {
$loop = new MyLoop($function, "Loop number $x"); $loop = new MyLoop($function, "Loop number $x");
$loop->start(); $loop->start();
yield delay(100); delay(100);
$loops []= $loop; $loops []= $loop;
} }
});

View File

@ -29,11 +29,11 @@ class MyLoop extends ResumableLoop
* *
* @return \Generator * @return \Generator
*/ */
public function loop(): \Generator public function loop()
{ {
$number = 0; $number = 0;
while (true) { while (true) {
yield $this->pause(1000); $this->pause(1000);
echo "$this: $number".PHP_EOL; echo "$this: $number".PHP_EOL;
$number++; $number++;
} }
@ -73,17 +73,15 @@ class MyLoop extends ResumableLoop
} }
} }
AmpLoop::run(function () {
$loops = []; $loops = [];
for ($x = 0; $x < 10; $x++) { for ($x = 0; $x < 10; $x++) {
$loop = new MyLoop("Loop number $x"); $loop = new MyLoop("Loop number $x");
$loop->start(); $loop->start();
yield delay(100); delay(100);
$loops []= $loop; $loops []= $loop;
} }
yield delay(5000); delay(5000);
echo "Resuming prematurely all loops!".PHP_EOL; echo "Resuming prematurely all loops!".PHP_EOL;
foreach ($loops as $loop) { foreach ($loops as $loop) {
$loop->resume(); $loop->resume();
} }
});

View File

@ -29,11 +29,11 @@ class ResSigLoop extends ResumableSignalLoop
* *
* @return \Generator * @return \Generator
*/ */
public function loop(): \Generator public function loop()
{ {
$number = 0; $number = 0;
while (true) { while (true) {
if (yield $this->waitSignal($this->pause(1000))) { if ($this->waitSignal($this->pause(1000))) {
echo "Got exit signal in $this!".PHP_EOL; echo "Got exit signal in $this!".PHP_EOL;
return; return;
} }
@ -52,24 +52,22 @@ class ResSigLoop extends ResumableSignalLoop
} }
} }
Loop::run(function () {
/** @var ResSigLoop[] */ /** @var ResSigLoop[] */
$loops = []; $loops = [];
for ($x = 0; $x < 10; $x++) { for ($x = 0; $x < 10; $x++) {
$loop = new ResSigLoop("Loop number $x"); $loop = new ResSigLoop("Loop number $x");
$loop->start(); $loop->start();
yield delay(100); delay(100);
$loops []= $loop; $loops []= $loop;
} }
yield delay(5000); delay(5000);
echo "Resuming prematurely all loops!".PHP_EOL; echo "Resuming prematurely all loops!".PHP_EOL;
foreach ($loops as $loop) { foreach ($loops as $loop) {
$loop->resume(); $loop->resume();
} }
echo "OK done, waiting 5 more seconds!".PHP_EOL; echo "OK done, waiting 5 more seconds!".PHP_EOL;
yield delay(5000); delay(5000);
echo "Closing all loops!".PHP_EOL; echo "Closing all loops!".PHP_EOL;
foreach ($loops as $loop) { foreach ($loops as $loop) {
$loop->signal(true); $loop->signal(true);
} }
});

View File

@ -29,11 +29,11 @@ class SigLoop extends SignalLoop
* *
* @return \Generator * @return \Generator
*/ */
public function loop(): \Generator public function loop()
{ {
$number = 0; $number = 0;
while (true) { while (true) {
if (yield $this->waitSignal(delay(1000))) { if ($this->waitSignal(delay(1000))) {
echo "Got exit signal in $this!".PHP_EOL; echo "Got exit signal in $this!".PHP_EOL;
return; return;
} }
@ -52,18 +52,17 @@ class SigLoop extends SignalLoop
} }
} }
Loop::run(function () {
/** @var SigLoop[] */ /** @var SigLoop[] */
$loops = []; $loops = [];
for ($x = 0; $x < 10; $x++) { for ($x = 0; $x < 10; $x++) {
$loop = new SigLoop("Loop number $x"); $loop = new SigLoop("Loop number $x");
$loop->start(); $loop->start();
yield delay(100); delay(100);
$loops []= $loop; $loops []= $loop;
} }
yield delay(5000); delay(5000);
echo "Closing all loops!".PHP_EOL; echo "Closing all loops!".PHP_EOL;
foreach ($loops as $loop) { foreach ($loops as $loop) {
$loop->signal(true); $loop->signal(true);
} }
});

View File

@ -30,7 +30,7 @@ interface LoopInterface
* *
* @return \Generator * @return \Generator
*/ */
public function loop(): \Generator; public function loop();
/** /**
* Get name of the loop. * Get name of the loop.
* *

View File

@ -42,7 +42,7 @@ interface ResumableLoopInterface extends LoopInterface
/** /**
* Defer resuming the loop to next tick. * Defer resuming the loop to next tick.
* *
* Multiple consecutive calls will yield only one resume. * Multiple consecutive calls will only one resume.
* *
* @return Promise Resolved when the loop is paused again * @return Promise Resolved when the loop is paused again
*/ */

View File

@ -109,7 +109,7 @@ trait ResumableLoop
/** /**
* Defer resuming the loop to next tick. * Defer resuming the loop to next tick.
* *
* Multiple consecutive calls will yield only one resume. * Multiple consecutive calls will only one resume.
* *
* @return Future<null> Resolved when the loop is paused again * @return Future<null> Resolved when the loop is paused again
*/ */

View File

@ -31,7 +31,7 @@ class GenericTest extends AsyncTestCase
* *
* @dataProvider provideTrueFalse * @dataProvider provideTrueFalse
*/ */
public function testGeneric(bool $stopSig): \Generator public function testGeneric(bool $stopSig)
{ {
$runCount = 0; $runCount = 0;
$pauseTime = GenericLoop::PAUSE; $pauseTime = GenericLoop::PAUSE;
@ -40,7 +40,7 @@ class GenericTest extends AsyncTestCase
$runCount++; $runCount++;
return $pauseTime; return $pauseTime;
}; };
yield from $this->fixtureAssertions($callable, $runCount, $pauseTime, $stopSig, $zis, true); $this->fixtureAssertions($callable, $runCount, $pauseTime, $stopSig, $zis, true);
$obj = new class() { $obj = new class() {
public $pauseTime = GenericLoop::PAUSE; public $pauseTime = GenericLoop::PAUSE;
public $runCount = 0; public $runCount = 0;
@ -50,7 +50,7 @@ class GenericTest extends AsyncTestCase
return $this->pauseTime; return $this->pauseTime;
} }
}; };
yield from $this->fixtureAssertions([$obj, 'run'], $obj->runCount, $obj->pauseTime, $stopSig, $zisNew, false); $this->fixtureAssertions([$obj, 'run'], $obj->runCount, $obj->pauseTime, $stopSig, $zisNew, false);
$obj = new class() { $obj = new class() {
public $pauseTime = GenericLoop::PAUSE; public $pauseTime = GenericLoop::PAUSE;
public $runCount = 0; public $runCount = 0;
@ -60,7 +60,7 @@ class GenericTest extends AsyncTestCase
return $this->pauseTime; return $this->pauseTime;
} }
}; };
yield from $this->fixtureAssertions(\Closure::fromCallable([$obj, 'run']), $obj->runCount, $obj->pauseTime, $stopSig, $zisNew, false); $this->fixtureAssertions(\Closure::fromCallable([$obj, 'run']), $obj->runCount, $obj->pauseTime, $stopSig, $zisNew, false);
} }
/** /**
* Test generator loop. * Test generator loop.
@ -71,39 +71,39 @@ class GenericTest extends AsyncTestCase
* *
* @dataProvider provideTrueFalse * @dataProvider provideTrueFalse
*/ */
public function testGenerator(bool $stopSig): \Generator public function testGenerator(bool $stopSig)
{ {
$runCount = 0; $runCount = 0;
$pauseTime = GenericLoop::PAUSE; $pauseTime = GenericLoop::PAUSE;
$callable = function () use (&$runCount, &$pauseTime, &$zis): \Generator { $callable = function () use (&$runCount, &$pauseTime, &$zis) {
$zis = $this; $zis = $this;
yield delay(1); delay(1);
$runCount++; $runCount++;
return $pauseTime; return $pauseTime;
}; };
yield from $this->fixtureAssertions($callable, $runCount, $pauseTime, $stopSig, $zis, true); $this->fixtureAssertions($callable, $runCount, $pauseTime, $stopSig, $zis, true);
$obj = new class() { $obj = new class() {
public $pauseTime = GenericLoop::PAUSE; public $pauseTime = GenericLoop::PAUSE;
public $runCount = 0; public $runCount = 0;
public function run(): \Generator public function run()
{ {
yield delay(1); delay(1);
$this->runCount++; $this->runCount++;
return $this->pauseTime; return $this->pauseTime;
} }
}; };
yield from $this->fixtureAssertions([$obj, 'run'], $obj->runCount, $obj->pauseTime, $stopSig, $zisNew, false); $this->fixtureAssertions([$obj, 'run'], $obj->runCount, $obj->pauseTime, $stopSig, $zisNew, false);
$obj = new class() { $obj = new class() {
public $pauseTime = GenericLoop::PAUSE; public $pauseTime = GenericLoop::PAUSE;
public $runCount = 0; public $runCount = 0;
public function run(): \Generator public function run()
{ {
yield delay(1); delay(1);
$this->runCount++; $this->runCount++;
return $this->pauseTime; return $this->pauseTime;
} }
}; };
yield from $this->fixtureAssertions(\Closure::fromCallable([$obj, 'run']), $obj->runCount, $obj->pauseTime, $stopSig, $zisNew, false); $this->fixtureAssertions(\Closure::fromCallable([$obj, 'run']), $obj->runCount, $obj->pauseTime, $stopSig, $zisNew, false);
} }
/** /**
* Test promise loop. * Test promise loop.
@ -114,7 +114,7 @@ class GenericTest extends AsyncTestCase
* *
* @dataProvider provideTrueFalse * @dataProvider provideTrueFalse
*/ */
public function testPromise(bool $stopSig): \Generator public function testPromise(bool $stopSig)
{ {
$runCount = 0; $runCount = 0;
$pauseTime = GenericLoop::PAUSE; $pauseTime = GenericLoop::PAUSE;
@ -123,7 +123,7 @@ class GenericTest extends AsyncTestCase
$runCount++; $runCount++;
return new Success($pauseTime); return new Success($pauseTime);
}; };
yield from $this->fixtureAssertions($callable, $runCount, $pauseTime, $stopSig, $zis, true); $this->fixtureAssertions($callable, $runCount, $pauseTime, $stopSig, $zis, true);
$obj = new class() { $obj = new class() {
public $pauseTime = GenericLoop::PAUSE; public $pauseTime = GenericLoop::PAUSE;
public $runCount = 0; public $runCount = 0;
@ -133,7 +133,7 @@ class GenericTest extends AsyncTestCase
return new Success($this->pauseTime); return new Success($this->pauseTime);
} }
}; };
yield from $this->fixtureAssertions([$obj, 'run'], $obj->runCount, $obj->pauseTime, $stopSig, $zisNew, false); $this->fixtureAssertions([$obj, 'run'], $obj->runCount, $obj->pauseTime, $stopSig, $zisNew, false);
$obj = new class() { $obj = new class() {
public $pauseTime = GenericLoop::PAUSE; public $pauseTime = GenericLoop::PAUSE;
public $runCount = 0; public $runCount = 0;
@ -143,7 +143,7 @@ class GenericTest extends AsyncTestCase
return new Success($this->pauseTime); return new Success($this->pauseTime);
} }
}; };
yield from $this->fixtureAssertions(\Closure::fromCallable([$obj, 'run']), $obj->runCount, $obj->pauseTime, $stopSig, $zisNew, false); $this->fixtureAssertions(\Closure::fromCallable([$obj, 'run']), $obj->runCount, $obj->pauseTime, $stopSig, $zisNew, false);
} }
/** /**
* Fixture assertions for started loop. * Fixture assertions for started loop.
@ -170,7 +170,7 @@ class GenericTest extends AsyncTestCase
* *
* @return \Generator * @return \Generator
*/ */
private function fixtureAssertions(callable $closure, int &$runCount, ?int &$pauseTime, bool $stopSig, &$zis, bool $checkZis): \Generator private function fixtureAssertions(callable $closure, int &$runCount, ?int &$pauseTime, bool $stopSig, &$zis, bool $checkZis)
{ {
$loop = new class($closure, Fixtures::LOOP_NAME) extends GenericLoop implements LoggingPauseInterface { $loop = new class($closure, Fixtures::LOOP_NAME) extends GenericLoop implements LoggingPauseInterface {
use LoggingPause; use LoggingPause;
@ -185,7 +185,7 @@ class GenericTest extends AsyncTestCase
$this->assertEquals(0, $loop->getPauseCount()); $this->assertEquals(0, $loop->getPauseCount());
$loop->start(); $loop->start();
yield delay(2); delay(2);
if ($checkZis) { if ($checkZis) {
$this->assertEquals($loop, $zis); $this->assertEquals($loop, $zis);
} else { } else {
@ -199,21 +199,21 @@ class GenericTest extends AsyncTestCase
$pauseTime = 100; $pauseTime = 100;
$loop->resume(); $loop->resume();
yield delay(2); delay(2);
$this->fixtureStarted($loop); $this->fixtureStarted($loop);
$this->assertEquals(2, $runCount); $this->assertEquals(2, $runCount);
$this->assertEquals(2, $loop->getPauseCount()); $this->assertEquals(2, $loop->getPauseCount());
$this->assertEquals(100, $loop->getLastPause()); $this->assertEquals(100, $loop->getLastPause());
yield delay(48); delay(48);
$this->fixtureStarted($loop); $this->fixtureStarted($loop);
$this->assertEquals(2, $runCount); $this->assertEquals(2, $runCount);
$this->assertEquals(2, $loop->getPauseCount()); $this->assertEquals(2, $loop->getPauseCount());
$this->assertEquals(100, $loop->getLastPause()); $this->assertEquals(100, $loop->getLastPause());
yield delay(60); delay(60);
$this->fixtureStarted($loop); $this->fixtureStarted($loop);
$this->assertEquals(3, $runCount); $this->assertEquals(3, $runCount);
@ -221,7 +221,7 @@ class GenericTest extends AsyncTestCase
$this->assertEquals(100, $loop->getLastPause()); $this->assertEquals(100, $loop->getLastPause());
$loop->resume(); $loop->resume();
yield delay(1); delay(1);
$this->assertEquals(4, $runCount); $this->assertEquals(4, $runCount);
$this->assertEquals(4, $loop->getPauseCount()); $this->assertEquals(4, $loop->getPauseCount());
@ -233,7 +233,7 @@ class GenericTest extends AsyncTestCase
$pauseTime = GenericLoop::STOP; $pauseTime = GenericLoop::STOP;
$loop->resume(); $loop->resume();
} }
yield delay(1); delay(1);
$this->assertEquals($stopSig ? 4 : 5, $runCount); $this->assertEquals($stopSig ? 4 : 5, $runCount);
$this->assertEquals(4, $loop->getPauseCount()); $this->assertEquals(4, $loop->getPauseCount());
$this->assertEquals(100, $loop->getLastPause()); $this->assertEquals(100, $loop->getLastPause());

View File

@ -30,13 +30,13 @@ class LoopTest extends Fixtures
* *
* @dataProvider provideBasic * @dataProvider provideBasic
*/ */
public function testLoop(BasicInterface $loop): \Generator public function testLoop(BasicInterface $loop)
{ {
$this->assertPreStart($loop); $this->assertPreStart($loop);
$this->assertTrue($loop->start()); $this->assertTrue($loop->start());
$this->assertAfterStart($loop); $this->assertAfterStart($loop);
yield delay(110); delay(110);
$this->assertFinal($loop); $this->assertFinal($loop);
} }

View File

@ -31,7 +31,7 @@ class PeriodicTest extends AsyncTestCase
* *
* @dataProvider provideTrueFalse * @dataProvider provideTrueFalse
*/ */
public function testGeneric(bool $stopSig): \Generator public function testGeneric(bool $stopSig)
{ {
$runCount = 0; $runCount = 0;
$retValue = false; $retValue = false;
@ -40,7 +40,7 @@ class PeriodicTest extends AsyncTestCase
$runCount++; $runCount++;
return $retValue; return $retValue;
}; };
yield from $this->fixtureAssertions($callable, $runCount, $retValue, $stopSig, $zis, true); $this->fixtureAssertions($callable, $runCount, $retValue, $stopSig, $zis, true);
$obj = new class() { $obj = new class() {
public $retValue = false; public $retValue = false;
public $runCount = 0; public $runCount = 0;
@ -50,7 +50,7 @@ class PeriodicTest extends AsyncTestCase
return $this->retValue; return $this->retValue;
} }
}; };
yield from $this->fixtureAssertions([$obj, 'run'], $obj->runCount, $obj->retValue, $stopSig, $zisNew, false); $this->fixtureAssertions([$obj, 'run'], $obj->runCount, $obj->retValue, $stopSig, $zisNew, false);
$obj = new class() { $obj = new class() {
public $retValue = false; public $retValue = false;
public $runCount = 0; public $runCount = 0;
@ -60,7 +60,7 @@ class PeriodicTest extends AsyncTestCase
return $this->retValue; return $this->retValue;
} }
}; };
yield from $this->fixtureAssertions(\Closure::fromCallable([$obj, 'run']), $obj->runCount, $obj->retValue, $stopSig, $zisNew, false); $this->fixtureAssertions(\Closure::fromCallable([$obj, 'run']), $obj->runCount, $obj->retValue, $stopSig, $zisNew, false);
} }
/** /**
* Test generator loop. * Test generator loop.
@ -71,39 +71,39 @@ class PeriodicTest extends AsyncTestCase
* *
* @dataProvider provideTrueFalse * @dataProvider provideTrueFalse
*/ */
public function testGenerator(bool $stopSig): \Generator public function testGenerator(bool $stopSig)
{ {
$runCount = 0; $runCount = 0;
$retValue = false; $retValue = false;
$callable = function () use (&$runCount, &$retValue, &$zis): \Generator { $callable = function () use (&$runCount, &$retValue, &$zis) {
$zis = $this; $zis = $this;
yield delay(1); delay(1);
$runCount++; $runCount++;
return $retValue; return $retValue;
}; };
yield from $this->fixtureAssertions($callable, $runCount, $retValue, $stopSig, $zis, true); $this->fixtureAssertions($callable, $runCount, $retValue, $stopSig, $zis, true);
$obj = new class() { $obj = new class() {
public $retValue = false; public $retValue = false;
public $runCount = 0; public $runCount = 0;
public function run(): \Generator public function run()
{ {
yield delay(1); delay(1);
$this->runCount++; $this->runCount++;
return $this->retValue; return $this->retValue;
} }
}; };
yield from $this->fixtureAssertions([$obj, 'run'], $obj->runCount, $obj->retValue, $stopSig, $zisNew, false); $this->fixtureAssertions([$obj, 'run'], $obj->runCount, $obj->retValue, $stopSig, $zisNew, false);
$obj = new class() { $obj = new class() {
public $retValue = false; public $retValue = false;
public $runCount = 0; public $runCount = 0;
public function run(): \Generator public function run()
{ {
yield delay(1); delay(1);
$this->runCount++; $this->runCount++;
return $this->retValue; return $this->retValue;
} }
}; };
yield from $this->fixtureAssertions(\Closure::fromCallable([$obj, 'run']), $obj->runCount, $obj->retValue, $stopSig, $zisNew, false); $this->fixtureAssertions(\Closure::fromCallable([$obj, 'run']), $obj->runCount, $obj->retValue, $stopSig, $zisNew, false);
} }
/** /**
* Test promise loop. * Test promise loop.
@ -114,7 +114,7 @@ class PeriodicTest extends AsyncTestCase
* *
* @dataProvider provideTrueFalse * @dataProvider provideTrueFalse
*/ */
public function testPromise(bool $stopSig): \Generator public function testPromise(bool $stopSig)
{ {
$runCount = 0; $runCount = 0;
$retValue = false; $retValue = false;
@ -123,7 +123,7 @@ class PeriodicTest extends AsyncTestCase
$runCount++; $runCount++;
return new Success($retValue); return new Success($retValue);
}; };
yield from $this->fixtureAssertions($callable, $runCount, $retValue, $stopSig, $zis, true); $this->fixtureAssertions($callable, $runCount, $retValue, $stopSig, $zis, true);
$obj = new class() { $obj = new class() {
public $retValue = false; public $retValue = false;
public $runCount = 0; public $runCount = 0;
@ -133,7 +133,7 @@ class PeriodicTest extends AsyncTestCase
return new Success($this->retValue); return new Success($this->retValue);
} }
}; };
yield from $this->fixtureAssertions([$obj, 'run'], $obj->runCount, $obj->retValue, $stopSig, $zisNew, false); $this->fixtureAssertions([$obj, 'run'], $obj->runCount, $obj->retValue, $stopSig, $zisNew, false);
$obj = new class() { $obj = new class() {
public $retValue = false; public $retValue = false;
public $runCount = 0; public $runCount = 0;
@ -143,7 +143,7 @@ class PeriodicTest extends AsyncTestCase
return new Success($this->retValue); return new Success($this->retValue);
} }
}; };
yield from $this->fixtureAssertions(\Closure::fromCallable([$obj, 'run']), $obj->runCount, $obj->retValue, $stopSig, $zisNew, false); $this->fixtureAssertions(\Closure::fromCallable([$obj, 'run']), $obj->runCount, $obj->retValue, $stopSig, $zisNew, false);
} }
/** /**
* Fixture assertions for started loop. * Fixture assertions for started loop.
@ -170,7 +170,7 @@ class PeriodicTest extends AsyncTestCase
* *
* @return \Generator * @return \Generator
*/ */
private function fixtureAssertions(callable $closure, int &$runCount, bool &$retValue, bool $stopSig, &$zis, bool $checkZis): \Generator private function fixtureAssertions(callable $closure, int &$runCount, bool &$retValue, bool $stopSig, &$zis, bool $checkZis)
{ {
$loop = new class($closure, Fixtures::LOOP_NAME, 100) extends PeriodicLoop implements LoggingInterface { $loop = new class($closure, Fixtures::LOOP_NAME, 100) extends PeriodicLoop implements LoggingInterface {
use Logging; use Logging;
@ -184,7 +184,7 @@ class PeriodicTest extends AsyncTestCase
$this->assertEquals(0, $runCount); $this->assertEquals(0, $runCount);
$loop->start(); $loop->start();
yield delay(2); delay(2);
if ($checkZis) { if ($checkZis) {
$this->assertEquals($loop, $zis); $this->assertEquals($loop, $zis);
} else { } else {
@ -194,18 +194,18 @@ class PeriodicTest extends AsyncTestCase
$this->assertEquals(1, $runCount); $this->assertEquals(1, $runCount);
yield delay(48); delay(48);
$this->fixtureStarted($loop); $this->fixtureStarted($loop);
$this->assertEquals(1, $runCount); $this->assertEquals(1, $runCount);
yield delay(60); delay(60);
$this->fixtureStarted($loop); $this->fixtureStarted($loop);
$this->assertEquals(2, $runCount); $this->assertEquals(2, $runCount);
$loop->resume(); $loop->resume();
yield delay(1); delay(1);
$this->assertEquals(3, $runCount); $this->assertEquals(3, $runCount);
@ -215,7 +215,7 @@ class PeriodicTest extends AsyncTestCase
$retValue = true; $retValue = true;
$loop->resume(); $loop->resume();
} }
yield delay(1); delay(1);
$this->assertEquals($stopSig ? 3 : 4, $runCount); $this->assertEquals($stopSig ? 3 : 4, $runCount);
$this->assertFalse($loop->isRunning()); $this->assertFalse($loop->isRunning());

View File

@ -28,7 +28,7 @@ class ResumableTest extends Fixtures
* *
* @dataProvider provideResumable * @dataProvider provideResumable
*/ */
public function testResumable(ResumableInterface $loop): \Generator public function testResumable(ResumableInterface $loop)
{ {
$paused = $loop->resume(); // Returned promise will resolve on next pause $paused = $loop->resume(); // Returned promise will resolve on next pause
@ -36,9 +36,9 @@ class ResumableTest extends Fixtures
$this->assertTrue($loop->start()); $this->assertTrue($loop->start());
$this->assertAfterStart($loop); $this->assertAfterStart($loop);
yield delay(10); delay(10);
$this->assertTrue(self::isResolved($paused)); $this->assertTrue(self::isResolved($paused));
yield delay(100); delay(100);
$this->assertFinal($loop); $this->assertFinal($loop);
} }
@ -59,7 +59,7 @@ class ResumableTest extends Fixtures
$this->assertPreStart($loop); $this->assertPreStart($loop);
$this->assertTrue($loop->start()); $this->assertTrue($loop->start());
yield delay(1); delay(1);
$this->assertFalse(self::isResolved($paused)); // Did not pause $this->assertFalse(self::isResolved($paused)); // Did not pause
// Invert the order as the afterTest assertions will begin the test anew // Invert the order as the afterTest assertions will begin the test anew
@ -77,11 +77,11 @@ class ResumableTest extends Fixtures
* *
* @dataProvider provideResumableInterval * @dataProvider provideResumableInterval
*/ */
public function testResumableForeverPremature(ResumableInterface $loop, ?int $interval, bool $deferred): \Generator public function testResumableForeverPremature(ResumableInterface $loop, ?int $interval, bool $deferred)
{ {
$paused = $deferred ? $loop->resumeDefer() : $loop->resume(); // Will resolve on next pause $paused = $deferred ? $loop->resumeDefer() : $loop->resume(); // Will resolve on next pause
if ($deferred) { if ($deferred) {
yield delay(1); // Avoid resuming after starting delay(1); // Avoid resuming after starting
} }
$loop->setInterval($interval); $loop->setInterval($interval);
@ -90,17 +90,17 @@ class ResumableTest extends Fixtures
$this->assertAfterStart($loop); $this->assertAfterStart($loop);
yield delay(1); delay(1);
$this->assertTrue(self::isResolved($paused)); // Did pause $this->assertTrue(self::isResolved($paused)); // Did pause
$paused = $deferred ? $loop->resumeDefer() : $loop->resume(); $paused = $deferred ? $loop->resumeDefer() : $loop->resume();
if ($deferred) { if ($deferred) {
$this->assertAfterStart($loop); $this->assertAfterStart($loop);
yield delay(1); delay(1);
} }
$this->assertFinal($loop); $this->assertFinal($loop);
yield delay(1); delay(1);
$this->assertFalse(self::isResolved($paused)); // Did not pause again $this->assertFalse(self::isResolved($paused)); // Did not pause again
} }
@ -113,11 +113,11 @@ class ResumableTest extends Fixtures
* *
* @dataProvider provideResumable * @dataProvider provideResumable
*/ */
public function testResumableDeferOnce(ResumableInterface $loop): \Generator public function testResumableDeferOnce(ResumableInterface $loop)
{ {
$paused1 = $loop->resumeDeferOnce(); // Will resolve on next pause $paused1 = $loop->resumeDeferOnce(); // Will resolve on next pause
$paused2 = $loop->resumeDeferOnce(); // Will resolve on next pause $paused2 = $loop->resumeDeferOnce(); // Will resolve on next pause
yield delay(1); // Avoid resuming after starting delay(1); // Avoid resuming after starting
$loop->setInterval(10000); $loop->setInterval(10000);
$this->assertPreStart($loop); $this->assertPreStart($loop);
@ -125,17 +125,17 @@ class ResumableTest extends Fixtures
$this->assertAfterStart($loop); $this->assertAfterStart($loop);
yield delay(1); delay(1);
$this->assertTrue(self::isResolved($paused1)); // Did pause $this->assertTrue(self::isResolved($paused1)); // Did pause
$this->assertTrue(self::isResolved($paused2)); // Did pause $this->assertTrue(self::isResolved($paused2)); // Did pause
$paused1 = $loop->resumeDeferOnce(); $paused1 = $loop->resumeDeferOnce();
$paused2 = $loop->resumeDeferOnce(); $paused2 = $loop->resumeDeferOnce();
$this->assertAfterStart($loop); $this->assertAfterStart($loop);
yield delay(1); delay(1);
$this->assertFinal($loop); $this->assertFinal($loop);
yield delay(1); delay(1);
$this->assertFalse(self::isResolved($paused1)); // Did not pause again $this->assertFalse(self::isResolved($paused1)); // Did not pause again
$this->assertFalse(self::isResolved($paused2)); // Did not pause again $this->assertFalse(self::isResolved($paused2)); // Did not pause again
} }
@ -163,14 +163,14 @@ class ResumableTest extends Fixtures
* *
* @return \Generator * @return \Generator
*/ */
public function provideResumableInterval(): \Generator public function provideResumableInterval()
{ {
foreach ([true, false] as $deferred) { foreach ([true, false] as $deferred) {
foreach ([10000, null] as $interval) { foreach ([10000, null] as $interval) {
foreach ($this->provideResumable() as $params) { foreach ($this->provideResumable() as $params) {
$params[] = $interval; $params[] = $interval;
$params[] = $deferred; $params[] = $deferred;
yield $params; $params;
} }
} }
} }

View File

@ -28,7 +28,7 @@ class SignalTest extends Fixtures
* *
* @dataProvider provideSignal * @dataProvider provideSignal
*/ */
public function testSignal(SignalInterface $loop): \Generator public function testSignal(SignalInterface $loop)
{ {
$loop->setInterval(500); // Wait 0.5 seconds before returning null $loop->setInterval(500); // Wait 0.5 seconds before returning null
@ -59,7 +59,7 @@ class SignalTest extends Fixtures
$loop->setInterval(100); // Wait 0.1 seconds before returning null $loop->setInterval(100); // Wait 0.1 seconds before returning null
$loop->signal(true); // Move along loop to apply new interval $loop->signal(true); // Move along loop to apply new interval
yield delay(110); delay(110);
$this->assertNull($loop->getPayload()); // Result of sleep $this->assertNull($loop->getPayload()); // Result of sleep
$loop->signal($e = new \RuntimeException('Test')); $loop->signal($e = new \RuntimeException('Test'));

View File

@ -55,7 +55,7 @@ trait Basic
public function loop(): Generator public function loop(): Generator
{ {
$this->inited = true; $this->inited = true;
yield delay(100); delay(100);
$this->ran = true; $this->ran = true;
} }
/** /**

View File

@ -39,7 +39,7 @@ trait Resumable
public function loop(): Generator public function loop(): Generator
{ {
$this->inited = true; $this->inited = true;
yield $this->pause($this->interval); $this->pause($this->interval);
$this->ran = true; $this->ran = true;
} }
} }

View File

@ -54,9 +54,9 @@ trait Signal
* *
* @return \Generator * @return \Generator
*/ */
private function testGenerator(int $interval): \Generator private function testGenerator(int $interval)
{ {
yield delay($interval); delay($interval);
} }
/** /**
* Loop implementation. * Loop implementation.
@ -68,7 +68,7 @@ trait Signal
$this->inited = true; $this->inited = true;
try { try {
while (true) { while (true) {
$this->payload = yield $this->waitSignal($this instanceof ResumableLoopInterface ? $this->pause($this->interval) : $this->testGenerator($this->interval)); $this->payload = $this->waitSignal($this instanceof ResumableLoopInterface ? $this->pause($this->interval) : $this->testGenerator($this->interval));
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
$this->exception = $e; $this->exception = $e;