ClientCalls { public static <ReqT, RespT> ListenableFuture<RespT> futureUnaryCall( ClientCall<ReqT, RespT> call, ReqT param) { GrpcFuture<RespT> responseFuture = new GrpcFuture<RespT>(call); asyncUnaryRequestCall(call, param, new UnaryStreamToFuture<RespT>(responseFuture), false); return responseFuture; } private  ClientCalls(); static void asyncUnaryCall(
      ClientCall<ReqT, RespT> call,
      ReqT param,
      StreamObserver<RespT> observer); static void asyncServerStreamingCall(
      ClientCall<ReqT, RespT> call,
      ReqT param,
      StreamObserver<RespT> responseObserver); static StreamObserver<ReqT> asyncClientStreamingCall(
      ClientCall<ReqT, RespT> call,
      StreamObserver<RespT> responseObserver); static StreamObserver<ReqT> asyncBidiStreamingCall(
      ClientCall<ReqT, RespT> call, StreamObserver<RespT> responseObserver); static RespT blockingUnaryCall(ClientCall<ReqT, RespT> call, ReqT param); static RespT blockingUnaryCall(
      Channel channel, MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, ReqT param); static Iterator<RespT> blockingServerStreamingCall(
      ClientCall<ReqT, RespT> call, ReqT param); static Iterator<RespT> blockingServerStreamingCall(
      Channel channel, MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, ReqT param); static ListenableFuture<RespT> futureUnaryCall(
      ClientCall<ReqT, RespT> call,
      ReqT param);  }
@Test public void unaryFutureCallSuccess() throws Exception { Integer req = 2; ListenableFuture<String> future = ClientCalls.futureUnaryCall(call, req); ArgumentCaptor<ClientCall.Listener<String>> listenerCaptor = ArgumentCaptor.forClass(null); verify(call).start(listenerCaptor.capture(), any(Metadata.class)); ClientCall.Listener<String> listener = listenerCaptor.getValue(); verify(call).sendMessage(req); verify(call).halfClose(); listener.onMessage("bar"); listener.onClose(Status.OK, new Metadata()); assertEquals("bar", future.get()); }
@Test public void unaryFutureCallFailed() throws Exception { Integer req = 2; ListenableFuture<String> future = ClientCalls.futureUnaryCall(call, req); ArgumentCaptor<ClientCall.Listener<String>> listenerCaptor = ArgumentCaptor.forClass(null); verify(call).start(listenerCaptor.capture(), any(Metadata.class)); ClientCall.Listener<String> listener = listenerCaptor.getValue(); listener.onClose(Status.INTERNAL, new Metadata()); try { future.get(); fail("Should fail"); } catch (ExecutionException e) { Status status = Status.fromThrowable(e.getCause()); assertEquals(Status.INTERNAL, status); } }
@Test public void unaryFutureCallCancelled() throws Exception { Integer req = 2; ListenableFuture<String> future = ClientCalls.futureUnaryCall(call, req); ArgumentCaptor<ClientCall.Listener<String>> listenerCaptor = ArgumentCaptor.forClass(null); verify(call).start(listenerCaptor.capture(), any(Metadata.class)); ClientCall.Listener<String> listener = listenerCaptor.getValue(); future.cancel(true); verify(call).cancel("GrpcFuture was cancelled", null); listener.onMessage("bar"); listener.onClose(Status.OK, new Metadata()); try { future.get(); fail("Should fail"); } catch (CancellationException e) { } }