|
27 | 27 | import java.net.HttpRetryException; |
28 | 28 | import java.net.ServerSocket; |
29 | 29 | import java.net.SocketTimeoutException; |
| 30 | +import java.net.UnknownHostException; |
30 | 31 | import java.time.Duration; |
31 | 32 | import java.util.concurrent.TimeUnit; |
32 | 33 | import java.util.function.Predicate; |
33 | 34 | import java.util.logging.Level; |
34 | 35 | import java.util.logging.Logger; |
| 36 | +import java.util.stream.Stream; |
35 | 37 | import okhttp3.OkHttpClient; |
36 | 38 | import okhttp3.Request; |
37 | 39 | import okhttp3.Response; |
|
40 | 42 | import org.junit.jupiter.api.extension.ExtendWith; |
41 | 43 | import org.junit.jupiter.api.extension.RegisterExtension; |
42 | 44 | import org.junit.jupiter.params.ParameterizedTest; |
| 45 | +import org.junit.jupiter.params.provider.Arguments; |
| 46 | +import org.junit.jupiter.params.provider.MethodSource; |
43 | 47 | import org.junit.jupiter.params.provider.ValueSource; |
44 | 48 | import org.mockito.Mock; |
45 | 49 | import org.mockito.junit.jupiter.MockitoExtension; |
@@ -217,26 +221,30 @@ private OkHttpClient connectTimeoutClient() { |
217 | 221 | .build(); |
218 | 222 | } |
219 | 223 |
|
220 | | - @Test |
221 | | - void isRetryableException() { |
222 | | - // Should retry on connection timeouts, where error message is "Connect timed out" or "connect |
223 | | - // timed out" |
224 | | - assertThat(retrier.shouldRetryOnException(new SocketTimeoutException("Connect timed out"))) |
225 | | - .isTrue(); |
226 | | - assertThat(retrier.shouldRetryOnException(new SocketTimeoutException("connect timed out"))) |
227 | | - .isTrue(); |
228 | | - // Shouldn't retry on read timeouts, where error message is "Read timed out" |
229 | | - assertThat(retrier.shouldRetryOnException(new SocketTimeoutException("Read timed out"))) |
230 | | - .isFalse(); |
231 | | - // Shouldn't retry on write timeouts or other IOException |
232 | | - assertThat(retrier.shouldRetryOnException(new SocketTimeoutException("timeout"))).isFalse(); |
233 | | - assertThat(retrier.shouldRetryOnException(new SocketTimeoutException())).isTrue(); |
234 | | - assertThat(retrier.shouldRetryOnException(new IOException("error"))).isFalse(); |
235 | | - |
236 | | - // Testing configured predicate |
237 | | - assertThat(retrier.shouldRetryOnException(new HttpRetryException("error", 400))).isFalse(); |
238 | | - assertThat(retrier.shouldRetryOnException(new HttpRetryException("timeout retry", 400))) |
239 | | - .isTrue(); |
| 224 | + @ParameterizedTest |
| 225 | + @MethodSource("isRetryableExceptionArgs") |
| 226 | + void isRetryableException(IOException exception, boolean expectedRetryResult) { |
| 227 | + assertThat(retrier.shouldRetryOnException(exception)).isEqualTo(expectedRetryResult); |
| 228 | + } |
| 229 | + |
| 230 | + private static Stream<Arguments> isRetryableExceptionArgs() { |
| 231 | + return Stream.of( |
| 232 | + // Should retry on SocketTimeoutExceptions |
| 233 | + Arguments.of(new SocketTimeoutException("Connect timed out"), true), |
| 234 | + Arguments.of(new SocketTimeoutException("connect timed out"), true), |
| 235 | + Arguments.of(new SocketTimeoutException("timeout"), true), |
| 236 | + Arguments.of(new SocketTimeoutException("Read timed out"), true), |
| 237 | + Arguments.of(new SocketTimeoutException(), true), |
| 238 | + // Should retry on UnknownHostExceptions |
| 239 | + Arguments.of(new UnknownHostException("host"), true), |
| 240 | + // Should retry on ConnectException |
| 241 | + Arguments.of( |
| 242 | + new ConnectException("Failed to connect to localhost/[0:0:0:0:0:0:0:1]:62611"), true), |
| 243 | + // Shouldn't retry other IOException |
| 244 | + Arguments.of(new IOException("error"), false), |
| 245 | + // Testing configured predicate |
| 246 | + Arguments.of(new HttpRetryException("error", 400), false), |
| 247 | + Arguments.of(new HttpRetryException("timeout retry", 400), true)); |
240 | 248 | } |
241 | 249 |
|
242 | 250 | @Test |
|
0 commit comments