首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用mockwebserver的RetryInterceptor测试取消连接

使用mockwebserver的RetryInterceptor测试取消连接
EN

Stack Overflow用户
提问于 2021-12-23 20:55:25
回答 1查看 453关注 0票数 0

我有以下设置:使用okHttp3客户端的spring引导应用程序。我有一个自定义的重试机制,需要在每种情况下在有限的时间内重试调用。拦截器如下: spring引导: 2.5.5 okhttp: 4.9.3

代码语言:javascript
复制
@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);
    }
}

我想用以下测试测试拦截器的功能:

代码语言:javascript
复制
 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());
    }

当我运行测试时,会发生这样的情况:

代码语言:javascript
复制
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

我想找出问题出在哪里。它不是因为mockwebserver中发生的某些事情而起作用吗,还是这种真正预期的行为,当连接超时时,由于呼叫被取消而不能重试?

我怀疑原因在mockwebserver中,但我不知道如何修复它,也不知道问题出在哪里。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-06 10:45:14

这是MockWebServer中的一个漏洞,目前正在调查中:

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70467281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档