Skip to content

[ResourceLock] Remove unneeded [DoNotParallelize] from culture-mutation tests in TerminalTestReporterTests - #11229

Merged
Amaury Levé (Evangelink) merged 3 commits into
microsoft:mainfrom
nohwnd-bot:resource-lock/terminal-test-reporter-culture-tests-98a57ab2f7af04a4
Sep 13, 2026
Merged

Amaury Levé (Evangelink) merged 3 commits into
microsoft:mainfrom
nohwnd-bot:resource-lock/terminal-test-reporter-culture-tests-98a57ab2f7af04a4

Conversation

@nohwnd-bot

Copy link
Copy Markdown
Contributor

gh-aw-workflow-id: resource-lock-refactoring

Project and scope

test/UnitTests/Microsoft.Testing.Platform.UnitTests (Microsoft.Testing.Platform.UnitTests.csproj). The assembly is already parallelized: [assembly: Parallelize(Scope = ExecutionScope.MethodLevel, Workers = 0)] in Program.cs. This PR does not change that attribute or any other assembly-level parallelization/.runsettings/testconfig.json setting — it only removes two unnecessary method-level [DoNotParallelize] attributes that were narrower than the truth: the resource they guarded against was never actually process-global.

The change

In OutputDevice/Terminal/TerminalTestReporterTests.cs, removed [DoNotParallelize] from:

  • TestNodeResultsState_GetSingleActiveOrSummaryTask_WhenCultureChanges_ReformatsSummary
  • TestNodeResultsState_GetRunningTasks_WhenCultureChanges_ReformatsSummary

Both tests mutate CultureInfo.CurrentCulture / CultureInfo.CurrentUICulture inside a try/finally that restores the original values, against a TestNodeResultsState instance that is a private local variable created inside the test method — never shared across tests.

Resource coverage matrix

Resource Direct test writers Production readers/writers Tests invoking those production paths Protecting declaration
CultureInfo.CurrentCulture / CurrentUICulture (thread-local, ExecutionContext-scoped) The two edited tests (write, restore in finally) TestNodeResultsState.GetFullTestsCountText / GetMoreTestsCountText (src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/TestNodeResultsState.cs) read CultureInfo.CurrentCulture/CurrentUICulture to format and cache summary text Every other TestNodeResultsState_* test in the same file reads/formats through the same production methods, but each constructs its own private TestNodeResultsState instance and runs with whatever ambient culture its own thread has — it never observes a culture mutated by a sibling test None needed: .NET's CultureInfo.CurrentCulture/CurrentUICulture setters write to the current thread's ExecutionContext, and the .NET ThreadPool captures/restores ExecutionContext per queued work item. MSTest schedules every test method through Task.Run (see TestExecutionManager, TestMethodInfo.Lifecycle, confirmed via grep -rl "Task.Run" src/Adapter), so a culture mutation performed inside one test's Task.Run work item is invisible to every concurrently scheduled sibling test's own work item. There is no other shared resource: state is a new TestNodeResultsState(1) local, not a static/shared field.

I also verified empirically (outside the repo, as a throwaway console app) that mutating CultureInfo.CurrentCulture inside one of 20 concurrent Task.Run work items never becomes visible to any other concurrently running Task.Run work item, confirming the ExecutionContext isolation claim above.

This exact rationale (culture flows via ExecutionContext and cannot leak across Task.Run-scheduled MSTest tests) is already established and reviewed in this repository at test/UnitTests/Microsoft.Testing.Extensions.UnitTests/FfmpegVideoRecorderTests.cs (see the comment above ReadSegments_RelativeEntry_CombinesDirectoryAndParsesInvariantNumbers), which mutates CultureInfo.CurrentCulture with no [ResourceLock]/[DoNotParallelize] at all under the same kind of method-level-parallel assembly. This PR applies the identical, already-accepted reasoning to the two remaining un-annotated occurrences of the pattern in Microsoft.Testing.Platform.UnitTests.

Declaration-reconciliation self-review (category C)

  • The production read/write set for this resource is fully covered: TestNodeResultsState's two culture-aware formatting methods are the only production code that reads CultureInfo.CurrentCulture/CurrentUICulture in a way reachable from these tests, and both are exercised only through each test's own private TestNodeResultsState instance.
  • No other test in the assembly needs a matching declaration: correctness here does not depend on any [ResourceLock] key at all, because the underlying mechanism (ExecutionContext capture/restore per Task.Run work item) already isolates the mutation, independent of scheduling order or interleaving.
  • No correctness claim relies on a literal-key grep — the argument is the ExecutionContext/ThreadPool isolation semantics plus the confirmed fact that MSTest schedules test methods via Task.Run.
  • Concrete "why the previously excluded race can't happen": for any interleaving of tests T1 (one of the two edited tests) and T2 (any other test in the assembly, running concurrently on a different Task.Run work item), T1's CultureInfo.CurrentCulture = X assignment updates only the ExecutionContext flowing through T1's own work item/continuations. T2's work item captured a separate ExecutionContext when it was queued, so CultureInfo.CurrentCulture observed inside T2 is unaffected by T1's assignment regardless of timing. This holds whether T2 starts before, during, or after T1's mutation.

