-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds result objects that hold exceptions, timing and other information related to each of the retries.
- Loading branch information
Max Young
committed
Nov 3, 2017
1 parent
387508e
commit f8c204b
Showing
6 changed files
with
205 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
|
||
namespace Mulligan.Tests | ||
{ | ||
[TestClass] | ||
public class While | ||
{ | ||
[TestMethod] | ||
public void RetryWhileFalse() | ||
{ | ||
int index = 0; | ||
List<int> list = new List<int>() {1, 2, 3}; | ||
|
||
Func<int> func = () => | ||
{ | ||
try | ||
{ | ||
return list[index]; | ||
} | ||
finally | ||
{ | ||
index++; | ||
} | ||
}; | ||
|
||
Predicate<int> predicate = @int => @int != 3; | ||
|
||
RetryResults<int> results = Retry.While(predicate, func, TimeSpan.FromSeconds(1)); | ||
|
||
Assert.AreEqual(3, results.GetResult()); | ||
Assert.AreEqual(3, results.GetRetryCount()); | ||
Assert.IsTrue(results.SuccessResult.Success); | ||
Assert.IsTrue(results.FailureResults.All(f => !f.Success)); | ||
Assert.IsTrue(results.Retries.All(r => r.Exception is null)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Mulligan.Models | ||
{ | ||
public sealed class RetryResult<TResult> : RetryResult | ||
{ | ||
public TResult Result { get; set; } | ||
} | ||
|
||
public class RetryResult | ||
{ | ||
public DateTime Start { get; set; } | ||
|
||
public DateTime Finish { get; set; } | ||
|
||
public TimeSpan Duration => Finish.Subtract(Start); | ||
|
||
public Exception Exception { get; set; } | ||
|
||
public bool Success { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Mulligan.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Mulligan | ||
{ | ||
public sealed class RetryResults<TResult> : RetryResults | ||
{ | ||
public TResult GetResult() => Retries.Last().Result; | ||
|
||
public override int GetRetryCount() => Retries.Count(); | ||
|
||
public new List<RetryResult<TResult>> FailureResults => Retries.Where(r => r.Success == false).ToList(); | ||
|
||
public new RetryResult<TResult> SuccessResult => Retries.Single(r => r.Success); | ||
|
||
public new List<RetryResult<TResult>> Retries { get; private set; } = new List<RetryResult<TResult>>(); | ||
} | ||
|
||
public class RetryResults | ||
{ | ||
public TimeSpan GetDuration => new TimeSpan(Retries.Sum(r => r.Duration.Ticks)); | ||
|
||
public virtual int GetRetryCount() => Retries.Count(); | ||
|
||
public List<RetryResult> FailureResults => Retries.Where(r => r.Success == false).ToList(); | ||
|
||
public RetryResult SuccessResult => Retries.Single(r => r.Success); | ||
|
||
public List<RetryResult> Retries { get; private set; } = new List<RetryResult>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters