Repository navigation
[Validation only] MTP: consume TestCoverageReportMessage in the test host controller (microsoft/testfx#8904) - #797
Draft
Evangelink wants to merge 2 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
IDataConsumer.ConsumeAsyncis never called.builder.TestHost.AddDataConsumer(...)registers the consumer in the test host (child) process. Since coverlet.MTP 10.1.0, Coverlet publishesTestCoverageReportMessagefrom its ownOnTestHostProcessExitedAsync. That runs in the test host controller process, after the child has exited, so the child-side consumer never receives the message.OnTestHostProcessExitedAsyncruns before Coverlet's output. MTP calls controllerITestHostProcessLifetimeHandlers one after another, in registration order. Registration order comes from theTestingPlatformBuilderHookorder in the generatedSelfRegisteredExtensions.cs. Coverlet printsOut of process file artifacts produced:itself, from inside its ownOnTestHostProcessExitedAsync, 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 listscoverage.cobertura*.xmlin the results directory.ConsumeAsynclogs everyTestCoverageReportMessageit receives.64c4aaa25bcf7d773a483dceca925b2a8878dfb4— validation fix.CompositeExtensionFactory<ReportGeneratorOutOfProcessHandler>, so lifetime and data callbacks share a single instance.TestHostControllers.AddProcessLifetimeHandler(handlerFactory).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
IDataConsumerin the test host controller process. In MTP 2.5.3, the publicITestHostControllersManagerexposes onlyAddEnvironmentVariableProvider,AddProcessLifetimeHandler, and the experimentalAddTestHostLauncher.AddDataConsumer<T>(CompositeExtensionFactory<T>)exists only on the internalTestHostControllersManagerimplementation, 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
ConsumeAsyncin the controller, usingTestCoverageReportMessagewithCoverageFormat.Cobertura. Don't glob theTestResultsdirectory fromOnTestHostProcessExitedAsync. The message carries the exact path, works whichever hook is registered first, and avoids picking up stale files from earlier runs.Validation
Environment
2.5.3+0b1d8b5f7b2186835e2cf26292f3478f7cca087e10.1.1.1+a84a5a9ab1767_mtp_debug@7b72e3705c9983214da1227e4cbaac65c079fe1a(ProjectReference tosrc/ReportGenerator.Mtp)Repro
The repro is a net10.0 MSTest runner project (
EnableMSTestRunner=true) that referencesMSTest4.5.3 andcoverlet.MTP10.1.1, plus a ProjectReference tosrc/ReportGenerator.Mtp. It registers the ReportGenerator hook with the sameTestingPlatformBuilderHookitem asbuildMultiTargeting/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 inDirectory.Build.props, so it comes before Coverlet's package hook.Generated
SelfRegisteredExtensions.csorder (only the relevant lines):Before (diagnostics only,
655acc49)ReportGenerator hook first. This matches the log in microsoft/testfx#8904: the exit callback runs before Coverlet's artifacts, and
ConsumeAsyncnever runs.ReportGenerator hook after Coverlet. The file exists by the time the exit callback runs, but
ConsumeAsyncstill never runs: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
ConsumeAsyncnow gets the exact Cobertura path:ReportGenerator hook after Coverlet:
What changed with the fix, in both orders:
TestCoverageReportMessages (Custom.jsonand Cobertura.xml), and both files exist.--reportgeneratorbut without--coverlet, the controller still runs and the exit callback finds no Cobertura file. That matches what you'd expect.Limitations
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.obj\<configuration>\<tfm>\SelfRegisteredExtensions.cs.