Skip to content

Fix flaky MetricsTests exemplars dialog assertion - #21596

Merged
Mitch Denny (mitchdenny) merged 2 commits into
microsoft:mainfrom
davidfowl:fix-21126-metrics-exemplars-click
Nov 3, 2026
Merged

Mitch Denny (mitchdenny) merged 2 commits into
microsoft:mainfrom
davidfowl:fix-21126-metrics-exemplars-click

Conversation

@davidfowl

Copy link
Copy Markdown
Collaborator

Description

Fixes a flaky test: MetricsTests.ChartContainer_UnavailableHistogramPercentiles_TablePreservesExemplars sometimes fails with Assert.Single() Failure: The collection was empty at the Assert.Single(dialogs) that follows the exemplars button click. Seen on Windows in runs 39212142055 and 39216025977.

Root cause

This is a race in the test, not a product bug.

  • bUnit 2.11.3's Click() is fire-and-forget. It calls TriggerEvent, which does _ = TriggerEventAsync(...) and dispatches through renderer.Dispatcher.InvokeAsync.
  • ChartContainer runs a background PeriodicTimer that ticks every 200 ms in real time, because the test's BrowserTimeProvider uses real timers. Each tick re-renders the chart and table through InvokeAsync(StateHasChanged) from a thread-pool thread.
  • If a tick is rendering when the test clicks, the click is queued behind it and Click() returns before MetricTable.OpenExemplarsDialogAsync runs. Assert.Single(dialogs) then runs against an empty queue.
  • Awaiting ClickAsync isn't an option here. The handler awaits TestDialogService.ShowDialogAsync, which awaits instance.Result, and that only completes when the dialog closes.

Fix

Test-only. The test's dialog callback now completes a TaskCompletionSource when the dialog is shown. The test awaits it (with DefaultTimeout) before asserting, so the assertion depends on the dialog actually being shown, not on when the click happens to run. There are no sleeps or retries, and the assertions are unchanged.

To make the regression deterministic, the test holds the renderer dispatcher from another thread while it clicks, then releases it. This forces the "click queued behind a render" ordering on every run instead of only under CI load.

Evidence

Before: the old click-then-assert code with the dispatcher held fails all four theory cases with the exact CI signature:

failed ...ChartContainer_UnavailableHistogramPercentiles_TablePreservesExemplars(includeAvailableIntervals: False, onlyShowValueChanges: False)
  Assert.Single() Failure: The collection was empty
total: 4  failed: 4

After, all run locally on Linux:

  • The same dispatcher-held ordering passes.
  • The targeted test passed 20 out of 20 runs in a bounded loop.
  • MetricsTests passed 5 out of 5 runs (42/42 each).
  • The full Aspire.Dashboard.Components.Tests project: 1161 passed, 0 failed.

Fixes #21126

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
    • No

🤖 Posted by AspireAgent (AI Agent)

bUnit's Click() dispatches through the renderer dispatcher fire-and-forget.
When ChartContainer's background update (a real 200ms PeriodicTimer) is
rendering, the click is queued and Click() returns before the handler
opens the dialog, so Assert.Single(dialogs) saw an empty collection.

Signal when the dialog is shown and await it, and hold the renderer during
the click so the test always covers the queued ordering.

Fixes #21126

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 21596

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 21596"

@aspire-repo-bot
aspire-repo-bot Bot requested a balanced review from Copilot October 31, 2026 14:48
@github-actions github-actions Bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Oct 31, 2026

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

The metrics test can leave the renderer blocked and hang teardown if button lookup or clicking throws, so renderer release and holding-task cleanup need a finally block.

1 open issue
What changed in this PR

This test-only change addresses the flaky exemplars dialog assertion in dashboard metrics tests reported in #21126. It synchronizes assertions with dialog creation rather than assuming the click handler runs immediately.

Changes:

  • Signals dialog creation through a TaskCompletionSource and awaits it with a timeout.
  • Holds the renderer dispatcher during the click to exercise the queued-event ordering deterministically.
File Description
tests/​Aspire.Dashboard.Components.Tests/​Pages/​MetricsTests.cs Adds dialog synchronization and deterministic dispatcher contention to the exemplars test.

Review effort: Balanced

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

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

No active review issues are reported for the test-only fix targeting the flaky exemplars-dialog assertion.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@mitchdenny Mitch Denny (mitchdenny) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed the test synchronization and renderer cleanup. No actionable findings.

@mitchdenny
Mitch Denny (mitchdenny) merged commit 5c8c924 into microsoft:main Nov 3, 2026
61 checks passed
@microsoft-github-policy-service microsoft-github-policy-service Bot added this to the 17.0 milestone Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-health Fixes CI flakiness or CI infrastructure (AspireAgent queue) flaky-test needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners

Projects

None yet

4 participants