diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index a362e62..05aa6e2 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -30,7 +30,7 @@ jobs: run: docker run -d --name=dev-consul -e CONSUL_BIND_INTERFACE=eth0 --net=host consul:1.15.4 - name: Setup Services run: | - docker run -d --name jsonrpc -p 9501:9501 -p 9502:9502 -p 9503:9503 -p 9504:9504 limingxinleo/hyperf-jsonrpc-demo:latest + docker run -d --name jsonrpc -p 9501:9501 -p 9502:9502 -p 9503:9503 -p 9504:9504 -p 9505:9505 iisiam/hyperf-rpc-demo:latest sleep 10 php ./tests/register.php - name: Run Test Cases diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3b21407..5b4e2dd 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -27,5 +27,8 @@ + + + diff --git a/src/Transporter/MultiplexRpcTransporter.php b/src/Transporter/MultiplexRpcTransporter.php index cfd220e..5b89b04 100644 --- a/src/Transporter/MultiplexRpcTransporter.php +++ b/src/Transporter/MultiplexRpcTransporter.php @@ -11,10 +11,8 @@ namespace FriendsOfHyperf\Jet\Transporter; -use Exception; use FriendsOfHyperf\Jet\Exception\ConnectionException; use FriendsOfHyperf\Jet\Exception\RecvFailedException; -use RuntimeException; class MultiplexRpcTransporter extends StreamSocketTransporter { @@ -45,7 +43,7 @@ public function receive() } /** - * @throws Exception + * @throws \Exception */ private function readBytes(int $length): string { @@ -58,7 +56,7 @@ private function readBytes(int $length): string $selected = stream_select($read, $write, $except, $this->timeout); if ($selected === false) { - throw new RuntimeException('Failed to select stream.'); + throw new \RuntimeException('Failed to select stream.'); } if ($selected === 0) { diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 9b5505b..2c1ada5 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -12,8 +12,10 @@ namespace FriendsOfHyperf\Jet\Tests; use FriendsOfHyperf\Jet\ClientFactory; +use FriendsOfHyperf\Jet\DataFormatter\MultiplexDataFormatter; use FriendsOfHyperf\Jet\Metadata; use FriendsOfHyperf\Jet\MetadataManager; +use FriendsOfHyperf\Jet\Packer\JsonMultiplexPacker; use FriendsOfHyperf\Jet\RegistryManager; /** @@ -65,6 +67,21 @@ public function testCalculatorServiceByStreamSocketTransporter() $this->assertSame($a + $b, $client->add($a, $b)); } + public function testCalculatorServiceByMultiplexRpcTransporter() + { + $client = ClientFactory::create( + $this->service, + $this->createMultiplexRpcTransporter(), + new JsonMultiplexPacker(), + new MultiplexDataFormatter() + ); + + $a = rand(1, 99); + $b = rand(1, 99); + + $this->assertSame($a + $b, $client->add($a, $b)); + } + public function testMetadataManager() { MetadataManager::register( diff --git a/tests/TestCase.php b/tests/TestCase.php index 95e1f3a..2c625e0 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -13,6 +13,7 @@ use FriendsOfHyperf\Jet\Registry\ConsulRegistry; use FriendsOfHyperf\Jet\Transporter\GuzzleHttpTransporter; +use FriendsOfHyperf\Jet\Transporter\MultiplexRpcTransporter; use FriendsOfHyperf\Jet\Transporter\StreamSocketTransporter; /** @@ -37,6 +38,12 @@ class TestCase extends \PHPUnit\Framework\TestCase private $jsonrpcHttpTimeout; + private $jsonrpcLengthCheckHost; + + private $jsonrpcLengthCheckPort; + + private $jsonrpcLengthCheckTimeout; + public function __construct($name = null, array $data = [], $dataName = '') { parent::__construct($name, $data, $dataName); @@ -51,6 +58,10 @@ public function __construct($name = null, array $data = [], $dataName = '') $this->jsonrpcHttpHost = $_ENV['JSONRPC_HTTP_HOST'] ?? '127.0.0.1'; $this->jsonrpcHttpPort = (int) ($_ENV['JSONRPC_HTTP_PORT'] ?? 9502); $this->jsonrpcHttpTimeout = (int) ($_ENV['JSONRPC_HTTP_TIMEOUT'] ?? 2); + + $this->jsonrpcLengthCheckHost = $_ENV['JSONRPC_LENGTH_CHECK_HOST'] ?? '127.0.0.1'; + $this->jsonrpcLengthCheckPort = (int) ($_ENV['JSONRPC_LENGTH_CHECK_PORT'] ?? 9504); + $this->jsonrpcLengthCheckTimeout = (int) ($_ENV['JSONRPC_LENGTH_CHECK_TIMEOUT'] ?? 2); } public function createGuzzleHttpTransporter() @@ -63,6 +74,11 @@ public function createStreamSocketTransporter() return new StreamSocketTransporter($this->jsonrpcHost, $this->jsonrpcPort, $this->jsonrpcTimeout); } + public function createMultiplexRpcTransporter() + { + return new MultiplexRpcTransporter($this->jsonrpcLengthCheckHost, $this->jsonrpcLengthCheckPort, $this->jsonrpcLengthCheckTimeout); + } + protected function createRegistry() { return new ConsulRegistry(['uri' => $this->consulUri, 'timeout' => $this->consulTimeout]);