diff --git a/src/json-rpc-client/src/Transporter/TcpTransporter.php b/src/json-rpc-client/src/Transporter/FpmTcpTransporter.php similarity index 78% rename from src/json-rpc-client/src/Transporter/TcpTransporter.php rename to src/json-rpc-client/src/Transporter/FpmTcpTransporter.php index 4d53fedfc..594cf5ada 100644 --- a/src/json-rpc-client/src/Transporter/TcpTransporter.php +++ b/src/json-rpc-client/src/Transporter/FpmTcpTransporter.php @@ -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; diff --git a/src/json-rpc-client/tests/Cases/ClientTest.php b/src/json-rpc-client/tests/Cases/ClientTest.php index f82e6aef4..d52e2d2a5 100644 --- a/src/json-rpc-client/tests/Cases/ClientTest.php +++ b/src/json-rpc-client/tests/Cases/ClientTest.php @@ -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(); diff --git a/src/json-rpc-client/tests/Cases/ExampleTest.php b/src/json-rpc-client/tests/Cases/ExampleTest.php deleted file mode 100644 index dc4aff93a..000000000 --- a/src/json-rpc-client/tests/Cases/ExampleTest.php +++ /dev/null @@ -1,40 +0,0 @@ -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(); - } -}