From fd67db64b5592249f3e7703831443b58904cf305 Mon Sep 17 00:00:00 2001
From: Steve Niemitz <sniemitz@google.com>
Date: Thu, 8 Feb 2024 09:34:31 -0500
Subject: [PATCH] fix: Deflake Backup integration tests due to deleteBackup
 timeouts

---
 .../admin/v2/it/BigtableBackupIT.java         | 47 ++++++++++++-------
 1 file changed, 29 insertions(+), 18 deletions(-)

diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableBackupIT.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableBackupIT.java
index 0e8b0fb3f3..92132911c3 100644
--- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableBackupIT.java
+++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableBackupIT.java
@@ -23,6 +23,7 @@
 
 import com.google.api.gax.batching.Batcher;
 import com.google.api.gax.rpc.ApiException;
+import com.google.api.gax.rpc.DeadlineExceededException;
 import com.google.cloud.Policy;
 import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient;
 import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
@@ -51,6 +52,7 @@
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -104,6 +106,20 @@ public static void tearDownClass() {
     }
   }
 
+  private static void deleteBackupIgnoreErrors(
+      BigtableTableAdminClient tableAdmin, String clusterId, String backupId) {
+    try {
+      tableAdmin.deleteBackup(clusterId, backupId);
+    } catch (DeadlineExceededException ex) {
+      LOGGER.log(Level.WARNING, "Error deleting backup", ex);
+      // Don't rethrow
+    }
+  }
+
+  private void deleteBackupIgnoreErrors(String clusterId, String backupId) {
+    deleteBackupIgnoreErrors(tableAdmin, clusterId, backupId);
+  }
+
   @Test
   public void createAndGetBackupTest() {
     String backupId = prefixGenerator.newPrefix();
@@ -146,7 +162,7 @@ public void createAndGetBackupTest() {
           .isAnyOf(Backup.State.CREATING, Backup.State.READY);
 
     } finally {
-      tableAdmin.deleteBackup(targetCluster, backupId);
+      deleteBackupIgnoreErrors(targetCluster, backupId);
     }
   }
 
@@ -166,8 +182,8 @@ public void listBackupTest() {
           .that(response)
           .containsAtLeast(backupId1, backupId2);
     } finally {
-      tableAdmin.deleteBackup(targetCluster, backupId1);
-      tableAdmin.deleteBackup(targetCluster, backupId2);
+      deleteBackupIgnoreErrors(targetCluster, backupId1);
+      deleteBackupIgnoreErrors(targetCluster, backupId2);
     }
   }
 
@@ -183,24 +199,19 @@ public void updateBackupTest() {
       Backup backup = tableAdmin.updateBackup(req);
       assertWithMessage("Incorrect expire time").that(backup.getExpireTime()).isEqualTo(expireTime);
     } finally {
-      tableAdmin.deleteBackup(targetCluster, backupId);
+      deleteBackupIgnoreErrors(targetCluster, backupId);
     }
   }
 
   @Test
-  public void deleteBackupTest() throws InterruptedException {
+  public void deleteBackupTest() {
     String backupId = prefixGenerator.newPrefix();
 
     tableAdmin.createBackup(createBackupRequest(backupId));
     tableAdmin.deleteBackup(targetCluster, backupId);
 
     try {
-      for (int i = 0; i < BACKOFF_DURATION.length; i++) {
-        tableAdmin.getBackup(targetCluster, backupId);
-
-        LOGGER.info("Wait for " + BACKOFF_DURATION[i] + " seconds for deleting backup " + backupId);
-        Thread.sleep(BACKOFF_DURATION[i] * 1000);
-      }
+      tableAdmin.getBackup(targetCluster, backupId);
       fail("backup was not deleted.");
     } catch (ApiException ex) {
       assertWithMessage("Incorrect exception type")
@@ -240,7 +251,7 @@ public void restoreTableTest() throws InterruptedException, ExecutionException {
             .isEqualTo(restoredTableId);
       }
     } finally {
-      tableAdmin.deleteBackup(targetCluster, backupId);
+      deleteBackupIgnoreErrors(targetCluster, backupId);
       tableAdmin.deleteTable(restoredTableId);
     }
   }
@@ -298,7 +309,7 @@ public void crossInstanceRestoreTest()
         destTableAdmin.awaitOptimizeRestoredTable(result.getOptimizeRestoredTableOperationToken());
         destTableAdmin.getTable(restoredTableId);
       } finally {
-        tableAdmin.deleteBackup(targetCluster, backupId);
+        deleteBackupIgnoreErrors(targetCluster, backupId);
         instanceAdmin.deleteInstance(targetInstance);
       }
     }
@@ -340,8 +351,8 @@ public void copyBackupTest()
           .isAnyOf(Backup.State.CREATING, Backup.State.READY);
 
     } finally {
-      tableAdmin.deleteBackup(targetCluster, copiedBackupId);
-      tableAdmin.deleteBackup(targetCluster, backupId);
+      deleteBackupIgnoreErrors(targetCluster, copiedBackupId);
+      deleteBackupIgnoreErrors(targetCluster, backupId);
     }
   }
 
@@ -395,8 +406,8 @@ public void crossInstanceCopyBackupTest()
             .isAnyOf(Backup.State.CREATING, Backup.State.READY);
 
       } finally {
-        destTableAdmin.deleteBackup(destCluster, copiedBackupId);
-        tableAdmin.deleteBackup(targetCluster, backupId);
+        deleteBackupIgnoreErrors(destTableAdmin, destCluster, copiedBackupId);
+        deleteBackupIgnoreErrors(targetCluster, backupId);
         instanceAdmin.deleteInstance(destInstance);
       }
     }
@@ -430,7 +441,7 @@ public void backupIamTest() {
               "bigtable.backups.restore");
       assertThat(permissions).hasSize(4);
     } finally {
-      tableAdmin.deleteBackup(targetCluster, backupId);
+      deleteBackupIgnoreErrors(targetCluster, backupId);
     }
   }