Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,8 @@ private static async Task<ITestFramework> BuildTestFrameworkAsync(TestFrameworkB
{
if (consumerService is ITestSessionLifetimeHandler handler)
{
// Validate that a handler with the same UID is not already registered
testSessionLifetimeHandlers.ValidateUniqueExtension(handler);
testSessionLifetimeHandlers.Add(handler);
}

Expand All @@ -722,6 +724,8 @@ private static async Task<ITestFramework> BuildTestFrameworkAsync(TestFrameworkB
// We register the lifetime handler if we're connected to the dotnet test pipe
if (pushOnlyProtocolDataConsumer is not null)
{
// Validate that a handler with the same UID is not already registered
testSessionLifetimeHandlers.ValidateUniqueExtension(pushOnlyProtocolDataConsumer);
testSessionLifetimeHandlers.Add(pushOnlyProtocolDataConsumer);
}

Expand All @@ -735,6 +739,8 @@ private static async Task<ITestFramework> BuildTestFrameworkAsync(TestFrameworkB
// We register the data consumer handler if we're connected to the dotnet test pipe
if (pushOnlyProtocolDataConsumer is not null)
{
// Validate that a consumer with the same UID is not already registered
dataConsumersBuilder.ValidateUniqueExtension(pushOnlyProtocolDataConsumer);
dataConsumersBuilder.Add(pushOnlyProtocolDataConsumer);
}

Expand All @@ -746,6 +752,8 @@ private static async Task<ITestFramework> BuildTestFrameworkAsync(TestFrameworkB

if (await abortForMaxFailedTestsExtension.IsEnabledAsync().ConfigureAwait(false))
{
// Validate that a consumer with the same UID is not already registered
dataConsumersBuilder.ValidateUniqueExtension(abortForMaxFailedTestsExtension);
dataConsumersBuilder.Add(abortForMaxFailedTestsExtension);
}

Expand Down Expand Up @@ -795,6 +803,9 @@ private static async Task RegisterAsServiceOrConsumerOrBothAsync(object service,
return;
}

// Validate that a consumer with the same UID is not already registered
dataConsumersBuilder.ValidateUniqueExtension(dataConsumer);

dataConsumersBuilder.Add(dataConsumer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ public async Task TestSessionLifetimeHandle_DuplicatedIdWithCompositeFactory_Sho
Assert.IsTrue(invalidOperationException.Message.Contains("duplicatedId") && invalidOperationException.Message.Contains(typeof(TestSessionLifetimeHandler).ToString()));
}

[TestMethod]
public void DataConsumer_AddingDuplicateToExistingList_ShouldFail()
{
// Simulate the scenario in BuildTestFrameworkAsync where consumers are added to a list
List<IDataConsumer> existingConsumers = [new Consumer("duplicatedId")];
IDataConsumer newConsumer = new Consumer("duplicatedId");

// This should throw when trying to add a duplicate
InvalidOperationException invalidOperationException = Assert.ThrowsExactly<InvalidOperationException>(() => existingConsumers.ValidateUniqueExtension(newConsumer));
Assert.IsTrue(invalidOperationException.Message.Contains("duplicatedId") && invalidOperationException.Message.Contains(typeof(Consumer).ToString()));
}

[TestMethod]
public void TestSessionLifetimeHandler_AddingDuplicateToExistingList_ShouldFail()
{
// Simulate the scenario in BuildTestFrameworkAsync where handlers are added to a list
List<ITestSessionLifetimeHandler> existingHandlers = [new TestSessionLifetimeHandler("duplicatedId")];
ITestSessionLifetimeHandler newHandler = new TestSessionLifetimeHandler("duplicatedId");

// This should throw when trying to add a duplicate
InvalidOperationException invalidOperationException = Assert.ThrowsExactly<InvalidOperationException>(() => existingHandlers.ValidateUniqueExtension(newHandler));
Assert.IsTrue(invalidOperationException.Message.Contains("duplicatedId") && invalidOperationException.Message.Contains(typeof(TestSessionLifetimeHandler).ToString()));
}

[DataRow(true)]
[DataRow(false)]
[TestMethod]
Expand Down
Loading