-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
154 additions
and
30 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 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
101 changes: 101 additions & 0 deletions
101
src/Samples/Sample.AspNetCore.SystemTests/Test/Base/RetryWithExceptionAttribute.cs
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,101 @@ | ||
using NUnit.Framework; | ||
using NUnit.Framework.Interfaces; | ||
using NUnit.Framework.Internal; | ||
using NUnit.Framework.Internal.Commands; | ||
using System; | ||
|
||
namespace Sample.AspNetCore.SystemTests.Test.Base | ||
{ | ||
/// <summary> | ||
/// Specifies that a test method should be rerun on failure up to the specified | ||
/// maximum number of times. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] | ||
public class RetryWithExceptionAttribute : NUnitAttribute, IRepeatTest | ||
{ | ||
private readonly int _tryCount; | ||
|
||
/// <summary> | ||
/// Construct a <see cref="RetryAttribute" /> | ||
/// </summary> | ||
/// <param name="tryCount">The maximum number of times the test should be run if it fails</param> | ||
public RetryWithExceptionAttribute(int tryCount) | ||
{ | ||
_tryCount = tryCount; | ||
} | ||
|
||
#region IRepeatTest Members | ||
|
||
/// <summary> | ||
/// Wrap a command and return the result. | ||
/// </summary> | ||
/// <param name="command">The command to be wrapped</param> | ||
/// <returns>The wrapped command</returns> | ||
public TestCommand Wrap(TestCommand command) | ||
{ | ||
return new RetryCommand(command, _tryCount); | ||
} | ||
|
||
#endregion | ||
|
||
#region Nested RetryCommand Class | ||
|
||
/// <summary> | ||
/// The test command for the <see cref="RetryAttribute"/> | ||
/// </summary> | ||
public class RetryCommand : DelegatingTestCommand | ||
{ | ||
private readonly int _tryCount; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="RetryCommand"/> class. | ||
/// </summary> | ||
/// <param name="innerCommand">The inner command.</param> | ||
/// <param name="tryCount">The maximum number of repetitions</param> | ||
public RetryCommand(TestCommand innerCommand, int tryCount) | ||
: base(innerCommand) | ||
{ | ||
_tryCount = tryCount; | ||
} | ||
|
||
/// <summary> | ||
/// Runs the test, saving a TestResult in the supplied TestExecutionContext. | ||
/// </summary> | ||
/// <param name="context">The context in which the test should run.</param> | ||
/// <returns>A TestResult</returns> | ||
public override TestResult Execute(TestExecutionContext context) | ||
{ | ||
int count = _tryCount; | ||
|
||
while (count-- > 0) | ||
{ | ||
try | ||
{ | ||
context.CurrentResult = innerCommand.Execute(context); | ||
} | ||
// Commands are supposed to catch exceptions, but some don't | ||
// and we want to look at restructuring the API in the future. | ||
catch (Exception ex) | ||
{ | ||
if (context.CurrentResult == null) context.CurrentResult = context.CurrentTest.MakeTestResult(); | ||
context.CurrentResult.RecordException(ex); | ||
} | ||
|
||
if (context.CurrentResult.ResultState != ResultState.Failure && context.CurrentResult.ResultState != ResultState.Error) | ||
break; | ||
|
||
// Clear result for retry | ||
if (count > 0) | ||
{ | ||
context.CurrentResult = context.CurrentTest.MakeTestResult(); | ||
context.CurrentRepeatCount++; // increment Retry count for next iteration. will only happen if we are guaranteed another iteration | ||
} | ||
} | ||
|
||
return context.CurrentResult; | ||
} | ||
} | ||
|
||
#endregion | ||
} | ||
} |
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 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 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 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 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
Oops, something went wrong.