-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RetryFact to deal with twitchy HRESULT test
- Loading branch information
1 parent
a6f5533
commit 4d9d689
Showing
6 changed files
with
167 additions
and
5 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
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,44 @@ | ||
// Taken from Xuint samples. https://github.com/xunit/samples.xunit/tree/main/RetryFactExample | ||
|
||
using Xunit.Abstractions; | ||
using Xunit.Sdk; | ||
|
||
namespace Xunit; | ||
|
||
/// <summary> | ||
/// Used to capture messages to potentially be forwarded later. Messages are forwarded by | ||
/// disposing of the message bus. | ||
/// </summary> | ||
public class DelayedMessageBus : IMessageBus | ||
{ | ||
private readonly IMessageBus _innerBus; | ||
private readonly List<IMessageSinkMessage> _messages = []; | ||
|
||
public DelayedMessageBus(IMessageBus innerBus) => _innerBus = innerBus; | ||
|
||
public bool QueueMessage(IMessageSinkMessage message) | ||
{ | ||
// Technically speaking, this lock isn't necessary in our case, because we know we're using this | ||
// message bus for a single test (so there's no possibility of parallelism). However, it's good | ||
// practice when something might be used where parallel messages might arrive, so it's here in | ||
// this sample. | ||
lock (_messages) | ||
{ | ||
_messages.Add(message); | ||
} | ||
|
||
// No way to ask the inner bus if they want to cancel without sending them the message, so | ||
// we just go ahead and continue always. | ||
return true; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
foreach (var message in _messages) | ||
{ | ||
_innerBus.QueueMessage(message); | ||
} | ||
|
||
GC.SuppressFinalize(this); | ||
} | ||
} |
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,8 @@ | ||
// Taken from Xuint samples. https://github.com/xunit/samples.xunit/tree/main/RetryFactExample | ||
|
||
namespace Xunit; | ||
|
||
public class RetryFactAttribute : FactAttribute | ||
{ | ||
public int MaxRetries { get; set; } = 3; | ||
} |
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,32 @@ | ||
// Taken from Xuint samples. https://github.com/xunit/samples.xunit/tree/main/RetryFactExample | ||
|
||
using Xunit.Abstractions; | ||
using Xunit.Sdk; | ||
|
||
namespace Xunit; | ||
|
||
public class RetryFactDiscoverer : IXunitTestCaseDiscoverer | ||
{ | ||
private readonly IMessageSink _diagnosticMessageSink; | ||
|
||
public RetryFactDiscoverer(IMessageSink diagnosticMessageSink) => _diagnosticMessageSink = diagnosticMessageSink; | ||
|
||
public IEnumerable<IXunitTestCase> Discover( | ||
ITestFrameworkDiscoveryOptions discoveryOptions, | ||
ITestMethod testMethod, | ||
IAttributeInfo factAttribute) | ||
{ | ||
var maxRetries = factAttribute.GetNamedArgument<int>("MaxRetries"); | ||
if (maxRetries < 1) | ||
{ | ||
maxRetries = 3; | ||
} | ||
|
||
yield return new RetryTestCase( | ||
_diagnosticMessageSink, | ||
discoveryOptions.MethodDisplayOrDefault(), | ||
discoveryOptions.MethodDisplayOptionsOrDefault(), | ||
testMethod, | ||
maxRetries); | ||
} | ||
} |
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,70 @@ | ||
// Taken from Xuint samples. https://github.com/xunit/samples.xunit/tree/main/RetryFactExample | ||
|
||
using System.ComponentModel; | ||
using Xunit.Abstractions; | ||
using Xunit.Sdk; | ||
|
||
namespace Xunit; | ||
|
||
[Serializable] | ||
public class RetryTestCase : XunitTestCase | ||
{ | ||
private int _maxRetries; | ||
|
||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
[Obsolete("Called by the de-serializer; should only be called by deriving classes for de-serialization purposes")] | ||
public RetryTestCase() { } | ||
|
||
public RetryTestCase( | ||
IMessageSink diagnosticMessageSink, | ||
TestMethodDisplay testMethodDisplay, | ||
TestMethodDisplayOptions defaultMethodDisplayOptions, | ||
ITestMethod testMethod, | ||
int maxRetries) | ||
: base(diagnosticMessageSink, testMethodDisplay, defaultMethodDisplayOptions, testMethod, testMethodArguments: null) | ||
{ | ||
_maxRetries = maxRetries; | ||
} | ||
|
||
// This method is called by the xUnit test framework classes to run the test case. We will do the | ||
// loop here, forwarding on to the implementation in XunitTestCase to do the heavy lifting. We will | ||
// continue to re-run the test until the aggregator has an error (meaning that some internal error | ||
// condition happened), or the test runs without failure, or we've hit the maximum number of tries. | ||
public override async Task<RunSummary> RunAsync( | ||
IMessageSink diagnosticMessageSink, | ||
IMessageBus messageBus, | ||
object[] constructorArguments, | ||
ExceptionAggregator aggregator, | ||
CancellationTokenSource cancellationTokenSource) | ||
{ | ||
var runCount = 0; | ||
|
||
while (true) | ||
{ | ||
// This is really the only tricky bit: we need to capture and delay messages (since those will | ||
// contain run status) until we know we've decided to accept the final result; | ||
var delayedMessageBus = new DelayedMessageBus(messageBus); | ||
|
||
var summary = await base.RunAsync(diagnosticMessageSink, delayedMessageBus, constructorArguments, aggregator, cancellationTokenSource); | ||
if (aggregator.HasExceptions || summary.Failed == 0 || ++runCount >= _maxRetries) | ||
{ | ||
delayedMessageBus.Dispose(); // Sends all the delayed messages | ||
return summary; | ||
} | ||
|
||
diagnosticMessageSink.OnMessage(new DiagnosticMessage($"Execution of '{DisplayName}' failed (attempt #{runCount}), retrying...")); | ||
} | ||
} | ||
|
||
public override void Serialize(IXunitSerializationInfo data) | ||
{ | ||
base.Serialize(data); | ||
data.AddValue("MaxRetries", _maxRetries); | ||
} | ||
|
||
public override void Deserialize(IXunitSerializationInfo data) | ||
{ | ||
base.Deserialize(data); | ||
_maxRetries = data.GetValue<int>("MaxRetries"); | ||
} | ||
} |