Skip to content

Commit

Permalink
feat: events test
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Sep 29, 2024
1 parent 116210a commit eee0b19
Show file tree
Hide file tree
Showing 4 changed files with 363 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/Events/SwooleEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ class SwooleEvent implements EventInterface
protected int $_timerId = 0;

/**
* @param bool $debug 测试用
* @throws EventLoopException 如果没有启用拓展
*/
public function __construct()
public function __construct(bool $debug = false)
{
if (!extension_loaded('swoole')) {
if (!$debug and !extension_loaded('swoole')) {
throw new EventLoopException('Not support ext-swoole. ');
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Events/SwowEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ class SwowEvent implements EventInterface
protected null|WaitGroup $_waitGroup = null;

/**
* @param bool $debug 测试用
* @throws EventLoopException 如果没有启用拓展
*/
public function __construct()
public function __construct(bool $debug = false)
{
if (!extension_loaded('swow')) {
if (!$debug and !extension_loaded('swow')) {
throw new EventLoopException('Not support ext-swow. ');
}
}
Expand Down
170 changes: 170 additions & 0 deletions tests/EventsCase/SwooleEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

declare(strict_types=1);

namespace Workbunny\Tests\EventsCase;

use PHPUnit\Framework\TestCase;
use Mockery as m;
use Workbunny\WebmanCoroutine\Events\SwooleEvent;
use Workbunny\WebmanCoroutine\Exceptions\EventLoopException;
use Workerman\Events\EventInterface;

/**
* @runTestsInSeparateProcesses
*/
class SwooleEventTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

// 定义缺失的常量
if (!defined('SWOOLE_EVENT_READ')) {
define('SWOOLE_EVENT_READ', 1);
}
if (!defined('SWOOLE_EVENT_WRITE')) {
define('SWOOLE_EVENT_WRITE', 2);
}
}

protected function tearDown(): void
{
m::close();
}

public function testConstructWithoutSwooleExtension()
{
if (extension_loaded('swoole')) {
$this->markTestSkipped('Swoole extension is loaded.');
}
// normal
$this->expectException(EventLoopException::class);
$this->expectExceptionMessage('Not support ext-swoole.');
new SwooleEvent();

// debug
$swooleEvent = new SwooleEvent(true);
$this->assertInstanceOf(SwooleEvent::class, $swooleEvent);
}

public function testAddSignal()
{
$swooleEvent = new SwooleEvent(true);

$processMock = m::mock('alias:Swoole\Process');
$processMock->shouldReceive('signal')->andReturn(true);

$result = $swooleEvent->add(SIGTERM, EventInterface::EV_SIGNAL, function () {
echo 'Signal received';
});

$this->assertTrue($result);
}

public function testAddTimer()
{
$swooleEvent = new SwooleEvent(true);

$timerMock = m::mock('alias:Swoole\Timer');
$timerMock->shouldReceive('after')->andReturn(1);

$result = $swooleEvent->add(1, EventInterface::EV_TIMER, function () {
echo 'Timer triggered';
});

$this->assertEquals(0, $result);
}

public function testAddRead()
{
$swooleEvent = new SwooleEvent(true);

$eventMock = m::mock('alias:Swoole\Event');
$eventMock->shouldReceive('add')->andReturn(true);

$stream = fopen('php://memory', 'r+');
$result = $swooleEvent->add($stream, EventInterface::EV_READ, function () {
echo 'Read event';
});

$this->assertTrue($result);
fclose($stream);
}

public function testDelSignal()
{
$swooleEvent = new SwooleEvent(true);

$processMock = m::mock('alias:Swoole\Process');
$processMock->shouldReceive('signal')->andReturn(true);

$swooleEvent->add(SIGTERM, EventInterface::EV_SIGNAL, function () {
echo 'Signal received';
});

$result = $swooleEvent->del(SIGTERM, EventInterface::EV_SIGNAL);

$this->assertTrue($result);
}

public function testDelTimer()
{
$swooleEvent = new SwooleEvent(true);

$timerMock = m::mock('alias:Swoole\Timer');
$timerMock->shouldReceive('after')->andReturn(1);
$timerMock->shouldReceive('clear')->andReturn(true);

$timerId = $swooleEvent->add(1, EventInterface::EV_TIMER, function () {
echo 'Timer triggered';
});

$result = $swooleEvent->del($timerId, EventInterface::EV_TIMER);

$this->assertTrue($result);
}

public function testLoop()
{
$this->markTestSkipped('loop will exit()');

$this->expectException(\RuntimeException::class);

$swooleEvent = new SwooleEvent(true);
$eventMock = m::mock('alias:Swoole\Event');
$eventMock->shouldReceive('wait')->andReturn(true);

$this->expectOutputString('');
$swooleEvent->loop();
}

public function testDestroy()
{
$swooleEvent = new SwooleEvent(true);

$eventMock = m::mock('alias:Swoole\Event');
$eventMock->shouldReceive('exit')->andReturn(true);

$swooleEvent->destroy();

$this->assertEmpty($swooleEvent->getTimerCount());
}

public function testClearAllTimer()
{
$swooleEvent = new SwooleEvent(true);

$timerMock = m::mock('alias:Swoole\Timer');
$timerMock->shouldReceive('clear')->andReturn(true);
$timerMock->shouldReceive('after')->andReturnSelf();

$swooleEvent->add(1, EventInterface::EV_TIMER, function () {
echo 'Timer triggered';
});

$swooleEvent->clearAllTimer();

$this->assertEquals(0, $swooleEvent->getTimerCount());
}
}
187 changes: 187 additions & 0 deletions tests/EventsCase/SwowEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php

declare(strict_types=1);

namespace Workbunny\Tests\EventsCase;

use PHPUnit\Framework\TestCase;
use Mockery as m;
use Workbunny\WebmanCoroutine\Events\SwowEvent;
use Workbunny\WebmanCoroutine\Exceptions\EventLoopException;
use Workerman\Events\EventInterface;

/**
* @runTestsInSeparateProcesses
*/
class SwowEventTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
// 定义缺失的常量
if (!defined('STREAM_POLLIN')) {
define('STREAM_POLLIN', 1);
}
if (!defined('STREAM_POLLOUT')) {
define('STREAM_POLLOUT', 2);
}
if (!defined('STREAM_POLLHUP')) {
define('STREAM_POLLHUP', 4);
}
if (!defined('STREAM_POLLNONE')) {
define('STREAM_POLLNONE', 0);
}
}

protected function tearDown(): void
{
m::close();
}

public function testConstructWithoutSwowExtension()
{
if (extension_loaded('swow')) {
$this->markTestSkipped('The swow extension is loaded.');
}
// normal
$this->expectException(EventLoopException::class);
$this->expectExceptionMessage('Not support ext-swow.');
new SwowEvent();

// debug
$swowEvent = new SwowEvent(true);
$this->assertInstanceOf(SwowEvent::class, $swowEvent);
}

public function testAddSignal()
{
$swowEvent = new SwowEvent(true);

$coroutineMock = m::mock('alias:Swow\Coroutine');
$coroutineMock->shouldReceive('run')->andReturnSelf();

$signalMock = m::mock('alias:Swow\Signal');
$signalMock->shouldReceive('wait')->andReturn(true);

$result = $swowEvent->add(SIGTERM, EventInterface::EV_SIGNAL, function () {
echo 'Signal received';
});

$this->assertTrue($result);
}

public function testAddTimer()
{
$swowEvent = new SwowEvent(true);

$coroutineMock = m::mock('alias:Swow\Coroutine');
$coroutineMock->shouldReceive('run')->andReturnSelf();
$coroutineMock->shouldReceive('sleep')->andReturn(true);

$result = $swowEvent->add(1, EventInterface::EV_TIMER, function () {
echo 'Timer triggered';
});

$this->assertEquals(0, $result);
}

public function testAddRead()
{
$swowEvent = new SwowEvent(true);

$coroutineMock = m::mock('alias:Swow\Coroutine');
$coroutineMock->shouldReceive('run')->andReturnSelf();

$stream = fopen('php://memory', 'r+');
$result = $swowEvent->add($stream, EventInterface::EV_READ, function () {
echo 'Read event';
});

$this->assertTrue($result);
fclose($stream);
}

public function testDelSignal()
{
$swowEvent = new SwowEvent(true);

$coroutineMock = m::mock('alias:Swow\Coroutine');
$coroutineMock->shouldReceive('run')->andReturnSelf();
$coroutineMock->shouldReceive('kill')->andReturn(true);
$coroutineMock->shouldReceive('isExecuting')->andReturn(true);

$swowEvent->add(SIGTERM, EventInterface::EV_SIGNAL, function () {
echo 'Signal received';
});

$result = $swowEvent->del(SIGTERM, EventInterface::EV_SIGNAL);

$this->assertTrue($result);
}

public function testDelTimer()
{
$swowEvent = new SwowEvent(true);

$coroutineMock = m::mock('alias:Swow\Coroutine');
$coroutineMock->shouldReceive('run')->andReturnSelf();
$coroutineMock->shouldReceive('kill')->andReturn(true);
$coroutineMock->shouldReceive('isExecuting')->andReturn(true);

$timerId = $swowEvent->add(1, EventInterface::EV_TIMER, function () {
echo 'Timer triggered';
});

$result = $swowEvent->del($timerId, EventInterface::EV_TIMER);

$this->assertTrue($result);
}

public function testLoop()
{
$this->markTestSkipped('skip');

$swowEvent = new SwowEvent(true);

$waitGroupMock = m::mock('alias:Swow\Sync\WaitGroup');
$waitGroupMock->shouldReceive('add')->andReturn('add');
$waitGroupMock->shouldReceive('wait')->andReturn(true);

$this->expectOutputString('');
$swowEvent->loop();
}

public function testDestroy()
{
$swowEvent = new SwowEvent(true);

$coroutineMock = m::mock('alias:Swow\Coroutine');
$coroutineMock->shouldReceive('killAll')->andReturn(true);

$waitGroupMock = m::mock('alias:Swow\Sync\WaitGroup');
$waitGroupMock->shouldReceive('done')->andReturn(true);

$swowEvent->destroy();

$this->assertEmpty($swowEvent->getTimerCount());
}

public function testClearAllTimer()
{
$swowEvent = new SwowEvent(true);

$coroutineMock = m::mock('alias:Swow\Coroutine');
$coroutineMock->shouldReceive('kill')->andReturn(true);
$coroutineMock->shouldReceive('isExecuting')->andReturn(true);
$coroutineMock->shouldReceive('run')->andReturnSelf();

$swowEvent->add(1, EventInterface::EV_TIMER, function () {
echo 'Timer triggered';
});

$swowEvent->clearAllTimer();

$this->assertEquals(0, $swowEvent->getTimerCount());
}

}

0 comments on commit eee0b19

Please sign in to comment.