16
16
17
17
package com .google .gcloud ;
18
18
19
+ import static com .google .gcloud .RetryHelper .runWithRetries ;
19
20
import static java .util .concurrent .Executors .callable ;
20
21
import static org .junit .Assert .assertEquals ;
21
22
import static org .junit .Assert .assertNull ;
30
31
import org .junit .Test ;
31
32
32
33
import java .io .IOException ;
34
+ import java .net .SocketTimeoutException ;
33
35
import java .util .Arrays ;
34
36
import java .util .Iterator ;
35
37
import java .util .concurrent .Callable ;
@@ -67,7 +69,7 @@ public void testTriesWithExceptionHandling() {
67
69
.retryOn (IOException .class ).abortOn (RuntimeException .class ).build ();
68
70
final AtomicInteger count = new AtomicInteger (3 );
69
71
try {
70
- RetryHelper . runWithRetries (new Callable <Void >() {
72
+ runWithRetries (new Callable <Void >() {
71
73
@ Override public Void call () throws IOException , NullPointerException {
72
74
if (count .decrementAndGet () == 2 ) {
73
75
assertEquals (1 , RetryHelper .getContext ().getAttemptNumber ());
@@ -91,7 +93,7 @@ public void testTriesWithExceptionHandling() {
91
93
final Iterator <? extends E1Exception > exceptions = Arrays .asList (
92
94
new E1Exception (), new E2Exception (), new E4Exception (), new E3Exception ()).iterator ();
93
95
try {
94
- RetryHelper . runWithRetries (new Callable <Void >() {
96
+ runWithRetries (new Callable <Void >() {
95
97
@ Override public Void call () throws E1Exception {
96
98
throw exceptions .next ();
97
99
}
@@ -113,7 +115,7 @@ public void testTriesAtLeastMinTimes() {
113
115
.build ();
114
116
final int timesToFail = 7 ;
115
117
assertNull (RetryHelper .getContext ());
116
- int attempted = RetryHelper . runWithRetries (new Callable <Integer >() {
118
+ int attempted = runWithRetries (new Callable <Integer >() {
117
119
int timesCalled ;
118
120
@ Override public Integer call () throws IOException {
119
121
timesCalled ++;
@@ -140,7 +142,7 @@ public void testTriesNoMoreThanMaxTimes() {
140
142
.build ();
141
143
final AtomicInteger timesCalled = new AtomicInteger (0 );
142
144
try {
143
- RetryHelper . runWithRetries (callable (new Runnable () {
145
+ runWithRetries (callable (new Runnable () {
144
146
@ Override public void run () {
145
147
// Throw an exception up to maxAttempts times, should never be called beyond that
146
148
if (timesCalled .incrementAndGet () <= maxAttempts ) {
@@ -186,22 +188,47 @@ public void testTriesNoMoreLongerThanTotalRetryPeriod() {
186
188
final int sleepOnAttempt = 8 ;
187
189
final AtomicInteger timesCalled = new AtomicInteger (0 );
188
190
try {
189
- RetryHelper . runWithRetries (callable (new Runnable () {
191
+ runWithRetries (callable (new Runnable () {
190
192
@ Override public void run () {
191
193
timesCalled .incrementAndGet ();
192
194
if (timesCalled .get () == sleepOnAttempt ) {
193
195
ticker .advance (1000 , TimeUnit .MILLISECONDS );
194
196
}
195
197
throw new RuntimeException ();
196
198
}
197
- }), params , handler , stopwatch );
199
+ }), params , handler , stopwatch , true );
198
200
fail ();
199
201
} catch (RetriesExhaustedException expected ) {
200
202
// verify timesCalled
201
203
assertEquals (sleepOnAttempt , timesCalled .get ());
202
204
}
203
205
}
204
206
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
+
205
232
@ Test
206
233
public void testBackoffIsExponential () {
207
234
// 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) {
248
275
if (level < 0 ) {
249
276
return 0 ;
250
277
}
251
- return RetryHelper . runWithRetries (new Callable <Integer >() {
278
+ return runWithRetries (new Callable <Integer >() {
252
279
@ Override
253
280
public Integer call () throws IOException {
254
281
if (RetryHelper .getContext ().getAttemptNumber () < retries ) {
0 commit comments