test: pass grpc test

This commit is contained in:
Reasno 2020-09-14 13:54:53 +08:00
parent 1df8f30585
commit b30f33f018
6 changed files with 22 additions and 18 deletions

View File

@ -102,7 +102,7 @@ class BaseClient
) {
$streamId = retry($this->options['retry_attempts'] ?? 3, function () use ($method, $argument, $options) {
$streamId = $this->send($this->buildRequest($method, $argument, $options));
if ($streamId === 0) {
if ($streamId <= 0) {
$this->init();
// The client should not be used after this exception
throw new GrpcClientException('Failed to send the request to server', StatusCode::INTERNAL);

View File

@ -11,6 +11,9 @@ declare(strict_types=1);
*/
namespace Hyperf\GrpcClient;
use Hyperf\Grpc\StatusCode;
use Hyperf\GrpcClient\Exception\GrpcClientException;
class ClientStreamingCall extends StreamingCall
{
/**
@ -24,7 +27,6 @@ class ClientStreamingCall extends StreamingCall
$this->received = true;
return parent::recv($timeout);
}
trigger_error('ClientStreamingCall can only recv once!', E_USER_ERROR);
return false;
throw new GrpcClientException('ClientStreamingCall can only call recv once!', StatusCode::INTERNAL);
}
}

View File

@ -217,7 +217,7 @@ class GrpcClient
* Open a stream and return the id.
* @param mixed $data
*/
public function openStream(string $path, $data = '', string $method = '', bool $usePipelineread = false): int
public function openStream(string $path, $data = '', string $method = '', bool $usePipelineRead = false): int
{
$method = $method ?: ($data ? 'POST' : 'GET');
$request = new Request($method);
@ -226,7 +226,7 @@ class GrpcClient
$request->data = $data;
}
$request->pipeline = true;
if ($usePipelineread) {
if ($usePipelineRead) {
// @phpstan-ignore-next-line
if (SWOOLE_VERSION_ID < 40503) {
throw new InvalidArgumentException('Require Swoole version >= 4.5.3');

View File

@ -11,6 +11,7 @@ declare(strict_types=1);
*/
namespace Hyperf\GrpcClient;
use Hyperf\Grpc\StatusCode;
use Hyperf\GrpcClient\Exception\GrpcClientException;
/**
@ -21,6 +22,6 @@ class ServerStreamingCall extends StreamingCall
{
public function push($message): void
{
throw new GrpcClientException('ServerStreamingCall can not push data by client');
throw new GrpcClientException('ServerStreamingCall can not push data from client', StatusCode::INTERNAL);
}
}

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Hyperf\GrpcClient;
use Hyperf\Grpc\Parser;
use Hyperf\Grpc\StatusCode;
use Hyperf\GrpcClient\Exception\GrpcClientException;
use RuntimeException;
@ -68,18 +69,20 @@ class StreamingCall
public function send($message = null): void
{
if (! $this->getStreamId()) {
$this->setStreamId($this->client->openStream(
if ($this->getStreamId() <= 0) {
$streamId = $this->client->openStream(
$this->method,
Parser::serializeMessage($message),
'',
true
));
if ($this->getStreamId() <= 0) {
);
if ($streamId <= 0) {
throw $this->newException();
}
$this->setStreamId($streamId);
return;
}
throw new RuntimeException('You can only send once by a streaming call except connection closed and you retry.');
throw new RuntimeException('You can only send a streaming call once unless retrying after the connection is closed.');
}
public function push($message): void
@ -131,6 +134,6 @@ class StreamingCall
private function newException(): GrpcClientException
{
return new GrpcClientException('the remote server may have been disconnected or timed out');
return new GrpcClientException('the remote server may have been disconnected or timed out', StatusCode::INTERNAL);
}
}

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace HyperfTest\GrpcClient;
use Hyperf\Di\Container;
use Hyperf\GrpcClient\Exception\GrpcClientException;
use Hyperf\GrpcClient\StreamingCall;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\ChannelPool;
@ -30,7 +31,6 @@ class RouteGuideClientTest extends TestCase
{
public function testGrpcRouteGuideGetFeature()
{
$this->getContainer();
$client = new RouteGuideClient('127.0.0.1:50051', ['retry_attempts' => 0]);
$point = new Point();
@ -42,7 +42,6 @@ class RouteGuideClientTest extends TestCase
public function testGrpcRouteGuideListFeatures()
{
$this->getContainer();
$client = new RouteGuideClient('127.0.0.1:50051', ['retry_attempts' => 0]);
$hi = new Point();
@ -75,7 +74,6 @@ class RouteGuideClientTest extends TestCase
public function testGrpcRouteGuideRecordRoute()
{
$this->getContainer();
$client = new RouteGuideClient('127.0.0.1:50051', ['retry_attempts' => 0]);
$first = new Point();
@ -97,7 +95,6 @@ class RouteGuideClientTest extends TestCase
public function testGrpcRouteGuideRouteChat()
{
$this->getContainer();
$client = new RouteGuideClient('127.0.0.1:50051', ['retry_attempts' => 0]);
$num = rand(0, 1000000);
@ -120,7 +117,8 @@ class RouteGuideClientTest extends TestCase
$call = $client->routeChat();
$call->push($firstNote);
// 第一个点应该无法收到回复
$this->assertFalse($call->recv(1)[2]);
$this->expectException(GrpcClientException::class);
$call->recv(1);
$call->push($firstNote);
/** @var RouteNote $note */
[$note,] = $call->recv();
@ -132,7 +130,7 @@ class RouteGuideClientTest extends TestCase
$this->assertEquals($second->getLatitude(), $note->getLocation()->getLatitude());
}
protected function getContainer()
public function setUp()
{
$container = \Mockery::mock(Container::class);
$container->shouldReceive('get')->with(ChannelPool::class)->andReturn(new ChannelPool());