Search in sources :

Example 36 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class OperationCallableImplTest method testFutureCallInitialDoneMetaWrongType.

@Test
public void testFutureCallInitialDoneMetaWrongType() throws Exception {
    String opName = "testFutureCallInitialDoneMetaWrongType";
    Color resp = getColor(1.0f);
    Color meta = getColor(1.0f);
    OperationSnapshot resultOperation = getOperation(opName, resp, null, meta, true);
    UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, resultOperation);
    LongRunningClient longRunningClient = new UnsupportedOperationApi();
    OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable(initialCallable, callSettings, initialContext, longRunningClient);
    OperationFuture<Color, Currency> future = callable.futureCall(2, FakeCallContext.createDefault());
    assertFutureSuccessMetaFail(future, resp, FakeStatusCode.of(StatusCode.Code.OK));
    assertThat(executor.getIterationsCount()).isEqualTo(0);
}
Also used : Color(java.awt.Color) Currency(java.util.Currency) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) FakeOperationSnapshot(com.google.api.gax.rpc.testing.FakeOperationSnapshot) Test(org.junit.Test)

Example 37 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class OperationCallableImplTest method testFutureCallInitialOperationUnexpectedFail.

@Test
public void testFutureCallInitialOperationUnexpectedFail() throws Exception {
    String opName = "testFutureCallInitialOperationUnexpectedFail";
    OperationSnapshot initialOperation = getOperation(opName, null, null, null, false);
    OperationSnapshot resultOperation = getOperation(opName, null, null, null, false);
    UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, initialOperation);
    LongRunningClient longRunningClient = mockGetOperation(StatusCode.Code.OK, resultOperation);
    OperationCallableImpl<Integer, Color, Currency> callableImpl = Callables.longRunningOperationImpl(initialCallable, callSettings, initialContext, longRunningClient);
    RuntimeException thrownException = new RuntimeException();
    ApiFuture<OperationSnapshot> initialFuture = ApiFutures.immediateFailedFuture(thrownException);
    OperationFuture<Color, Currency> future = callableImpl.futureCall(initialFuture, FakeCallContext.createDefault());
    assertFutureFailMetaFail(future, RuntimeException.class, null);
    assertThat(executor.getIterationsCount()).isEqualTo(0);
}
Also used : Color(java.awt.Color) Currency(java.util.Currency) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) FakeOperationSnapshot(com.google.api.gax.rpc.testing.FakeOperationSnapshot) Test(org.junit.Test)

Example 38 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class OperationCallableImplTest method mockGetOpSnapshotCallable.

private <RequestT> UnaryCallable<RequestT, OperationSnapshot> mockGetOpSnapshotCallable(final StatusCode.Code returnStatusCode, final OperationSnapshot... results) {
    return new UnaryCallable<RequestT, OperationSnapshot>() {

        private int index = 0;

        @Override
        public ApiFuture<OperationSnapshot> futureCall(RequestT request, ApiCallContext context) {
            FakeCallContext fakeCallContext = (FakeCallContext) context;
            if (fakeCallContext != null && fakeCallContext.getTimeout() != null && fakeCallContext.getTimeout().isZero()) {
                throw new DeadlineExceededException("Invalid timeout of 0 s", null, FakeStatusCode.of(StatusCode.Code.DEADLINE_EXCEEDED), true);
            }
            OperationSnapshot response = results[index];
            if (index < results.length - 1) {
                index += 1;
            }
            return newFuture(returnStatusCode, response);
        }
    };
}
Also used : FakeCallContext(com.google.api.gax.rpc.testing.FakeCallContext) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) FakeOperationSnapshot(com.google.api.gax.rpc.testing.FakeOperationSnapshot)

Example 39 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class OperationCallSettingsTest method testBuilderFromSettings.

