简体   繁体   中英

Issues in Testing gRPC ServerInterceptor In Java

I have an gRPC interceptor written in java

my gRPC interceptor looks like this

public class GrpcServerInterceptor implements ServerInterceptor {
    @Override
    public <R, T> ServerCall.Listener<R> interceptCall(ServerCall<R, T> call,
                                                                 Metadata requestHeaders, ServerCallHandler<R, T> next) {

        if(call == null || next == null)
            return null;

        if(call != null) {
            String actionName = call.getMethodDescriptor().getBareMethodName();
            String serviceName = call.getMethodDescriptor().getServiceName();
            State.Holder.set(State.newBuilder().withControllerName(serviceName).withActionName(actionName).withFramework("grpc").build());
        }

        ServerCall.Listener<R> delegate = next.startCall(call, requestHeaders);

        return new ForwardingServerCallListener.SimpleForwardingServerCallListener<R>(delegate) {
            @Override
            public void onHalfClose() {
                try {
                    super.onHalfClose();
                } catch (Exception e) {
                    call.close(Status.INTERNAL
                            .withCause (e)
                            .withDescription("error message"), new Metadata());
                }
            }
        };
    }
}

I just want to unit test for above interceptor in junit.

I am facing issues around building ServerCall, Metaddata and ServerCallHandler Objects and passing them around.

I tried to create Server Call object like below in my unit test.

      ServerCall serverCall = new ForwardingServerCall() {
        @Override
        protected ServerCall delegate() {
          return null;
        }

        @Override
        public MethodDescriptor getMethodDescriptor() {
          return MethodDescriptor.newBuilder().
              setType(MethodType.UNKNOWN).
              setRequestMarshaller(ProtoUtils.marshaller((StudentRequest.getDefaultInstance()))).
              setResponseMarshaller(ProtoUtils.marshaller(StudentResponse.getDefaultInstance())).
              setFullMethodName(generateFullMethodName("com.test.cloud.sc.grpc.backend.service.StudentServiceImpl", "getStudentInfo")).
              build();
        }
      };

But above codeblock has issues around setting Request and Response Marshaller.

How can i unit test all the scenarios for my interceptor with minimal code setup and I don't want to start grpc server at all?

EDIT 1

How can i improve null check handling in gRPC interceptor?

Many Thanks

Take a look at https://github.com/grpc/grpc-java/blob/master/api/src/test/java/io/grpc/ServerInterceptorsTest.java or https://github.com/grpc/grpc-java/blob/master/core/src/test/java/io/grpc/util/UtilServerInterceptorsTest.java to see how server interceptors are tested.

Regarding null , the grpc framework will not pass null in call or next so I don't see any reason to check. In any case consider using com.google.common.base.Preconditions.checkNotNull from Guava.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM