Skip to content

Commit

Permalink
docs(samples): fix flaky test case for undelete table (#757)
Browse files Browse the repository at this point in the history
* docs(samples): fix flaky test case for undelete table

* docs(samples): lint
  • Loading branch information
Praful Makani committed Sep 25, 2020
1 parent 99e5c1f commit 85b7069
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Expand Up @@ -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 {
Expand All @@ -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 =
Expand Down
Expand Up @@ -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.");
}
}

0 comments on commit 85b7069

Please sign in to comment.