Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Implement Retry Across Helpers #35

Open
abilson opened this issue Mar 6, 2019 · 1 comment
Open

Implement Retry Across Helpers #35

abilson opened this issue Mar 6, 2019 · 1 comment

Comments

@abilson
Copy link
Contributor

abilson commented Mar 6, 2019

One issue faced by many who run integration tests is the fickle nature of RSAPI calls in a test environment. At times this leads to failed tests because a setup method didn't succeed. A typical way to handle this is to implement retry functionality. Here's a proposed implementation:

The Command pattern is a common solution to the problem of retry. We could implement an abstract class like the one below and inherit from it commands for each helper method as we need it.

public abstract HelperCommand
{
int NumberOfRetries = Constant.NumberOfRetries;
int CurrentRetryCount = 0;

public void IncrementRetry() => CurrentRetryCount++;
public bool TryAgain() => CurrentRetryCount >= NumberOfRetries;
}

public DeleteCommand : HelperCommand
{
private Action _actionToExecute;
public DeleteCommand(Action actionToExecute)
{
_actionToExecute = actionToExecute;
}

public void Execute()
{
do
{
try { _actionToExecute.Invoke();  }
catch (Exception e)
{ 
base.IncrementRetry();
// handle exception
 }

} while (base.TryAgain())
}

}

// Usage example
public void Delete()
{
var deleteCommand = new DeleteCommand( () => DeleteUser(userID) );
deleteCommand.Execute();
}

A similar example could be done for a CreateCommand, except it will use Func instead of Action as the executing code.

@abilson
Copy link
Contributor Author

abilson commented Mar 6, 2019

An interesting follow-on to this issue could be implementing rollback functionality. Another issue that happens in integration tests from time-to-time is a test failing in an unexpected place and leaving artifacts in the environment which affect the next tests (typically artifacts at the Admin level). A rollback feature could register the commands executed and execute their corollary (for create, delete for example) in the case of failure, such as a finally block.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant