Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit 857ea9d

Browse files
Frasslenosami
authored andcommitted
Change cancellation test to not depend on how AwaitTask works (dotnet#7467)
* Change cancellation test to not depend on how AwaitTask works 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) * Add test that AwaitTask ignore async cancellation
1 parent fcfcd23 commit 857ea9d

File tree

1 file changed

+32
-5
lines changed
  • tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control

1 file changed

+32
-5
lines changed

tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control/AsyncType.fs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,24 +145,26 @@ type AsyncType() =
145145
[<Test>]
146146
member this.StartAsTaskCancellation () =
147147
let cts = new CancellationTokenSource()
148-
let tcs = TaskCompletionSource<unit>()
148+
let mutable spinloop = true
149+
let doSpinloop () = while spinloop do ()
149150
let a = async {
150151
cts.CancelAfter (100)
151-
do! tcs.Task |> Async.AwaitTask }
152+
doSpinloop()
153+
}
152154
#if !NET46
153155
let t : Task<unit> =
154156
#else
155157
use t : Task<unit> =
156158
#endif
157159
Async.StartAsTask(a, cancellationToken = cts.Token)
158160

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

165-
tcs.SetCanceled()
167+
spinloop <- false
166168

167169
try
168170
this.WaitASec t
@@ -172,6 +174,31 @@ type AsyncType() =
172174
| _ -> reraise()
173175
Assert.IsTrue (t.IsCompleted, "Task is not completed")
174176

177+
[<Test>]
178+
member this.``AwaitTask ignores Async cancellation`` () =
179+
let cts = new CancellationTokenSource()
180+
let tcs = new TaskCompletionSource<unit>()
181+
let innerTcs = new TaskCompletionSource<unit>()
182+
let a = innerTcs.Task |> Async.AwaitTask
183+
184+
Async.StartWithContinuations(a, tcs.SetResult, tcs.SetException, ignore >> tcs.SetCanceled, cts.Token)
185+
186+
cts.CancelAfter(100)
187+
try
188+
let result = tcs.Task.Wait(300)
189+
Assert.IsFalse (result)
190+
with :? AggregateException -> Assert.Fail "Should not finish, yet"
191+
192+
innerTcs.SetResult ()
193+
194+
try
195+
this.WaitASec tcs.Task
196+
with :? AggregateException as a ->
197+
match a.InnerException with
198+
| :? TaskCanceledException -> ()
199+
| _ -> reraise()
200+
Assert.IsTrue (tcs.Task.IsCompleted, "Task is not completed")
201+
175202
[<Test>]
176203
member this.StartTask () =
177204
let s = "Hello tasks!"

0 commit comments

Comments
 (0)