Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Runtime.ExceptionServices;
using System.Runtime.CompilerServices;
using System.Threading.Tasks.Dataflow;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -1201,6 +1202,7 @@ private static RetirementAssertionContext CreateContractRetirementContext()
{
var application = provider.GetRequiredService<FluxFlowApplication>();
application.StartAsync().GetAwaiter().GetResult().IsApplied.ShouldBeTrue();
ForceFullCollection();
fixture.Closure.IsAlive.ShouldBeTrue();
ReplaceContractRevisionAsync(application, fixture.Source).GetAwaiter().GetResult();
return new RetirementAssertionContext(provider, application, fixture.Closure);
Expand All @@ -1225,6 +1227,7 @@ private static RetirementAssertionContext CreatePredicateRetirementContext()
{
var application = provider.GetRequiredService<FluxFlowApplication>();
application.StartAsync().GetAwaiter().GetResult().IsApplied.ShouldBeTrue();
ForceFullCollection();
fixture.Closure.IsAlive.ShouldBeTrue();
ReplacePredicateRevisionAsync(application, fixture.Source).GetAwaiter().GetResult();
return new RetirementAssertionContext(provider, application, fixture.Closure);
Expand Down Expand Up @@ -1263,6 +1266,21 @@ private static T RunOnTerminatedThread<T>(Func<T> 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()
{
Expand Down
Loading