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