Skip to content

[Validation only] MTP: consume TestCoverageReportMessage in the test host controller (microsoft/testfx#8904) - #797

Draft
Evangelink wants to merge 2 commits into
danielpalme:767_mtp_debugfrom
Evangelink:mtp-8904-controller-consumer
Draft

Evangelink wants to merge 2 commits into
danielpalme:767_mtp_debugfrom
Evangelink:mtp-8904-controller-consumer

Conversation

@Evangelink

Copy link
Copy Markdown

Important

Validation only — please do not merge. This draft backs up the analysis in microsoft/testfx#8904. It uses reflection on an internal Microsoft.Testing.Platform (MTP) type and adds console tracing.

Summary

Two separate problems explain the behavior reported in microsoft/testfx#8904:

  1. IDataConsumer.ConsumeAsync is never called. builder.TestHost.AddDataConsumer(...) registers the consumer in the test host (child) process. Since coverlet.MTP 10.1.0, Coverlet publishes TestCoverageReportMessage from its own OnTestHostProcessExitedAsync. That runs in the test host controller process, after the child has exited, so the child-side consumer never receives the message.
  2. OnTestHostProcessExitedAsync runs before Coverlet's output. MTP calls controller ITestHostProcessLifetimeHandlers one after another, in registration order. Registration order comes from the TestingPlatformBuilderHook order in the generated SelfRegisteredExtensions.cs. Coverlet prints Out of process file artifacts produced: itself, from inside its own OnTestHostProcessExitedAsync, so the console order is the real callback order. When ReportGenerator's hook is registered before Coverlet's, the Cobertura file does not exist yet when ReportGenerator looks for it.

Also, the local-function factory creates one instance per registration, so the existing comment "create a single handler instance" doesn't hold. The baseline shows the constructor running twice, in two different processes.

Commits

  • 655acc49d6a82017c0a14f4bed99fa9b7e84772b — diagnostics only. Each callback logs a timestamp, PID, instance hash, and test host PID. The exit callback lists coverage.cobertura*.xml in the results directory. ConsumeAsync logs every TestCoverageReportMessage it receives.
  • 64c4aaa25bcf7d773a483dceca925b2a8878dfb4 — validation fix.
    • Uses one CompositeExtensionFactory<ReportGeneratorOutOfProcessHandler>, so lifetime and data callbacks share a single instance.
    • Keeps TestHostControllers.AddProcessLifetimeHandler(handlerFactory).
    • Replaces builder.TestHost.AddDataConsumer(handlerFactory) with a controller-side data consumer registration.

Why reflection (and why this must not ship)

MTP has no public API to register an IDataConsumer in the test host controller process. In MTP 2.5.3, the public ITestHostControllersManager exposes only AddEnvironmentVariableProvider, AddProcessLifetimeHandler, and the experimental AddTestHostLauncher. AddDataConsumer<T>(CompositeExtensionFactory<T>) exists only on the internal TestHostControllersManager implementation, and this PR calls it by reflection just to show the behavior. A supported API has to come from MTP; that part is tracked in the microsoft/testfx#8904 discussion.

Recommended product direction (once a supported API exists)

Generate the report from ConsumeAsync in the controller, using TestCoverageReportMessage with CoverageFormat.Cobertura. Don't glob the TestResults directory from OnTestHostProcessExitedAsync. The message carries the exact path, works whichever hook is registered first, and avoids picking up stale files from earlier runs.

Validation

Environment

Component Version
Microsoft.Testing.Platform 2.5.3+0b1d8b5f7b2186835e2cf26292f3478f7cca087e
MSTest 4.5.3
coverlet.MTP 10.1.1.1+a84a5a9ab1
ReportGenerator base 767_mtp_debug @ 7b72e3705c9983214da1227e4cbaac65c079fe1a (ProjectReference to src/ReportGenerator.Mtp)
Runtime / SDK .NET 10.0.15, win-x64 / .NET SDK 11.0.100

Repro

The repro is a net10.0 MSTest runner project (EnableMSTestRunner=true) that references MSTest 4.5.3 and coverlet.MTP 10.1.1, plus a ProjectReference to src/ReportGenerator.Mtp. It registers the ReportGenerator hook with the same TestingPlatformBuilderHook item as buildMultiTargeting/dotnet-reportgenerator-mtp.props:

  • RgFirst=false: the item is in the .csproj, so it comes after Coverlet's package hook.
  • RgFirst=true: the item is in Directory.Build.props, so it comes before Coverlet's package hook.
dotnet build -p:RgFirst=<true|false>
bin\Debug\net10.0\Repro.Tests.exe --coverlet --coverlet-include-test-assembly --reportgenerator --diagnostic --diagnostic-output-directory <out>\diag --diagnostic-verbosity trace

Generated SelfRegisteredExtensions.cs order (only the relevant lines):

RgFirst=false: ... Coverlet.MTP.TestingPlatformBuilderHook  ->  Palmmedia.ReportGenerator.Mtp.TestingPlatformBuilderHook
RgFirst=true:  Palmmedia.ReportGenerator.Mtp.TestingPlatformBuilderHook  ->  ...  ->  Coverlet.MTP.TestingPlatformBuilderHook

Before (diagnostics only, 655acc49)

ReportGenerator hook first. This matches the log in microsoft/testfx#8904: the exit callback runs before Coverlet's artifacts, and ConsumeAsync never runs.

ReportGenerator.MTP [12:34:11.949 pid 262188] - ReportGeneratorOutOfProcessHandler constructor (instance 33132169)
ReportGenerator.MTP [12:34:12.756 pid 262188] - OnTestHostProcessStartedAsync (instance 33132169, test host pid 322228)
ReportGenerator.MTP [12:34:12.801 pid 322228] - ReportGeneratorOutOfProcessHandler constructor (instance 25474675)
ReportGenerator.MTP [12:34:13.530 pid 262188] - OnTestHostProcessExitedAsync (instance 33132169, test host pid 322228)
ReportGenerator.MTP [12:34:13.531 pid 262188] -   Cobertura files in '...\TestResults': <none>
  Out of process file artifacts produced:
    - ...\TestResults\coverage.201126113413552.json
    - ...\TestResults\coverage.cobertura.201126113413660.xml

ReportGenerator hook after Coverlet. The file exists by the time the exit callback runs, but ConsumeAsync still never runs:

ReportGenerator.MTP [12:34:05.011 pid 185056] - ReportGeneratorOutOfProcessHandler constructor (instance 33132169)
ReportGenerator.MTP [12:34:05.937 pid 56856] - ReportGeneratorOutOfProcessHandler constructor (instance 42194754)
  Out of process file artifacts produced:
    - ...\TestResults\coverage.201126113406698.json
    - ...\TestResults\coverage.cobertura.201126113406808.xml
ReportGenerator.MTP [12:34:06.853 pid 185056] - OnTestHostProcessExitedAsync (instance 33132169, test host pid 56856)
ReportGenerator.MTP [12:34:06.853 pid 185056] -   Cobertura files in '...\TestResults': coverage.cobertura.201126113406808.xml

In both orders, the constructor runs in the controller and again in the test host (second PID).

After (validation fix, 64c4aaa2)

ReportGenerator hook first. The exit callback still runs too early, but ConsumeAsync now gets the exact Cobertura path:

ReportGenerator.MTP [12:37:01.458 pid 199516] - ReportGeneratorOutOfProcessHandler constructor (instance 33132169)
ReportGenerator.MTP [12:37:03.113 pid 199516] - OnTestHostProcessExitedAsync (instance 33132169, test host pid 335432)
ReportGenerator.MTP [12:37:03.114 pid 199516] -   Cobertura files in '...\TestResults': <none>
  Out of process file artifacts produced:
    - ...\TestResults\coverage.201126113703138.json
    - ...\TestResults\coverage.cobertura.201126113703246.xml
ReportGenerator.MTP [12:37:03.280 pid 199516] - ConsumeAsync (instance 33132169): TestCoverageReportMessage from 'CoverletExtension', format Custom, path '...\TestResults\coverage.201126113703138.json', exists True
ReportGenerator.MTP [12:37:03.281 pid 199516] - ConsumeAsync (instance 33132169): TestCoverageReportMessage from 'CoverletExtension', format Cobertura, path '...\TestResults\coverage.cobertura.201126113703246.xml', exists True

ReportGenerator hook after Coverlet:

ReportGenerator.MTP [12:36:55.010 pid 287860] - ReportGeneratorOutOfProcessHandler constructor (instance 28065025)
  Out of process file artifacts produced:
    - ...\TestResults\coverage.201126113656911.json
    - ...\TestResults\coverage.cobertura.201126113657019.xml
ReportGenerator.MTP [12:36:57.053 pid 287860] - ConsumeAsync (instance 28065025): TestCoverageReportMessage from 'CoverletExtension', format Custom, path '...\TestResults\coverage.201126113656911.json', exists True
ReportGenerator.MTP [12:36:57.053 pid 287860] - ConsumeAsync (instance 28065025): TestCoverageReportMessage from 'CoverletExtension', format Cobertura, path '...\TestResults\coverage.cobertura.201126113657019.xml', exists True
ReportGenerator.MTP [12:36:57.063 pid 287860] - OnTestHostProcessExitedAsync (instance 28065025, test host pid 330720)

What changed with the fix, in both orders:

  • The constructor runs once, in the controller only.
  • The same instance receives the lifetime callbacks and both TestCoverageReportMessages (Custom .json and Cobertura .xml), and both files exist.
  • Run with no extension options, the app does not restart into a test host controller (only one diagnostic log), so a disabled consumer adds no overhead.
  • Run with --reportgenerator but without --coverlet, the controller still runs and the exit callback finds no Cobertura file. That matches what you'd expect.

Limitations

  • The reflection call targets an internal MTP type and can break on any MTP update. It only demonstrates the fix.
  • In this tiny repro, Coverlet instrumented no modules (Selected: 8, Instrumented: 0; the test assembly was skipped by preflight), so the Cobertura content is empty. The lifecycle and message evidence above doesn't depend on that content.
  • Hook order in your environment isn't verified here. Please check the order in obj\<configuration>\<tfm>\SelfRegisteredExtensions.cs.

Evangelink and others added 2 commits November 20, 2026 12:35
Log the process ID, handler instance, test host PID and the Cobertura
files present in the test results directory, so the callback order and
the process each callback runs in can be observed.

Co-authored-by: Copilot App <[email protected]>
Validation only for microsoft/testfx#8904, not meant to be merged as is.

coverlet.MTP (>= 10.1.0) publishes TestCoverageReportMessage from its
OnTestHostProcessExitedAsync, on the message bus of the test host
controller process. The handler was registered as a data consumer with
builder.TestHost.AddDataConsumer, which creates a separate instance in the
test host process, so ConsumeAsync never received the report.

Register the handler as a data consumer in the test host controller
process instead, sharing one CompositeExtensionFactory with the process
lifetime handler registration so a single instance handles both.
ITestHostControllersManager has no public AddDataConsumer, so reflection
on the internal implementation is used to validate the behavior.

Co-authored-by: Copilot App <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant