Skip to content

RPC 测试 ​

RPC 模块的测试分为 Codec、Client、Server 和端到端测试。

1. 运行测试 ​

bash
# 全部 RPC 测试
./vendor/bin/phpunit tests/rpc

# 单个测试文件
./vendor/bin/phpunit tests/rpc/codec/HttpJsonRpcCodecTest.php

# 单个测试方法
./vendor/bin/phpunit --filter testHandlesJsonRpcRequest tests/rpc/server/JsonRpcHttpRequestHandlerTest.php

2. Codec 测试 ​

重点覆盖:

  • supports() 协议/序列化匹配
  • 请求编码/解码
  • 响应编码/解码
  • 错误码映射
  • @extended、id 回显
  • TarsHttpJsonRpcCodec 的多返回值、out 参数与业务错误码

示例:

php
$codec = new HttpJsonRpcCodec();

$transportRequest = $codec->encodeRequest($request);
$data = json_decode($transportRequest->getContent(), true);

$this->assertSame('2.0', $data['jsonrpc']);
$this->assertSame('demo.DemoService.hello', $data['method']);

参考:

  • tests/rpc/codec/HttpJsonRpcCodecTest.php
  • tests/rpc/codec/TcpJsonRpcCodecTest.php

3. Client 测试 ​

Mock Guzzle ​

php
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Response;
use kuiper\rpc\client\DefaultRpcRequestHandler;
use kuiper\rpc\client\ProxyGenerator;
use kuiper\rpc\client\RpcExecutorFactory;
use kuiper\rpc\codec\CodecRegistry;
use kuiper\rpc\codec\HttpJsonRpcCodec;
use kuiper\rpc\transporter\GuzzleHttpTransporter;

$httpClient = $this->createMock(ClientInterface::class);
$httpClient->method('send')->willReturn(new Response(200, [], json_encode([
    'jsonrpc' => '2.0',
    'result' => ['Hello, Kuiper'], // 服务端约定 result 是数组
    'id' => 1,
])));

$transporter = new GuzzleHttpTransporter($httpClient);
$handler = new DefaultRpcRequestHandler(
    $transporter,
    new CodecRegistry([new HttpJsonRpcCodec()])
);
$executorFactory = new RpcExecutorFactory($handler, []);
$generated = (new ProxyGenerator($executorFactory))
    ->generate(DemoService::class);
$generated->eval();

$service = new ($generated->getClassName())($executorFactory);
$this->assertSame('Hello, Kuiper', $service->hello('Kuiper'));

参考 tests/rpc/client/RpcClientIntegrationTest.php。

Fake Transporter ​

php
class FakeTransporter implements TransporterInterface
{
    public array $requests = [];

    public function send(TransportRequestInterface $request): TransportResponseInterface
    {
        $this->requests[] = $request;

        return new HttpTransportResponse(200, '{"jsonrpc":"2.0","result":[1],"id":1}', []);
    }

    public function sendDirect(mixed $connection, TransportRequestInterface $request): TransportResponseInterface
    {
        return $this->send($request);
    }

    public function close(): void
    {
    }
}

4. Server 测试 ​

RpcServer + InMemoryServiceRegistry ​

php
use kuiper\rpc\codec\CodecRegistry;
use kuiper\rpc\codec\HttpJsonRpcCodec;
use kuiper\rpc\server\DefaultRpcServerResponseFactory;
use kuiper\rpc\server\InMemoryServiceRegistry;
use kuiper\rpc\server\RpcServer;
use kuiper\rpc\server\RpcServerRpcRequestHandler;
use kuiper\rpc\transporter\message\HttpTransportRequest;

$registry = new InMemoryServiceRegistry();
$registry->register('demo.DemoService', new DemoServiceImpl());

$handler = new RpcServerRpcRequestHandler(
    $registry,
    new DefaultRpcServerResponseFactory()
);

$server = new RpcServer(
    new CodecRegistry([new HttpJsonRpcCodec()]),
    $handler
);

$response = $server->handle(new HttpTransportRequest(
    'http://localhost/rpc',
    'POST',
    '{"jsonrpc":"2.0","id":1,"method":"demo.DemoService.hello","params":{"name":"Kuiper"}}',
    []
));

$data = json_decode($response->getContent(), true);
$this->assertSame(['Hello, Kuiper!'], $data['result']);

错误码测试 ​

php
$response = $server->handle(new HttpTransportRequest(
    'http://localhost/rpc',
    'POST',
    '{"jsonrpc":"2.0","id":1,"method":"demo.DemoService.missing","params":[]}',
    []
));

$data = json_decode($response->getContent(), true);
$this->assertSame(-32601, $data['error']['code']);

参数校验测试 ​

php
$response = $server->handle(new HttpTransportRequest(
    'http://localhost/rpc',
    'POST',
    '{"jsonrpc":"2.0","id":1,"method":"demo.DemoService.add","params":{"a":1,"b":"x"}}',
    []
));

$data = json_decode($response->getContent(), true);
$this->assertSame(-32602, $data['error']['code']);

参考:

  • tests/rpc/server/RpcServerTest.php
  • tests/rpc/server/RpcServerRpcRequestHandlerTest.php
  • tests/rpc/server/RpcArgumentValidatorTest.php
  • tests/rpc/server/JsonRpcHttpRequestHandlerTest.php
  • tests/rpc/server/JsonRpcHttpApplicationTest.php

5. PSR-15 桥接测试 ​

php
$psr17 = new \Nyholm\Psr7\Factory\Psr17Factory();
$handler = new JsonRpcHttpRequestHandler($server, $psr17);

$request = (new \Nyholm\Psr7\ServerRequest('POST', 'http://localhost/rpc'))
    ->withHeader('Content-Type', 'application/json')
    ->withBody($psr17->createStream($json));

$response = $handler->handle($request);

覆盖:

  • 正常 JSON-RPC
  • malformed JSON → -32700
  • 非法请求结构 → -32600
  • 内部异常 → -32603
  • 健康检查 / 404 / 405

6. FrankenPHP 端到端测试 ​

启动服务:

bash
cd examples
PORT=18087 frankenphp run --config /tmp/Caddyfile.v9bench

调用:

bash
curl -sS -X POST http://127.0.0.1:18087/rpc \
  -H 'Content-Type: application/json' \
  -d '{"jsonrpc":"2.0","id":1,"method":"demo.DemoService.hello","params":{"name":"Kuiper"}}'

检查:

  • HTTP 200
  • result 为数组
  • id 回显
  • 业务错误码保留
  • 健康检查 GET / 返回 ok

7. 测试建议 ​

  • Codec 测试优先覆盖协议边界:id、@extended、错误码、非法参数
  • Server 测试使用 InMemoryServiceRegistry,避免依赖外部服务
  • Client 测试 mock Guzzle,避免真实网络
  • 中间件单独测试:重试、熔断、服务发现
  • 连接池测试使用 fake connection,避免真实 socket
  • FrankenPHP 端到端测试建议使用固定缓存 IP,避免外部 API 影响

基于 MIT 许可发布