Skip to content

Commit

Permalink
refactor: Improve readability for Execute and add a retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Dor-bl committed Aug 10, 2023
1 parent 1281c06 commit b9aa703
Showing 1 changed file with 62 additions and 23 deletions.
85 changes: 62 additions & 23 deletions src/Appium.Net/Appium/Service/AppiumCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal class AppiumCommandExecutor : ICommandExecutor
private ICommandExecutor RealExecutor;
private bool isDisposed;
private const string IdempotencyHeader = "X-Idempotency-Key";
private const int MaxRetryAttempts = 3;

private static ICommandExecutor CreateRealExecutor(Uri remoteAddress, TimeSpan commandTimeout)
{
Expand Down Expand Up @@ -50,38 +51,76 @@ public Response Execute(Command commandToExecute)
{
Response result = null;

try
for (int attempt = 1; attempt <= MaxRetryAttempts; attempt++)
{
if (commandToExecute.Name == DriverCommand.NewSession)
try
{
Service?.Start();
RealExecutor = ModifyNewSessionHttpRequestHeader(RealExecutor);
}
HandleNewSessionCommand(commandToExecute);

result = RealExecutor.Execute(commandToExecute);
return result;
}
catch (Exception e)
{
if ((commandToExecute.Name == DriverCommand.NewSession))
result = RealExecutor.Execute(commandToExecute);

HandleCommandCompletion(commandToExecute, result);

if (result.Status == WebDriverResult.Success)
{
return result;
}
}
catch (Exception)
{
Service?.Dispose();
HandleCommandException(commandToExecute);
throw;
}
}
return result;
}

throw;
/// <summary>
/// Handles a new session command.
/// If the command is a NewSession command, it starts the service if not already started,
/// and modifies the HTTP request header of the real executor for a new session.
/// </summary>
/// <param name="command">The command to handle.</param>
private void HandleNewSessionCommand(Command command)
{
if (command.Name == DriverCommand.NewSession)
{
Service?.Start();
RealExecutor = ModifyNewSessionHttpRequestHeader(RealExecutor);
}
finally
}

/// <summary>
/// Handles the completion of a command.
/// If the result indicates a failure for a NewSession command, disposes the resources.
/// If the command is a Quit command, disposes the resources.
/// </summary>
/// <param name="command">The command that was executed.</param>
/// <param name="result">The result of the command execution.</param>
private void HandleCommandCompletion(Command command, Response result)
{
if (result != null && result.Status != WebDriverResult.Success &&
command.Name == DriverCommand.NewSession)
{
if (result != null && result.Status != WebDriverResult.Success &&
commandToExecute.Name == DriverCommand.NewSession)
{
Dispose();
}
Dispose();
}

if (commandToExecute.Name == DriverCommand.Quit)
{
Dispose();
}
if (command.Name == DriverCommand.Quit)
{
Dispose();
}
}

/// <summary>
/// Handles exceptions that occur while processing a command.
/// If the command is a new session command, it disposes the resources.
/// </summary>
/// <param name="command">The command to handle.</param>
private void HandleCommandException(Command command)
{
if (command.Name == DriverCommand.NewSession)
{
Dispose();
}
}

Expand Down

0 comments on commit b9aa703

Please sign in to comment.