Search in sources :

Example 16 with OperationSnapshot

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

the class OperationCallableImplTest method testFutureCancelImmediately.

@Test
public void testFutureCancelImmediately() throws Exception {
    int iterationsCount = 3;
    String opName = "testCancelImmediately";
    Color resp = getColor(0.5f);
    Currency meta = Currency.getInstance("UAH");
    OperationSnapshot initialOperation = getOperation(opName, null, null, null, false);
    UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, initialOperation);
    OperationSnapshot[] pollOperations = new OperationSnapshot[iterationsCount];
    for (int i = 0; i < iterationsCount; i++) {
        pollOperations[i] = getOperation(opName, null, null, null, false);
    }
    pollOperations[iterationsCount - 1] = getOperation(opName, resp, null, meta, true);
    LongRunningClient longRunningClient = mockGetOperation(StatusCode.Code.OK, pollOperations);
    CountDownLatch retryScheduledLatch = new CountDownLatch(1);
    LatchCountDownScheduler scheduler = LatchCountDownScheduler.get(retryScheduledLatch, 0L, 20L);
    ClientContext schedulerContext = getClientContext(initialChannel, scheduler);
    OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable(initialCallable, callSettings, schedulerContext, longRunningClient);
    OperationFuture<Color, Currency> future = callable.futureCall(2, FakeCallContext.createDefault());
    while (!future.cancel(true) && !future.isDone()) {
        LockSupport.parkNanos(1000L);
    }
    assertFutureCancelMetaCancel(future);
    scheduler.shutdownNow();
}
Also used : Color(java.awt.Color) CountDownLatch(java.util.concurrent.CountDownLatch) Currency(java.util.Currency) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) FakeOperationSnapshot(com.google.api.gax.rpc.testing.FakeOperationSnapshot) Test(org.junit.Test)

Example 17 with OperationSnapshot

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

the class OperationCallableImplTest method testFutureCallPollDoneOnFirst.

@Test
public void testFutureCallPollDoneOnFirst() throws Exception {
    String opName = "testFutureCallPollDoneOnFirst";
    Color resp = getColor(0.5f);
    Currency meta = Currency.getInstance("UAH");
    OperationSnapshot initialOperation = getOperation(opName, null, null, null, false);
    OperationSnapshot resultOperation = getOperation(opName, resp, null, meta, true);
    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());
    assertFutureSuccessMetaSuccess(opName, future, resp, meta);
    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 18 with OperationSnapshot

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

the class OperationCallableImplTest method testFutureCallPollError.

@Test
public void testFutureCallPollError() throws Exception {
    String opName = "testFutureCallPollError";
    Currency meta = Currency.getInstance("UAH");
    Color resp = getColor(1.0f);
    OperationSnapshot initialOperation = getOperation(opName, resp, null, meta, false);
    UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, initialOperation);
    OperationSnapshot resultOperation = getOperation(opName, resp, null, meta, false);
    LongRunningClient longRunningClient = mockGetOperation(StatusCode.Code.ALREADY_EXISTS, resultOperation);
    OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable(initialCallable, callSettings, initialContext, longRunningClient);
    OperationFuture<Color, Currency> future = callable.futureCall(2, FakeCallContext.createDefault());
    assertFutureFailMetaFail(future, null, FakeStatusCode.of(StatusCode.Code.ALREADY_EXISTS));
    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) Test(org.junit.Test)

Example 19 with OperationSnapshot

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

the class OperationCallableImplTest method testResumeFutureCall.

@Test
public void testResumeFutureCall() throws Exception {
    String opName = "testResumeFutureCall";
    Color resp = getColor(0.5f);
    Currency meta = Currency.getInstance("UAH");
    OperationSnapshot resultOperation = getOperation(opName, resp, null, meta, true);
    LongRunningClient longRunningClient = mockGetOperation(StatusCode.Code.OK, resultOperation);
    ClientContext mockContext = getClientContext(new FakeChannel(), executor);
    OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable(getUnexpectedStartCallable(), callSettings, mockContext, longRunningClient);
    OperationFuture<Color, Currency> future = callable.resumeFutureCall(opName);
    assertFutureSuccessMetaSuccess(opName, future, resp, meta);
    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) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Test(org.junit.Test)

Example 20 with OperationSnapshot

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

the class OperationCallableImplTest method testInitialServerSideCancel.

@Test
public void testInitialServerSideCancel() throws Exception {
    String opName = "testInitialServerSideCancel";
    StatusCode errorCode = FakeStatusCode.of(StatusCode.Code.CANCELLED);
    Currency meta = Currency.getInstance("UAH");
    OperationSnapshot resultOperation = getOperation(opName, null, errorCode, 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());
    String errorMessage = "Operation with name \"" + opName + "\" failed with status = " + errorCode + " and message = " + "null";
    assertFutureFailMetaSuccess(future, meta, FakeStatusCode.of(StatusCode.Code.CANCELLED), 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)

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