use of com.google.api.gax.longrunning.OperationSnapshot in project java-spanner by googleapis.
the class SpannerSample method cancelCreateBackup.
// [END spanner_create_backup]
// [START spanner_cancel_backup_create]
static void cancelCreateBackup(DatabaseAdminClient dbAdminClient, DatabaseId databaseId, BackupId backupId) {
// Set expire time to 14 days from now.
Timestamp expireTime = Timestamp.ofTimeMicroseconds(TimeUnit.MICROSECONDS.convert(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14), TimeUnit.MILLISECONDS));
// Create a backup instance.
Backup backup = dbAdminClient.newBackupBuilder(backupId).setDatabase(databaseId).setExpireTime(expireTime).build();
// Start the creation of a backup.
System.out.println("Creating backup [" + backup.getId() + "]...");
OperationFuture<Backup, CreateBackupMetadata> op = backup.create();
try {
// Try to cancel the backup operation.
System.out.println("Cancelling create backup operation for [" + backup.getId() + "]...");
dbAdminClient.cancelOperation(op.getName());
// Get a polling future for the running operation. This future will regularly poll the server
// for the current status of the backup operation.
RetryingFuture<OperationSnapshot> pollingFuture = op.getPollingFuture();
// successful or not.
while (!pollingFuture.get().isDone()) {
System.out.println("Waiting for the cancelled backup operation to finish...");
Thread.sleep(TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS));
}
if (pollingFuture.get().getErrorCode() == null) {
// Backup was created before it could be cancelled. Delete the backup.
backup.delete();
System.out.println("Backup operation for [" + backup.getId() + "] successfully finished before it could be cancelled");
} else if (pollingFuture.get().getErrorCode().getCode() == StatusCode.Code.CANCELLED) {
System.out.println("Backup operation for [" + backup.getId() + "] successfully cancelled");
}
} catch (ExecutionException e) {
throw SpannerExceptionFactory.newSpannerException(e.getCause());
} catch (InterruptedException e) {
throw SpannerExceptionFactory.propagateInterrupt(e);
}
}
use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.
the class ProtoOperationTransformersTest method testAnyResponseTransformer_mismatchedTypes.
@Test
public void testAnyResponseTransformer_mismatchedTypes() {
ResponseTransformer<Money> transformer = ResponseTransformer.create(Money.class);
Status status = Status.newBuilder().setCode(Code.OK.value()).build();
OperationSnapshot operationSnapshot = GrpcOperationSnapshot.create(Operation.newBuilder().setResponse(Any.pack(Color.getDefaultInstance())).setError(status).build());
Exception exception = assertThrows(UnknownException.class, () -> transformer.apply(operationSnapshot));
Truth.assertThat(exception).hasMessageThat().contains("encountered a problem unpacking it");
}
use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.
the class ProtoOperationTransformersTest method testAnyResponseTransformer_exception.
@Test
public void testAnyResponseTransformer_exception() {
ResponseTransformer<Money> transformer = ResponseTransformer.create(Money.class);
Money inputMoney = Money.newBuilder().setCurrencyCode("USD").build();
Status status = Status.newBuilder().setCode(Code.UNAVAILABLE.value()).build();
OperationSnapshot operationSnapshot = GrpcOperationSnapshot.create(Operation.newBuilder().setResponse(Any.pack(inputMoney)).setError(status).build());
Exception exception = assertThrows(UnavailableException.class, () -> transformer.apply(operationSnapshot));
Truth.assertThat(exception).hasMessageThat().contains("failed with status = GrpcStatusCode{transportCode=UNAVAILABLE}");
}
use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.
the class ProtoOperationTransformersTest method testAnyMetadataTransformer.
@Test
public void testAnyMetadataTransformer() {
MetadataTransformer<Money> transformer = MetadataTransformer.create(Money.class);
Money inputMoney = Money.newBuilder().setCurrencyCode("USD").build();
OperationSnapshot operationSnapshot = GrpcOperationSnapshot.create(Operation.newBuilder().setMetadata(Any.pack(inputMoney)).build());
Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney);
}
use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.
the class HttpJsonOperationSnapshotCallableTest method futureCallTestException.
@Test
public void futureCallTestException() throws InterruptedException {
Option request = Option.newBuilder().setName("Arizona").build();
Field field = Field.newBuilder().setName("Georgia").build();
ApiCallContext context = mock(ApiCallContext.class);
OperationSnapshot operationSnapshot = HttpJsonOperationSnapshot.newBuilder().setName("California").setMetadata(2).setDone(true).setResponse("Florida").setError(0, "no error").build();
SettableApiFuture<Field> settableApiFuture = SettableApiFuture.create();
settableApiFuture.setException(new RuntimeException("Seattle"));
when(operationSnapshotFactory.create(request, field)).thenReturn(operationSnapshot);
when(innerCallable.futureCall(request, context)).thenReturn(settableApiFuture);
try {
operationSnapCallable.futureCall(request, context).get();
Assert.fail("Exception should have been thrown");
} catch (ExecutionException e) {
Truth.assertThat(e).hasMessageThat().contains("Seattle");
}
}
Aggregations