2022-12-24 14:36:39 +01:00
|
|
|
<?php declare(strict_types=1);
|
2020-07-23 13:26:16 +02:00
|
|
|
/**
|
|
|
|
* Loop test.
|
|
|
|
*
|
|
|
|
* @author Daniil Gentili <daniil@daniil.it>
|
|
|
|
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
|
|
|
|
* @license https://opensource.org/licenses/MIT MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace danog\Loop\Test;
|
|
|
|
|
|
|
|
use danog\Loop\Loop;
|
2023-01-23 22:18:52 +01:00
|
|
|
use danog\Loop\PeriodicLoop;
|
2020-07-23 13:26:16 +02:00
|
|
|
use danog\Loop\Test\Interfaces\LoggingInterface;
|
|
|
|
use danog\Loop\Test\Traits\Basic;
|
|
|
|
use danog\Loop\Test\Traits\Logging;
|
|
|
|
|
|
|
|
use function Amp\delay;
|
|
|
|
|
2023-01-24 12:23:10 +01:00
|
|
|
class PeriodicTest extends Fixtures
|
2020-07-23 13:26:16 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test basic loop.
|
|
|
|
*
|
|
|
|
* @param bool $stopSig Whether to stop with signal
|
|
|
|
*
|
2022-12-24 15:03:00 +01:00
|
|
|
*
|
2020-07-23 13:26:16 +02:00
|
|
|
*
|
|
|
|
* @dataProvider provideTrueFalse
|
|
|
|
*/
|
2022-12-24 15:03:00 +01:00
|
|
|
public function testGeneric(bool $stopSig): void
|
2020-07-23 13:26:16 +02:00
|
|
|
{
|
|
|
|
$runCount = 0;
|
|
|
|
$retValue = false;
|
2023-01-23 22:18:52 +01:00
|
|
|
$callable = function (?PeriodicLoop $loop) use (&$runCount, &$retValue, &$l) {
|
|
|
|
$l = $loop;
|
2020-07-23 13:26:16 +02:00
|
|
|
$runCount++;
|
|
|
|
return $retValue;
|
|
|
|
};
|
2023-01-23 22:18:52 +01:00
|
|
|
$this->fixtureAssertions($callable, $runCount, $retValue, $stopSig, $l);
|
2020-07-24 19:24:04 +02:00
|
|
|
$obj = new class() {
|
|
|
|
public $retValue = false;
|
|
|
|
public $runCount = 0;
|
2023-01-23 22:18:52 +01:00
|
|
|
public ?PeriodicLoop $loop = null;
|
|
|
|
public function run(PeriodicLoop $periodicLoop)
|
2020-07-24 19:24:04 +02:00
|
|
|
{
|
2023-01-23 22:18:52 +01:00
|
|
|
$this->loop = $periodicLoop;
|
2020-07-24 19:24:04 +02:00
|
|
|
$this->runCount++;
|
|
|
|
return $this->retValue;
|
|
|
|
}
|
|
|
|
};
|
2023-01-23 22:18:52 +01:00
|
|
|
$this->fixtureAssertions([$obj, 'run'], $obj->runCount, $obj->retValue, $stopSig, $obj->loop);
|
2020-07-24 19:24:04 +02:00
|
|
|
$obj = new class() {
|
|
|
|
public $retValue = false;
|
|
|
|
public $runCount = 0;
|
2023-01-23 22:18:52 +01:00
|
|
|
public ?PeriodicLoop $loop = null;
|
|
|
|
public function run(PeriodicLoop $periodicLoop)
|
2020-07-24 19:24:04 +02:00
|
|
|
{
|
2023-01-23 22:18:52 +01:00
|
|
|
$this->loop = $periodicLoop;
|
2020-07-24 19:24:04 +02:00
|
|
|
$this->runCount++;
|
|
|
|
return $this->retValue;
|
|
|
|
}
|
|
|
|
};
|
2023-01-23 22:18:52 +01:00
|
|
|
$this->fixtureAssertions(\Closure::fromCallable([$obj, 'run']), $obj->runCount, $obj->retValue, $stopSig, $obj->loop);
|
2020-07-23 13:26:16 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Test generator loop.
|
|
|
|
*
|
|
|
|
* @param bool $stopSig Whether to stop with signal
|
|
|
|
*
|
2022-12-24 15:03:00 +01:00
|
|
|
*
|
2020-07-23 13:26:16 +02:00
|
|
|
*
|
|
|
|
* @dataProvider provideTrueFalse
|
|
|
|
*/
|
2022-12-24 15:03:00 +01:00
|
|
|
public function testGenerator(bool $stopSig): void
|
2020-07-23 13:26:16 +02:00
|
|
|
{
|
|
|
|
$runCount = 0;
|
|
|
|
$retValue = false;
|
2023-01-23 22:18:52 +01:00
|
|
|
$callable = function (?PeriodicLoop $loop) use (&$runCount, &$retValue, &$l) {
|
|
|
|
$l = $loop;
|
2020-07-23 13:26:16 +02:00
|
|
|
$runCount++;
|
|
|
|
return $retValue;
|
|
|
|
};
|
2023-01-23 22:18:52 +01:00
|
|
|
$this->fixtureAssertions($callable, $runCount, $retValue, $stopSig, $l);
|
2020-07-24 19:24:04 +02:00
|
|
|
$obj = new class() {
|
|
|
|
public $retValue = false;
|
|
|
|
public $runCount = 0;
|
2023-01-23 22:18:52 +01:00
|
|
|
public ?PeriodicLoop $loop = null;
|
|
|
|
public function run(?PeriodicLoop $loop)
|
2020-07-24 19:24:04 +02:00
|
|
|
{
|
2023-01-23 22:18:52 +01:00
|
|
|
$this->loop = $loop;
|
2020-07-24 19:24:04 +02:00
|
|
|
$this->runCount++;
|
|
|
|
return $this->retValue;
|
|
|
|
}
|
|
|
|
};
|
2023-01-23 22:18:52 +01:00
|
|
|
$this->fixtureAssertions([$obj, 'run'], $obj->runCount, $obj->retValue, $stopSig, $obj->loop);
|
2020-07-24 19:24:04 +02:00
|
|
|
$obj = new class() {
|
|
|
|
public $retValue = false;
|
|
|
|
public $runCount = 0;
|
2023-01-23 22:18:52 +01:00
|
|
|
public ?PeriodicLoop $loop = null;
|
|
|
|
public function run(?PeriodicLoop $loop)
|
2020-07-24 19:24:04 +02:00
|
|
|
{
|
2023-01-23 22:18:52 +01:00
|
|
|
$this->loop = $loop;
|
2020-07-24 19:24:04 +02:00
|
|
|
$this->runCount++;
|
|
|
|
return $this->retValue;
|
|
|
|
}
|
|
|
|
};
|
2023-01-23 22:18:52 +01:00
|
|
|
$this->fixtureAssertions(\Closure::fromCallable([$obj, 'run']), $obj->runCount, $obj->retValue, $stopSig, $obj->loop);
|
2020-07-23 13:26:16 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Fixture assertions for started loop.
|
|
|
|
*/
|
2023-01-23 00:24:18 +01:00
|
|
|
private function fixtureStarted(PeriodicLoop&LoggingInterface $loop): void
|
2020-07-23 13:26:16 +02:00
|
|
|
{
|
|
|
|
$this->assertTrue($loop->isRunning());
|
|
|
|
$this->assertEquals(1, $loop->startCounter());
|
|
|
|
$this->assertEquals(0, $loop->endCounter());
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Run fixture assertions.
|
|
|
|
*
|
2020-07-24 19:24:04 +02:00
|
|
|
* @param callable $closure Closure
|
|
|
|
* @param integer $runCount Run count
|
2020-07-23 13:26:16 +02:00
|
|
|
* @param bool $retValue Pause time
|
2020-07-24 19:24:04 +02:00
|
|
|
* @param bool $stopSig Whether to stop with signal
|
2020-07-23 13:26:16 +02:00
|
|
|
*/
|
2023-01-23 22:18:52 +01:00
|
|
|
private function fixtureAssertions(callable $closure, int &$runCount, bool &$retValue, bool $stopSig, ?PeriodicLoop &$l): void
|
2020-07-23 13:26:16 +02:00
|
|
|
{
|
2023-01-23 00:24:18 +01:00
|
|
|
$loop = new class($closure, Fixtures::LOOP_NAME, 0.1) extends PeriodicLoop implements LoggingInterface {
|
2020-07-23 13:26:16 +02:00
|
|
|
use Logging;
|
|
|
|
};
|
|
|
|
$this->assertEquals(Fixtures::LOOP_NAME, "$loop");
|
|
|
|
|
|
|
|
$this->assertFalse($loop->isRunning());
|
|
|
|
$this->assertEquals(0, $loop->startCounter());
|
|
|
|
$this->assertEquals(0, $loop->endCounter());
|
|
|
|
|
|
|
|
$this->assertEquals(0, $runCount);
|
|
|
|
|
2023-01-23 16:05:36 +01:00
|
|
|
$this->assertTrue($loop->start());
|
2020-07-23 13:26:16 +02:00
|
|
|
$this->fixtureStarted($loop);
|
2023-01-24 12:23:10 +01:00
|
|
|
self::waitTick();
|
2023-01-23 22:18:52 +01:00
|
|
|
|
2023-01-24 12:31:57 +01:00
|
|
|
$this->assertTrue($loop->isPaused());
|
2020-07-23 13:26:16 +02:00
|
|
|
$this->assertEquals(1, $runCount);
|
|
|
|
|
2023-01-24 03:49:03 +01:00
|
|
|
$this->assertEquals($loop, $l);
|
|
|
|
|
2022-12-24 17:30:32 +01:00
|
|
|
delay(0.048);
|
2020-07-23 13:26:16 +02:00
|
|
|
$this->fixtureStarted($loop);
|
|
|
|
|
2023-01-24 12:31:57 +01:00
|
|
|
$this->assertTrue($loop->isPaused());
|
2020-07-23 13:26:16 +02:00
|
|
|
$this->assertEquals(1, $runCount);
|
|
|
|
|
2022-12-24 17:30:32 +01:00
|
|
|
delay(0.060);
|
2020-07-23 13:26:16 +02:00
|
|
|
$this->fixtureStarted($loop);
|
|
|
|
|
2023-01-24 12:31:57 +01:00
|
|
|
$this->assertTrue($loop->isPaused());
|
2020-07-23 13:26:16 +02:00
|
|
|
$this->assertEquals(2, $runCount);
|
|
|
|
|
2023-01-23 00:38:49 +01:00
|
|
|
$this->assertTrue($loop->resume());
|
2023-01-24 12:23:10 +01:00
|
|
|
self::waitTick();
|
2020-07-23 13:26:16 +02:00
|
|
|
|
2023-01-24 12:31:57 +01:00
|
|
|
$this->assertTrue($loop->isPaused());
|
2020-07-23 13:26:16 +02:00
|
|
|
$this->assertEquals(3, $runCount);
|
|
|
|
|
|
|
|
if ($stopSig) {
|
2023-01-23 00:38:49 +01:00
|
|
|
$this->assertTrue($loop->stop());
|
2020-07-23 13:26:16 +02:00
|
|
|
} else {
|
|
|
|
$retValue = true;
|
2023-01-23 00:38:49 +01:00
|
|
|
$this->assertTrue($loop->resume());
|
2020-07-23 13:26:16 +02:00
|
|
|
}
|
2023-01-24 12:23:10 +01:00
|
|
|
self::waitTick();
|
2020-07-23 13:26:16 +02:00
|
|
|
$this->assertEquals($stopSig ? 3 : 4, $runCount);
|
|
|
|
|
2023-01-24 12:31:57 +01:00
|
|
|
$this->assertTrue($loop->isPaused());
|
2020-07-23 13:26:16 +02:00
|
|
|
$this->assertFalse($loop->isRunning());
|
|
|
|
|
|
|
|
$this->assertEquals(1, $loop->startCounter());
|
|
|
|
$this->assertEquals(1, $loop->endCounter());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide true false.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function provideTrueFalse(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[true],
|
|
|
|
[false]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|