@Test
public void testBuilderFromSettings() throws Exception {
    OperationCallSettings.Builder<Integer, String, Long> builder = OperationCallSettings.newBuilder();
    UnaryCallSettings<Integer, OperationSnapshot> initialCallSettings = UnaryCallSettings.<Integer, OperationSnapshot>newUnaryCallSettingsBuilder().setRetryableCodes(Code.UNAVAILABLE).build();
    TimedRetryAlgorithm pollingAlgorithm = Mockito.mock(TimedRetryAlgorithm.class);
    ResponseTransformer responseTransformer = new ResponseTransformer();
    MetadataTransformer metadataTransformer = new MetadataTransformer();
    builder.setPollingAlgorithm(pollingAlgorithm);
    builder.setResponseTransformer(responseTransformer);
    builder.setMetadataTransformer(metadataTransformer);
    builder.setInitialCallSettings(initialCallSettings);
    Truth.assertThat(builder.getInitialCallSettings()).isSameInstanceAs(initialCallSettings);
    OperationCallSettings settings = builder.build();
    OperationCallSettings.Builder newBuilder = settings.toBuilder();
    Truth.assertThat(newBuilder.getPollingAlgorithm()).isSameInstanceAs(pollingAlgorithm);
    Truth.assertThat(newBuilder.getResponseTransformer()).isSameInstanceAs(responseTransformer);
    Truth.assertThat(newBuilder.getMetadataTransformer()).isSameInstanceAs(metadataTransformer);
    Truth.assertThat(newBuilder.getInitialCallSettings()).isNotNull();
    Truth.assertThat(newBuilder.getInitialCallSettings().getRetryableCodes().size()).isEqualTo(1);
}
Also used : TimedRetryAlgorithm(com.google.api.gax.retrying.TimedRetryAlgorithm) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) Test(org.junit.Test)

Example 40 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class OperationCallableImplTest method testFutureCallPollDoneOnMany.

@Test
public void testFutureCallPollDoneOnMany() throws Exception {
    final int iterationsCount = 1000;
    String opName = "testFutureCallPollDoneOnMany";
    Color resp = getColor(0.5f);
    Currency meta = Currency.getInstance("UAH");
    OperationSnapshot initialOperation = getOperation(opName, null, null, null, false);
    OperationSnapshot[] pollOperations = new OperationSnapshot[iterationsCount];
    for (int i = 0; i < iterationsCount - 1; i++) {
        pollOperations[i] = getOperation(opName, null, null, meta, false);
    }
    pollOperations[iterationsCount - 1] = getOperation(opName, resp, null, meta, true);
    UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, initialOperation);
    LongRunningClient longRunningClient = mockGetOperation(StatusCode.Code.OK, pollOperations);
    pollingAlgorithm = OperationTimedPollAlgorithm.create(FAST_RECHECKING_SETTINGS.toBuilder().setTotalTimeout(Duration.ofMillis(iterationsCount)).build(), clock);
    callSettings = callSettings.toBuilder().setPollingAlgorithm(pollingAlgorithm).build();
    OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable(initialCallable, callSettings, initialContext, longRunningClient);
    OperationFuture<Color, Currency> future = callable.futureCall(2, FakeCallContext.createDefault());
    Truth.assertThat(future.get(5, TimeUnit.SECONDS)).isEqualTo(resp);
    assertFutureSuccessMetaSuccess(opName, future, resp, meta);
    assertThat(executor.getIterationsCount()).isEqualTo(iterationsCount - 1);
}
Also used : Color(java.awt.Color) Currency(java.util.Currency) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) FakeOperationSnapshot(com.google.api.gax.rpc.testing.FakeOperationSnapshot) Test(org.junit.Test)

Aggregations

OperationSnapshot (com.google.api.gax.longrunning.OperationSnapshot)55 Test (org.junit.Test)44 FakeOperationSnapshot (com.google.api.gax.rpc.testing.FakeOperationSnapshot)28 Color (java.awt.Color)23 Currency (java.util.Currency)23 Money (com.google.type.Money)13 UnavailableException (com.google.api.gax.rpc.UnavailableException)7 UnknownException (com.google.api.gax.rpc.UnknownException)6 Status (com.google.rpc.Status)6 FakeStatusCode (com.google.api.gax.rpc.testing.FakeStatusCode)4 ExecutionException (java.util.concurrent.ExecutionException)4 FakeApiClock (com.google.api.gax.core.FakeApiClock)2 FakeApiMessage (com.google.api.gax.httpjson.testing.FakeApiMessage)2 TimedRetryAlgorithm (com.google.api.gax.retrying.TimedRetryAlgorithm)2 ApiCallContext (com.google.api.gax.rpc.ApiCallContext)2 ApiException (com.google.api.gax.rpc.ApiException)2 FakeChannel (com.google.api.gax.rpc.testing.FakeChannel)2 CancellationException (java.util.concurrent.CancellationException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 BatchingSettings (com.google.api.gax.batching.BatchingSettings)1