Skip to content

Commit e6d0363

Browse files
authored
Randomize the order of tests (#5831)
## Summary of changes Randomize the order of the tests. ## Reason for change Flaky tests are much harder to fix when we discover them long after they have been written. By randomizing the order of the tests, I'm hoping to make them fail earlier. In practice, this could temporarily increase the overall flakiness, but I expect this will reduce the overall effort spent on fixing tests. ## Implementation details In `CustomTestFramework`, randomize the list of all tests in each collections, and the collections themselves. The seed is displayed in the output. When a test order causes tests to fail, this allows to deterministically reproduce that test order. ## Other details Four other issues were found thanks to this: #6535, #6532, #6511, #6509
1 parent de7ffac commit e6d0363

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

.azure-pipelines/steps/run-in-docker.yml

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ steps:
100100
--env DD_LOGGER_SYSTEM_PULLREQUEST_SOURCEBRANCH \
101101
--env DD_LOGGER_DD_TAGS \
102102
--env IS_SSI_RUN \
103+
--env RANDOM_SEED \
103104
${{ parameters.extraArgs }} \
104105
dd-trace-dotnet/${{ parameters.baseImage }}-${{ parameters.target }}:$sdkVersion \
105106
dotnet /build/bin/Debug/_build.dll ${{ parameters.command }}

docker-compose.yml

+5
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ services:
454454
- DD_LOGGER_SYSTEM_PULLREQUEST_SOURCEBRANCH
455455
- DD_LOGGER_DD_TAGS
456456
- IS_SSI_RUN
457+
- RANDOM_SEED
457458
hostname: integrationtests
458459
depends_on:
459460
- servicestackredis
@@ -524,6 +525,7 @@ services:
524525
- DD_LOGGER_SYSTEM_PULLREQUEST_SOURCEBRANCH
525526
- DD_LOGGER_DD_TAGS
526527
- IS_SSI_RUN
528+
- RANDOM_SEED
527529
hostname: integrationtests
528530

529531
IntegrationTests.Serverless:
@@ -580,6 +582,7 @@ services:
580582
- DD_LOGGER_SYSTEM_PULLREQUEST_SOURCEBRANCH
581583
- DD_LOGGER_DD_TAGS
582584
- IS_SSI_RUN
585+
- RANDOM_SEED
583586
hostname: integrationtests
584587

585588
ExplorationTests:
@@ -722,6 +725,7 @@ services:
722725
- DD_LOGGER_SYSTEM_PULLREQUEST_SOURCEBRANCH
723726
- DD_LOGGER_DD_TAGS
724727
- IS_SSI_RUN
728+
- RANDOM_SEED
725729
depends_on:
726730
- servicestackredis_arm64
727731
- stackexchangeredis_arm64
@@ -800,6 +804,7 @@ services:
800804
- DD_LOGGER_SYSTEM_PULLREQUEST_SOURCEBRANCH
801805
- DD_LOGGER_DD_TAGS
802806
- IS_SSI_RUN
807+
- RANDOM_SEED
803808

804809
start-test-agent:
805810
image: andrewlock/wait-for-dependencies

tracer/test/Datadog.Trace.TestHelpers/CustomTestFramework.cs

+30-2
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,26 @@ protected override async Task<RunSummary> RunTestCollectionsAsync(IMessageBus me
126126
})
127127
.ToList();
128128

129+
if (Environment.GetEnvironmentVariable("RANDOM_SEED") is not { } environmentSeed
130+
|| !int.TryParse(environmentSeed, out var seed))
131+
{
132+
seed = new Random().Next();
133+
}
134+
135+
DiagnosticMessageSink.OnMessage(new DiagnosticMessage($"Using seed {seed} to randomize tests order"));
136+
137+
var random = new Random(seed);
138+
Shuffle(collections, random);
139+
140+
foreach (var collection in collections)
141+
{
142+
Shuffle(collection.TestCases, random);
143+
}
144+
129145
_runTestCollectionsCallback?.Invoke(DiagnosticMessageSink, collections.SelectMany(c => c.TestCases));
130146

131147
var summary = new RunSummary();
132-
133148
using var runner = new ConcurrentRunner();
134-
135149
var tasks = new List<Task<RunSummary>>();
136150

137151
foreach (var test in collections.Where(t => !t.DisableParallelization))
@@ -172,6 +186,20 @@ private static bool IsParallelizationDisabled(ITestCollection collection)
172186

173187
return attr?.GetNamedArgument<bool>(nameof(CollectionDefinitionAttribute.DisableParallelization)) is true;
174188
}
189+
190+
private static void Shuffle<T>(IList<T> list, Random rng)
191+
{
192+
int n = list.Count;
193+
194+
while (n > 1)
195+
{
196+
n--;
197+
int k = rng.Next(n + 1);
198+
var value = list[k];
199+
list[k] = list[n];
200+
list[n] = value;
201+
}
202+
}
175203
}
176204

177205
private class CustomTestCollectionRunner : XunitTestCollectionRunner

0 commit comments

Comments
 (0)