Search in sources :

Example 1 with TooManyRequestsException

use of com.cloudant.client.org.lightcouch.TooManyRequestsException 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 TooManyRequestsException

use of com.cloudant.client.org.lightcouch.TooManyRequestsException in project java-cloudant by cloudant.

the class HttpTest method test429BackoffMaxDefault.

/**
 * Test that the default maximum number of retries is reached and the backoff is of at least the
 * expected duration.
 *
 * @throws Exception
 */
@TestTemplate
public void test429BackoffMaxDefault() throws Exception {
    // Always respond 429 for this test
    mockWebServer.setDispatcher(MockWebServerResources.ALL_429);
    TestTimer t = TestTimer.startTimer();
    try {
        CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).interceptors(Replay429Interceptor.WITH_DEFAULTS).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);
        // 3 backoff periods for 4 attempts: 250 + 500 + 1000 = 1750 ms
        assertTrue(duration >= 1750, "The duration should be at least 1750 ms, but was " + duration);
        assertEquals(4, mockWebServer.getRequestCount(), "There should be 4 request attempts");
    }
}
Also used : 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 3 with TooManyRequestsException

use of com.cloudant.client.org.lightcouch.TooManyRequestsException 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 TooManyRequestsException (com.cloudant.client.org.lightcouch.TooManyRequestsException)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 TestTemplate (org.junit.jupiter.api.TestTemplate)3 Replay429Interceptor (com.cloudant.http.interceptors.Replay429Interceptor)2 TestTimer (com.cloudant.tests.util.TestTimer)2