Skip to content

Commit 1ee87d5

Browse files
authored
test: increased timeout and added retries to flaky tests (GoogleCloudPlatform#7491)
* test: add retries to flaky tests * test: Fix lint issue * test: add logging * test: add extra retry to failing test * test: update timeout to 10 min * test: revert debugging * refactor dataset creation
1 parent 1e60a1c commit 1ee87d5

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

asset/src/test/java/com/example/asset/QuickStartIT.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,35 @@
2222
import com.google.cloud.asset.v1.ContentType;
2323
import com.google.cloud.bigquery.BigQuery;
2424
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
25+
import com.google.cloud.bigquery.BigQueryException;
2526
import com.google.cloud.bigquery.BigQueryOptions;
26-
import com.google.cloud.bigquery.Dataset;
2727
import com.google.cloud.bigquery.DatasetId;
2828
import com.google.cloud.bigquery.DatasetInfo;
2929
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
3030
import com.google.cloud.storage.BlobInfo;
3131
import com.google.cloud.storage.Storage;
3232
import com.google.cloud.storage.Storage.BlobListOption;
3333
import com.google.cloud.storage.StorageOptions;
34+
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
3435
import java.io.ByteArrayOutputStream;
3536
import java.io.PrintStream;
3637
import java.util.UUID;
38+
import java.util.concurrent.TimeUnit;
3739
import org.junit.After;
3840
import org.junit.Before;
41+
import org.junit.Rule;
3942
import org.junit.Test;
43+
import org.junit.rules.Timeout;
4044
import org.junit.runner.RunWith;
4145
import org.junit.runners.JUnit4;
4246

4347
/** Tests for quickstart sample. */
4448
@RunWith(JUnit4.class)
4549
@SuppressWarnings("checkstyle:abbreviationaswordinname")
4650
public class QuickStartIT {
51+
@Rule public final Timeout testTimeout = new Timeout(10, TimeUnit.MINUTES);
52+
@Rule public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);
53+
4754
private static final String bucketName = "java-docs-samples-testing";
4855
private static final String path = UUID.randomUUID().toString();
4956
private static final String datasetName = RemoteBigQueryHelper.generateDatasetName();
@@ -69,9 +76,6 @@ private static final void deleteObjects() {
6976
@Before
7077
public void setUp() {
7178
bigquery = BigQueryOptions.getDefaultInstance().getService();
72-
if (bigquery.getDataset(datasetName) == null) {
73-
Dataset dataset = bigquery.create(DatasetInfo.newBuilder(datasetName).build());
74-
}
7579
bout = new ByteArrayOutputStream();
7680
out = new PrintStream(bout);
7781
originalPrintStream = System.out;
@@ -98,8 +102,7 @@ public void testExportAssetExample() throws Exception {
98102

99103
@Test
100104
public void testExportAssetBigqueryPerTypeExample() throws Exception {
101-
String dataset =
102-
String.format("projects/%s/datasets/%s", ServiceOptions.getDefaultProjectId(), datasetName);
105+
String dataset = getDataset();
103106
String table = "java_test_per_type";
104107
ExportAssetsBigqueryExample.exportBigQuery(
105108
dataset, table, ContentType.RESOURCE, /*perType*/ true);
@@ -109,8 +112,7 @@ public void testExportAssetBigqueryPerTypeExample() throws Exception {
109112

110113
@Test
111114
public void testExportAssetBigqueryExample() throws Exception {
112-
String dataset =
113-
String.format("projects/%s/datasets/%s", ServiceOptions.getDefaultProjectId(), datasetName);
115+
String dataset = getDataset();
114116
String table = "java_test";
115117
ExportAssetsBigqueryExample.exportBigQuery(
116118
dataset, table, ContentType.RESOURCE, /*perType*/ false);
@@ -127,4 +129,13 @@ public void testBatchGetAssetsHistory() throws Exception {
127129
assertThat(got).contains(bucketAssetName);
128130
}
129131
}
132+
133+
protected String getDataset() throws BigQueryException {
134+
if (bigquery.getDataset(datasetName) == null) {
135+
bigquery.create(DatasetInfo.newBuilder(datasetName).build());
136+
}
137+
return String.format(
138+
"projects/%s/datasets/%s", ServiceOptions.getDefaultProjectId(), datasetName);
139+
140+
}
130141
}

automl/src/test/java/beta/automl/TablesCreateModelTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static junit.framework.TestCase.assertNotNull;
2121

22+
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
2223
import java.io.ByteArrayOutputStream;
2324
import java.io.IOException;
2425
import java.io.PrintStream;
@@ -27,13 +28,15 @@
2728
import org.junit.After;
2829
import org.junit.Before;
2930
import org.junit.BeforeClass;
31+
import org.junit.Rule;
3032
import org.junit.Test;
3133
import org.junit.runner.RunWith;
3234
import org.junit.runners.JUnit4;
3335

3436
@RunWith(JUnit4.class)
3537
@SuppressWarnings("checkstyle:abbreviationaswordinname")
3638
public class TablesCreateModelTest {
39+
@Rule public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);
3740

3841
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
3942
private static final String DATASET_ID = "TBL00000000000000000000";

automl/src/test/java/beta/automl/TablesPredictTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.cloud.automl.v1.DeployModelRequest;
2424
import com.google.cloud.automl.v1.Model;
2525
import com.google.cloud.automl.v1.ModelName;
26+
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
2627
import com.google.protobuf.Value;
2728
import java.io.ByteArrayOutputStream;
2829
import java.io.IOException;
@@ -33,13 +34,15 @@
3334
import org.junit.After;
3435
import org.junit.Before;
3536
import org.junit.BeforeClass;
37+
import org.junit.Rule;
3638
import org.junit.Test;
3739
import org.junit.runner.RunWith;
3840
import org.junit.runners.JUnit4;
3941

4042
@RunWith(JUnit4.class)
4143
@SuppressWarnings("checkstyle:abbreviationaswordinname")
4244
public class TablesPredictTest {
45+
@Rule public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);
4346

4447
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
4548
private static final String MODEL_ID = "TBL7972827093840953344";

automl/src/test/java/com/example/automl/LanguageTextClassificationCreateModelTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static junit.framework.TestCase.assertNotNull;
2121

22+
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
2223
import java.io.ByteArrayOutputStream;
2324
import java.io.IOException;
2425
import java.io.PrintStream;
@@ -27,13 +28,15 @@
2728
import org.junit.After;
2829
import org.junit.Before;
2930
import org.junit.BeforeClass;
31+
import org.junit.Rule;
3032
import org.junit.Test;
3133
import org.junit.runner.RunWith;
3234
import org.junit.runners.JUnit4;
3335

3436
@RunWith(JUnit4.class)
3537
@SuppressWarnings("checkstyle:abbreviationaswordinname")
3638
public class LanguageTextClassificationCreateModelTest {
39+
@Rule public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);
3740

3841
private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID");
3942
private static final String DATASET_ID = "TCN00000000000000000000";

0 commit comments

Comments
 (0)