Search code examples
javaspring-bootjunitokhttpmockwebserver

RetryInterceptor testing with mockwebserver cancels connection


I have the following setup: spring boot application with an okHttp3 client. I have a custom retry mechanism that needs to retry the call in every situation for a limited amount of times. The interceptor is as follows: spring boot: 2.5.5 okhttp: 4.9.3


@Slf4j
@RequiredArgsConstructor
public class RetryOnFailureInterceptor implements Interceptor {

    private final int maxRetryCount;

 
    @Override
    public Response intercept(final Chain chain) throws IOException {
        final Request request = chain.request();

        int tryCount = 0;

        Response response = null;

        // first call is actual call, following are first retries
        while ((response == null || !response.isSuccessful()) && tryCount < this.maxRetryCount) {
            tryCount++;

            try {
                response = chain.proceed(request);
                if (response.isSuccessful()) {
                    return response;
                }

                // close response before retry
                response.close();
                log.info("Intercept, request failed, retry_count={}/{} ", tryCount, this.maxRetryCount);

            } catch (final IOException ioException) {
                log.info("Caught exception with message={}, retry_count={}/{} ",
                        ioException.getMessage(), tryCount, this.maxRetryCount);

                if (response != null && response.body() != null) {
                    response.close();
                }
            }

        }

        // last try should proceed as is
        return chain.proceed(request);
    }
}

I want to test the functioning of the interceptor with the following test:

 private MockWebServer mockWebServer;

    @BeforeEach
    void setUp() throws IOException {
        this.mockWebServer = new MockWebServer();
        this.mockWebServer.start(8080);
    }

    @AfterEach
    void tearDown() throws IOException {
        this.mockWebServer.shutdown();
    }

   @Test
    void withRetryInterceptorsTest() throws IOException {
        final OkHttpClient httpClient = new CustomOkHttpClientBuilder(getHttpSettings())
.withRetryInterceptor(5)
.build(); //custom client builder I made, it sets timeouts and the interceptor. Also it sets okhttpclient.retryOnConnectionFailure(false) to avoid duplicate retry mechanisms. Also enabling it does not fix it.

        final MockResponse timeout1 = new MockResponse().setBodyDelay(20, TimeUnit.DAYS).setHeadersDelay(20, TimeUnit.DAYS);
        final MockResponse timeout2 = new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE); //another way to timeout
        final MockResponse failure = new MockResponse().setResponseCode(400);
        final MockResponse success = new MockResponse().setResponseCode(200);

        this.mockWebServer.enqueue(timeout1);
        this.mockWebServer.enqueue(timeout2);
        this.mockWebServer.enqueue(failure);
        this.mockWebServer.enqueue(success);

        final Request request = new Request.Builder()
                .url("http://localhost:8080")
                .get()
                .build();

        final Response response = httpClient.newCall(request).execute();

        assertEquals(200, response.code());
    }

When I run the test this happens:

21:47:05.337 [main] INFO RetryOnFailureInterceptor - Caught exception with message=Socket closed, retry_count=1/5 
21:47:05.341 [main] INFO RetryOnFailureInterceptor - Caught exception with message=Canceled, retry_count=2/5 
21:47:05.341 [main] INFO RetryOnFailureInterceptor - Caught exception with message=Canceled, retry_count=3/5 
21:47:05.342 [main] INFO RetryOnFailureInterceptor - Caught exception with message=Canceled, retry_count=4/5 
21:47:05.342 [main] INFO RetryOnFailureInterceptor - Caught exception with message=Canceled, retry_count=5/5

I am trying to find out what te problem is. Is it not working because of something that happens in the mockwebserver, or is this real expected behaviour that when a connection times out you cannot retry because the call gets cancelled?

I suspect that the cause is in the mockwebserver, but I would not know how to fix it, or where the problem is.


Solution

  • It is a bug in MockWebServer and currently under investigation:

    https://github.com/square/okhttp/issues/6976