SRJ
SRJ

Reputation: 2816

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

Upvotes: 0

Views: 1472

Answers (0)

Related Questions