[automated] Fix flaky test: thread-unsafe collections in FakeContainerRuntime and MockImageBuilder#14708
Merged
[automated] Fix flaky test: thread-unsafe collections in FakeContainerRuntime and MockImageBuilder#14708
Conversation
…nd MockImageBuilder Convert List<T> to ConcurrentBag<T> in FakeContainerRuntime and MockImageBuilder test fakes. The deployment pipeline runs steps concurrently via Task.WhenAll, causing data loss in non-thread-safe List<T>.Add() calls when multiple compute environments are used. Fixes #13287 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 14708Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 14708" |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a quarantined/flaky Azure deployment test by making test fakes safe under concurrent pipeline execution (e.g., steps running via Task.WhenAll).
Changes:
- Replaced
List<T>call-tracking collections withConcurrentBag<T>inFakeContainerRuntimeandMockImageBuilder. - Updated tests to avoid indexed access on call-tracking collections by using
Assert.Single()to retrieve the sole entry.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tests/Aspire.Hosting.Tests/Publishing/FakeContainerRuntime.cs | Makes runtime call-tracking thread-safe with ConcurrentBag<T>. |
| tests/Aspire.Hosting.Tests/MockImageBuilder.cs | Makes image manager call-tracking thread-safe with ConcurrentBag<T>; replaces AddRange with per-item adds. |
| tests/Aspire.Hosting.Tests/Publishing/ResourceContainerImageManagerTests.cs | Switches from [0] indexing to Assert.Single() for BuildImageCalls. |
| tests/Aspire.Hosting.Docker.Tests/DockerComposeTests.cs | Switches from [0] indexing to Assert.Single() for TagImageCalls. |
JamesNK
approved these changes
Feb 26, 2026
Contributor
🎬 CLI E2E Test RecordingsThe following terminal recordings are available for commit
📹 Recordings uploaded automatically from CI run #22423191351 |
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.
Flaky Test Fix Summary
Test
Aspire.Hosting.Azure.Tests.AzureDeployerTests.DeployAsync_WithMultipleComputeEnvironments_Workstests/Aspire.Hosting.Azure.Tests/Root Cause
FakeContainerRuntimeandMockImageBuildertest fakes used plainList<T>collections to track method calls. The deployment pipeline executes steps concurrently viaTask.WhenAll(each compute environment's ACR login and image push run as independent pipeline steps). ConcurrentList<T>.Add()calls caused data loss — some entries were silently dropped, leading toAssert.Contains()failures where the collection was missing expected entries.This matches the Thread-unsafe collections pattern from the test review guidelines.
Fix
Converted all
List<T>tracking collections toConcurrentBag<T>in both test fakes. Also updated two call sites that used indexed access ([0]) to useAssert.Single()return value instead, sinceConcurrentBag<T>doesn't support indexing.Verification
CI verification run: https://github.com/dotnet/aspire/actions/runs/22422968260
Files Changed
tests/Aspire.Hosting.Tests/Publishing/FakeContainerRuntime.cs—List<T>→ConcurrentBag<T>for all call-tracking collectionstests/Aspire.Hosting.Tests/MockImageBuilder.cs—List<T>→ConcurrentBag<T>for all call-tracking collectionstests/Aspire.Hosting.Tests/Publishing/ResourceContainerImageManagerTests.cs— UseAssert.Single()return value instead of indexingtests/Aspire.Hosting.Docker.Tests/DockerComposeTests.cs— UseAssert.Single()return value instead of indexingNext Steps