Added timeout for FpmTcpTransporter.

This commit is contained in:
李铭昕 2020-07-17 10:05:19 +08:00
parent eb85134ea5
commit fb4e143dd5
3 changed files with 26 additions and 45 deletions

View File

@ -13,7 +13,7 @@ namespace Hyperf\JsonRpcClient\Transporter;
use Hyperf\JsonRpcClient\Exception\ConnectionException;
class TcpTransporter implements TransporterInterface
class FpmTcpTransporter implements TransporterInterface
{
/**
* @var null|resource
@ -30,10 +30,16 @@ class TcpTransporter implements TransporterInterface
*/
protected $port;
public function __construct(string $ip, int $port)
/**
* @var float
*/
protected $timeout;
public function __construct(string $ip, int $port, float $timeout = 1.0)
{
$this->ip = $ip;
$this->port = $port;
$this->timeout = $timeout;
$this->connect();
}
@ -59,9 +65,10 @@ class TcpTransporter implements TransporterInterface
fclose($this->client);
unset($this->client);
}
$client = stream_socket_client("tcp://{$this->ip}:{$this->port}");
$client = stream_socket_client("tcp://{$this->ip}:{$this->port}", $errno, $errstr, $this->timeout);
if ($client === false) {
throw new ConnectionException('Connect failed.');
throw new ConnectionException(sprintf('[%d] %s', $errno, $errstr));
}
$this->client = $client;

View File

@ -13,8 +13,9 @@ namespace HyperfTest\JsonRpcClient\Cases;
use Hyperf\JsonRpcClient\Exception\ServerException;
use Hyperf\JsonRpcClient\Packer\JsonLengthPacker;
use Hyperf\JsonRpcClient\Transporter\FpmTcpTransporter;
use Hyperf\JsonRpcClient\Transporter\TransporterInterface;
use HyperfTest\Stub\IdGenerator;
use HyperfTest\JsonRpcClient\Stub\IdGenerator;
use PHPUnit\Framework\TestCase;
/**
@ -28,6 +29,19 @@ class ClientTest extends TestCase
\Mockery::close();
}
// public function testRealCall()
// {
// $client = new IdGenerator('IdGenerateService', new FpmTcpTransporter('127.0.0.1', 9502), new JsonLengthPacker());
//
// $ret = $client->id($id = uniqid());
// $this->assertStringContainsString($id, $ret);
//
// $this->expectException(ServerException::class);
// $this->expectExceptionCode(500);
// $this->expectExceptionMessage('Inner Server Error');
// $client->exception();
// }
public function testSendAndRecv()
{
$packer = new JsonLengthPacker();

View File

@ -1,40 +0,0 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\JsonRpcClient\Cases;
use Hyperf\JsonRpcClient\Exception\ServerException;
use Hyperf\JsonRpcClient\Packer\JsonLengthPacker;
use Hyperf\JsonRpcClient\Transporter\TcpTransporter;
use HyperfTest\Stub\IdGenerator;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class ExampleTest extends TestCase
{
public function testExample()
{
$this->assertTrue(true);
// $client = new IdGenerator('IdGenerateService', new TcpTransporter('127.0.0.1', 9502), new JsonLengthPacker());
//
// $ret = $client->id($id = uniqid());
// $this->assertStringContainsString($id, $ret);
//
// $this->expectException(ServerException::class);
// $this->expectExceptionCode(500);
// $this->expectExceptionMessage('Inner Server Error');
// $client->exception();
}
}