Search in sources :

Example 1 with TestTimer

use of com.cloudant.tests.util.TestTimer 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 TestTimer

use of com.cloudant.tests.util.TestTimer 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 TestTimer

use of com.cloudant.tests.util.TestTimer in project java-cloudant by cloudant.

the class HttpTest method test429BackoffRetryAfter.

/**
 * Test that an integer number of seconds delay specified by a Retry-After header is honoured.
 *
 * @throws Exception
 */
@TestTemplate
public void test429BackoffRetryAfter() throws Exception {
    mockWebServer.enqueue(MockWebServerResources.get429().addHeader("Retry-After", "1"));
    mockWebServer.enqueue(new MockResponse());
    TestTimer t = TestTimer.startTimer();
    CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).interceptors(Replay429Interceptor.WITH_DEFAULTS).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 at least 1000 ms, but was " + duration);
    assertEquals(2, mockWebServer.getRequestCount(), "There should be 2 request attempts");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) 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 4 with TestTimer

use of com.cloudant.tests.util.TestTimer 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)

Aggregations

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