From 85b70691cc380beeafa839be4f10ec513342389f Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Fri, 25 Sep 2020 20:05:12 +0530 Subject: [PATCH] docs(samples): fix flaky test case for undelete table (#757) * docs(samples): fix flaky test case for undelete table * docs(samples): lint --- .../com/example/bigquery/UndeleteTable.java | 22 ++++++++++++++----- .../com/example/bigquery/UndeleteTableIT.java | 5 ++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java b/samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java index 17deac762..cc09afbcb 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java +++ b/samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java @@ -23,7 +23,9 @@ import com.google.cloud.bigquery.CopyJobConfiguration; import com.google.cloud.bigquery.Job; import com.google.cloud.bigquery.JobInfo; +import com.google.cloud.bigquery.Table; import com.google.cloud.bigquery.TableId; +import org.threeten.bp.Instant; // Sample to undeleting a table public class UndeleteTable { @@ -42,15 +44,25 @@ public static void undeleteTable(String datasetName, String tableName, String re // once, and can be reused for multiple requests. BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); - // "Accidentally" delete the table. - bigquery.delete(TableId.of(datasetName, tableName)); - // Record the current time. We'll use this as the snapshot time // for recovering the table. - long snapTime = System.currentTimeMillis(); + long snapshotEpoch = Instant.now().toEpochMilli(); + + // [START_EXCLUDE] + // Due to very short lifecycle of the table, ensure we're not picking a time + // prior to the table creation due to time drift between backend and client. + Table table = bigquery.getTable(TableId.of(datasetName, tableName)); + Long createdEpoch = table.getCreationTime(); + if (createdEpoch > snapshotEpoch) { + snapshotEpoch = createdEpoch; + } + // [END_EXCLUDE] + + // "Accidentally" delete the table. + bigquery.delete(TableId.of(datasetName, tableName)); // Construct the restore-from tableID using a snapshot decorator. - String snapshotTableId = String.format("%s@%d", tableName, snapTime); + String snapshotTableId = String.format("%s@%d", tableName, snapshotEpoch); // Construct and run a copy job. CopyJobConfiguration configuration = diff --git a/samples/snippets/src/test/java/com/example/bigquery/UndeleteTableIT.java b/samples/snippets/src/test/java/com/example/bigquery/UndeleteTableIT.java index 61a91341f..f6c725db6 100644 --- a/samples/snippets/src/test/java/com/example/bigquery/UndeleteTableIT.java +++ b/samples/snippets/src/test/java/com/example/bigquery/UndeleteTableIT.java @@ -77,8 +77,7 @@ public void tearDown() { @Test public void testUndeleteTable() { - // TODO(pmakani): revisit this in the future due to recent flakiness - // UndeleteTable.undeleteTable(BIGQUERY_DATASET_NAME, tableName, recoverTableName); - // assertThat(bout.toString()).contains("Undelete table recovered successfully."); + UndeleteTable.undeleteTable(BIGQUERY_DATASET_NAME, tableName, recoverTableName); + assertThat(bout.toString()).contains("Undelete table recovered successfully."); } }