Skip to content

Commit 85b7069

Browse files
author
Praful Makani
authored
docs(samples): fix flaky test case for undelete table (#757)
* docs(samples): fix flaky test case for undelete table * docs(samples): lint
1 parent 99e5c1f commit 85b7069

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import com.google.cloud.bigquery.CopyJobConfiguration;
2424
import com.google.cloud.bigquery.Job;
2525
import com.google.cloud.bigquery.JobInfo;
26+
import com.google.cloud.bigquery.Table;
2627
import com.google.cloud.bigquery.TableId;
28+
import org.threeten.bp.Instant;
2729

2830
// Sample to undeleting a table
2931
public class UndeleteTable {
@@ -42,15 +44,25 @@ public static void undeleteTable(String datasetName, String tableName, String re
4244
// once, and can be reused for multiple requests.
4345
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
4446

45-
// "Accidentally" delete the table.
46-
bigquery.delete(TableId.of(datasetName, tableName));
47-
4847
// Record the current time. We'll use this as the snapshot time
4948
// for recovering the table.
50-
long snapTime = System.currentTimeMillis();
49+
long snapshotEpoch = Instant.now().toEpochMilli();
50+
51+
// [START_EXCLUDE]
52+
// Due to very short lifecycle of the table, ensure we're not picking a time
53+
// prior to the table creation due to time drift between backend and client.
54+
Table table = bigquery.getTable(TableId.of(datasetName, tableName));
55+
Long createdEpoch = table.getCreationTime();
56+
if (createdEpoch > snapshotEpoch) {
57+
snapshotEpoch = createdEpoch;
58+
}
59+
// [END_EXCLUDE]
60+
61+
// "Accidentally" delete the table.
62+
bigquery.delete(TableId.of(datasetName, tableName));
5163

5264
// Construct the restore-from tableID using a snapshot decorator.
53-
String snapshotTableId = String.format("%s@%d", tableName, snapTime);
65+
String snapshotTableId = String.format("%s@%d", tableName, snapshotEpoch);
5466

5567
// Construct and run a copy job.
5668
CopyJobConfiguration configuration =

samples/snippets/src/test/java/com/example/bigquery/UndeleteTableIT.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ public void tearDown() {
7777

7878
@Test
7979
public void testUndeleteTable() {
80-
// TODO(pmakani): revisit this in the future due to recent flakiness
81-
// UndeleteTable.undeleteTable(BIGQUERY_DATASET_NAME, tableName, recoverTableName);
82-
// assertThat(bout.toString()).contains("Undelete table recovered successfully.");
80+
UndeleteTable.undeleteTable(BIGQUERY_DATASET_NAME, tableName, recoverTableName);
81+
assertThat(bout.toString()).contains("Undelete table recovered successfully.");
8382
}
8483
}

0 commit comments

Comments
 (0)