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");
}
}
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");
}
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");
}
}
Aggregations