Search in sources :

Example 1 with Color

use of com.google.type.Color in project gax-java by googleapis.

the class GrpcDirectStreamingCallableTest method testBidiStreaming.

@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();
}
Also used : Money(com.google.type.Money) Color(com.google.type.Color) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with Color

use of com.google.type.Color in project gax-java by googleapis.

the class GrpcDirectStreamingCallableTest method testBidiStreamingServerError.

@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();
}
Also used : Money(com.google.type.Money) Color(com.google.type.Color) StatusRuntimeException(io.grpc.StatusRuntimeException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with Color

use of com.google.type.Color in project gax-java by googleapis.

the class GrpcDirectStreamingCallableTest method testClientStreaming.

@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();
}
Also used : Money(com.google.type.Money) Color(com.google.type.Color) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with Color

use of com.google.type.Color in project gax-java by googleapis.

the class HttpJsonDirectServerStreamingCallableTest method testBlockingServerStreaming.

@Test
public void testBlockingServerStreaming() {
    MOCK_SERVICE.addResponse(new Money[] { DEFAULT_RESPONSE });
    Color request = Color.newBuilder().setRed(0.5f).build();
    ServerStream<Money> response = streamingCallable.call(request);
    List<Money> responseData = Lists.newArrayList(response);
    Money expected = Money.newBuilder().setCurrencyCode("USD").setUnits(127).build();
    Truth.assertThat(responseData).containsExactly(expected);
}
Also used : Money(com.google.type.Money) Color(com.google.type.Color) Test(org.junit.Test)

Example 5 with Color

use of com.google.type.Color in project gax-java by googleapis.

the class GrpcClientCallsTest method testTimeoutBeforeDeadline.

@Test
public void testTimeoutBeforeDeadline() {
    MethodDescriptor<Color, Money> descriptor = FakeServiceGrpc.METHOD_RECOGNIZE;
    @SuppressWarnings("unchecked") ClientCall<Color, Money> mockClientCall = Mockito.mock(ClientCall.class);
    @SuppressWarnings("unchecked") ClientCall.Listener<Money> mockListener = Mockito.mock(ClientCall.Listener.class);
    Channel mockChannel = Mockito.mock(ManagedChannel.class);
    ArgumentCaptor<CallOptions> capturedCallOptions = ArgumentCaptor.forClass(CallOptions.class);
    Mockito.when(mockChannel.newCall(Mockito.eq(descriptor), capturedCallOptions.capture())).thenReturn(mockClientCall);
    // Configure a timeout that occurs before the grpc deadline
    Duration timeout = Duration.ofSeconds(5);
    Deadline subsequentDeadline = Deadline.after(10, TimeUnit.SECONDS);
    Deadline minExpectedDeadline = Deadline.after(timeout.getSeconds(), TimeUnit.SECONDS);
    GrpcCallContext context = GrpcCallContext.createDefault().withChannel(mockChannel).withCallOptions(CallOptions.DEFAULT.withDeadline(subsequentDeadline)).withTimeout(timeout);
    GrpcClientCalls.newCall(descriptor, context).start(mockListener, new Metadata());
    // Verify that the timeout is ignored
    Deadline maxExpectedDeadline = Deadline.after(timeout.getSeconds(), TimeUnit.SECONDS);
    Truth.assertThat(capturedCallOptions.getValue().getDeadline()).isAtLeast(minExpectedDeadline);
    Truth.assertThat(capturedCallOptions.getValue().getDeadline()).isAtMost(maxExpectedDeadline);
}
Also used : Color(com.google.type.Color) ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) Deadline(io.grpc.Deadline) Metadata(io.grpc.Metadata) Duration(org.threeten.bp.Duration) CallOptions(io.grpc.CallOptions) Money(com.google.type.Money) ClientCall(io.grpc.ClientCall) Test(org.junit.Test)

Aggregations

Color (com.google.type.Color)19 Money (com.google.type.Money)19 Test (org.junit.Test)15 ManagedChannel (io.grpc.ManagedChannel)8 Metadata (io.grpc.Metadata)6 CallOptions (io.grpc.CallOptions)5 Channel (io.grpc.Channel)5 ClientCall (io.grpc.ClientCall)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Deadline (io.grpc.Deadline)3 Before (org.junit.Before)3 Duration (org.threeten.bp.Duration)3 FakeChannelFactory (com.google.api.gax.grpc.testing.FakeChannelFactory)2 Operation (com.google.longrunning.Operation)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 FakeApiClock (com.google.api.gax.core.FakeApiClock)1 FakeServiceImplBase (com.google.api.gax.grpc.testing.FakeServiceGrpc.FakeServiceImplBase)1 FakeServiceImpl (com.google.api.gax.grpc.testing.FakeServiceImpl)1 OperationSnapshot (com.google.api.gax.longrunning.OperationSnapshot)1 TransportChannel (com.google.api.gax.rpc.TransportChannel)1