Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit 5016cde

Browse files
committed
chore: Enable exponential backoff for retries in tests
1 parent c241d5e commit 5016cde

2 files changed

Lines changed: 25 additions & 22 deletions

File tree

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelper.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class RemoteBigQueryHelper {
4545
private static final String MODEL_NAME_PREFIX = "model_";
4646
private static final String ROUTINE_NAME_PREFIX = "routine_";
4747
private final BigQueryOptions options;
48-
private static final int connectTimeout = 60000;
48+
private static final int CONNECT_TIMEOUT_IN_MS = 60000;
4949

5050
private RemoteBigQueryHelper(BigQueryOptions options) {
5151
this.options = options;
@@ -96,8 +96,8 @@ public static RemoteBigQueryHelper create(String projectId, InputStream keyStrea
9696
HttpTransportOptions transportOptions = BigQueryOptions.getDefaultHttpTransportOptions();
9797
transportOptions =
9898
transportOptions.toBuilder()
99-
.setConnectTimeout(connectTimeout)
100-
.setReadTimeout(connectTimeout)
99+
.setConnectTimeout(CONNECT_TIMEOUT_IN_MS)
100+
.setReadTimeout(CONNECT_TIMEOUT_IN_MS)
101101
.build();
102102
BigQueryOptions bigqueryOptions =
103103
BigQueryOptions.newBuilder()
@@ -133,8 +133,8 @@ public static RemoteBigQueryHelper create(BigQueryOptions.Builder bigqueryOption
133133
HttpTransportOptions transportOptions = BigQueryOptions.getDefaultHttpTransportOptions();
134134
transportOptions =
135135
transportOptions.toBuilder()
136-
.setConnectTimeout(connectTimeout)
137-
.setReadTimeout(connectTimeout)
136+
.setConnectTimeout(CONNECT_TIMEOUT_IN_MS)
137+
.setReadTimeout(CONNECT_TIMEOUT_IN_MS)
138138
.build();
139139
BigQueryOptions.Builder builder =
140140
bigqueryOptionsBuilder
@@ -143,21 +143,25 @@ public static RemoteBigQueryHelper create(BigQueryOptions.Builder bigqueryOption
143143
return new RemoteBigQueryHelper(builder.build());
144144
}
145145

146+
// Opt to keep these settings a small as possible to minimize the total test time.
147+
// These values can be adjusted per test case, but these serve as default values.
146148
private static RetrySettings retrySettings() {
147-
double retryDelayMultiplier = 1.0;
149+
double multiplier = 1.5;
148150
int maxAttempts = 10;
149-
long initialRetryDelay = 250L;
150-
long maxRetryDelay = 30000L;
151-
long totalTimeOut = 120000L;
151+
long initialRetryDelayMs = 100L; // 0.1s initial retry delay
152+
long maxRetryDelayMs = 1000L; // 1s max retry delay between retry
153+
long initialRpcTimeoutMs = 1000L; // 1s initial rpc duration
154+
long maxRpcTimeoutMs = 2000L; // 2s max rpc duration
155+
long totalTimeOut = 3000L; // 3s total timeout
152156
return RetrySettings.newBuilder()
153157
.setMaxAttempts(maxAttempts)
154-
.setMaxRetryDelayDuration(Duration.ofMillis(maxRetryDelay))
155158
.setTotalTimeoutDuration(Duration.ofMillis(totalTimeOut))
156-
.setInitialRetryDelayDuration(Duration.ofMillis(initialRetryDelay))
157-
.setRetryDelayMultiplier(retryDelayMultiplier)
158-
.setInitialRpcTimeoutDuration(Duration.ofMillis(totalTimeOut))
159-
.setRpcTimeoutMultiplier(retryDelayMultiplier)
160-
.setMaxRpcTimeoutDuration(Duration.ofMillis(totalTimeOut))
159+
.setInitialRetryDelayDuration(Duration.ofMillis(initialRetryDelayMs))
160+
.setMaxRetryDelayDuration(Duration.ofMillis(maxRetryDelayMs))
161+
.setRetryDelayMultiplier(multiplier)
162+
.setInitialRpcTimeoutDuration(Duration.ofMillis(initialRpcTimeoutMs))
163+
.setMaxRpcTimeoutDuration(Duration.ofMillis(maxRpcTimeoutMs))
164+
.setRpcTimeoutMultiplier(multiplier)
161165
.build();
162166
}
163167

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelperTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@
2626
import java.io.ByteArrayInputStream;
2727
import java.io.InputStream;
2828
import java.time.Duration;
29-
import java.util.concurrent.ExecutionException;
3029
import org.junit.jupiter.api.Test;
3130
import org.junit.jupiter.api.extension.ExtendWith;
3231
import org.mockito.Mockito;
3332
import org.mockito.junit.jupiter.MockitoExtension;
3433

3534
@ExtendWith(MockitoExtension.class)
36-
public class RemoteBigQueryHelperTest {
35+
class RemoteBigQueryHelperTest {
3736

3837
private static final String DATASET_NAME = "dataset-name";
3938
private static final String PROJECT_ID = "project-id";
@@ -67,7 +66,7 @@ public class RemoteBigQueryHelperTest {
6766
private static final InputStream JSON_KEY_STREAM = new ByteArrayInputStream(JSON_KEY.getBytes());
6867

6968
@Test
70-
public void testForceDelete() throws InterruptedException, ExecutionException {
69+
void testForceDelete() {
7170
BigQuery bigqueryMock = Mockito.mock(BigQuery.class);
7271
Mockito.when(bigqueryMock.delete(DATASET_NAME, DatasetDeleteOption.deleteContents()))
7372
.thenReturn(true);
@@ -76,15 +75,15 @@ public void testForceDelete() throws InterruptedException, ExecutionException {
7675
}
7776

7877
@Test
79-
public void testCreateFromStream() {
78+
void testCreateFromStream() {
8079
RemoteBigQueryHelper helper = RemoteBigQueryHelper.create(PROJECT_ID, JSON_KEY_STREAM);
8180
BigQueryOptions options = helper.getOptions();
8281
assertEquals(PROJECT_ID, options.getProjectId());
8382
assertEquals(60000, ((HttpTransportOptions) options.getTransportOptions()).getConnectTimeout());
8483
assertEquals(60000, ((HttpTransportOptions) options.getTransportOptions()).getReadTimeout());
8584
assertEquals(10, options.getRetrySettings().getMaxAttempts());
86-
assertEquals(Duration.ofMillis(30000), options.getRetrySettings().getMaxRetryDelayDuration());
87-
assertEquals(Duration.ofMillis(120000), options.getRetrySettings().getTotalTimeoutDuration());
88-
assertEquals(Duration.ofMillis(250), options.getRetrySettings().getInitialRetryDelayDuration());
85+
assertEquals(Duration.ofMillis(1000), options.getRetrySettings().getMaxRetryDelayDuration());
86+
assertEquals(Duration.ofMillis(3000), options.getRetrySettings().getTotalTimeoutDuration());
87+
assertEquals(Duration.ofMillis(100), options.getRetrySettings().getInitialRetryDelayDuration());
8988
}
9089
}

0 commit comments

Comments
 (0)