Skip to content
Permalink
Browse files
docs(samples): fix flaky test case for undelete table (#757)
* 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 85b70691cc380beeafa839be4f10ec513342389f
@@ -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 =
@@ -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.