1616
1717package com .google .gcloud ;
1818
19+ import static com .google .gcloud .RetryHelper .runWithRetries ;
1920import static java .util .concurrent .Executors .callable ;
2021import static org .junit .Assert .assertEquals ;
2122import static org .junit .Assert .assertNull ;
3031import org .junit .Test ;
3132
3233import java .io .IOException ;
34+ import java .net .SocketTimeoutException ;
3335import java .util .Arrays ;
3436import java .util .Iterator ;
3537import java .util .concurrent .Callable ;
@@ -67,7 +69,7 @@ public void testTriesWithExceptionHandling() {
6769 .retryOn (IOException .class ).abortOn (RuntimeException .class ).build ();
6870 final AtomicInteger count = new AtomicInteger (3 );
6971 try {
70- RetryHelper . runWithRetries (new Callable <Void >() {
72+ runWithRetries (new Callable <Void >() {
7173 @ Override public Void call () throws IOException , NullPointerException {
7274 if (count .decrementAndGet () == 2 ) {
7375 assertEquals (1 , RetryHelper .getContext ().getAttemptNumber ());
@@ -91,7 +93,7 @@ public void testTriesWithExceptionHandling() {
9193 final Iterator <? extends E1Exception > exceptions = Arrays .asList (
9294 new E1Exception (), new E2Exception (), new E4Exception (), new E3Exception ()).iterator ();
9395 try {
94- RetryHelper . runWithRetries (new Callable <Void >() {
96+ runWithRetries (new Callable <Void >() {
9597 @ Override public Void call () throws E1Exception {
9698 throw exceptions .next ();
9799 }
@@ -113,7 +115,7 @@ public void testTriesAtLeastMinTimes() {
113115 .build ();
114116 final int timesToFail = 7 ;
115117 assertNull (RetryHelper .getContext ());
116- int attempted = RetryHelper . runWithRetries (new Callable <Integer >() {
118+ int attempted = runWithRetries (new Callable <Integer >() {
117119 int timesCalled ;
118120 @ Override public Integer call () throws IOException {
119121 timesCalled ++;
@@ -140,7 +142,7 @@ public void testTriesNoMoreThanMaxTimes() {
140142 .build ();
141143 final AtomicInteger timesCalled = new AtomicInteger (0 );
142144 try {
143- RetryHelper . runWithRetries (callable (new Runnable () {
145+ runWithRetries (callable (new Runnable () {
144146 @ Override public void run () {
145147 // Throw an exception up to maxAttempts times, should never be called beyond that
146148 if (timesCalled .incrementAndGet () <= maxAttempts ) {
@@ -186,22 +188,47 @@ public void testTriesNoMoreLongerThanTotalRetryPeriod() {
186188 final int sleepOnAttempt = 8 ;
187189 final AtomicInteger timesCalled = new AtomicInteger (0 );
188190 try {
189- RetryHelper . runWithRetries (callable (new Runnable () {
191+ runWithRetries (callable (new Runnable () {
190192 @ Override public void run () {
191193 timesCalled .incrementAndGet ();
192194 if (timesCalled .get () == sleepOnAttempt ) {
193195 ticker .advance (1000 , TimeUnit .MILLISECONDS );
194196 }
195197 throw new RuntimeException ();
196198 }
197- }), params , handler , stopwatch );
199+ }), params , handler , stopwatch , true );
198200 fail ();
199201 } catch (RetriesExhaustedException expected ) {
200202 // verify timesCalled
201203 assertEquals (sleepOnAttempt , timesCalled .get ());
202204 }
203205 }
204206
207+ @ Test
208+ public void testNoRetriesOnSocketTimeout () {
209+ final FakeTicker ticker = new FakeTicker ();
210+ Stopwatch stopwatch = Stopwatch .createUnstarted (ticker );
211+ RetryParams params = RetryParams .builder ().initialRetryDelayMillis (0 )
212+ .totalRetryPeriodMillis (999 )
213+ .retryMinAttempts (5 )
214+ .retryMaxAttempts (10 )
215+ .build ();
216+ ExceptionHandler handler = ExceptionHandler .builder ().retryOn (RuntimeException .class ).build ();
217+ final AtomicInteger timesCalled = new AtomicInteger (0 );
218+ try {
219+ RetryHelper .runWithRetries (callable (new Runnable () {
220+ @ Override public void run () {
221+ timesCalled .incrementAndGet ();
222+ throw new RuntimeException (new SocketTimeoutException ("Simulated socket timeout" ));
223+ }
224+ }), params , handler , stopwatch , false );
225+ fail ();
226+ } catch (RuntimeException expected ) {
227+ // verify timesCalled
228+ assertEquals (1 , timesCalled .get ());
229+ }
230+ }
231+
205232 @ Test
206233 public void testBackoffIsExponential () {
207234 // Total retry period set to 60 seconds so as to not factor into test
@@ -248,7 +275,7 @@ private int invokeNested(final int level, final int retries) {
248275 if (level < 0 ) {
249276 return 0 ;
250277 }
251- return RetryHelper . runWithRetries (new Callable <Integer >() {
278+ return runWithRetries (new Callable <Integer >() {
252279 @ Override
253280 public Integer call () throws IOException {
254281 if (RetryHelper .getContext ().getAttemptNumber () < retries ) {
0 commit comments