Added test for socket from process.

This commit is contained in:
李铭昕 2019-10-23 17:35:45 +08:00
parent 1b74847d9e
commit fdc12012f7
3 changed files with 35 additions and 2 deletions

View File

@ -39,4 +39,5 @@ before_script:
script:
- composer analyse src/di src/json-rpc src/tracer
- composer test
- composer test -- --exclude-group NonCoroutine
- vendor/bin/phpunit --group NonCoroutine

View File

@ -33,11 +33,13 @@
<directory suffix="Test.php">./src/model-cache/tests</directory>
<directory suffix="Test.php">./src/paginator/tests</directory>
<directory suffix="Test.php">./src/pool/tests</directory>
<directory suffix="Test.php">./src/protocol/tests</directory>
<directory suffix="Test.php">./src/redis/tests</directory>
<directory suffix="Test.php">./src/rpc/tests</directory>
<directory suffix="Test.php">./src/server/tests</directory>
<directory suffix="Test.php">./src/service-governance/tests</directory>
<directory suffix="Test.php">./src/snowflake/tests</directory>
<directory suffix="Test.php">./src/socket/tests</directory>
<directory suffix="Test.php">./src/task/tests</directory>
<directory suffix="Test.php">./src/testing/tests</directory>
<directory suffix="Test.php">./src/tracer/tests</directory>

View File

@ -18,6 +18,7 @@ use HyperfTest\Socket\Stub\DemoStub;
use Mockery;
use PHPUnit\Framework\TestCase;
use Swoole\Coroutine\Socket as CoSocket;
use Swoole\Process;
/**
* @internal
@ -25,6 +26,11 @@ use Swoole\Coroutine\Socket as CoSocket;
*/
class SocketTest extends TestCase
{
protected function tearDown()
{
Mockery::close();
}
public function testSocketSend()
{
$cosocket = Mockery::mock(CoSocket::class);
@ -43,7 +49,7 @@ class SocketTest extends TestCase
$cosocket = Mockery::mock(CoSocket::class);
$packer = new SerializePacker();
$demo = new DemoStub();
$cosocket->shouldReceive('recvAll')->once()->with(Mockery::any(), Mockery::any())->andReturnUsing(function ($length, $timeout) use ($demo) {
$cosocket->shouldReceive('recvAll')->twice()->with(Mockery::any(), Mockery::any())->andReturnUsing(function ($length, $timeout) use ($demo) {
$this->assertEquals(10, $timeout);
if ($length == SerializePacker::HEAD_LENGTH) {
return pack('N', strlen(serialize($demo)));
@ -55,4 +61,28 @@ class SocketTest extends TestCase
$res = $socket->recv(10.0);
$this->assertEquals($demo, $res);
}
/**
* @group NonCoroutine
*/
public function testProcessSocket()
{
$demo = new DemoStub();
$process = new Process(function (Process $process) use ($demo) {
$socket = new Socket($process->exportSocket(), new SerializePacker());
$ret = $socket->recv();
$this->assertSame($demo->unique, $ret->unique);
$socket->send('end');
}, false, SOCK_STREAM, true);
$process->start();
run(function () use ($process, $demo) {
$socket = new Socket($process->exportSocket(), new SerializePacker());
$ret = $socket->send($demo);
$this->assertSame(81, $ret);
$ret = $socket->recv();
$this->assertSame('end', $ret);
});
}
}