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

Commit 1dfafb7

Browse files
committed
Making RetryExecutor accept RetrySettings when creating it
1 parent 8872a55 commit 1dfafb7

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

google-cloud-datastore/src/main/java/com/google/cloud/datastore/RetryExecutor.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,27 @@
1515
*/
1616
package com.google.cloud.datastore;
1717

18-
import com.google.api.core.ApiClock;
18+
import static com.google.cloud.RetryHelper.runWithRetries;
19+
1920
import com.google.api.gax.retrying.ResultRetryAlgorithm;
2021
import com.google.api.gax.retrying.RetrySettings;
2122
import com.google.cloud.RetryHelper;
2223
import java.util.concurrent.Callable;
2324

2425
public class RetryExecutor {
2526

26-
public <T> T execute(Callable<T> block, RetrySettings retrySettings,
27-
ResultRetryAlgorithm<?> resultRetryAlgorithm, ApiClock clock) {
28-
return RetryHelper.runWithRetries(block, retrySettings, resultRetryAlgorithm, clock);
27+
private final RetrySettings retrySettings;
28+
private final DatastoreOptions datastoreOptions;
29+
30+
public RetryExecutor(RetrySettings retrySettings,
31+
DatastoreOptions datastoreOptions) {
32+
this.retrySettings = retrySettings;
33+
this.datastoreOptions = datastoreOptions;
34+
}
35+
36+
public <T> T execute(Callable<T> block, ResultRetryAlgorithm<?> resultRetryAlgorithm) {
37+
return runWithRetries(block, retrySettings, resultRetryAlgorithm,
38+
this.datastoreOptions.getClock());
2939
}
3040

3141
}

google-cloud-datastore/src/test/java/com/google/cloud/datastore/RetryExecutorTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,27 @@
1818
import static org.hamcrest.CoreMatchers.equalTo;
1919
import static org.hamcrest.MatcherAssert.assertThat;
2020

21-
import com.google.api.core.NanoClock;
2221
import com.google.api.gax.retrying.BasicResultRetryAlgorithm;
2322
import com.google.api.gax.retrying.RetrySettings;
2423
import java.util.concurrent.Callable;
2524
import org.junit.Test;
2625

2726
public class RetryExecutorTest {
2827

29-
private RetryExecutor retryExecutor = new RetryExecutor();
28+
private static final int MAX_ATTEMPTS = 4;
29+
30+
private final RetrySettings retrySettings = RetrySettings.newBuilder()
31+
.setMaxAttempts(MAX_ATTEMPTS)
32+
.build();
33+
private final DatastoreOptions datastoreOptions = DatastoreOptions.getDefaultInstance();
34+
private final RetryExecutor retryExecutor = new RetryExecutor(retrySettings, datastoreOptions);
35+
3036

3137
@Test
3238
public void shouldRetryWhenErrorOccurred() {
33-
RetrySettings retrySettings = RetrySettings.newBuilder()
34-
.setMaxAttempts(4)
35-
.build();
3639
CustomErrorCallable callable = new CustomErrorCallable(3);
3740

38-
retryExecutor.execute(callable, retrySettings, new BasicResultRetryAlgorithm<>(),
39-
NanoClock.getDefaultClock());
41+
retryExecutor.execute(callable, new BasicResultRetryAlgorithm<>());
4042

4143
assertThat(callable.invokedTimes(), equalTo(4));
4244
}

0 commit comments

Comments
 (0)