Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Jun 2, 2022
1 parent d0a40f0 commit e9a2de3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 313 deletions.
57 changes: 36 additions & 21 deletions test/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,64 @@
namespace WorkBunny\Test;

use PHPUnit\Framework\TestCase;
use WorkBunny\EventLoop\Drivers\AbstractLoop;
use WorkBunny\EventLoop\Drivers\LoopInterface;

abstract class AbstractTest extends TestCase
{
/**
* @var LoopInterface|null
*/
protected ?LoopInterface $loop = null;
/** @var int */
const PHP_DEFAULT_CHUNK_SIZE = 8192;

/** @var float 模拟loop一圈的时长,20ms */
public float $tickTimeout = 0.02;

/** @var ?string */
protected ?string $received = null;

/** @var AbstractLoop|null */
protected ?AbstractLoop $loop = null;

/** 创建循环 */
abstract public function createLoop();

/** 获取循环 */
protected function getLoop(): ? LoopInterface
public function getLoop(): ? AbstractLoop
{
return $this->loop;
}

/** 单次loop */
protected function tickLoop(float $delay = 0.0)
/** @before */
public function setUpLoop()
{
$this->getLoop()->addTimer($delay, 0.0, function () {
$this->getLoop()->destroy();
});

$this->getLoop()->loop();
$this->loop = $this->createLoop();
}

/** 比较时间 */
protected function assertRunSlowerThan(LoopInterface $loop, float $minInterval)
/** 创建socket */
public function createSocketPair()
{
$start = microtime(true);
$domain = (DIRECTORY_SEPARATOR === '\\') ? STREAM_PF_INET : STREAM_PF_UNIX;
$sockets = stream_socket_pair($domain, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);

$this->getLoop()->loop();
foreach ($sockets as $socket) {
if (function_exists('stream_set_read_buffer')) {
stream_set_read_buffer($socket, 0);
}
}
return $sockets;
}

$end = microtime(true);
$interval = $end - $start;
/** 单次loop */
public function tickLoop(float $delay = 0.0)
{
$this->getLoop()->addTimer($delay, 0.0, function () {
$this->getLoop()->destroy();
});

$this->assertLessThan($interval, $minInterval);
$this->getLoop()->loop();
}

/** 比较时间 */
protected function assertRunFasterThan(float $maxInterval)
public function assertRunFasterThan(float $maxInterval)
{
$start = microtime(true);

Expand All @@ -55,6 +70,6 @@ protected function assertRunFasterThan(float $maxInterval)
$end = microtime(true);
$interval = $end - $start;

$this->assertLessThan($maxInterval, $interval);
$this->assertGreaterThan($interval, $maxInterval);
}
}
25 changes: 18 additions & 7 deletions test/EventLoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
namespace WorkBunny\Test;

use WorkBunny\EventLoop\Drivers\EventLoop;
use WorkBunny\Test\Events\TimerTest;

class EventLoopTest extends AbstractLoopTest
{
/** @var null|string */
private ?string $fifoPath = null;

/** 创建循环 */
public function createLoop(): EventLoop
Expand All @@ -20,12 +19,24 @@ public function createLoop(): EventLoop
}

/**
* @after
* 延迟定时器的优先级
* @see TimerTest::testDelayRepeatTimerPriority()
* @return void
*/
public function tearDownFile()
public function testDelayRepeatTimerPriority()
{
if ($this->fifoPath !== null && file_exists($this->fifoPath)) {
unlink($this->fifoPath);
}
$string = '';
$this->getLoop()->addTimer(0.1, 0.1, function () use(&$string){
$string .= 'timer1' . PHP_EOL;
});
$this->getLoop()->addTimer(0.1, 0.1, function () use(&$string){
$string .= 'timer2' . PHP_EOL;
});

# 区别于其他循环,这里需要多等一个模拟周期,否则timer2不能正常输出
$this->tickLoop(0.1 + $this->tickTimeout);

$this->assertEquals('timer1' . PHP_EOL . 'timer2' . PHP_EOL, $string);
}

}
Loading

0 comments on commit e9a2de3

Please sign in to comment.