From 997412c4cf021575299ea5079c93b1aa319d6bdc Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Mon, 21 Oct 2024 08:54:16 +0800 Subject: [PATCH 1/3] Fix EventsCanBePublishedNonBlockingConcurrent race condition --- .../Eventing/DistributedApplicationBuilderEventingTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Aspire.Hosting.Tests/Eventing/DistributedApplicationBuilderEventingTests.cs b/tests/Aspire.Hosting.Tests/Eventing/DistributedApplicationBuilderEventingTests.cs index 9e0fc14fa3e..acef7551c37 100644 --- a/tests/Aspire.Hosting.Tests/Eventing/DistributedApplicationBuilderEventingTests.cs +++ b/tests/Aspire.Hosting.Tests/Eventing/DistributedApplicationBuilderEventingTests.cs @@ -87,14 +87,14 @@ public async Task EventsCanBePublishedNonBlockingConcurrent() builder.Eventing.Subscribe(async (@event, ct) => { await blockSubscriptionExecution.Task; - hitCount++; + Interlocked.Increment(ref hitCount); blockAssertionSub1.SetResult(); }); builder.Eventing.Subscribe(async (@event, ct) => { await blockSubscriptionExecution.Task; - hitCount++; + Interlocked.Increment(ref hitCount); blockAssertionSub2.SetResult(); }); From 45eeca2a1b4fe9b2807e019c94d7fc39f6cad2a7 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Mon, 21 Oct 2024 08:59:04 +0800 Subject: [PATCH 2/3] More --- .../DistributedApplicationBuilderEventingTests.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Aspire.Hosting.Tests/Eventing/DistributedApplicationBuilderEventingTests.cs b/tests/Aspire.Hosting.Tests/Eventing/DistributedApplicationBuilderEventingTests.cs index acef7551c37..11b9c6fe975 100644 --- a/tests/Aspire.Hosting.Tests/Eventing/DistributedApplicationBuilderEventingTests.cs +++ b/tests/Aspire.Hosting.Tests/Eventing/DistributedApplicationBuilderEventingTests.cs @@ -23,13 +23,13 @@ public async Task EventsCanBePublishedBlockSequential() builder.Eventing.Subscribe(async (@event, ct) => { blockAssertionTcs.SetResult(); - hitCount++; + Interlocked.Increment(ref hitCount); await blockFirstSubscriptionTcs.Task; }); builder.Eventing.Subscribe((@event, ct) => { - hitCount++; + Interlocked.Increment(ref hitCount); return Task.CompletedTask; }); @@ -54,14 +54,14 @@ public async Task EventsCanBePublishedBlockConcurrent() builder.Eventing.Subscribe(async (@event, ct) => { - hitCount++; + Interlocked.Increment(ref hitCount); blockAssertionSub1.SetResult(); await blockSubscriptionCompletion.Task; }); builder.Eventing.Subscribe(async (@event, ct) => { - hitCount++; + Interlocked.Increment(ref hitCount); blockAssertionSub2.SetResult(); await blockSubscriptionCompletion.Task; }); @@ -122,14 +122,14 @@ public async Task EventsCanBePublishedNonBlockingSequential() { blockAssert1.SetResult(); await blockEventSub1.Task; - hitCount++; + Interlocked.Increment(ref hitCount); blockAssert2.SetResult(); await blockEventSub2.Task; }); builder.Eventing.Subscribe((@event, ct) => { - hitCount++; + Interlocked.Increment(ref hitCount); blockAssert3.SetResult(); return Task.CompletedTask; }); From f629e5a5c3cc2c02d142c92db5c35bd5f17661e1 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Mon, 21 Oct 2024 09:00:46 +0800 Subject: [PATCH 3/3] More --- .../Health/ResourceHealthCheckServiceTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Aspire.Hosting.Tests/Health/ResourceHealthCheckServiceTests.cs b/tests/Aspire.Hosting.Tests/Health/ResourceHealthCheckServiceTests.cs index 334a2134dd0..7bc37e09e4a 100644 --- a/tests/Aspire.Hosting.Tests/Health/ResourceHealthCheckServiceTests.cs +++ b/tests/Aspire.Hosting.Tests/Health/ResourceHealthCheckServiceTests.cs @@ -209,7 +209,7 @@ public async Task PoorlyImplementedHealthChecksDontCauseMonitoringLoopToCrashout var hitCount = 0; builder.Services.AddHealthChecks().AddCheck("resource_check", (check) => { - hitCount++; + Interlocked.Increment(ref hitCount); throw new InvalidOperationException("Random failure instead of result!"); }); @@ -250,7 +250,7 @@ public async Task ResourceHealthCheckServiceDoesNotRunHealthChecksUnlessResource var checkStatus = HealthCheckResult.Unhealthy(); builder.Services.AddHealthChecks().AddCheck("parent_test", () => { - hitCount++; + Interlocked.Increment(ref hitCount); return checkStatus; }); @@ -314,7 +314,7 @@ public async Task ResourceHealthCheckServiceOnlyRaisesResourceReadyOnce() var healthCheckHits = 0; builder.Services.AddHealthChecks().AddCheck("parent_test", () => { - healthCheckHits++; + Interlocked.Increment(ref healthCheckHits); return HealthCheckResult.Healthy(); }); @@ -327,7 +327,7 @@ public async Task ResourceHealthCheckServiceOnlyRaisesResourceReadyOnce() var resourceReadyEventFired = new TaskCompletionSource(); builder.Eventing.Subscribe(parent.Resource, (@event, ct) => { - eventHits++; + Interlocked.Increment(ref eventHits); return Task.CompletedTask; });