From 4d11bd889679aa40970be18f8d4e0772732f2137 Mon Sep 17 00:00:00 2001 From: meisa Date: Sat, 5 Sep 2026 16:57:04 +0200 Subject: [PATCH] Stabilize retired closure collection assertions --- .../ApplicationRuntimeAssemblerTests.cs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Engine/Runtime/FluxFlow.Engine.Tests/ApplicationRuntimeAssemblerTests.cs b/tests/Unit/Engine/Runtime/FluxFlow.Engine.Tests/ApplicationRuntimeAssemblerTests.cs index df3971e5..330ba8c8 100644 --- a/tests/Unit/Engine/Runtime/FluxFlow.Engine.Tests/ApplicationRuntimeAssemblerTests.cs +++ b/tests/Unit/Engine/Runtime/FluxFlow.Engine.Tests/ApplicationRuntimeAssemblerTests.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using System.Runtime.ExceptionServices; using System.Runtime.CompilerServices; using System.Threading.Tasks.Dataflow; @@ -481,7 +482,7 @@ public async Task Successful_contract_replacement_retires_captured_factory_closu await using var context = RunOnTerminatedThread( static () => CreateContractRetirementContext()); - ForceFullCollection(); + await CollectRetiredClosureAsync(context.Closure); context.Closure.IsAlive.ShouldBeFalse(); await context.Application.StopAsync(); @@ -493,7 +494,7 @@ public async Task Successful_code_predicate_replacement_retires_captured_closure await using var context = RunOnTerminatedThread( static () => CreatePredicateRetirementContext()); - ForceFullCollection(); + await CollectRetiredClosureAsync(context.Closure); context.Closure.IsAlive.ShouldBeFalse(); await context.Application.StopAsync(); @@ -1201,6 +1202,7 @@ private static RetirementAssertionContext CreateContractRetirementContext() { var application = provider.GetRequiredService(); application.StartAsync().GetAwaiter().GetResult().IsApplied.ShouldBeTrue(); + ForceFullCollection(); fixture.Closure.IsAlive.ShouldBeTrue(); ReplaceContractRevisionAsync(application, fixture.Source).GetAwaiter().GetResult(); return new RetirementAssertionContext(provider, application, fixture.Closure); @@ -1225,6 +1227,7 @@ private static RetirementAssertionContext CreatePredicateRetirementContext() { var application = provider.GetRequiredService(); application.StartAsync().GetAwaiter().GetResult().IsApplied.ShouldBeTrue(); + ForceFullCollection(); fixture.Closure.IsAlive.ShouldBeTrue(); ReplacePredicateRevisionAsync(application, fixture.Source).GetAwaiter().GetResult(); return new RetirementAssertionContext(provider, application, fixture.Closure); @@ -1263,6 +1266,21 @@ private static T RunOnTerminatedThread(Func operation) "The retirement assertion thread completed without a result."); } + private static async Task CollectRetiredClosureAsync(WeakReference closure) + { + var elapsed = Stopwatch.StartNew(); + do + { + ForceFullCollection(); + if (!closure.IsAlive) + return; + + // Completed async operations can still have stack roots while unwinding. + await Task.Delay(10); + } + while (elapsed.Elapsed < TimeSpan.FromSeconds(5)); + } + [MethodImpl(MethodImplOptions.NoInlining)] private static void ForceFullCollection() {