Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@
<env name="JSONRPC_HOST" value="127.0.0.1" />
<env name="JSONRPC_PORT" value="9503" />
<env name="JSONRPC_TIMEOUT" value="2" />
<env name="JSONRPC_LENGTH_CHECK_HOST" value="127.0.0.1" />
<env name="JSONRPC_LENGTH_CHECK_PORT" value="9505" />
<env name="JSONRPC_LENGTH_CHECK_TIMEOUT" value="2" />
</php>
</phpunit>
6 changes: 2 additions & 4 deletions src/Transporter/MultiplexRpcTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -45,7 +43,7 @@ public function receive()
}

/**
* @throws Exception
* @throws \Exception
*/
private function readBytes(int $length): string
{
Expand All @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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(
Expand Down
16 changes: 16 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use FriendsOfHyperf\Jet\Registry\ConsulRegistry;
use FriendsOfHyperf\Jet\Transporter\GuzzleHttpTransporter;
use FriendsOfHyperf\Jet\Transporter\MultiplexRpcTransporter;
use FriendsOfHyperf\Jet\Transporter\StreamSocketTransporter;

/**
Expand All @@ -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);
Expand All @@ -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()
Expand All @@ -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]);
Expand Down
Loading