GRPC Java 登录测试

GRPC Java logging in tests

所以,我正在使用 grpc-testin 库进行一些集成测试。当我使用此代码时:

@Rule
public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();

@Test
public void doTest() throws IOException {
        String serverName = InProcessServerBuilder.generateName();

        grpcCleanup.register(InProcessServerBuilder
                .forName(serverName).directExecutor().addService(service).build().start());

        SomeServerGrpc.SomeServerBlockingStub blockingStub = SomeServerGrpc.newBlockingStub(
                grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build()));

...

每当代码中出现异常时,我都会得到

io.grpc.StatusRuntimeException: UNKNOWN

    at io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:233)
    at io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:214)
    at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:139)

如何使 grpc 成为 return 异常的真正原因而不仅仅是 UNKNOWN?

好的,我终于找到方法了:

grpcCleanup.register(InProcessServerBuilder.forName(serverName).intercept(si).directExecutor().addService(service).build().start());

private ServerInterceptor si = new ServerInterceptor() {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            ServerCall<ReqT, RespT> wrappedCall = new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
                @Override
                public void sendMessage(RespT message) {
                    super.sendMessage(message);
                }

                @Override
                public void close(Status status, Metadata trailers) {
                    System.out.println("Interceptor: " + (status.getCause() == null ? "null" : status.getCause().getClass().getName()));
                    if (status.getCode() == Status.Code.UNKNOWN
                            && status.getDescription() == null
                            && status.getCause() != null) {
                        Throwable e = status.getCause();
                        status = Status.INTERNAL
                                .withDescription(e.getMessage())
                                .augmentDescription(stacktraceToString(e));
                    }
                    super.close(status, trailers);
                }
            };

            return next.startCall(wrappedCall, headers);
        }

        private String stacktraceToString(Throwable e) {
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);
            return stringWriter.toString();
        }
    };