|
| 1 | +using System; |
| 2 | +using System.ComponentModel; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Xunit.Abstractions; |
| 6 | +using Xunit.Sdk; |
| 7 | + |
| 8 | +namespace BitFaster.Caching.UnitTests.Retry |
| 9 | +{ |
| 10 | + [Serializable] |
| 11 | + public class RetryTestCase : XunitTestCase |
| 12 | + { |
| 13 | + private int maxRetries; |
| 14 | + |
| 15 | + [EditorBrowsable(EditorBrowsableState.Never)] |
| 16 | + [Obsolete("Called by the de-serializer; should only be called by deriving classes for de-serialization purposes")] |
| 17 | + public RetryTestCase() { } |
| 18 | + |
| 19 | + public RetryTestCase(IMessageSink diagnosticMessageSink, TestMethodDisplay testMethodDisplay, TestMethodDisplayOptions defaultMethodDisplayOptions, ITestMethod testMethod, int maxRetries, object[] testMethodArguments = null) |
| 20 | + : base(diagnosticMessageSink, testMethodDisplay, defaultMethodDisplayOptions, testMethod, testMethodArguments: testMethodArguments) |
| 21 | + { |
| 22 | + this.maxRetries = maxRetries; |
| 23 | + } |
| 24 | + |
| 25 | + // This method is called by the xUnit test framework classes to run the test case. We will do the |
| 26 | + // loop here, forwarding on to the implementation in XunitTestCase to do the heavy lifting. We will |
| 27 | + // continue to re-run the test until the aggregator has an error (meaning that some internal error |
| 28 | + // condition happened), or the test runs without failure, or we've hit the maximum number of tries. |
| 29 | + public override async Task<RunSummary> RunAsync(IMessageSink diagnosticMessageSink, |
| 30 | + IMessageBus messageBus, |
| 31 | + object[] constructorArguments, |
| 32 | + ExceptionAggregator aggregator, |
| 33 | + CancellationTokenSource cancellationTokenSource) |
| 34 | + { |
| 35 | + var runCount = 0; |
| 36 | + |
| 37 | + while (true) |
| 38 | + { |
| 39 | + // This is really the only tricky bit: we need to capture and delay messages (since those will |
| 40 | + // contain run status) until we know we've decided to accept the final result; |
| 41 | + var delayedMessageBus = new DelayedMessageBus(messageBus); |
| 42 | + |
| 43 | + var summary = await base.RunAsync(diagnosticMessageSink, delayedMessageBus, constructorArguments, aggregator, cancellationTokenSource); |
| 44 | + if (aggregator.HasExceptions || summary.Failed == 0 || ++runCount >= maxRetries) |
| 45 | + { |
| 46 | + delayedMessageBus.Dispose(); // Sends all the delayed messages |
| 47 | + return summary; |
| 48 | + } |
| 49 | + |
| 50 | + diagnosticMessageSink.OnMessage(new DiagnosticMessage("Execution of '{0}' failed (attempt #{1}), retrying...", DisplayName, runCount)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public override void Serialize(IXunitSerializationInfo data) |
| 55 | + { |
| 56 | + base.Serialize(data); |
| 57 | + |
| 58 | + data.AddValue("MaxRetries", maxRetries); |
| 59 | + } |
| 60 | + |
| 61 | + public override void Deserialize(IXunitSerializationInfo data) |
| 62 | + { |
| 63 | + base.Deserialize(data); |
| 64 | + |
| 65 | + maxRetries = data.GetValue<int>("MaxRetries"); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments