-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
363 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |