Search in sources :

Example 1 with BackOffRetryer

use of com.dtflys.forest.retryer.BackOffRetryer in project forest by dromara.

the class TestAsyncGetClient method testRetrySimpleGetTimeout.

@Test
public void testRetrySimpleGetTimeout() throws InterruptedException {
    server.enqueue(new MockResponse().setHeadersDelay(10, TimeUnit.SECONDS).setBody(EXPECTED));
    AtomicReference<BackOffRetryer> retryerAtomicReference = new AtomicReference<>(null);
    CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean success = new AtomicBoolean(true);
    getClient.retrySimpleGetTimeout((ex, request, response) -> {
        success.set(false);
        retryerAtomicReference.set((BackOffRetryer) request.getRetryer());
        latch.countDown();
    });
    latch.await(30, TimeUnit.SECONDS);
    assertThat(success.get()).isFalse();
    BackOffRetryer retryer = retryerAtomicReference.get();
    Assertions.assertThat(retryer).isNotNull();
    Assertions.assertThat(retryer.getMaxRetryCount()).isEqualTo(2);
    Assertions.assertThat(retryer.getMaxRetryInterval()).isEqualTo(500);
    Assertions.assertThat(retryer.getWaitedTime()).isEqualTo(500 + 500);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) BackOffRetryer(com.dtflys.forest.retryer.BackOffRetryer) Test(org.junit.Test)

Example 2 with BackOffRetryer

use of com.dtflys.forest.retryer.BackOffRetryer in project forest by dromara.

the class TestAsyncGetClient method testRetrySimpleGetError.

@Test
public void testRetrySimpleGetError() throws InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(404));
    AtomicReference<BackOffRetryer> retryerAtomicReference = new AtomicReference<>(null);
    CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean success = new AtomicBoolean(true);
    getClient.retrySimpleGetError((ex, request, response) -> {
        retryerAtomicReference.set((BackOffRetryer) request.getRetryer());
        success.set(false);
        latch.countDown();
    });
    latch.await(10, TimeUnit.SECONDS);
    assertThat(success.get()).isFalse();
    BackOffRetryer retryer = retryerAtomicReference.get();
    Assertions.assertThat(retryer).isNotNull();
    Assertions.assertThat(retryer.getMaxRetryCount()).isEqualTo(2);
    Assertions.assertThat(retryer.getMaxRetryInterval()).isEqualTo(500);
    Assertions.assertThat(retryer.getWaitedTime()).isEqualTo(500 + 500);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) BackOffRetryer(com.dtflys.forest.retryer.BackOffRetryer) Test(org.junit.Test)

Example 3 with BackOffRetryer

use of com.dtflys.forest.retryer.BackOffRetryer in project forest by dromara.

the class TestErrorClient method testErrorGetWithRetry.

@Test
public void testErrorGetWithRetry() {
    server.enqueue(new MockResponse().setResponseCode(400).setBody(EXPECTED));
    AtomicReference<BackOffRetryer> retryerAtomicReference = new AtomicReference<>(null);
    getClient.errorGetWithRetry((ex, request, response) -> {
        retryerAtomicReference.set((BackOffRetryer) request.getRetryer());
    });
    BackOffRetryer retryer = retryerAtomicReference.get();
    assertThat(retryer).isNotNull();
    assertThat(retryer.getMaxRetryCount()).isEqualTo(3);
    assertThat(retryer.getMaxRetryInterval()).isEqualTo(2000);
    assertThat(retryer.getWaitedTime()).isEqualTo(1000 + 2000 + 2000);
    assertThat(retryer.getCurrentRetryCount()).isEqualTo(3);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) BackOffRetryer(com.dtflys.forest.retryer.BackOffRetryer) Test(org.junit.Test)

Aggregations

BackOffRetryer (com.dtflys.forest.retryer.BackOffRetryer)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 Test (org.junit.Test)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2