Skip to content

Commit a1bf2a8

Browse files
authored
test: retry backup deletion if optimization is still pending (#1295)
* test: retry backup deletion if optimization is still pending * fix: lint
1 parent 3dfdc0b commit a1bf2a8

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

samples/snippets/src/test/java/com/example/spanner/SpannerSampleIT.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,33 @@ public void testEncryptedDatabaseAndBackupSamples() throws Exception {
451451
} finally {
452452
// Delete the backups from the test instance first, as the instance can only be deleted once
453453
// all backups have been deleted.
454-
for (Backup backup : dbClient.listBackups(instanceId).iterateAll()) {
455-
backup.delete();
456-
}
454+
deleteAllBackups(instanceId);
457455
instanceAdminClient.deleteInstance(instanceId);
458456
}
459457
}
460458

459+
private void deleteAllBackups(String instanceId) throws InterruptedException {
460+
for (Backup backup : dbClient.listBackups(instanceId).iterateAll()) {
461+
int attempts = 0;
462+
while (attempts < 30) {
463+
try {
464+
attempts++;
465+
backup.delete();
466+
break;
467+
} catch (SpannerException e) {
468+
if (e.getErrorCode() == ErrorCode.FAILED_PRECONDITION && e.getMessage()
469+
.contains("Please try deleting the backup once the restore or post-restore optimize "
470+
+ "operations have completed on these databases.")) {
471+
// Wait 30 seconds and then retry.
472+
Thread.sleep(30_000L);
473+
} else {
474+
throw e;
475+
}
476+
}
477+
}
478+
}
479+
}
480+
461481
private String runSampleRunnable(Runnable sample) {
462482
PrintStream stdOut = System.out;
463483
ByteArrayOutputStream bout = new ByteArrayOutputStream();

0 commit comments

Comments
 (0)