This repository has been archived by the owner on Oct 18, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
10 changed files
with
187 additions
and
6 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
37 changes: 37 additions & 0 deletions
37
tests/Prism.Plugin.Popups.Tests.Shared/Helpers/DelayedMessageBus.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,37 @@ | ||
using System.Collections.Generic; | ||
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 = new List<IMessageSinkMessage>(); | ||
|
||
public DelayedMessageBus(IMessageBus innerBus) | ||
{ | ||
this.innerBus = innerBus; | ||
} | ||
|
||
public bool QueueMessage(IMessageSinkMessage message) | ||
{ | ||
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); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/Prism.Plugin.Popups.Tests.Shared/Helpers/RetryAttribute.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,27 @@ | ||
using Xunit; | ||
using Xunit.Sdk; | ||
|
||
namespace Xunit | ||
{ | ||
/// <summary> | ||
/// Works just like [Fact] except that failures are retried (by default, 3 times). | ||
/// </summary> | ||
#if DRYIOC | ||
[XunitTestCaseDiscoverer("Xunit.RetryFactDiscoverer", "Prism.Plugin.Popups.DryIoc.Tests")] | ||
#else | ||
[XunitTestCaseDiscoverer("Xunit.RetryFactDiscoverer", "Prism.Plugin.Popups.Unity.Tests")] | ||
#endif | ||
public class RetryAttribute : FactAttribute | ||
{ | ||
public RetryAttribute(int maxRetries = 3) | ||
{ | ||
MaxRetries = maxRetries; | ||
} | ||
|
||
/// <summary> | ||
/// Number of retries allowed for a failed test. If unset (or set less than 1), will | ||
/// default to 3 attempts. | ||
/// </summary> | ||
public int MaxRetries { get; set; } | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/Prism.Plugin.Popups.Tests.Shared/Helpers/RetryFactDiscoverer.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,25 @@ | ||
using System.Collections.Generic; | ||
using Xunit.Abstractions; | ||
using Xunit.Sdk; | ||
|
||
namespace Xunit | ||
{ | ||
public class RetryFactDiscoverer : IXunitTestCaseDiscoverer | ||
{ | ||
readonly IMessageSink diagnosticMessageSink; | ||
|
||
public RetryFactDiscoverer(IMessageSink diagnosticMessageSink) | ||
{ | ||
this.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(), TestMethodDisplayOptions.All, testMethod, maxRetries); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
tests/Prism.Plugin.Popups.Tests.Shared/Helpers/RetryTestCase.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,79 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
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", true)] | ||
public RetryTestCase() { } | ||
|
||
//[Obsolete] | ||
//public RetryTestCase(IMessageSink diagnosticMessageSink, TestMethodDisplay testMethodDisplay, ITestMethod testMethod, int maxRetries) | ||
// : base(diagnosticMessageSink, testMethodDisplay, testMethod, testMethodArguments: null) | ||
//{ | ||
// this.maxRetries = maxRetries; | ||
//} | ||
|
||
public RetryTestCase(IMessageSink diagnosticMessageSink, TestMethodDisplay defaultMethodDisplay, TestMethodDisplayOptions defaultMethodDisplayOptions, ITestMethod testMethod, int maxRetries, object[] testMethodArguments = null) | ||
: base(diagnosticMessageSink, defaultMethodDisplay, defaultMethodDisplayOptions, testMethod, testMethodArguments) | ||
{ | ||
this.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 '{0}' failed (attempt #{1}), retrying...", DisplayName, runCount)); | ||
Xamarin.Forms.Mocks.MockForms.Init(); | ||
Prism.Navigation.PageNavigationRegistry.ClearRegistrationCache(); | ||
GC.Collect(); | ||
await Task.Delay(100); | ||
} | ||
} | ||
|
||
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"); | ||
} | ||
} | ||
} |
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