2020-07-23 13:26:16 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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 Amp\PHPUnit\AsyncTestCase;
|
|
|
|
use Amp\Promise;
|
|
|
|
use Amp\Success;
|
|
|
|
use danog\Loop\Generic\PeriodicLoop;
|
|
|
|
use danog\Loop\Loop;
|
|
|
|
use danog\Loop\Test\Interfaces\LoggingInterface;
|
|
|
|
use danog\Loop\Test\Traits\Basic;
|
|
|
|
use danog\Loop\Test\Traits\Logging;
|
|
|
|
|
|
|
|
use function Amp\delay;
|
|
|
|
|
|
|
|
class PeriodicTest extends AsyncTestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test basic loop.
|
|
|
|
*
|
|
|
|
* @param bool $stopSig Whether to stop with signal
|
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
*
|
|
|
|
* @dataProvider provideTrueFalse
|
|
|
|
*/
|
2022-12-23 20:45:00 +01:00
|
|
|
public function testGeneric(bool $stopSig)
|
2020-07-23 13:26:16 +02:00
|
|
|
{
|
|
|
|
$runCount = 0;
|
|
|
|
$retValue = false;
|
2020-07-24 19:24:04 +02:00
|
|
|
$callable = function () use (&$runCount, &$retValue, &$zis) {
|
|
|
|
$zis = $this;
|
2020-07-23 13:26:16 +02:00
|
|
|
$runCount++;
|
|
|
|
return $retValue;
|
|
|
|
};
|
2022-12-23 20:45:00 +01:00
|
|
|
$this->fixtureAssertions($callable, $runCount, $retValue, $stopSig, $zis, true);
|
2020-07-24 19:24:04 +02:00
|
|
|
$obj = new class() {
|
|
|
|
public $retValue = false;
|
|
|
|
public $runCount = 0;
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$this->runCount++;
|
|
|
|
return $this->retValue;
|
|
|
|
}
|
|
|
|
};
|
2022-12-23 20:45:00 +01:00
|
|
|
$this->fixtureAssertions([$obj, 'run'], $obj->runCount, $obj->retValue, $stopSig, $zisNew, false);
|
2020-07-24 19:24:04 +02:00
|
|
|
$obj = new class() {
|
|
|
|
public $retValue = false;
|
|
|
|
public $runCount = 0;
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$this->runCount++;
|
|
|
|
return $this->retValue;
|
|
|
|
}
|
|
|
|
};
|
2022-12-23 20:45:00 +01:00
|
|
|
$this->fixtureAssertions(\Closure::fromCallable([$obj, 'run']), $obj->runCount, $obj->retValue, $stopSig, $zisNew, false);
|
2020-07-23 13:26:16 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Test generator loop.
|
|
|
|
*
|
|
|
|
* @param bool $stopSig Whether to stop with signal
|
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
*
|
|
|
|
* @dataProvider provideTrueFalse
|
|
|
|
*/
|
2022-12-23 20:45:00 +01:00
|
|
|
public function testGenerator(bool $stopSig)
|
2020-07-23 13:26:16 +02:00
|
|
|
{
|
|
|
|
$runCount = 0;
|
|
|
|
$retValue = false;
|
2022-12-23 20:45:00 +01:00
|
|
|
$callable = function () use (&$runCount, &$retValue, &$zis) {
|
2020-07-24 19:24:04 +02:00
|
|
|
$zis = $this;
|
2022-12-23 20:45:00 +01:00
|
|
|
delay(1);
|
2020-07-23 13:26:16 +02:00
|
|
|
$runCount++;
|
|
|
|
return $retValue;
|
|
|
|
};
|
2022-12-23 20:45:00 +01:00
|
|
|
$this->fixtureAssertions($callable, $runCount, $retValue, $stopSig, $zis, true);
|
2020-07-24 19:24:04 +02:00
|
|
|
$obj = new class() {
|
|
|
|
public $retValue = false;
|
|
|
|
public $runCount = 0;
|
2022-12-23 20:45:00 +01:00
|
|
|
public function run()
|
2020-07-24 19:24:04 +02:00
|
|
|
{
|
2022-12-23 20:45:00 +01:00
|
|
|
delay(1);
|
2020-07-24 19:24:04 +02:00
|
|
|
$this->runCount++;
|
|
|
|
return $this->retValue;
|
|
|
|
}
|
|
|
|
};
|
2022-12-23 20:45:00 +01:00
|
|
|
$this->fixtureAssertions([$obj, 'run'], $obj->runCount, $obj->retValue, $stopSig, $zisNew, false);
|
2020-07-24 19:24:04 +02:00
|
|
|
$obj = new class() {
|
|
|
|
public $retValue = false;
|
|
|
|
public $runCount = 0;
|
2022-12-23 20:45:00 +01:00
|
|
|
public function run()
|
2020-07-24 19:24:04 +02:00
|
|
|
{
|
2022-12-23 20:45:00 +01:00
|
|
|
delay(1);
|
2020-07-24 19:24:04 +02:00
|
|
|
$this->runCount++;
|
|
|
|
return $this->retValue;
|
|
|
|
}
|
|
|
|
};
|
2022-12-23 20:45:00 +01:00
|
|
|
$this->fixtureAssertions(\Closure::fromCallable([$obj, 'run']), $obj->runCount, $obj->retValue, $stopSig, $zisNew, false);
|
2020-07-23 13:26:16 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Test promise loop.
|
|
|
|
*
|
|
|
|
* @param bool $stopSig Whether to stop with signal
|
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
*
|
|
|
|
* @dataProvider provideTrueFalse
|
|
|
|
*/
|
2022-12-23 20:45:00 +01:00
|
|
|
public function testPromise(bool $stopSig)
|
2020-07-23 13:26:16 +02:00
|
|
|
{
|
|
|
|
$runCount = 0;
|
|
|
|
$retValue = false;
|
2020-07-24 19:24:04 +02:00
|
|
|
$callable = function () use (&$runCount, &$retValue, &$zis): Promise {
|
|
|
|
$zis = $this;
|
2020-07-23 13:26:16 +02:00
|
|
|
$runCount++;
|
|
|
|
return new Success($retValue);
|
|
|
|
};
|
2022-12-23 20:45:00 +01:00
|
|
|
$this->fixtureAssertions($callable, $runCount, $retValue, $stopSig, $zis, true);
|
2020-07-24 19:24:04 +02:00
|
|
|
$obj = new class() {
|
|
|
|
public $retValue = false;
|
|
|
|
public $runCount = 0;
|
|
|
|
public function run(): Promise
|
|
|
|
{
|
|
|
|
$this->runCount++;
|
|
|
|
return new Success($this->retValue);
|
|
|
|
}
|
|
|
|
};
|
2022-12-23 20:45:00 +01:00
|
|
|
$this->fixtureAssertions([$obj, 'run'], $obj->runCount, $obj->retValue, $stopSig, $zisNew, false);
|
2020-07-24 19:24:04 +02:00
|
|
|
$obj = new class() {
|
|
|
|
public $retValue = false;
|
|
|
|
public $runCount = 0;
|
|
|
|
public function run(): Promise
|
|
|
|
{
|
|
|
|
$this->runCount++;
|
|
|
|
return new Success($this->retValue);
|
|
|
|
}
|
|
|
|
};
|
2022-12-23 20:45:00 +01:00
|
|
|
$this->fixtureAssertions(\Closure::fromCallable([$obj, 'run']), $obj->runCount, $obj->retValue, $stopSig, $zisNew, false);
|
2020-07-23 13:26:16 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Fixture assertions for started loop.
|
|
|
|
*
|
|
|
|
* @param LoggingInterface $loop Loop
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function fixtureStarted(LoggingInterface $loop): void
|
|
|
|
{
|
|
|
|
$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
|
|
|
|
* @param bool $zis Reference to closure's this
|
|
|
|
* @param bool $checkZis Whether to check zis
|
2020-07-23 13:26:16 +02:00
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
*/
|
2022-12-23 20:45:00 +01:00
|
|
|
private function fixtureAssertions(callable $closure, int &$runCount, bool &$retValue, bool $stopSig, &$zis, bool $checkZis)
|
2020-07-23 13:26:16 +02:00
|
|
|
{
|
|
|
|
$loop = new class($closure, Fixtures::LOOP_NAME, 100) extends PeriodicLoop implements LoggingInterface {
|
|
|
|
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);
|
|
|
|
|
|
|
|
$loop->start();
|
2022-12-23 20:45:00 +01:00
|
|
|
delay(2);
|
2020-07-24 19:24:04 +02:00
|
|
|
if ($checkZis) {
|
|
|
|
$this->assertEquals($loop, $zis);
|
|
|
|
} else {
|
|
|
|
$this->assertNull($zis);
|
|
|
|
}
|
2020-07-23 13:26:16 +02:00
|
|
|
$this->fixtureStarted($loop);
|
|
|
|
|
|
|
|
$this->assertEquals(1, $runCount);
|
|
|
|
|
2022-12-23 20:45:00 +01:00
|
|
|
delay(48);
|
2020-07-23 13:26:16 +02:00
|
|
|
$this->fixtureStarted($loop);
|
|
|
|
|
|
|
|
$this->assertEquals(1, $runCount);
|
|
|
|
|
2022-12-23 20:45:00 +01:00
|
|
|
delay(60);
|
2020-07-23 13:26:16 +02:00
|
|
|
$this->fixtureStarted($loop);
|
|
|
|
|
|
|
|
$this->assertEquals(2, $runCount);
|
|
|
|
|
|
|
|
$loop->resume();
|
2022-12-23 20:45:00 +01:00
|
|
|
delay(1);
|
2020-07-23 13:26:16 +02:00
|
|
|
|
|
|
|
$this->assertEquals(3, $runCount);
|
|
|
|
|
|
|
|
if ($stopSig) {
|
|
|
|
$loop->signal(true);
|
|
|
|
} else {
|
|
|
|
$retValue = true;
|
|
|
|
$loop->resume();
|
|
|
|
}
|
2022-12-23 20:45:00 +01:00
|
|
|
delay(1);
|
2020-07-23 13:26:16 +02:00
|
|
|
$this->assertEquals($stopSig ? 3 : 4, $runCount);
|
|
|
|
|
|
|
|
$this->assertFalse($loop->isRunning());
|
|
|
|
|
|
|
|
$this->assertEquals(1, $loop->startCounter());
|
|
|
|
$this->assertEquals(1, $loop->endCounter());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide true false.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provideTrueFalse(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[true],
|
|
|
|
[false]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|