|
7 | 7 | import org.junit.jupiter.api.Test; |
8 | 8 |
|
9 | 9 | import java.io.IOException; |
| 10 | +import java.net.SocketTimeoutException; |
10 | 11 |
|
11 | 12 | public class AuthInterceptorTest { |
12 | 13 |
|
@@ -166,6 +167,89 @@ public Call call() { |
166 | 167 | } |
167 | 168 | } |
168 | 169 |
|
| 170 | + @Test |
| 171 | + @Tag("unit") |
| 172 | + public void testRetry_onSocketTimeout_thenSuccess_retriesAndReturnsSuccess() throws IOException { |
| 173 | + authInterceptor.setRetryConfig(RetryConfig.builder().retryLimit(3).retryDelay(10).build()); |
| 174 | + Request request = new Request.Builder() |
| 175 | + .url("https://api.contentstack.io/v3/user") |
| 176 | + .get() |
| 177 | + .build(); |
| 178 | + TimeoutTestChain chain = new TimeoutTestChain(request, 1, 200); |
| 179 | + try (Response response = authInterceptor.intercept(chain)) { |
| 180 | + Assertions.assertEquals(200, response.code()); |
| 181 | + Assertions.assertEquals(2, chain.getProceedCount()); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + @Tag("unit") |
| 187 | + public void testRetry_onSocketTimeout_exhaustsRetries_throws() { |
| 188 | + authInterceptor.setRetryConfig(RetryConfig.builder().retryLimit(2).retryDelay(10).build()); |
| 189 | + Request request = new Request.Builder() |
| 190 | + .url("https://api.contentstack.io/v3/user") |
| 191 | + .get() |
| 192 | + .build(); |
| 193 | + TimeoutTestChain chain = new TimeoutTestChain(request, 5, 200); |
| 194 | + Assertions.assertThrows(SocketTimeoutException.class, () -> authInterceptor.intercept(chain)); |
| 195 | + Assertions.assertEquals(3, chain.getProceedCount()); |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + @Tag("unit") |
| 200 | + public void testRetry_onSocketTimeout_zeroRetryLimit_throwsImmediately() { |
| 201 | + authInterceptor.setRetryConfig(RetryConfig.builder().retryLimit(0).retryDelay(10).build()); |
| 202 | + Request request = new Request.Builder() |
| 203 | + .url("https://api.contentstack.io/v3/user") |
| 204 | + .get() |
| 205 | + .build(); |
| 206 | + TimeoutTestChain chain = new TimeoutTestChain(request, 5, 200); |
| 207 | + Assertions.assertThrows(SocketTimeoutException.class, () -> authInterceptor.intercept(chain)); |
| 208 | + Assertions.assertEquals(1, chain.getProceedCount()); |
| 209 | + } |
| 210 | + |
| 211 | + private static class TimeoutTestChain implements Interceptor.Chain { |
| 212 | + private final Request originalRequest; |
| 213 | + private final int timeoutCount; |
| 214 | + private final int successCode; |
| 215 | + private int proceedCount = 0; |
| 216 | + |
| 217 | + TimeoutTestChain(Request request, int timeoutCount, int successCode) { |
| 218 | + this.originalRequest = request; |
| 219 | + this.timeoutCount = timeoutCount; |
| 220 | + this.successCode = successCode; |
| 221 | + } |
| 222 | + |
| 223 | + int getProceedCount() { return proceedCount; } |
| 224 | + |
| 225 | + @Override |
| 226 | + public Request request() { return originalRequest; } |
| 227 | + |
| 228 | + @Override |
| 229 | + public Response proceed(Request request) throws IOException { |
| 230 | + proceedCount++; |
| 231 | + if (proceedCount <= timeoutCount) { |
| 232 | + throw new SocketTimeoutException("timeout"); |
| 233 | + } |
| 234 | + return new Response.Builder() |
| 235 | + .request(request) |
| 236 | + .protocol(Protocol.HTTP_1_1) |
| 237 | + .code(successCode) |
| 238 | + .message("OK") |
| 239 | + .body(ResponseBody.create("{}", MediaType.parse("application/json"))) |
| 240 | + .build(); |
| 241 | + } |
| 242 | + |
| 243 | + @Override public Connection connection() { return null; } |
| 244 | + @Override public int connectTimeoutMillis() { return 0; } |
| 245 | + @Override public Interceptor.Chain withConnectTimeout(int timeout, java.util.concurrent.TimeUnit unit) { return this; } |
| 246 | + @Override public int readTimeoutMillis() { return 0; } |
| 247 | + @Override public Interceptor.Chain withReadTimeout(int timeout, java.util.concurrent.TimeUnit unit) { return this; } |
| 248 | + @Override public int writeTimeoutMillis() { return 0; } |
| 249 | + @Override public Interceptor.Chain withWriteTimeout(int timeout, java.util.concurrent.TimeUnit unit) { return this; } |
| 250 | + @Override public Call call() { return null; } |
| 251 | + } |
| 252 | + |
169 | 253 | @Test |
170 | 254 | public void AuthInterceptor() { |
171 | 255 | AuthInterceptor expected = new AuthInterceptor("abc"); |
|
0 commit comments