Skip to content

[Devtooling-1063] Logging when max retries hit #998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion resources/sdk/purecloudjava/templates/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ public class ApiClient implements AutoCloseable {
return new ApiResponseWrapper<>(statusCode, reasonPhrase, headers, body, entity);
}
else {
String message = "error";
String message = (statusCode == 429) ? "Max number of retries exceeded" : "error";
String body = response.readBody();
throw new ApiException(statusCode, message, headers, body);
}
Expand Down Expand Up @@ -1586,6 +1586,7 @@ public class ApiClient implements AutoCloseable {

private boolean waitBeforeRetry(long retryAfterMs){
try {
System.out.printf("Rate limit encountered. Retry the request in [%d] seconds\n", retryAfterMs/1000L);
Thread.sleep(retryAfterMs);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Expand Down
31 changes: 31 additions & 0 deletions resources/sdk/purecloudjava/tests/ApiClientRetryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,37 @@ public void invokeTestWith_429_And_No_MaxRetryTime() throws IOException {
}
}

@Test
public void invokeTestWith_429_MaxRetriesExceeded() throws IOException {
ApiClient.RetryConfiguration retryConfiguration = new ApiClient.RetryConfiguration();

retryConfiguration.setMaxRetryTimeSec(6);
retryConfiguration.setRetryAfterDefaultMs(100);
retryConfiguration.setRetryMax(0);
retryConfiguration.setMaxRetriesBeforeBackoff(2);

apiClient = getApiClient(retryConfiguration);

mockResponse = getMockCloseableHttpResponse(429);

Header header = mock(Header.class);
when(header.getName()).thenReturn("Retry-After");
when(header.getValue()).thenReturn("3");

when(mockResponse.getAllHeaders()).thenReturn(new Header[]{header});
doReturn(mockResponse).when(spyClient).execute(any(HttpUriRequest.class));

try {
stopwatch = Stopwatch.createStarted();
ApiResponse<ApiClientConnectorResponse> response = apiClient.invoke(getConnectorRequest(), getReturnType());
} catch (ApiException ex) {
Assert.assertEquals(429, ex.getStatusCode());
Assert.assertEquals("Max number of retries exceeded", ex.getMessage());
Assert.assertTrue(stopwatch.elapsed(TimeUnit.MILLISECONDS) >= 3000 && stopwatch.elapsed(TimeUnit.MILLISECONDS) < 3100, "Since retryMax is 0, after one retry exception is thrown");
stopwatch.stop();
}
}

@Test
public void invokeTestWith_502() throws IOException {
ApiClient.RetryConfiguration retryConfiguration = new ApiClient.RetryConfiguration();
Expand Down