2020-07-21 18:06:19 +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 danog\Loop\Impl\Loop;
|
|
|
|
use danog\Loop\Impl\ResumableLoop;
|
|
|
|
use danog\Loop\Impl\ResumableSignalLoop;
|
|
|
|
use danog\Loop\Impl\SignalLoop;
|
2020-07-21 19:53:57 +02:00
|
|
|
use danog\Loop\Test\Interfaces\BasicInterface;
|
2020-07-21 18:06:19 +02:00
|
|
|
use danog\Loop\Test\Traits\Basic;
|
2020-07-21 19:53:57 +02:00
|
|
|
use danog\Loop\Test\Traits\BasicException;
|
2020-07-21 18:06:19 +02:00
|
|
|
|
|
|
|
use function Amp\delay;
|
|
|
|
|
2020-07-21 19:53:57 +02:00
|
|
|
class LoopTest extends Fixtures
|
2020-07-21 18:06:19 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test basic loop.
|
|
|
|
*
|
|
|
|
* @param BasicInterface $loop Loop
|
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
*
|
|
|
|
* @dataProvider provideBasic
|
|
|
|
*/
|
|
|
|
public function testLoop(BasicInterface $loop): \Generator
|
|
|
|
{
|
2020-07-21 19:12:11 +02:00
|
|
|
$this->assertPreStart($loop);
|
|
|
|
$this->assertTrue($loop->start());
|
|
|
|
$this->assertAfterStart($loop);
|
|
|
|
|
|
|
|
yield delay(110);
|
|
|
|
|
|
|
|
$this->assertFinal($loop);
|
|
|
|
}
|
|
|
|
/**
|
2020-07-21 19:53:57 +02:00
|
|
|
* Test basic exception in loop.
|
2020-07-21 19:12:11 +02:00
|
|
|
*
|
|
|
|
* @param BasicInterface $loop Loop
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*
|
2020-07-21 19:53:57 +02:00
|
|
|
* @dataProvider provideBasicExceptions
|
2020-07-21 19:12:11 +02:00
|
|
|
*/
|
2020-07-21 19:53:57 +02:00
|
|
|
public function testException(BasicInterface $loop): void
|
2020-07-21 19:12:11 +02:00
|
|
|
{
|
2020-07-21 19:53:57 +02:00
|
|
|
$this->expectException(\RuntimeException::class);
|
2020-07-21 18:06:19 +02:00
|
|
|
|
2020-07-21 19:53:57 +02:00
|
|
|
$this->assertPreStart($loop);
|
|
|
|
$this->assertTrue($loop->start());
|
2020-07-21 18:06:19 +02:00
|
|
|
$this->assertFalse($loop->isRunning());
|
|
|
|
|
2020-07-21 19:12:11 +02:00
|
|
|
$this->assertTrue($loop->inited());
|
2020-07-21 19:53:57 +02:00
|
|
|
|
2020-07-21 19:12:11 +02:00
|
|
|
$this->assertEquals(1, $loop->startCounter());
|
|
|
|
$this->assertEquals(1, $loop->endCounter());
|
2020-07-21 18:06:19 +02:00
|
|
|
}
|
2020-07-21 19:53:57 +02:00
|
|
|
|
2020-07-21 18:06:19 +02:00
|
|
|
/**
|
|
|
|
* Provide loop implementations.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provideBasic(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[new class() extends Loop implements BasicInterface {
|
|
|
|
use Basic;
|
|
|
|
}],
|
|
|
|
[new class() extends SignalLoop implements BasicInterface {
|
|
|
|
use Basic;
|
|
|
|
}],
|
|
|
|
[new class() extends ResumableLoop implements BasicInterface {
|
|
|
|
use Basic;
|
|
|
|
}],
|
|
|
|
[new class() extends ResumableSignalLoop implements BasicInterface {
|
|
|
|
use Basic;
|
2020-07-21 19:12:11 +02:00
|
|
|
}]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
/**
|
2020-07-21 19:53:57 +02:00
|
|
|
* Provide loop implementations.
|
2020-07-21 19:12:11 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2020-07-21 19:53:57 +02:00
|
|
|
public function provideBasicExceptions(): array
|
2020-07-21 19:12:11 +02:00
|
|
|
{
|
|
|
|
return [
|
2020-07-21 19:53:57 +02:00
|
|
|
[new class() extends Loop implements BasicInterface {
|
|
|
|
use BasicException;
|
2020-07-21 18:06:19 +02:00
|
|
|
}],
|
2020-07-21 19:53:57 +02:00
|
|
|
[new class() extends SignalLoop implements BasicInterface {
|
|
|
|
use BasicException;
|
|
|
|
}],
|
|
|
|
[new class() extends ResumableLoop implements BasicInterface {
|
|
|
|
use BasicException;
|
2020-07-21 18:06:19 +02:00
|
|
|
}],
|
2020-07-21 19:53:57 +02:00
|
|
|
[new class() extends ResumableSignalLoop implements BasicInterface {
|
|
|
|
use BasicException;
|
|
|
|
}]
|
2020-07-21 18:06:19 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|