-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTaskSeq.WithCancellation.Tests.fs
More file actions
172 lines (130 loc) · 5.09 KB
/
TaskSeq.WithCancellation.Tests.fs
File metadata and controls
172 lines (130 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
module TaskSeq.Tests.``WithCancellation``
open System
open System.Collections.Generic
open System.Threading
open System.Threading.Tasks
open Xunit
open FsUnit.Xunit
open FSharp.Control
/// A simple IAsyncEnumerable whose GetAsyncEnumerator records the token it was called with.
type TokenCapturingSeq<'T>(items: 'T list) =
let mutable capturedToken = CancellationToken.None
member _.CapturedToken = capturedToken
interface IAsyncEnumerable<'T> with
member _.GetAsyncEnumerator(ct) =
capturedToken <- ct
let source = taskSeq {
for x in items do
yield x
}
source.GetAsyncEnumerator(ct)
module ``Null check`` =
[<Fact>]
let ``TaskSeq-withCancellation: null source throws ArgumentNullException`` () =
assertNullArg
<| fun () -> TaskSeq.withCancellation CancellationToken.None null
module ``Token threading`` =
[<Fact>]
let ``TaskSeq-withCancellation: passes supplied token to GetAsyncEnumerator`` () = task {
let source = TokenCapturingSeq([ 1; 2; 3 ])
use cts = new CancellationTokenSource()
let wrapped = TaskSeq.withCancellation cts.Token (source :> IAsyncEnumerable<_>)
let! _ = TaskSeq.toArrayAsync wrapped
source.CapturedToken |> should equal cts.Token
}
[<Fact>]
let ``TaskSeq-withCancellation: overrides any token passed to GetAsyncEnumerator`` () = task {
let source = TokenCapturingSeq([ 1; 2; 3 ])
use cts = new CancellationTokenSource()
let wrapped = TaskSeq.withCancellation cts.Token (source :> IAsyncEnumerable<_>)
// Consume with a different token; withCancellation should win
use outerCts = new CancellationTokenSource()
let enum = wrapped.GetAsyncEnumerator(outerCts.Token)
while! enum.MoveNextAsync() do
()
source.CapturedToken |> should equal cts.Token
}
[<Fact>]
let ``TaskSeq-withCancellation: CancellationToken.None passes through correctly`` () = task {
let source = TokenCapturingSeq([ 10; 20 ])
let wrapped = TaskSeq.withCancellation CancellationToken.None (source :> IAsyncEnumerable<_>)
let! _ = TaskSeq.toArrayAsync wrapped
source.CapturedToken |> should equal CancellationToken.None
}
module ``Cancellation behaviour`` =
[<Fact>]
let ``TaskSeq-withCancellation: pre-cancelled token causes OperationCanceledException on iteration`` () = task {
use cts = new CancellationTokenSource()
cts.Cancel()
let source = taskSeq {
while true do
yield 1
}
let wrapped = TaskSeq.withCancellation cts.Token source
fun () -> TaskSeq.iter ignore wrapped |> Task.ignore
|> should throwAsync typeof<OperationCanceledException>
}
[<Fact>]
let ``TaskSeq-withCancellation: token cancelled mid-iteration raises OperationCanceledException`` () = task {
use cts = new CancellationTokenSource()
let source = taskSeq {
for i in 1..100 do
yield i
}
let wrapped = TaskSeq.withCancellation cts.Token source
fun () ->
task {
let mutable count = 0
use enum = wrapped.GetAsyncEnumerator(CancellationToken.None)
while! enum.MoveNextAsync() do
count <- count + 1
if count = 3 then
cts.Cancel()
}
|> Task.ignore
|> should throwAsync typeof<OperationCanceledException>
}
module ``Sequence contents`` =
[<Fact>]
let ``TaskSeq-withCancellation: empty source produces empty sequence`` () =
TaskSeq.empty<int>
|> TaskSeq.withCancellation CancellationToken.None
|> verifyEmpty
[<Fact>]
let ``TaskSeq-withCancellation: finite source produces all items`` () = task {
let! result =
taskSeq {
for i in 1..10 do
yield i
}
|> TaskSeq.withCancellation CancellationToken.None
|> TaskSeq.toArrayAsync
result |> should equal [| 1..10 |]
}
[<Fact>]
let ``TaskSeq-withCancellation: can be used with TaskSeq combinators`` () = task {
use cts = new CancellationTokenSource()
let! result =
taskSeq {
for i in 1..5 do
yield i
}
|> TaskSeq.withCancellation cts.Token
|> TaskSeq.map (fun x -> x * 2)
|> TaskSeq.toArrayAsync
result |> should equal [| 2; 4; 6; 8; 10 |]
}
[<Fact>]
let ``TaskSeq-withCancellation: can be piped like .WithCancellation usage pattern`` () = task {
use cts = new CancellationTokenSource()
let mutable collected = ResizeArray()
let source = taskSeq {
for i in 1..5 do
yield i
}
do!
source
|> TaskSeq.withCancellation cts.Token
|> TaskSeq.iterAsync (fun x -> task { collected.Add(x) })
collected |> Seq.toArray |> should equal [| 1..5 |]
}