-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wasm] Add support for a random test case orderer, for xunit tests (#…
…65628) * [wasm] Add support for a random test case orderer, for xunit tests This is enabled by default for wasm with `$(XUnitUseRandomizedTestOrderer)=true`. When the library tests run, they print two messages like: ``` info: Using random seed for test cases: 700149826 info: Using random seed for collections: 700149826 info: Starting: System.Collections.Immutable.Tests.dll ``` These seeds are picked randomly every time the tests are run. To run the tests with a specific seed, use environment variable `XUNIT_RANDOM_ORDER_SEED`. When running with tests, this can be used as: `WasmXHarnessMonoArgs="--setenv=XUNIT_RANDOM_ORDER_SEED=<seed>` * Enable test orderer only for libraries tests * [wasm] Automatically pass XUNIT_RANDOM_ORDER_SEED envvar to wasm apps * Disable random test ordering for `System.Xml.RW.XmlWriterApi.Tests` `System.Xml.Tests.TCCloseOutput.*` tests seem to depend on the order of execution. ``` [06:35:14] fail: [FAIL] System.Xml.Tests.TCCloseOutput.CloseOutput_4(utils: XmlWriterUtils { Async = False, WriterType = UTF8Writer }, outputType: "Stream") [06:35:14] info: System.IO.FileNotFoundException : File Not Found: writer.out [06:35:14] info: at XmlCoreTest.Common.FilePathUtil.getStream(String filename) [06:35:14] info: at System.Xml.Tests.TCCloseOutput.CloseOutput_4(XmlWriterUtils utils, String outputType) [06:35:14] info: at System.Reflection.RuntimeMethodInfo.InvokeWorker(Object obj, BindingFlags invokeAttr, Span`1 parameters) ``` * [wasm] Disable random tests ordering for `System.Runtime.Loader` It seems to cause failures like, reproducible with `XUNIT_RANDOM_ORDER_SEED=2106784294`: ``` [06:25:43] fail: [FAIL] System.Runtime.Loader.Tests.SatelliteAssembliesTests.SatelliteLoadsCorrectly_FromName(alc: "Empty", assemblyName: "System.Runtime.Loader.Tests", culture: "en") [06:25:43] info: Assert.Same() Failure [06:25:43] info: Expected: "Default" System.Runtime.Loader.DefaultAssemblyLoadContext #0 [06:25:43] info: Actual: "Empty" System.Runtime.Loader.AssemblyLoadContext #4 [06:25:43] info: at System.Runtime.Loader.Tests.SatelliteAssembliesTests.SatelliteLoadsCorrectly_FromName(String alc, String assemblyName, String culture) [06:25:43] info: at System.Reflection.RuntimeMethodInfo.InvokeWorker(Object obj, BindingFlags invokeAttr, Span`1 parameters) ``` * [wasm] Disable random test ordering for `System.Runtime.Numerics` Randomized runs seem to fail with: ``` [03:29:54] info: Starting: System.Runtime.Numerics.Tests.dll [03:29:58] fail: [FAIL] System.Numerics.Tests.cast_toTest.RunDoubleExplicitCastToBigIntegerTests [03:29:58] info: Assert.Equal() Failure [03:29:58] info: ↓ (pos 0) [03:29:58] info: Expected: -0 [03:29:58] info: Actual: 0 [03:29:58] info: ↑ (pos 0) [03:29:58] info: at System.Numerics.Tests.cast_toTest.VerifyDoubleExplicitCastToBigInteger(Double value) [03:29:58] info: at System.Numerics.Tests.cast_toTest.RunDoubleExplicitCastToBigIntegerTests() [03:29:58] info: at System.Reflection.RuntimeMethodInfo.InvokeWorker(Object obj, BindingFlags invokeAttr, Span`1 parameters) ``` Reproducible with `XUNIT_RANDOM_SEED_ORDER=1883302047`. * Add issues for the failing tests
- Loading branch information
Showing
11 changed files
with
138 additions
and
3 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
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
77 changes: 77 additions & 0 deletions
77
src/libraries/Common/tests/TestUtilities/RandomTestCaseOrderer.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,77 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Threading; | ||
using Xunit.Abstractions; | ||
using Xunit.Sdk; | ||
|
||
#nullable enable | ||
|
||
namespace TestUtilities; | ||
|
||
// Based on https://github.com/xunit/xunit/blob/v2/src/xunit.execution/Sdk/DefaultTestCaseOrderer.cs | ||
|
||
public class RandomTestCaseOrderer : ITestCaseOrderer | ||
{ | ||
public const string RandomSeedEnvironmentVariableName = "XUNIT_RANDOM_ORDER_SEED"; | ||
|
||
public static readonly Lazy<int> LazySeed = new (GetSeed, LazyThreadSafetyMode.ExecutionAndPublication); | ||
private readonly IMessageSink _diagnosticMessageSink; | ||
|
||
private static int GetSeed() | ||
{ | ||
string? seedEnvVar = Environment.GetEnvironmentVariable(RandomSeedEnvironmentVariableName); | ||
if (string.IsNullOrEmpty(seedEnvVar) || !int.TryParse(seedEnvVar, out int seed)) | ||
{ | ||
seed = new Random().Next(); | ||
} | ||
|
||
return seed; | ||
} | ||
|
||
public RandomTestCaseOrderer(IMessageSink diagnosticMessageSink) | ||
{ | ||
diagnosticMessageSink.OnMessage(new DiagnosticMessage($"Using random seed for test cases: {LazySeed.Value}")); | ||
_diagnosticMessageSink = diagnosticMessageSink; | ||
} | ||
|
||
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases) where TTestCase : ITestCase | ||
=> TryRandomize(testCases.ToList(), _diagnosticMessageSink, out List<TTestCase>? randomizedTests) | ||
? randomizedTests | ||
: testCases; | ||
|
||
public static bool TryRandomize<T>(List<T> tests, IMessageSink messageSink, [NotNullWhen(true)] out List<T>? randomizedTests) | ||
{ | ||
randomizedTests = null; | ||
try | ||
{ | ||
randomizedTests = Randomize(tests.ToList()); | ||
return true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
messageSink.OnMessage(new DiagnosticMessage($"Failed to randomize test cases: {ex}")); | ||
return false; | ||
} | ||
|
||
static List<T> Randomize(List<T> tests) | ||
{ | ||
var result = new List<T>(tests.Count); | ||
|
||
var randomizer = new Random(LazySeed.Value); | ||
|
||
while (tests.Count > 0) | ||
{ | ||
int next = randomizer.Next(tests.Count); | ||
result.Add(tests[next]); | ||
tests.RemoveAt(next); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/libraries/Common/tests/TestUtilities/RandomTestCollectionOrderer.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,29 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
using Xunit.Sdk; | ||
|
||
#nullable enable | ||
|
||
namespace TestUtilities; | ||
|
||
public class RandomTestCollectionOrderer : ITestCollectionOrderer | ||
{ | ||
private readonly IMessageSink _diagnosticMessageSink; | ||
|
||
public RandomTestCollectionOrderer(IMessageSink diagnosticMessageSink) | ||
{ | ||
diagnosticMessageSink.OnMessage(new DiagnosticMessage( | ||
$"Using random seed for collections: {RandomTestCaseOrderer.LazySeed.Value}")); | ||
_diagnosticMessageSink = diagnosticMessageSink; | ||
} | ||
|
||
public IEnumerable<ITestCollection> OrderTestCollections(IEnumerable<ITestCollection> testCollections) | ||
=> RandomTestCaseOrderer.TryRandomize(testCollections.ToList(), _diagnosticMessageSink, out List<ITestCollection>? randomizedTests) | ||
? randomizedTests | ||
: testCollections; | ||
} |
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
7 changes: 7 additions & 0 deletions
7
src/libraries/Common/tests/Tests/RandomizedTestOrderAssemblyInfo.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,7 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
|
||
[assembly: TestCaseOrderer("TestUtilities.RandomTestCaseOrderer", "TestUtilities")] | ||
[assembly: TestCollectionOrderer("TestUtilities.RandomTestCollectionOrderer", "TestUtilities")] |
1 change: 1 addition & 0 deletions
1
...ies/System.Private.Xml/tests/Writers/XmlWriterApi/System.Xml.RW.XmlWriterApi.Tests.csproj
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