From b721baa7b8ad39e27313a9bbd61c8f4020a84eb5 Mon Sep 17 00:00:00 2001 From: Jakub Majocha <1760221+majocha@users.noreply.github.com> Date: Wed, 27 Nov 2024 16:00:54 +0100 Subject: [PATCH] unflake test --- .../Microsoft.FSharp.Control/AsyncModule.fs | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control/AsyncModule.fs b/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control/AsyncModule.fs index e3ead3bc768..d5bf29374d0 100644 --- a/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control/AsyncModule.fs +++ b/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control/AsyncModule.fs @@ -469,27 +469,29 @@ type AsyncModule() = member _.``error on one workflow should cancel all others``() = task { use failOnlyOne = new Semaphore(0, 1) - // Start from 1. - let mutable running = new CountdownEvent(1) + let mutable cancelled = 0 + let mutable started = 0 let job i = async { - use! holder = Async.OnCancel (running.Signal >> ignore) - running.AddCount 1 - do! failOnlyOne |> Async.AwaitWaitHandle |> Async.Ignore - running.Signal() |> ignore - failwith "boom" + let! ct = Async.CancellationToken + Interlocked.Increment &started |> ignore + try + do! failOnlyOne |> Async.AwaitWaitHandle |> Async.Ignore + failwith "boom" + finally + if ct.IsCancellationRequested then + Interlocked.Increment &cancelled |> ignore + } let test = Async.Parallel [ for i in 1 .. 100 -> job i ] |> Async.Catch |> Async.Ignore |> Async.StartAsTask // Wait for more than one job to start - while running.CurrentCount < 2 do + while started < 2 do do! Task.Yield() - printfn $"started jobs: {running.CurrentCount - 1}" + printfn $"started jobs: {started}" failOnlyOne.Release() |> ignore do! test - // running.CurrentCount should eventually settle back at 1. Signal it one more time and it should be 0. - running.Signal() |> ignore - return! Async.AwaitWaitHandle running.WaitHandle + Assert.Equal(cancelled, started - 1) } []