From 665ef05d17cc9615651ffe8880529f653040cab2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 14 May 2026 09:19:29 +0000 Subject: [PATCH 1/5] Initial plan From 117e9f56cb58760c9ea95f67e9c4e0603df4a4fe Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 14 May 2026 09:36:12 +0000 Subject: [PATCH 2/5] Register ServerTelemetry in correct location of TestHostBuilder Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com> --- .../Hosts/ServerTestHost.cs | 4 +--- .../Hosts/TestHostBuilder.Modes.cs | 5 ++++ .../Services/ServiceProvider.cs | 24 +++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.cs b/src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.cs index 3c8b6d80f41..c6672dfde48 100644 --- a/src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.cs +++ b/src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.cs @@ -29,7 +29,6 @@ internal sealed partial class ServerTestHost : CommonHost, IServerTestHost, IDis private readonly IMessageHandlerFactory _messageHandlerFactory; private readonly TestFrameworkManager _testFrameworkManager; private readonly TestHostManager _testSessionManager; - private readonly ServerTelemetry _telemetryService; private readonly IAsyncMonitor _messageMonitor; private readonly IEnvironment _environment; private readonly ILogger _logger; @@ -66,7 +65,6 @@ public ServerTestHost( _messageHandlerFactory = messageHandlerFactory; _testFrameworkManager = testFrameworkManager; _testSessionManager = testSessionManager; - _telemetryService = new ServerTelemetry(this); _clientToServerRequests = new(); _serverToClientRequests = new(); @@ -571,7 +569,7 @@ await ExecuteRequestAsync( requestExecuteStart, (DateTimeOffset)requestExecuteStop, testNodeUpdateProcessor.GetTestNodeStatistics().TotalDiscoveredTests); - await _telemetryService.LogEventAsync(isRunRequest ? TelemetryEvents.TestsRunEventName : TelemetryEvents.TestsDiscoveryEventName, metadata, cancellationToken).ConfigureAwait(false); + await ServiceProvider.GetTelemetryCollector().LogEventAsync(isRunRequest ? TelemetryEvents.TestsRunEventName : TelemetryEvents.TestsDiscoveryEventName, metadata, cancellationToken).ConfigureAwait(false); return isRunRequest ? new RunResponseArgs([.. testNodeUpdateProcessor.Artifacts]) diff --git a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs index 4b40a731b31..cc4a916a061 100644 --- a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs +++ b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs @@ -217,6 +217,11 @@ private async Task BuildServerTestHostAsync(BuildContext context, NamedPi (TestFrameworkManager)TestFramework!, (TestHostManager)TestHost); + // Register ServerTelemetry in the service provider so that all telemetry events in server mode + // are forwarded to the client via JSON-RPC. This replaces the NopTelemetryService registered + // earlier in SetupCommonServicesAsync. + context.ServiceProvider.ReplaceService(new ServerTelemetry(serverTestHost)); + #pragma warning disable CA1416 // Preserve existing browser behavior while splitting the method. IHost actualTestHost = testControllerConnection is not null ? new TestHostControlledHost(testControllerConnection, serverTestHost, context.TestApplicationCancellationTokenSource.CancellationToken) diff --git a/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs b/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs index 7433e983fbf..85315dc6d91 100644 --- a/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs +++ b/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs @@ -72,6 +72,30 @@ public bool TryAddService(object service) return true; } + /// + /// Replaces the first registered service instance that is assignable to the given type with the new service. + /// If no such service is found, the new service is added at the end of the list. + /// + /// The type of service to replace. + internal void ReplaceService(T newService) + where T : class + { + ArgumentGuard.Ensure( + newService is not ITestFramework || AllowTestAdapterFrameworkRegistration, + nameof(newService), + PlatformResources.ServiceProviderShouldNotRegisterTestFramework); + + int index = _services.FindIndex(s => s is T); + if (index >= 0) + { + _services[index] = newService; + } + else + { + _services.Add(newService); + } + } + public IEnumerable GetServicesInternal( Type serviceType, bool stopAtFirst = false, From 1653cac834510ec0852f4f7cfad2b77c8992a58a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 14 May 2026 09:37:23 +0000 Subject: [PATCH 3/5] Fix ReplaceService XML doc comment wording Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com> --- .../Microsoft.Testing.Platform/Services/ServiceProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs b/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs index 85315dc6d91..e83826d1463 100644 --- a/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs +++ b/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs @@ -73,7 +73,7 @@ public bool TryAddService(object service) } /// - /// Replaces the first registered service instance that is assignable to the given type with the new service. + /// Replaces the first registered service instance that is an instance of with the new service. /// If no such service is found, the new service is added at the end of the list. /// /// The type of service to replace. From aef656625cd2615e5be0a846b871490b50dd18fc Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 14 May 2026 10:48:06 +0000 Subject: [PATCH 4/5] Address review comments: null-guard, IsEnabled check, and ReplaceService unit tests Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com> --- .../Hosts/TestHostBuilder.Modes.cs | 5 ++- .../Services/ServiceProvider.cs | 1 + .../Services/ServiceProviderTests.cs | 41 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs index cc4a916a061..71800ea5834 100644 --- a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs +++ b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs @@ -220,7 +220,10 @@ private async Task BuildServerTestHostAsync(BuildContext context, NamedPi // Register ServerTelemetry in the service provider so that all telemetry events in server mode // are forwarded to the client via JSON-RPC. This replaces the NopTelemetryService registered // earlier in SetupCommonServicesAsync. - context.ServiceProvider.ReplaceService(new ServerTelemetry(serverTestHost)); + if (context.ServiceProvider.GetTelemetryInformation().IsEnabled) + { + context.ServiceProvider.ReplaceService(new ServerTelemetry(serverTestHost)); + } #pragma warning disable CA1416 // Preserve existing browser behavior while splitting the method. IHost actualTestHost = testControllerConnection is not null diff --git a/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs b/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs index e83826d1463..7b41dda0612 100644 --- a/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs +++ b/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs @@ -80,6 +80,7 @@ public bool TryAddService(object service) internal void ReplaceService(T newService) where T : class { + _ = newService ?? throw new ArgumentNullException(nameof(newService)); ArgumentGuard.Ensure( newService is not ITestFramework || AllowTestAdapterFrameworkRegistration, nameof(newService), diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/ServiceProviderTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/ServiceProviderTests.cs index b2c838a5a6e..441b280a175 100644 --- a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/ServiceProviderTests.cs +++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/ServiceProviderTests.cs @@ -148,6 +148,47 @@ public void TryAddService_SameInstance_ShouldReturnFalse() Assert.IsFalse(_serviceProvider.TryAddService(instance)); } + [TestMethod] + public void ReplaceService_WhenMatchingServiceExists_ReplacesInPlace() + { + TestSessionLifetimeHandler first = new(); + TestSessionLifetimeHandler second = new(); + _serviceProvider.AddService(first); + _serviceProvider.ReplaceService(second); + + Assert.HasCount(1, _serviceProvider.Services); + Assert.AreSame(second, _serviceProvider.Services.First()); + } + + [TestMethod] + public void ReplaceService_WhenNoMatchingServiceExists_AppendsService() + { + TestSessionLifetimeHandler instance = new(); + _serviceProvider.ReplaceService(instance); + + Assert.HasCount(1, _serviceProvider.Services); + Assert.AreSame(instance, _serviceProvider.Services.First()); + } + + [TestMethod] + public void ReplaceService_ReplacesFirstMatch_PreservesOtherServices() + { + TestHostProcessLifetimeHandler other = new(); + TestSessionLifetimeHandler first = new(); + TestSessionLifetimeHandler second = new(); + _serviceProvider.AddService(other); + _serviceProvider.AddService(first); + _serviceProvider.ReplaceService(second); + + Assert.HasCount(2, _serviceProvider.Services); + Assert.AreSame(other, _serviceProvider.Services.First()); + Assert.AreSame(second, _serviceProvider.Services.Last()); + } + + [TestMethod] + public void ReplaceService_NullService_ThrowsArgumentNullException() + => Assert.ThrowsExactly(() => _serviceProvider.ReplaceService(null!)); + [TestMethod] public void GetServicesInternal_ExtensionMethod_InternalExtension_ShouldReturn() { From 4e97fb61d5cdd34007d92947b53e39abd6b0a962 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 14 May 2026 12:34:27 +0000 Subject: [PATCH 5/5] Fix server mode test failure: always register ServerTelemetry unconditionally The IsEnabled guard introduced in the previous commit was incorrect. In server mode, ServerTelemetry (JSON-RPC forwarding) must always be registered regardless of whether platform telemetry collection is enabled/disabled. Without it, NopTelemetryService(false) is left in place and throws InvalidOperationException when ExecuteRequestAsync calls LogEventAsync at the end of each test run/discovery request. Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com> --- .../Hosts/TestHostBuilder.Modes.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs index 71800ea5834..c77b9314927 100644 --- a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs +++ b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs @@ -219,11 +219,10 @@ private async Task BuildServerTestHostAsync(BuildContext context, NamedPi // Register ServerTelemetry in the service provider so that all telemetry events in server mode // are forwarded to the client via JSON-RPC. This replaces the NopTelemetryService registered - // earlier in SetupCommonServicesAsync. - if (context.ServiceProvider.GetTelemetryInformation().IsEnabled) - { - context.ServiceProvider.ReplaceService(new ServerTelemetry(serverTestHost)); - } + // earlier in SetupCommonServicesAsync. This is always registered regardless of whether platform + // telemetry is enabled or disabled, because server-mode telemetry is a protocol concern (JSON-RPC + // forwarding to the client), not a data-collection concern. + context.ServiceProvider.ReplaceService(new ServerTelemetry(serverTestHost)); #pragma warning disable CA1416 // Preserve existing browser behavior while splitting the method. IHost actualTestHost = testControllerConnection is not null