1
0
mirror of https://github.com/danog/loop.git synced 2024-12-13 01:37:23 +01:00
loop/test/ResumableTest.php

179 lines
5.2 KiB
PHP
Raw Normal View History

2020-07-21 19:53:57 +02:00
<?php
/**
* Resumable 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;
2020-07-21 21:45:22 +02:00
use danog\Loop\Interfaces\ResumableLoopInterface;
use danog\Loop\ResumableLoop;
use danog\Loop\ResumableSignalLoop;
2020-07-21 19:53:57 +02:00
use danog\Loop\Test\Interfaces\ResumableInterface;
2020-07-21 21:45:22 +02:00
use danog\Loop\Test\Traits\Resumable;
2020-07-21 19:53:57 +02:00
use function Amp\delay;
class ResumableTest extends Fixtures
{
/**
* Test pausing loop.
*
* @param ResumableInterface $loop Loop
*
* @return \Generator
*
* @dataProvider provideResumable
*/
public function testResumable(ResumableInterface $loop): \Generator
{
$paused = $loop->resume(); // Returned promise will resolve on next pause
$this->assertPreStart($loop);
$this->assertTrue($loop->start());
$this->assertAfterStart($loop);
yield delay(10);
$this->assertTrue(self::isResolved($paused));
yield delay(100);
$this->assertFinal($loop);
}
/**
* Test pausing loop with negative value.
*
* @param ResumableInterface $loop Loop
*
* @return \Generator
*
* @dataProvider provideResumable
*/
public function testResumableNegative(ResumableInterface $loop)
{
$paused = $loop->resume(); // Will resolve on next pause
$loop->setInterval(-1); // Will not pause, and finish right away!
$this->assertPreStart($loop);
$this->assertTrue($loop->start());
yield delay(1);
$this->assertFalse(self::isResolved($paused)); // Did not pause
// Invert the order as the afterTest assertions will begin the test anew
$this->assertFinal($loop);
$this->assertAfterStart($loop, false);
}
/**
* Test pausing loop forever, or for 10 seconds, prematurely resuming it.
*
* @param ResumableInterface $loop Loop
* @param ?int $interval Interval
* @param bool $deferred Deferred
*
* @return \Generator
*
* @dataProvider provideResumableInterval
*/
public function testResumableForeverPremature(ResumableInterface $loop, ?int $interval, bool $deferred): \Generator
{
$paused = $deferred ? $loop->resumeDefer() : $loop->resume(); // Will resolve on next pause
if ($deferred) {
yield delay(1); // Avoid resuming after starting
}
$loop->setInterval($interval);
$this->assertPreStart($loop);
$this->assertTrue($loop->start());
$this->assertAfterStart($loop);
yield delay(1);
$this->assertTrue(self::isResolved($paused)); // Did pause
$paused = $deferred ? $loop->resumeDefer() : $loop->resume();
if ($deferred) {
$this->assertAfterStart($loop);
yield delay(1);
}
$this->assertFinal($loop);
yield delay(1);
$this->assertFalse(self::isResolved($paused)); // Did not pause again
}
2021-04-21 15:34:59 +02:00
/**
2021-04-21 15:41:15 +02:00
* Test pausing loop and then resuming it with deferOnce.
2021-04-21 15:34:59 +02:00
*
* @param ResumableInterface $loop Loop
*
* @return \Generator
*
* @dataProvider provideResumable
*/
public function testResumableDeferOnce(ResumableInterface $loop): \Generator
{
$paused1 = $loop->resumeDeferOnce(); // Will resolve on next pause
$paused2 = $loop->resumeDeferOnce(); // Will resolve on next pause
yield delay(1); // Avoid resuming after starting
$loop->setInterval(10000);
$this->assertPreStart($loop);
$this->assertTrue($loop->start());
$this->assertAfterStart($loop);
yield delay(1);
$this->assertTrue(self::isResolved($paused1)); // Did pause
$this->assertTrue(self::isResolved($paused2)); // Did pause
$paused1 = $loop->resumeDeferOnce();
$paused2 = $loop->resumeDeferOnce();
$this->assertAfterStart($loop);
yield delay(1);
$this->assertFinal($loop);
yield delay(1);
$this->assertFalse(self::isResolved($paused1)); // Did not pause again
$this->assertFalse(self::isResolved($paused2)); // Did not pause again
}
2020-07-21 19:53:57 +02:00
/**
* Provide resumable loop implementations.
*
* @return array
2020-07-22 18:24:26 +02:00
*
2020-07-22 18:18:57 +02:00
* @psalm-return array<int, array<int, ResumableInterface>>
2020-07-21 19:53:57 +02:00
*/
public function provideResumable(): array
{
return [
2020-07-21 21:45:22 +02:00
[new class() extends ResumableLoop implements ResumableInterface, ResumableLoopInterface {
use Resumable;
2020-07-21 19:53:57 +02:00
}],
2020-07-21 21:45:22 +02:00
[new class() extends ResumableSignalLoop implements ResumableInterface, ResumableLoopInterface {
use Resumable;
2020-07-21 19:53:57 +02:00
}],
];
}
/**
* Provide resumable loop implementations and interval.
*
* @return \Generator
*/
public function provideResumableInterval(): \Generator
{
foreach ([true, false] as $deferred) {
2020-07-22 18:18:57 +02:00
foreach ([10000, null] as $interval) {
2020-07-21 19:53:57 +02:00
foreach ($this->provideResumable() as $params) {
$params[] = $interval;
$params[] = $deferred;
yield $params;
}
}
}
}
}