RetryHelper can be configured to not retry on socket timeouts. by mderka · Pull Request #882 · googleapis/google-cloud-java · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions gcloud-java-core/src/main/java/com/google/gcloud/RetryHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.junit.Test;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -194,14 +195,40 @@ public void testTriesNoMoreLongerThanTotalRetryPeriod() {
}
throw new RuntimeException();
}
}), params, handler, stopwatch);
}), params, handler, stopwatch, true);
fail();
} catch (RetriesExhaustedException expected) {
// verify timesCalled
assertEquals(sleepOnAttempt, timesCalled.get());
}
}

@Test
public void testNoRetriesOnSocketTimeout() {
final FakeTicker ticker = new FakeTicker();
Stopwatch stopwatch = Stopwatch.createUnstarted(ticker);
RetryParams params = RetryParams.builder().initialRetryDelayMillis(0)
.totalRetryPeriodMillis(999)
.retryMinAttempts(5)
.retryMaxAttempts(10)
.build();
ExceptionHandler handler = ExceptionHandler.builder().retryOn(RuntimeException.class).build();
final AtomicInteger timesCalled = new AtomicInteger(0);
try {

This comment was marked as spam.

RetryHelper.runWithRetries(callable(new Runnable() {
@Override public void run() {
timesCalled.incrementAndGet();

This comment was marked as spam.

throw new RetryHelper.RetryHelperException(
new SocketTimeoutException("Simulated socket timeout"));
}
}), params, handler, stopwatch, false);
fail();
} catch (RetryHelper.RetryHelperException expected) {
// verify timesCalled
assertEquals(1, timesCalled.get());
}
}

@Test
public void testBackoffIsExponential() {
// Total retry period set to 60 seconds so as to not factor into test
Expand Down