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);
}
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);
}
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);
}
};
}
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);
}
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);
}
Aggregations