plugins: Add 'break' at RedifshClient request re-try loop (#4846)

* Break loop if no exception on http request

* Add new tests ensuring the correct execution flow of the RedfishClient retry

* Log retry as "retry attempt %d/%d"
This commit is contained in:
Gabriel Beims Bräscher 2021-03-24 03:54:40 -03:00 committed by GitHub
parent 61de2371ab
commit 96dd7280f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -231,8 +231,9 @@ public class RedfishClient {
for (int attempt = 1; attempt < redfishRequestMaxRetries + 1; attempt++) {
try {
TimeUnit.SECONDS.sleep(WAIT_FOR_REQUEST_RETRY);
LOGGER.debug(String.format("Retry HTTP %s request [URL: %s], attempt %d/%d.", httpReq.getMethod(), url, attempt, redfishRequestMaxRetries));
LOGGER.debug(String.format("HTTP %s request retry attempt %d/%d [URL: %s].", httpReq.getMethod(), url, attempt, redfishRequestMaxRetries));
response = client.execute(httpReq);
break;
} catch (IOException | InterruptedException e) {
if (attempt == redfishRequestMaxRetries) {
throw new RedfishException(String.format("Failed to execute HTTP %s request retry attempt %d/%d [URL: %s] due to exception %s", httpReq.getMethod(), attempt, redfishRequestMaxRetries,url, e));

View File

@ -207,4 +207,26 @@ public class RedfishClientTest {
Mockito.verify(newRedfishClientspy, Mockito.times(1)).retryHttpRequest(Mockito.anyString(), Mockito.any(), Mockito.any());
Mockito.verify(client, Mockito.times(3)).execute(Mockito.any());
}
@Test(expected = RedfishException.class)
public void retryHttpRequestExceptionAfterTwoRetries() throws IOException {
Mockito.when(client.execute(httpReq)).thenThrow(IOException.class).thenThrow(IOException.class);
RedfishClient newRedfishClientspy = Mockito.spy(new RedfishClient(USERNAME, PASSWORD, true, true, REDFISHT_REQUEST_RETRIES));
newRedfishClientspy.retryHttpRequest(url, httpReq, client);
Mockito.verify(newRedfishClientspy, Mockito.never()).retryHttpRequest(Mockito.anyString(), Mockito.any(), Mockito.any());
Mockito.verify(client, Mockito.never()).execute(Mockito.any());
}
@Test
public void retryHttpRequestSuccessAtTheSecondRetry() throws IOException {
Mockito.when(client.execute(httpReq)).thenThrow(IOException.class).thenReturn(httpResponse);
RedfishClient newRedfishClientspy = Mockito.spy(new RedfishClient(USERNAME, PASSWORD, true, true, REDFISHT_REQUEST_RETRIES));
newRedfishClientspy.retryHttpRequest(url, httpReq, client);
Mockito.verify(newRedfishClientspy, Mockito.times(1)).retryHttpRequest(Mockito.anyString(), Mockito.any(), Mockito.any());
Mockito.verify(client, Mockito.times(REDFISHT_REQUEST_RETRIES)).execute(Mockito.any());
}
}