Search in sources :

Example 1 with Replay429Interceptor

use of com.cloudant.http.interceptors.Replay429Interceptor in project java-cloudant by cloudant.

the class HttpTest method test429BackoffMaxConfigured.

/**
 * Test that the configured maximum number of retries is reached and the backoff is of at least
 * the expected duration.
 *
 * @throws Exception
 */
@TestTemplate
public void test429BackoffMaxConfigured() throws Exception {
    // Always respond 429 for this test
    mockWebServer.setDispatcher(MockWebServerResources.ALL_429);
    TestTimer t = TestTimer.startTimer();
    try {
        CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).interceptors(new Replay429Interceptor(10, 1, true)).build();
        String response = c.executeRequest(Http.GET(c.getBaseUri())).responseAsString();
        fail("There should be a TooManyRequestsException instead had response " + response);
    } catch (TooManyRequestsException e) {
        long duration = t.stopTimer(TimeUnit.MILLISECONDS);
        // 9 backoff periods for 10 attempts: 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256 = 511 ms
        assertTrue(duration >= 511, "The duration should be at least 511 ms, but was " + duration);
        assertEquals(10, mockWebServer.getRequestCount(), "There should be 10 request attempts");
    }
}
Also used : Replay429Interceptor(com.cloudant.http.interceptors.Replay429Interceptor) TooManyRequestsException(com.cloudant.client.org.lightcouch.TooManyRequestsException) CloudantClient(com.cloudant.client.api.CloudantClient) TestTimer(com.cloudant.tests.util.TestTimer) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 2 with Replay429Interceptor

use of com.cloudant.http.interceptors.Replay429Interceptor in project java-cloudant by cloudant.

the class HttpTest method test429IgnoreRetryAfter.

@TestTemplate
public void test429IgnoreRetryAfter() throws Exception {
    mockWebServer.enqueue(MockWebServerResources.get429().addHeader("Retry-After", "1"));
    mockWebServer.enqueue(new MockResponse());
    TestTimer t = TestTimer.startTimer();
    CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).interceptors(new Replay429Interceptor(1, 1, false)).build();
    String response = c.executeRequest(Http.GET(c.getBaseUri())).responseAsString();
    assertTrue(response.isEmpty(), "There should be no response body on the mock response");
    long duration = t.stopTimer(TimeUnit.MILLISECONDS);
    assertTrue(duration < 1000, "The duration should be less than 1000 ms, but was " + duration);
    assertEquals(2, mockWebServer.getRequestCount(), "There should be 2 request attempts");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Replay429Interceptor(com.cloudant.http.interceptors.Replay429Interceptor) CloudantClient(com.cloudant.client.api.CloudantClient) TestTimer(com.cloudant.tests.util.TestTimer) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 3 with Replay429Interceptor

use of com.cloudant.http.interceptors.Replay429Interceptor in project java-cloudant by cloudant.

the class HttpTest method test429BackoffMaxMoreThanRetriesAllowed.

/**
 * Test that the outer number of configured retries takes precedence.
 *
 * @throws Exception
 */
@TestTemplate
public void test429BackoffMaxMoreThanRetriesAllowed() throws Exception {
    // Always respond 429 for this test
    mockWebServer.setDispatcher(MockWebServerResources.ALL_429);
    try {
        CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).interceptors(new Replay429Interceptor(10, 1, true)).build();
        String response = c.executeRequest(Http.GET(c.getBaseUri()).setNumberOfRetries(3)).responseAsString();
        fail("There should be a TooManyRequestsException instead had response " + response);
    } catch (TooManyRequestsException e) {
        assertEquals(3, mockWebServer.getRequestCount(), "There should be 3 request attempts");
    }
}
Also used : Replay429Interceptor(com.cloudant.http.interceptors.Replay429Interceptor) TooManyRequestsException(com.cloudant.client.org.lightcouch.TooManyRequestsException) CloudantClient(com.cloudant.client.api.CloudantClient) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TestTemplate(org.junit.jupiter.api.TestTemplate)

Aggregations

CloudantClient (com.cloudant.client.api.CloudantClient)3 Replay429Interceptor (com.cloudant.http.interceptors.Replay429Interceptor)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 TestTemplate (org.junit.jupiter.api.TestTemplate)3 TooManyRequestsException (com.cloudant.client.org.lightcouch.TooManyRequestsException)2 TestTimer (com.cloudant.tests.util.TestTimer)2 MockResponse (okhttp3.mockwebserver.MockResponse)1