Validation

  • bash ./build.sh — full repo build succeeded (0 errors).
  • PATH="$PWD/.dotnet:$PATH" dotnet build test/UnitTests/Microsoft.Testing.Platform.UnitTests -f net8.0 -c Debug — succeeded, 0 errors.
  • PATH="$PWD/.dotnet:$PATH" dotnet run --project test/UnitTests/Microsoft.Testing.Platform.UnitTests -f net8.0 --no-build -c Debug -- --filter "FullyQualifiedName~TerminalTestReporterTests" — run 4 times in a row to probe for scheduling-order flakiness: 184/184 passed every time, 0 failed.
  • PATH="$PWD/.dotnet:$PATH" dotnet run --project test/UnitTests/Microsoft.Testing.Platform.UnitTests -f net8.0 --no-build -c Debug (full project suite, method-level parallel) — 2541 succeeded, 21 skipped (pre-existing, unrelated), 0 failed.
  • DOTNET_CLI_TELEMETRY_SESSIONID was left untouched/inherited by these commands (not explicitly overridden).
  • Workflow run: https://github.com/microsoft/testfx/actions/runs/34740007978

Acceptance/integration tests were not run for this change: it touches only a unit-test file with no product-code or packaging impact, so -pack was not required or attempted.

Notes

  • This project is already parallelized (MethodLevel, Workers = 0); this PR is not an opt-in to parallelization, it narrows an already-parallel project's over-broad [DoNotParallelize] usage per option 1 of the refactoring priorities.
  • No assembly-level Parallelize/DoNotParallelize attribute, .runsettings, or testconfig.json parallelization setting was added or changed.

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • southcentralus0.in.applicationinsights.azure.com

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "southcentralus0.in.applicationinsights.azure.com"

See Network Configuration for more information.

Automated by the ResourceLock refactoring workflow. · auto · 623.8 AIC · ⌖ 12.6 AIC · ⊞ 11.9K | [History]( · ◷)

…minalTestReporterTests

CultureInfo.CurrentCulture/CurrentUICulture mutations flow via ExecutionContext,
which the thread pool captures/restores per Task.Run work item (MSTest's
scheduling unit), so they cannot leak across concurrently scheduled sibling
tests under this assembly's method-level parallelization. The TestNodeResultsState
instances mutated in these two tests are private locals, not shared state, so no
[ResourceLock] key is needed either -- matching the existing, reviewed rationale
already applied to the same pattern in FfmpegVideoRecorderTests.cs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings September 13, 2026 05:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Correct the rationale comments to avoid the inaccurate scheduling guarantee.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Removes unnecessary [DoNotParallelize] attributes from two culture-mutation tests.

Changes:

  • Enables method-level parallel execution for both tests.
  • Retains local test state and culture restoration.
  • Adds isolation rationale comments.
File summaries
File Summary
test/UnitTests/Microsoft.Testing.Platform.UnitTests/OutputDevice/Terminal/TerminalTestReporterTests.cs Removes two parallelization exclusions; rationale comments should avoid claiming each test has a separate Task.Run work item.
Review details

Suppressed comments (1)

test/UnitTests/Microsoft.Testing.Platform.UnitTests/OutputDevice/Terminal/TerminalTestReporterTests.cs:2170

  • This duplicate rationale repeats the inaccurate claim that sibling tests are Task.Run-scheduled work items. The runner schedules worker tasks, then runs multiple test methods sequentially inside each worker, so this comment should describe the finally restoration and worker-level isolation instead of attributing safety to a per-test Task.Run.
    // Same rationale as TestNodeResultsState_GetSingleActiveOrSummaryTask_WhenCultureChanges_ReformatsSummary
    // above: CultureInfo.CurrentCulture/CurrentUICulture flow via ExecutionContext and cannot leak across
    // the Task.Run-scheduled sibling tests that make up MSTest's method-level parallelization, and `state`
    // is a private local instance, so no [ResourceLock]/[DoNotParallelize] is required here either.
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite (auto)

Note

Copilot is running an experiment and ran this review at Lite.


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings September 13, 2026 07:09
@Evangelink Amaury Levé (Evangelink) added the state/needs-review Awaiting review from the team. label Sep 13, 2026
@Evangelink
Amaury Levé (Evangelink) marked this pull request as ready for review September 13, 2026 07:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Review comments request corrections to the execution-model rationale before approval.

Get a fresh assessment by requesting another Copilot review.

Review details

Suppressed comments (1)

test/UnitTests/Microsoft.Testing.Platform.UnitTests/OutputDevice/Terminal/TerminalTestReporterTests.cs:2165

  • The scheduler creates one task per worker and each worker drains multiple test chunks; sibling test methods are not individually Task.Run-scheduled. Please describe the worker-level ExecutionContext isolation instead, otherwise this rationale misstates the execution model it relies on.
    // above: CultureInfo.CurrentCulture/CurrentUICulture flow via ExecutionContext and cannot leak across
    // the Task.Run-scheduled sibling tests that make up MSTest's method-level parallelization, and `state`
    // is a private local instance, so no [ResourceLock]/[DoNotParallelize] is required here either.
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite (auto)

Note

Copilot is running an experiment and ran this review at Lite.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings September 13, 2026 08:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The tests use local state, restore ambient culture in finally, and MSTest worker execution provides the claimed isolation.

Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@Evangelink
Amaury Levé (Evangelink) merged commit 4c18886 into microsoft:main Sep 13, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

state/needs-review Awaiting review from the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants