From ad346193b39b8eae22e6cf855b1d4ab926d8d8e6 Mon Sep 17 00:00:00 2001 From: Zsolt Feher Date: Sun, 17 Aug 2025 12:47:05 +0200 Subject: [PATCH] Resolve concurrency with test stub to resolve #49745. --- .../test/HealthCheckPublisherHostedServiceTest.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/HealthChecks/HealthChecks/test/HealthCheckPublisherHostedServiceTest.cs b/src/HealthChecks/HealthChecks/test/HealthCheckPublisherHostedServiceTest.cs index bfdea1968900..8e8252317afc 100644 --- a/src/HealthChecks/HealthChecks/test/HealthCheckPublisherHostedServiceTest.cs +++ b/src/HealthChecks/HealthChecks/test/HealthCheckPublisherHostedServiceTest.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -799,9 +800,17 @@ private class TestPublisher : IHealthCheckPublisher public TestPublisher() { _started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + _entries = new ConcurrentQueue<(HealthReport report, CancellationToken cancellationToken)>(); } - public List<(HealthReport report, CancellationToken cancellationToken)> Entries { get; } = new List<(HealthReport report, CancellationToken cancellationToken)>(); + private readonly ConcurrentQueue<(HealthReport report, CancellationToken cancellationToken)> _entries; + public IReadOnlyList<(HealthReport report, CancellationToken cancellationToken)> Entries + { + get + { + return _entries.ToList(); + } + } public Exception? Exception { get; set; } @@ -811,7 +820,7 @@ public TestPublisher() public async Task PublishAsync(HealthReport report, CancellationToken cancellationToken) { - Entries.Add((report, cancellationToken)); + _entries.Enqueue((report, cancellationToken)); // Signal that we've started _started.SetResult(null);