Search in sources :

Example 16 with Money

use of com.google.type.Money 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 17 with Money

use of com.google.type.Money 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 18 with Money

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

the class GrpcDirectServerStreamingCallableTest method testObserverErrorCancelsCall.

@Test
public void testObserverErrorCancelsCall() throws Throwable {
    final RuntimeException expectedCause = new RuntimeException("some error");
    final SettableApiFuture<Throwable> actualErrorF = SettableApiFuture.create();
    ResponseObserver<Money> moneyObserver = new StateCheckingResponseObserver<Money>() {

        @Override
        protected void onStartImpl(StreamController controller) {
        }

        @Override
        protected void onResponseImpl(Money response) {
            throw expectedCause;
        }

        @Override
        protected void onErrorImpl(Throwable t) {
            actualErrorF.set(t);
        }

        @Override
        protected void onCompleteImpl() {
            actualErrorF.set(null);
        }
    };
    streamingCallable.call(DEFAULT_REQUEST, moneyObserver);
    Throwable actualError = actualErrorF.get(500, TimeUnit.MILLISECONDS);
    Truth.assertThat(actualError).isInstanceOf(ApiException.class);
    Truth.assertThat(((ApiException) actualError).getStatusCode().getCode()).isEqualTo(StatusCode.Code.CANCELLED);
    // grpc is responsible for the immediate cancellation
    Truth.assertThat(actualError.getCause()).isInstanceOf(StatusRuntimeException.class);
    // and the client error is cause for grpc to cancel it
    Truth.assertThat(actualError.getCause().getCause()).isSameInstanceAs(expectedCause);
}
Also used : StreamController(com.google.api.gax.rpc.StreamController) Money(com.google.type.Money) StatusRuntimeException(io.grpc.StatusRuntimeException) StateCheckingResponseObserver(com.google.api.gax.rpc.StateCheckingResponseObserver) Test(org.junit.Test)

Example 19 with Money

use of com.google.type.Money 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 20 with Money

use of com.google.type.Money 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

Money (com.google.type.Money)35 Test (org.junit.Test)28 Color (com.google.type.Color)19 OperationSnapshot (com.google.api.gax.longrunning.OperationSnapshot)13 ManagedChannel (io.grpc.ManagedChannel)8 UnavailableException (com.google.api.gax.rpc.UnavailableException)6 UnknownException (com.google.api.gax.rpc.UnknownException)6 Status (com.google.rpc.Status)6 Metadata (io.grpc.Metadata)6 CallOptions (io.grpc.CallOptions)5 Channel (io.grpc.Channel)5 ClientCall (io.grpc.ClientCall)4 DataLabelingJob (com.google.cloud.aiplatform.v1.DataLabelingJob)3 DatasetName (com.google.cloud.aiplatform.v1.DatasetName)3 JobServiceClient (com.google.cloud.aiplatform.v1.JobServiceClient)3 JobServiceSettings (com.google.cloud.aiplatform.v1.JobServiceSettings)3 LocationName (com.google.cloud.aiplatform.v1.LocationName)3 Value (com.google.protobuf.Value)3 Deadline (io.grpc.Deadline)3 StatusRuntimeException (io.grpc.StatusRuntimeException)3