Skip to content

Commit

Permalink
ParallelGroup Order Property (#1825)
Browse files Browse the repository at this point in the history
* ParallelGroup Order Property

* Equality members
  • Loading branch information
thomhurst authored Feb 12, 2025
1 parent 1164f6c commit 8c96327
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
6 changes: 4 additions & 2 deletions TUnit.Core/Attributes/ParallelGroupAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ namespace TUnit.Core;

public class ParallelGroupAttribute(string group) : TUnitAttribute, ITestDiscoveryEventReceiver
{
public int Order => 0;
int IEventReceiver.Order => 0;

public int Order { get; set; }

public string Group { get; } = group;

public void OnTestDiscovery(DiscoveredTestContext discoveredTestContext)
{
discoveredTestContext.SetParallelConstraint(new ParallelGroupConstraint(Group));
discoveredTestContext.SetParallelConstraint(new ParallelGroupConstraint(Group, Order));
}
}
22 changes: 21 additions & 1 deletion TUnit.Core/ParallelGroupConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,30 @@

namespace TUnit.Core;

public record ParallelGroupConstraint(string Group) : IParallelConstraint,
public record ParallelGroupConstraint(string Group, int Order) : IParallelConstraint,
IComparable<ParallelGroupConstraint>,
IComparable
{
public virtual bool Equals(ParallelGroupConstraint? other)
{
if (other is null)
{
return false;
}

if (ReferenceEquals(this, other))
{
return true;
}

return Group == other.Group;
}

public override int GetHashCode()
{
return Group.GetHashCode();
}

public int CompareTo(ParallelGroupConstraint? other)
{
return string.Compare(Group, other?.Group, StringComparison.Ordinal);
Expand Down
5 changes: 3 additions & 2 deletions TUnit.Engine/Extensions/TestContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.ExceptionServices;
using Microsoft.Testing.Platform.Extensions.TestFramework;
Expand Down Expand Up @@ -64,7 +65,7 @@ await testContext.GetService<TestRegistrar>().RegisterInstance(newTest,
Parallel = [newTest],
NotInParallel = new PriorityQueue<DiscoveredTest, int>(),
KeyedNotInParallel = new Dictionary<ConstraintKeysCollection, PriorityQueue<DiscoveredTest, int>>(),
ParallelGroups = new Dictionary<string, List<DiscoveredTest>>()
ParallelGroups = new ConcurrentDictionary<ParallelGroupConstraint, List<DiscoveredTest>>()
}, null, testContext.GetService<ExecuteRequestContext>());
}

Expand Down
5 changes: 3 additions & 2 deletions TUnit.Engine/Models/GroupedTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using TUnit.Core;
using System.Collections.Concurrent;
using TUnit.Core;

namespace TUnit.Engine.Models;

Expand All @@ -8,5 +9,5 @@ internal record GroupedTests
public required PriorityQueue<DiscoveredTest, int> NotInParallel { get; init; }
public required IDictionary<ConstraintKeysCollection, PriorityQueue<DiscoveredTest, int>> KeyedNotInParallel { get; init; }
public required IList<DiscoveredTest> Parallel { get; init; }
public required IDictionary<string, List<DiscoveredTest>> ParallelGroups { get; set; }
public required ConcurrentDictionary<ParallelGroupConstraint, List<DiscoveredTest>> ParallelGroups { get; set; }
}
4 changes: 2 additions & 2 deletions TUnit.Engine/Services/TestGrouper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public GroupedTests OrganiseTests(IReadOnlyCollection<DiscoveredTest> testCases)
var notInParallel = new PriorityQueue<DiscoveredTest, int>();
var keyedNotInParallel = new ConcurrentDictionary<ConstraintKeysCollection, PriorityQueue<DiscoveredTest, int>>();
var parallel = new List<DiscoveredTest>();
var parallelGroups = new ConcurrentDictionary<string, List<DiscoveredTest>>();
var parallelGroups = new ConcurrentDictionary<ParallelGroupConstraint, List<DiscoveredTest>>();

foreach (var discoveredTest in testCases)
{
Expand All @@ -35,7 +35,7 @@ public GroupedTests OrganiseTests(IReadOnlyCollection<DiscoveredTest> testCases)
}
else if (discoveredTest.TestDetails.ParallelConstraint is ParallelGroupConstraint parallelGroupConstraint)
{
parallelGroups.GetOrAdd(parallelGroupConstraint.Group, _ => []).Add(discoveredTest);
parallelGroups.GetOrAdd(parallelGroupConstraint, _ => []).Add(discoveredTest);
}
else
{
Expand Down
7 changes: 4 additions & 3 deletions TUnit.Engine/Services/TestsExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EnumerableAsyncProcessor.Extensions;
using System.Collections.Concurrent;
using EnumerableAsyncProcessor.Extensions;
using Microsoft.Testing.Platform.CommandLine;
using Microsoft.Testing.Platform.Extensions.TestFramework;
using Microsoft.Testing.Platform.Requests;
Expand Down Expand Up @@ -83,10 +84,10 @@ await testsToProcess.Values
.ProcessInParallel(_maximumParallelTests);
}

private async Task ProcessParallelGroups(IDictionary<string, List<DiscoveredTest>> groups,
private async Task ProcessParallelGroups(ConcurrentDictionary<ParallelGroupConstraint, List<DiscoveredTest>> groups,
ITestExecutionFilter? filter, ExecuteRequestContext context)
{
foreach (var (_, value) in groups)
foreach (var (_, value) in groups.OrderBy(x => x.Key.Order))
{
await ProcessParallelTests(value, filter, context);
}
Expand Down

0 comments on commit 8c96327

Please sign in to comment.