Skip to content

Commit

Permalink
Change cancellation test to not depend on how AwaitTask works
Browse files Browse the repository at this point in the history
This test is to check that when an Async is started as a Task (via
StartAsTask) that when cancelled the Task isn't immediately marked as
cancelled but instead waits for the underlying Async to finish (normally
via cancellation)
  • Loading branch information
Frassle committed Aug 29, 2019
1 parent 8710f8c commit 5b96759
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,26 @@ type AsyncType() =
[<Test>]
member this.StartAsTaskCancellation () =
let cts = new CancellationTokenSource()
let tcs = TaskCompletionSource<unit>()
let mutable spinloop = true
let doSpinloop () = while spinloop do ()
let a = async {
cts.CancelAfter (100)
do! tcs.Task |> Async.AwaitTask }
doSpinloop()
}
#if !NET46
let t : Task<unit> =
#else
use t : Task<unit> =
#endif
Async.StartAsTask(a, cancellationToken = cts.Token)

// Should not finish
// Should not finish, we don't eagerly mark the task done just because it's been signaled to cancel.
try
let result = t.Wait(300)
Assert.IsFalse (result)
with :? AggregateException -> Assert.Fail "Task should not finish, jet"
with :? AggregateException -> Assert.Fail "Task should not finish, yet"

tcs.SetCanceled()
spinloop <- false

try
this.WaitASec t
Expand Down

0 comments on commit 5b96759

Please sign in to comment.