Search in sources :

Example 21 with OperationSnapshot

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

the class OperationCallableImplTest method testFutureCallPollDoneWithError.

@Test
public void testFutureCallPollDoneWithError() throws Exception {
    String opName = "testFutureCallPollDoneWithError";
    Currency meta = Currency.getInstance("UAH");
    Color resp = getColor(1.0f);
    OperationSnapshot initialOperation = getOperation(opName, resp, null, meta, false, "Already exists error");
    UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, initialOperation);
    StatusCode errorCode = FakeStatusCode.of(StatusCode.Code.ALREADY_EXISTS);
    OperationSnapshot resultOperation = getOperation(opName, null, errorCode, meta, true, "Already exists error");
    LongRunningClient longRunningClient = mockGetOperation(StatusCode.Code.OK, resultOperation);
    OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable(initialCallable, callSettings, initialContext, longRunningClient);
    OperationFuture<Color, Currency> future = callable.futureCall(2, FakeCallContext.createDefault());
    String errorMessage = "Operation with name \"" + opName + "\" failed with status = " + errorCode + " and message = " + "Already exists error";
    assertFutureFailMetaSuccess(future, meta, FakeStatusCode.of(StatusCode.Code.ALREADY_EXISTS), errorMessage);
    assertThat(executor.getIterationsCount()).isEqualTo(0);
}
Also used : Currency(java.util.Currency) Color(java.awt.Color) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) FakeOperationSnapshot(com.google.api.gax.rpc.testing.FakeOperationSnapshot) FakeStatusCode(com.google.api.gax.rpc.testing.FakeStatusCode) Test(org.junit.Test)

Example 22 with OperationSnapshot

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

the class OperationCallableImplTest method testFutureCallPollRPCTimeout.

@Test
public void testFutureCallPollRPCTimeout() throws Exception {
    String opName = "testFutureCallPollRPCTimeout";
    pollingAlgorithm = OperationTimedPollAlgorithm.create(FAST_RECHECKING_SETTINGS.toBuilder().setInitialRpcTimeout(Duration.ofMillis(100)).setMaxRpcTimeout(Duration.ofSeconds(1)).setRpcTimeoutMultiplier(2).setTotalTimeout(Duration.ofSeconds(5L)).build(), clock);
    callSettings = callSettings.toBuilder().setPollingAlgorithm(pollingAlgorithm).build();
    Color resp = getColor(0.5f);
    Currency meta1 = Currency.getInstance("UAH");
    Currency meta2 = Currency.getInstance("USD");
    OperationSnapshot initialOperation = getOperation(opName, null, null, null, false);
    OperationSnapshot resultOperation1 = getOperation(opName, null, null, meta1, false);
    OperationSnapshot resultOperation2 = getOperation(opName, null, null, meta1, false);
    OperationSnapshot resultOperation3 = getOperation(opName, resp, null, meta2, true);
    UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, initialOperation);
    LongRunningClient longRunningClient = Mockito.mock(LongRunningClient.class);
    @SuppressWarnings("unchecked") UnaryCallable<String, OperationSnapshot> getOpCallable = Mockito.mock(UnaryCallable.class);
    ArgumentCaptor<ApiCallContext> callContextCaptor = ArgumentCaptor.forClass(ApiCallContext.class);
    Mockito.when(longRunningClient.getOperationCallable()).thenReturn(getOpCallable);
    Mockito.when(getOpCallable.futureCall(Mockito.<String>any(), callContextCaptor.capture())).thenReturn(ApiFutures.immediateFuture(resultOperation1)).thenReturn(ApiFutures.immediateFuture(resultOperation2)).thenReturn(ApiFutures.immediateFuture(resultOperation3));
    OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable(initialCallable, callSettings, initialContext, longRunningClient);
    callable.futureCall(2, FakeCallContext.createDefault()).get(10, TimeUnit.SECONDS);
    List<Duration> actualTimeouts = Lists.newArrayList();
    for (ApiCallContext callContext : callContextCaptor.getAllValues()) {
        actualTimeouts.add(callContext.getTimeout());
    }
    List<Duration> expectedTimeouts = Lists.newArrayList(Duration.ofMillis(100), Duration.ofMillis(200), Duration.ofMillis(400));
    assertThat(actualTimeouts).isEqualTo(expectedTimeouts);
}
Also used : Color(java.awt.Color) Duration(org.threeten.bp.Duration) Currency(java.util.Currency) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) FakeOperationSnapshot(com.google.api.gax.rpc.testing.FakeOperationSnapshot) Test(org.junit.Test)

Example 23 with OperationSnapshot

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

the class OperationCallableImplTest method setUp.

@Before
public void setUp() throws IOException {
    initialChannel = mock(FakeChannel.class);
    pollTransportChannel = mock(TransportChannel.class);
    TransportChannelProvider operationsChannelProvider = mock(TransportChannelProvider.class);
    when(operationsChannelProvider.getTransportChannel()).thenReturn(pollTransportChannel);
    clock = new FakeApiClock(0L);
    executor = RecordingScheduler.create(clock);
    pollingAlgorithm = OperationTimedPollAlgorithm.create(FAST_RECHECKING_SETTINGS, clock);
    UnaryCallSettings<Integer, OperationSnapshot> initialCallSettings = UnaryCallSettings.<Integer, OperationSnapshot>newUnaryCallSettingsBuilder().setRetrySettings(FAST_RETRY_SETTINGS.toBuilder().setMaxAttempts(1).build()).build();
    callSettings = OperationCallSettings.<Integer, Color, Currency>newBuilder().setInitialCallSettings(initialCallSettings).setResponseTransformer(new ResponseTransformer()).setMetadataTransformer(new MetadataTransformer()).setPollingAlgorithm(pollingAlgorithm).build();
    initialContext = getClientContext(initialChannel, executor);
}
Also used : Color(java.awt.Color) Currency(java.util.Currency) FakeApiClock(com.google.api.gax.core.FakeApiClock) FakeTransportChannel(com.google.api.gax.rpc.testing.FakeTransportChannel) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) FakeOperationSnapshot(com.google.api.gax.rpc.testing.FakeOperationSnapshot) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Before(org.junit.Before)

Example 24 with OperationSnapshot

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

the class OperationCallableImplTest method testCall.

@Test
public void testCall() {
    Color resp = getColor(1.0f);
    Currency meta = Currency.getInstance("UAH");
    OperationSnapshot resultOperation = getOperation("testCall", 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);
    Color response = callable.call(2, FakeCallContext.createDefault());
    Truth.assertThat(response).isEqualTo(resp);
    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 25 with OperationSnapshot

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

the class OperationCallableImplTest method testFutureCallPollCancelOnTimeoutExceeded.

@Test
public void testFutureCallPollCancelOnTimeoutExceeded() throws Exception {
    String opName = "testFutureCallPollCancelOnPollingTimeoutExceeded";
    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);
    OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable(initialCallable, callSettings, initialContext, longRunningClient);
    OperationFuture<Color, Currency> future = callable.futureCall(2, FakeCallContext.createDefault());
    assertFutureCancelMetaCancel(future);
    assertThat(executor.getIterationsCount()).isEqualTo(5);
}
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