|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.ComponentModel; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Xunit.Abstractions; |
| 10 | +using Xunit.Sdk; |
| 11 | + |
| 12 | +namespace Microsoft.ML.TestFrameworkCommon |
| 13 | +{ |
| 14 | + [Serializable] |
| 15 | + public class RetryTestCase : XunitTestCase |
| 16 | + { |
| 17 | + private int maxRetries; |
| 18 | + |
| 19 | + [EditorBrowsable(EditorBrowsableState.Never)] |
| 20 | + [Obsolete("Called by the de-serializer", true)] |
| 21 | + public RetryTestCase() { } |
| 22 | + |
| 23 | + public RetryTestCase(IMessageSink diagnosticMessageSink, TestMethodDisplay testMethodDisplay, |
| 24 | + ITestMethod testMethod, int maxRetries) |
| 25 | + : base(diagnosticMessageSink, testMethodDisplay, TestMethodDisplayOptions.None, testMethod, testMethodArguments: null) |
| 26 | + { |
| 27 | + this.maxRetries = maxRetries; |
| 28 | + } |
| 29 | + |
| 30 | + // This method is called by the xUnit test framework classes to run the test case. We will do the |
| 31 | + // loop here, forwarding on to the implementation in XunitTestCase to do the heavy lifting. We will |
| 32 | + // continue to re-run the test until the aggregator has an error (meaning that some internal error |
| 33 | + // condition happened), or the test runs without failure, or we've hit the maximum number of tries. |
| 34 | + public override async Task<RunSummary> RunAsync(IMessageSink diagnosticMessageSink, |
| 35 | + IMessageBus messageBus, |
| 36 | + object[] constructorArguments, |
| 37 | + ExceptionAggregator aggregator, |
| 38 | + CancellationTokenSource cancellationTokenSource) |
| 39 | + { |
| 40 | + var runCount = 0; |
| 41 | + |
| 42 | + while (true) |
| 43 | + { |
| 44 | + // This is really the only tricky bit: we need to capture and delay messages (since those will |
| 45 | + // contain run status) until we know we've decided to accept the final result; |
| 46 | + var delayedMessageBus = new DelayedMessageBus(messageBus); |
| 47 | + |
| 48 | + RunSummary summary = await base.RunAsync(diagnosticMessageSink, delayedMessageBus, constructorArguments, aggregator, cancellationTokenSource); |
| 49 | + if (aggregator.HasExceptions || summary.Failed > 0) |
| 50 | + { |
| 51 | + var details = ExtractTestFailDetailsFromMessageBus(delayedMessageBus); |
| 52 | + var errorMessage = $"Execution of '{DisplayName}' failed (attempt #{runCount + 1}) with details {details}."; |
| 53 | + |
| 54 | + diagnosticMessageSink.OnMessage(new DiagnosticMessage(errorMessage)); |
| 55 | + Console.WriteLine(errorMessage); |
| 56 | + } |
| 57 | + |
| 58 | + if (summary.Failed == 0 || ++runCount >= maxRetries) |
| 59 | + { |
| 60 | + delayedMessageBus.Dispose(); // Sends all the delayed messages |
| 61 | + return summary; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static string ExtractTestFailDetailsFromMessageBus(DelayedMessageBus delayedMessageBus) |
| 67 | + { |
| 68 | + string details = ""; |
| 69 | + |
| 70 | + foreach (var message in delayedMessageBus.messages) |
| 71 | + { |
| 72 | + if (message.ToString() == "Xunit.Sdk.TestFailed") |
| 73 | + { |
| 74 | + try |
| 75 | + { |
| 76 | + var messages = (string[])message.GetType().GetProperty("Messages").GetValue(message); |
| 77 | + var exceptionTypes = (string[])message.GetType().GetProperty("ExceptionTypes").GetValue(message); |
| 78 | + var stackTraces = (string[])message.GetType().GetProperty("StackTraces").GetValue(message); |
| 79 | + |
| 80 | + if (messages != null && messages.Length > 0) |
| 81 | + { |
| 82 | + details += "Messages: " + string.Join(";", messages) + ". "; |
| 83 | + } |
| 84 | + |
| 85 | + if (exceptionTypes != null && exceptionTypes.Length > 0) |
| 86 | + { |
| 87 | + details += "ExceptionTypes: " + string.Join(";", exceptionTypes) + ". "; |
| 88 | + } |
| 89 | + |
| 90 | + if (stackTraces != null && stackTraces.Length > 0) |
| 91 | + { |
| 92 | + details += "StackTraces: " + string.Join(";", stackTraces) + "."; |
| 93 | + } |
| 94 | + } |
| 95 | + catch |
| 96 | + { |
| 97 | + Console.WriteLine($"Fail to read test fail message from message bus."); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return details; |
| 103 | + } |
| 104 | + |
| 105 | + public override void Serialize(IXunitSerializationInfo data) |
| 106 | + { |
| 107 | + base.Serialize(data); |
| 108 | + |
| 109 | + data.AddValue("MaxRetries", maxRetries); |
| 110 | + } |
| 111 | + |
| 112 | + public override void Deserialize(IXunitSerializationInfo data) |
| 113 | + { |
| 114 | + base.Deserialize(data); |
| 115 | + |
| 116 | + maxRetries = data.GetValue<int>("MaxRetries"); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
0 commit comments