Skip to content

Commit

Permalink
Add retry count server configurations and add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
osandamaleesha committed Sep 19, 2024
1 parent edba811 commit a8fffd8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void setRequestEntity(HttpPost httpPost, String jsonRequest, AuthMethods
private ActionInvocationResponse executeRequest(HttpPost request) {

int attempts = 0;
int retryCount = 2; // todo: read from server configurations
int retryCount = ActionExecutorConfig.getInstance().getHttpRequestRetryCount();
ActionInvocationResponse actionInvocationResponse = null;

while (attempts < retryCount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class ActionExecutorConfig {
"Actions.HTTPClient.HTTPConnectionRequestTimeout";
private static final String HTTP_CONNECTION_TIMEOUT_PROPERTY = "Actions.HTTPClient.HTTPConnectionTimeout";
private static final String HTTP_CONNECTION_POOL_SIZE_PROPERTY = "Actions.HTTPClient.HTTPConnectionPoolSize";
private static final String HTTP_REQUEST_RETRY_COUNT_PROPERTY = "Actions.HTTPClient.HTTPRequestRetryCount";
private static final int DEFAULT_HTTP_REQUEST_RETRY_COUNT = 2;
private static final int DEFAULT_HTTP_CONNECTION_POOL_SIZE = 20;
private static final int DEFAULT_HTTP_READ_TIMEOUT_IN_MILLIS = 5000;
private static final int DEFAULT_HTTP_CONNECTION_REQUEST_TIMEOUT_IN_MILLIS = 2000;
Expand Down Expand Up @@ -82,6 +84,28 @@ public boolean isExecutionForActionTypeEnabled(ActionType actionType) {
}
}

/**
* Returns the HTTP request retry count based on the system configuration.
*
* @return The HTTP request retry count, or the default if the property is missing or invalid.
*/
public int getHttpRequestRetryCount() {

int retryCountPropertyValue = DEFAULT_HTTP_REQUEST_RETRY_COUNT;
String retryCountValue = (String) IdentityConfigParser.getInstance().getConfiguration().
get(HTTP_REQUEST_RETRY_COUNT_PROPERTY);
if (StringUtils.isNotBlank(retryCountValue)) {
try {
retryCountPropertyValue = Integer.parseInt(retryCountValue);
} catch (NumberFormatException e) {
LOG.debug("Failed to read Http request retry count property in identity.xml." +
" Expects a number. Using the default value: " +
DEFAULT_HTTP_REQUEST_RETRY_COUNT, e);
}
}
return retryCountPropertyValue;
}

/**
* Returns the HTTP connection pool size based on the system configuration.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void setUp() throws Exception {
ActionExecutorConfig actionExecutorConfig = mock(ActionExecutorConfig.class);
actionExecutorConfigStatic.when(ActionExecutorConfig::getInstance).thenReturn(actionExecutorConfig);
MockitoAnnotations.openMocks(this);
when(actionExecutorConfig.getHttpRequestRetryCount()).thenReturn(2);
setField(apiClient, "httpClient", httpClient);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,23 @@ public void testGetHttpConnectionPoolSizeForInvalidConfig() {
when(mockIdentityConfigParser.getConfiguration()).thenReturn(configMap);
Assert.assertEquals(20, actionExecutorConfig.getHttpConnectionPoolSize());
}

@Test
public void testGetHttpRequestRetryCount() {

Map<String, Object> configMap = new HashMap<>();
configMap.put("Actions.HTTPClient.HTTPRequestRetryCount", "2");
when(mockIdentityConfigParser.getConfiguration()).thenReturn(configMap);
Assert.assertEquals(2, actionExecutorConfig.getHttpRequestRetryCount());
}

@Test
public void testGetHttpRequestRetryCountForInvalidConfig() {

//If the server configuration value is not a number, the default http request retry count value of 2 is parsed
Map<String, Object> configMap = new HashMap<>();
configMap.put("Actions.HTTPClient.HTTPRequestRetryCount", "value");
when(mockIdentityConfigParser.getConfiguration()).thenReturn(configMap);
Assert.assertEquals(2, actionExecutorConfig.getHttpRequestRetryCount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2025,6 +2025,7 @@
<HTTPReadTimeout>{{actions.http_client.read_timeout}}</HTTPReadTimeout>
<HTTPConnectionRequestTimeout>{{actions.http_client.request_timeout}}</HTTPConnectionRequestTimeout>
<HTTPConnectionPoolSize>{{actions.http_client.connection_pool_size}}</HTTPConnectionPoolSize>
<HTTPRequestRetryCount>{{actions.http_client.retry_count}}</HTTPRequestRetryCount>
</HTTPClient>
<MaximumActionsPerActionType>{{actions.maximum_actions_per_action_type}}</MaximumActionsPerActionType>
<ActionRequest>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,7 @@
"actions.http_client.read_timeout": 5000,
"actions.http_client.request_timeout": 2000,
"actions.http_client.connection_pool_size": 20,
"actions.http_client.retry_count": 2,
"actions.maximum_actions_per_action_type": 1,
"actions.action_request.excluded_headers": [
"authorization",
Expand Down

0 comments on commit a8fffd8

Please sign in to comment.