-
Notifications
You must be signed in to change notification settings - Fork 1
/
RetryCommand.cs
49 lines (40 loc) · 1.35 KB
/
RetryCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
namespace SkbKontur.NUnit.Retries
{
public class RetryCommand : DelegatingTestCommand
{
public RetryCommand(TestCommand innerCommand, IRetryStrategy strategy)
: base(innerCommand)
{
this.strategy = strategy;
}
public override TestResult Execute(TestExecutionContext context)
{
var count = strategy.TryCount;
while (count-- > 0)
{
var start = DateTimeOffset.UtcNow;
try
{
context.CurrentResult = innerCommand.Execute(context);
}
catch (Exception ex)
{
context.CurrentResult ??= context.CurrentTest.MakeTestResult();
context.CurrentResult.RecordException(ex);
}
if (count <= 0 || !strategy.ShouldRetry(context.CurrentResult))
{
break;
}
strategy.OnTestFailed(context, start);
context.CurrentResult = context.CurrentTest.MakeTestResult();
context.CurrentRepeatCount++;
}
return context.CurrentResult;
}
private readonly IRetryStrategy strategy;
}
}