Register ServerTelemetry in the correct location of TestHostBuilder#8211
Conversation
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
|
/review |
|
❌ Expert Code Review (command) failed. Please review the logs for details. |
There was a problem hiding this comment.
Pull request overview
Fixes server-mode telemetry forwarding in Microsoft.Testing.Platform by ensuring ServerTelemetry is registered in the ServiceProvider (so GetTelemetryCollector() resolves to the forwarding implementation instead of NopTelemetryService), and by removing the direct ServerTelemetry coupling from ServerTestHost.
Changes:
- Added
ServiceProvider.ReplaceService<T>(T)to replace the first matching service instance while preserving lookup order. - Updated
TestHostBuilderserver-mode build path to registerServerTelemetryas theITelemetryCollector. - Updated
ServerTestHostto obtain the telemetry collector from theServiceProviderrather than maintaining its own private instance.
Show a summary per file
| File | Description |
|---|---|
| src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs | Adds ReplaceService<T> to support in-place replacement of an existing registration. |
| src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs | Registers ServerTelemetry in server mode by replacing the previously-registered telemetry collector. |
| src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.cs | Removes the private ServerTelemetry field and uses ServiceProvider.GetTelemetryCollector() for event logging. |
Copilot's findings
- Files reviewed: 3/3 changed files
- Comments generated: 3
|
@copilot address review comments |
…ice unit tests Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Addressed all three review comments in commit
|
|
@copilot address review comments |
All three review comments were already addressed in commit
|
|
@copilot fix the broken test test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/ServerMode/v1.0.0/TestingPlatformClient.cs#L130 test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/ServerMode/v1.0.0/TestingPlatformClient.cs(130,1): error : [DiscoverAndRun ("net10.0")] [net11.0] Test method MSTest.Acceptance.IntegrationTests.ServerModeTests.DiscoverAndRun threw exception: |
…tionally 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>
Fixed in the latest commit. The |
Bug Fix
What was the bug?
ServerTelemetry(which forwards telemetry events to the JSON-RPC client in server mode) was instantiated inside theServerTestHostconstructor as a private field but never registered in the service provider. Any code callingserviceProvider.GetTelemetryCollector()— includingLogTestHostCreatedAsync— received theNopTelemetryServiceinstead, silently dropping telemetry rather than forwarding it to the client.How did you fix it?
ServiceProvider: AddedReplaceService<T>(T newService)— replaces the first registered instance of typeTin-place, preserving lookup order. Falls back to append if no existing instance is found.TestHostBuilder.Modes.cs(BuildServerTestHostAsync): AfterServerTestHostis constructed, createsServerTelemetryand registers it viaReplaceService<ITelemetryCollector>, displacing theNopTelemetryServicethatSetupCommonServicesAsyncadded earlier. This is the correct registration site — withinTestHostBuilder, not inside the host itself.ServerTestHost: Removed the private_telemetryServicefield and its constructor assignment. The single call site now usesServiceProvider.GetTelemetryCollector(), keeping the host decoupled from the concrete telemetry type.