Search in sources :

Example 6 with Color

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

the class GrpcClientCallsTest method testTimeoutToDeadlineConversion.

@Test
public void testTimeoutToDeadlineConversion() {
    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);
    Duration timeout = Duration.ofSeconds(10);
    Deadline minExpectedDeadline = Deadline.after(timeout.getSeconds(), TimeUnit.SECONDS);
    GrpcCallContext context = GrpcCallContext.createDefault().withChannel(mockChannel).withTimeout(timeout);
    GrpcClientCalls.newCall(descriptor, context).start(mockListener, new Metadata());
    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)

Example 7 with Color

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

the class GrpcDirectServerStreamingCallableTest method setUp.

@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);
}
Also used : Money(com.google.type.Money) FakeServiceImpl(com.google.api.gax.grpc.testing.FakeServiceImpl) Color(com.google.type.Color) Before(org.junit.Before)

Example 8 with Color

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

the class GrpcDirectServerStreamingCallableTest method testBlockingServerStreaming.

@Test
public void testBlockingServerStreaming() {
    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 9 with Color

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

the class GrpcDirectStreamingCallableTest method testBidiStreamingClientError.

@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);
    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.
}
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 10 with Color

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

the class GrpcLongRunningTest method testFutureCallPollDoneOnFirst.

@Test
public void testFutureCallPollDoneOnFirst() throws Exception {
    String opName = "testFutureCallPollDoneOnFirst";
    Color resp = getColor(0.5f);
    Money meta = getMoney("UAH");
    Operation initialOperation = getOperation(opName, null, null, false);
    Operation resultOperation = getOperation(opName, resp, meta, true);
    mockResponse(channel, Code.OK, initialOperation, resultOperation);
    OperationCallable<Integer, Color, Money> callable = GrpcCallableFactory.createOperationCallable(createGrpcSettings(), callSettings, initialContext, operationsStub);
    OperationFuture<Color, Money> future = callable.futureCall(2);
    assertFutureSuccessMetaSuccess(opName, future, resp, meta);
    assertThat(executor.getIterationsCount()).isEqualTo(0);
}
Also used : Money(com.google.type.Money) Color(com.google.type.Color) Operation(com.google.longrunning.Operation) 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