Skip to content

Commit

Permalink
Adds cancellation token to retry (#8)
Browse files Browse the repository at this point in the history
* Adds cancellation token to retry

- Adds cancellation token to retry
- Adds IsCanceled bool on RetryResult(s) model
- Adds Unit Tests for added cancellation functionality

* Test Fixes

Fixes Cancellation Tests to not be asynchronous
  • Loading branch information
gusvano authored and Max Young committed Oct 28, 2019
1 parent 81d9e1b commit d9648bd
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 3 deletions.
122 changes: 122 additions & 0 deletions Mulligan.Tests/RetryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Collections.Generic;
using System.Linq;
using Mulligan.Models;
using System.Threading;
using System.Threading.Tasks;

namespace Mulligan.Tests
{
Expand Down Expand Up @@ -185,5 +187,125 @@ void Action()
Assert.IsTrue(results.IsCompletedSuccessfully);
Assert.IsTrue(results.Failures.All(f => !f.IsCompletedSuccessfully));
}

[TestMethod]
public void RetryWhile_CancelRetry_Action()
{
CancellationTokenSource tokenSource = new CancellationTokenSource();

int index = 0;

List<Action> tasks = new List<Action>
{
() => throw new Exception("Exception 1"),
() => throw new Exception("Exception 2"),
() =>
{
tokenSource.Cancel();
throw new Exception("Exception 3");
},
() => { }
};

void Action()
{
try
{
tasks[index]();
}
finally
{
index++;
}
}

RetryResults results = Retry.While(Action, TimeSpan.FromSeconds(1), null, tokenSource.Token);

Assert.IsTrue(results.IsCanceled);
Assert.IsFalse(results.IsCompletedSuccessfully);
Assert.IsNotNull(results.Result.Exception);
Assert.IsTrue(results.Result.Exception is OperationCanceledException);
Assert.IsTrue(results.GetDuration < TimeSpan.FromSeconds(1));
}

[TestMethod]
public void RetryWhile_CancelRetry_NoPredicate()
{
CancellationTokenSource tokenSource = new CancellationTokenSource();

int index = 0;
List<Func<int>> tasks = new List<Func<int>>
{
() => throw new Exception("Exception 1"),
() => throw new Exception("Exception 2"),
() =>
{
tokenSource.Cancel();
throw new Exception("Exception 3");
},
() => 1
};

int Function()
{
try
{
return tasks[index]();
}
finally
{
index++;
}
}

RetryResults<int> results = Retry.While(Function, TimeSpan.FromSeconds(1), null, tokenSource.Token);

Assert.IsTrue(results.IsCanceled);
Assert.IsFalse(results.IsCompletedSuccessfully);
Assert.IsNotNull(results.Result.Exception);
Assert.IsTrue(results.Result.Exception is OperationCanceledException);
Assert.IsTrue(results.GetDuration < TimeSpan.FromSeconds(1));
}

[TestMethod]
public void RetryWhile_CancelRetry_Predicate()
{
CancellationTokenSource tokenSource = new CancellationTokenSource();

int index = 0;
List<Func<bool>> tasks = new List<Func<bool>>
{
() => false,
() => false,
() =>
{
tokenSource.Cancel();
return false;
},
() => true
};

bool Function()
{
try
{
return tasks[index]();
}
finally
{
index++;
}
}

bool ShouldRetry(bool @bool) => !@bool;

RetryResults<bool> results = Retry.While(ShouldRetry, Function, TimeSpan.FromSeconds(1), null, tokenSource.Token);

Assert.IsTrue(results.IsCanceled);
Assert.IsFalse(results.IsCompletedSuccessfully);
Assert.IsNotNull(results.Result.Exception);
Assert.IsTrue(results.Result.Exception is OperationCanceledException);
Assert.IsTrue(results.GetDuration < TimeSpan.FromSeconds(1));
}
}
}
5 changes: 5 additions & 0 deletions Mulligan/Models/RetryResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@ public class RetryResult
/// Gets whether the retry completed due to an unhandled exception
/// </summary>
public bool IsFaulted { get; internal set; }

/// <summary>
/// Gets whether the retry was canceled
/// </summary>
public bool IsCanceled { get; internal set; }
}
}
8 changes: 8 additions & 0 deletions Mulligan/Models/RetryResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public sealed class RetryResults<TResult> : RetryResults
/// <inheritdoc />
public override bool IsCompletedSuccessfully => Result?.IsCompletedSuccessfully ?? false;

/// <inheritdoc />
public override bool IsCanceled => Result?.IsCanceled ?? false;

/// <summary>
/// Returns a new List object that contains all the RetryResult with a IsCompleteSuccessfully of false
/// </summary>
Expand Down Expand Up @@ -53,6 +56,11 @@ public class RetryResults
/// </summary>
public virtual bool IsFaulted => Result?.IsFaulted ?? false;

/// <summary>
/// Gets wheter the last result was canceled
/// </summary>
public virtual bool IsCanceled => Result?.IsCanceled ?? false;

