private GrpcCallSettings<Integer, Operation> createGrpcSettings() { return GrpcCallSettings.create(FakeMethodDescriptor.<Integer, Operation>create()); } }
@Test public void testBidiStreamingServerError() throws Exception { BidiStreamingCallable<Color, Money> streamingCallable = GrpcCallableFactory.createBidiStreamingCallable( GrpcCallSettings.create(METHOD_STREAMING_RECOGNIZE_ERROR), null, clientContext); CountDownLatch latch = new CountDownLatch(1); GrpcDirectServerStreamingCallableTest.MoneyObserver moneyObserver = new GrpcDirectServerStreamingCallableTest.MoneyObserver(true, latch); Color request = Color.newBuilder().setRed(0.5f).build(); ClientStream<Color> stream = streamingCallable.splitCall(moneyObserver); stream.send(request); latch.await(20, TimeUnit.SECONDS); assertThat(moneyObserver.error).isNotNull(); assertThat(moneyObserver.error).isInstanceOf(ApiException.class); assertThat(moneyObserver.error.getCause()).isInstanceOf(StatusRuntimeException.class); assertThat(((StatusRuntimeException) moneyObserver.error.getCause()).getStatus()) .isEqualTo(Status.INVALID_ARGUMENT); assertThat(moneyObserver.response).isNull(); }
@Test public void testBidiStreamingClientError() throws Exception { BidiStreamingCallable<Color, Money> streamingCallable = GrpcCallableFactory.createBidiStreamingCallable( GrpcCallSettings.create(METHOD_STREAMING_RECOGNIZE_ERROR), null, clientContext); CountDownLatch latch = new CountDownLatch(1); GrpcDirectServerStreamingCallableTest.MoneyObserver moneyObserver = new GrpcDirectServerStreamingCallableTest.MoneyObserver(true, latch); Color request = Color.newBuilder().setRed(0.5f).build(); ClientStream<Color> stream = streamingCallable.splitCall(moneyObserver); Throwable clientError = new StatusRuntimeException(Status.CANCELLED); stream.closeSendWithError(clientError); latch.await(20, TimeUnit.SECONDS); assertThat(moneyObserver.error).isNotNull(); assertThat(moneyObserver.error).isInstanceOf(ApiException.class); assertThat(((ApiException) moneyObserver.error).getStatusCode().getCode()) .isEqualTo(Code.CANCELLED); assertThat(moneyObserver.response).isNull(); // As of gRPC 1.8, when the client closes, the server gRPC issues // io.grpc.StatusRuntimeException: CANCELLED: cancelled before receiving half close // to the server application, and our error is not propagated. // We don't check the error received by the server; we can't round-trip it. }
@Test public void testClientStreaming() throws Exception { ClientStreamingCallable<Color, Money> streamingCallable = GrpcCallableFactory.createClientStreamingCallable( GrpcCallSettings.create(METHOD_CLIENT_STREAMING_RECOGNIZE), null, clientContext); CountDownLatch latch = new CountDownLatch(1); MoneyObserver moneyObserver = new MoneyObserver(latch); Color request = Color.newBuilder().setRed(0.5f).build(); ApiStreamObserver<Color> requestObserver = streamingCallable.clientStreamingCall(moneyObserver); requestObserver.onNext(request); requestObserver.onCompleted(); latch.await(20, TimeUnit.SECONDS); assertThat(moneyObserver.error).isNull(); Money expected = Money.newBuilder().setCurrencyCode("USD").setUnits(127).build(); assertThat(moneyObserver.response).isEqualTo(expected); assertThat(moneyObserver.completed).isTrue(); }
@Test public void testBidiStreaming() throws Exception { BidiStreamingCallable<Color, Money> streamingCallable = GrpcCallableFactory.createBidiStreamingCallable( GrpcCallSettings.create(METHOD_STREAMING_RECOGNIZE), null, clientContext); CountDownLatch latch = new CountDownLatch(1); GrpcDirectServerStreamingCallableTest.MoneyObserver moneyObserver = new GrpcDirectServerStreamingCallableTest.MoneyObserver(true, latch); Color request = Color.newBuilder().setRed(0.5f).build(); ClientStream<Color> stream = streamingCallable.splitCall(moneyObserver); stream.send(request); stream.closeSend(); latch.await(20, TimeUnit.SECONDS); assertThat(moneyObserver.error).isNull(); Money expected = Money.newBuilder().setCurrencyCode("USD").setUnits(127).build(); assertThat(moneyObserver.response).isEqualTo(expected); assertThat(moneyObserver.completed).isTrue(); }
@Test public void testBadContext() { streamingCallable = GrpcCallableFactory.createServerStreamingCallable( GrpcCallSettings.create(METHOD_SERVER_STREAMING_RECOGNIZE), streamingCallSettings, clientContext .toBuilder() .setDefaultCallContext(FakeCallContext.createDefault()) .build()); CountDownLatch latch = new CountDownLatch(1); MoneyObserver observer = new MoneyObserver(true, latch); thrown.expect(IllegalArgumentException.class); streamingCallable.call(DEFAULT_REQUEST, observer); }
@Before public void setUp() throws InstantiationException, IllegalAccessException, IOException { String serverName = "fakeservice"; FakeServiceImpl serviceImpl = new FakeServiceImpl(); inprocessServer = new InProcessServer<>(serviceImpl, serverName); inprocessServer.start(); channel = InProcessChannelBuilder.forName(serverName).directExecutor().usePlaintext().build(); clientContext = ClientContext.newBuilder() .setTransportChannel(GrpcTransportChannel.create(channel)) .setDefaultCallContext(GrpcCallContext.of(channel, CallOptions.DEFAULT)) .build(); streamingCallSettings = ServerStreamingCallSettings.<Color, Money>newBuilder().build(); streamingCallable = GrpcCallableFactory.createServerStreamingCallable( GrpcCallSettings.create(METHOD_SERVER_STREAMING_RECOGNIZE), streamingCallSettings, clientContext); }
@Test public void createServerStreamingCallableRetryableExceptions() throws Exception { GrpcCallSettings<Color, Money> grpcCallSettings = GrpcCallSettings.create(FakeServiceGrpc.METHOD_STREAMING_RECOGNIZE_ERROR);
@Test public void testResponseMetadataUnaryCall() throws Exception { GrpcCallSettings<Color, Money> grpcCallSettings = GrpcCallSettings.create(FakeServiceGrpc.METHOD_RECOGNIZE); UnaryCallSettings<Color, Money> callSettings = UnaryCallSettings.<Color, Money>newUnaryCallSettingsBuilder().build(); UnaryCallable<Color, Money> callable = GrpcCallableFactory.createUnaryCallable(grpcCallSettings, callSettings, clientContext); Assert.assertNull(requestHeaders); GrpcResponseMetadata responseMetadata = new GrpcResponseMetadata(); callable.call(Color.getDefaultInstance(), responseMetadata.createContextWithHandlers()); Assert.assertNotNull(requestHeaders); Metadata metadata = responseMetadata.getMetadata(); Metadata trailingMetadata = responseMetadata.getTrailingMetadata(); Assert.assertEquals( metadata.get(Key.of(HEADER_KEY, Metadata.ASCII_STRING_MARSHALLER)), HEADER_VALUE); Assert.assertEquals( trailingMetadata.get(Key.of(TRAILER_KEY, Metadata.ASCII_STRING_MARSHALLER)), TRAILER_VALUE); } }