/// <summary>
/// Returns a new List object that contains all the RetryResult with a IsCompleteSuccessfully of false
/// </summary>
Expand Down
45 changes: 42 additions & 3 deletions Mulligan/Retry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public static class Retry
/// <param name="action">Action that will be retried</param>
/// <param name="timeout">Time the action will be retried</param>
/// <param name="retryInterval">Interval between retries</param>
public static RetryResults While(Action action, TimeSpan timeout, TimeSpan? retryInterval = null)
/// <param name="cancellationToken">Token to cancel retry operation</param>
public static RetryResults While(Action action, TimeSpan timeout, TimeSpan? retryInterval = null, CancellationToken cancellationToken = new CancellationToken())
{
DateTime start = DateTime.Now;
RetryResults results = new RetryResults();
Expand All @@ -26,10 +27,19 @@ public static RetryResults While(Action action, TimeSpan timeout, TimeSpan? retr

try
{
if (cancellationToken.IsCancellationRequested)
cancellationToken.ThrowIfCancellationRequested();

action();

result.IsCompletedSuccessfully = true;
}
catch (OperationCanceledException canceledException)
{
result.Exception = canceledException;
result.IsCompletedSuccessfully = false;
result.IsCanceled = true;
}
catch (Exception exception)
{
result.Exception = exception;
Expand All @@ -45,6 +55,9 @@ public static RetryResults While(Action action, TimeSpan timeout, TimeSpan? retr
if (result.IsCompletedSuccessfully)
return results;

if (result.IsCanceled)
return results;

if (IsTimedOut(start, timeout))
return results;

Expand All @@ -59,8 +72,9 @@ public static RetryResults While(Action action, TimeSpan timeout, TimeSpan? retr
/// <param name="function">Function that will be retried</param>
/// <param name="timeout">Time the action will be retried</param>
/// <param name="retryInterval">Interval between retries</param>
/// <param name="cancellationToken">Token to cancel retry operation</param>
/// <returns>Return of the function</returns>
public static RetryResults<TResult> While<TResult>(Func<TResult> function, TimeSpan timeout, TimeSpan? retryInterval = null)
public static RetryResults<TResult> While<TResult>(Func<TResult> function, TimeSpan timeout, TimeSpan? retryInterval = null, CancellationToken cancellationToken = new CancellationToken())
{
DateTime start = DateTime.Now;
RetryResults<TResult> results = new RetryResults<TResult>();
Expand All @@ -72,9 +86,18 @@ public static RetryResults<TResult> While<TResult>(Func<TResult> function, TimeS

try
{
if (cancellationToken.IsCancellationRequested)
cancellationToken.ThrowIfCancellationRequested();

result.Value = function();
result.IsCompletedSuccessfully = true;
}
catch(OperationCanceledException canceledException)
{
result.Exception = canceledException;
result.IsCompletedSuccessfully = false;
result.IsCanceled = true;
}
catch (Exception exception)
{
result.Exception = exception;
Expand All @@ -90,6 +113,9 @@ public static RetryResults<TResult> While<TResult>(Func<TResult> function, TimeS
if (result.IsCompletedSuccessfully)
return results;

if (result.IsCanceled)
return results;

if (IsTimedOut(start, timeout))
return results;

Expand All @@ -105,8 +131,9 @@ public static RetryResults<TResult> While<TResult>(Func<TResult> function, TimeS
/// <param name="function">Function that will be retried</param>
/// <param name="timeout">Time the action will be retried</param>
/// <param name="retryInterval">Interval between retries</param>
/// <param name="cancellationToken">Token to cancel retry operation</param>
/// <returns>Return of the function</returns>
public static RetryResults<TResult> While<TResult>(Predicate<TResult> shouldRetry, Func<TResult> function, TimeSpan timeout, TimeSpan? retryInterval = null)
public static RetryResults<TResult> While<TResult>(Predicate<TResult> shouldRetry, Func<TResult> function, TimeSpan timeout, TimeSpan? retryInterval = null, CancellationToken cancellationToken = new CancellationToken())
{
DateTime start = DateTime.Now;
RetryResults<TResult> results = new RetryResults<TResult>();
Expand All @@ -118,10 +145,19 @@ public static RetryResults<TResult> While<TResult>(Predicate<TResult> shouldRetr

try
{
if (cancellationToken.IsCancellationRequested)
cancellationToken.ThrowIfCancellationRequested();

result.Value = function();
if (!shouldRetry(result.Value))
result.IsCompletedSuccessfully = true;
}
catch(OperationCanceledException canceledException)
{
result.Exception = canceledException;
result.IsCompletedSuccessfully = false;
result.IsCanceled = true;
}
catch (Exception exception)
{
result.Exception = exception;
Expand All @@ -137,6 +173,9 @@ public static RetryResults<TResult> While<TResult>(Predicate<TResult> shouldRetr
if (result.IsCompletedSuccessfully)
return results;

if (result.IsCanceled)
return results;

if (IsTimedOut(start, timeout))
return results;

Expand Down

0 comments on commit d9648bd

Please sign in to comment.