-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTaskSeq.OfXXX.Tests.fs
More file actions
196 lines (157 loc) · 6.04 KB
/
TaskSeq.OfXXX.Tests.fs
File metadata and controls
196 lines (157 loc) · 6.04 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
module TaskSeq.Tests.``Conversion-From``
open Xunit
open FsUnit.Xunit
open FSharp.Control
let validateSequence sq =
TaskSeq.toArrayAsync sq
|> Task.map (Seq.toArray >> should equal [| 0..9 |])
module EmptySeq =
[<Fact>]
let ``Null source is invalid`` () =
// note: ofList and its variants do not have null as proper value
assertNullArg <| fun () -> TaskSeq.ofAsyncArray null
assertNullArg <| fun () -> TaskSeq.ofAsyncSeq null
assertNullArg <| fun () -> TaskSeq.ofTaskArray null
assertNullArg <| fun () -> TaskSeq.ofTaskSeq null
assertNullArg <| fun () -> TaskSeq.ofResizeArray null
assertNullArg <| fun () -> TaskSeq.ofArray null
assertNullArg <| fun () -> TaskSeq.ofSeq null
[<Fact>]
let ``TaskSeq-ofAsyncArray with empty set`` () =
Array.init 0 (fun x -> async { return x })
|> TaskSeq.ofAsyncArray
|> verifyEmpty
[<Fact>]
let ``TaskSeq-ofAsyncList with empty set`` () =
List.init 0 (fun x -> async { return x })
|> TaskSeq.ofAsyncList
|> verifyEmpty
[<Fact>]
let ``TaskSeq-ofAsyncSeq with empty set`` () =
Seq.init 0 (fun x -> async { return x })
|> TaskSeq.ofAsyncSeq
|> verifyEmpty
[<Fact>]
let ``TaskSeq-ofTaskArray with empty set`` () =
Array.init 0 (fun x -> task { return x })
|> TaskSeq.ofTaskArray
|> verifyEmpty
[<Fact>]
let ``TaskSeq-ofTaskList with empty set`` () =
List.init 0 (fun x -> task { return x })
|> TaskSeq.ofTaskList
|> verifyEmpty
[<Fact>]
let ``TaskSeq-ofTaskSeq with empty set`` () =
Seq.init 0 (fun x -> task { return x })
|> TaskSeq.ofTaskSeq
|> verifyEmpty
[<Fact>]
let ``TaskSeq-ofResizeArray with empty set`` () = ResizeArray() |> TaskSeq.ofResizeArray |> verifyEmpty
[<Fact>]
let ``TaskSeq-ofArray with empty set`` () = Array.init 0 id |> TaskSeq.ofArray |> verifyEmpty
[<Fact>]
let ``TaskSeq-ofList with empty set`` () = List.init 0 id |> TaskSeq.ofList |> verifyEmpty
[<Fact>]
let ``TaskSeq-ofSeq with empty set`` () = Seq.init 0 id |> TaskSeq.ofSeq |> verifyEmpty
module Immutable =
[<Fact>]
let ``TaskSeq-ofAsyncArray should succeed`` () =
Array.init 10 (fun x -> async { return x })
|> TaskSeq.ofAsyncArray
|> validateSequence
[<Fact>]
let ``TaskSeq-ofAsyncList should succeed`` () =
List.init 10 (fun x -> async { return x })
|> TaskSeq.ofAsyncList
|> validateSequence
[<Fact>]
let ``TaskSeq-ofAsyncSeq should succeed`` () =
Seq.init 10 (fun x -> async { return x })
|> TaskSeq.ofAsyncSeq
|> validateSequence
[<Fact>]
let ``TaskSeq-ofTaskArray should succeed`` () =
Array.init 10 (fun x -> task { return x })
|> TaskSeq.ofTaskArray
|> validateSequence
[<Fact>]
let ``TaskSeq-ofTaskList should succeed`` () =
List.init 10 (fun x -> task { return x })
|> TaskSeq.ofTaskList
|> validateSequence
[<Fact>]
let ``TaskSeq-ofTaskSeq should succeed`` () =
Seq.init 10 (fun x -> task { return x })
|> TaskSeq.ofTaskSeq
|> validateSequence
[<Fact>]
let ``TaskSeq-ofResizeArray should succeed`` () =
ResizeArray [ 0..9 ]
|> TaskSeq.ofResizeArray
|> validateSequence
[<Fact>]
let ``TaskSeq-ofArray should succeed`` () = Array.init 10 id |> TaskSeq.ofArray |> validateSequence
[<Fact>]
let ``TaskSeq-ofList should succeed`` () = List.init 10 id |> TaskSeq.ofList |> validateSequence
[<Fact>]
let ``TaskSeq-ofSeq should succeed`` () = Seq.init 10 id |> TaskSeq.ofSeq |> validateSequence
module SideEffects =
[<Fact>]
let ``ofSeq re-evaluates the underlying source seq on each re-enumeration`` () = task {
let mutable count = 0
// a lazy IEnumerable — each GetEnumerator() call re-executes the body
let lazySeq = seq {
for i in 1..3 do
count <- count + 1
yield i
}
let ts = TaskSeq.ofSeq lazySeq
let! arr1 = ts |> TaskSeq.toArrayAsync
// each item triggered the side effect once
count |> should equal 3
let! arr2 = ts |> TaskSeq.toArrayAsync
// the underlying seq is re-traversed on the second GetAsyncEnumerator call
count |> should equal 6
arr1 |> should equal arr2
}
[<Fact>]
let ``ofTaskSeq with lazy seq of tasks re-creates tasks on each re-enumeration`` () = task {
let mutable count = 0
// a lazy IEnumerable of Task objects — each seq iteration creates fresh Task objects
let lazyTaskSeq = seq {
for i in 1..3 do
yield task {
count <- count + 1
return i
}
}
let ts = TaskSeq.ofTaskSeq lazyTaskSeq
let! arr1 = ts |> TaskSeq.toArrayAsync
count |> should equal 3
let! arr2 = ts |> TaskSeq.toArrayAsync
// the underlying seq is re-iterated; new Task objects are created and run
count |> should equal 6
arr1 |> should equal arr2
}
[<Fact>]
let ``ofTaskArray does not re-run tasks on re-enumeration; task results are cached`` () = task {
let mutable count = 0
// tasks are created upfront; they run synchronously to completion when constructed
let tasks =
Array.init 3 (fun i -> task {
count <- count + 1
return i + 1
})
// all three tasks have already completed synchronously
count |> should equal 3
let ts = TaskSeq.ofTaskArray tasks
let! arr1 = ts |> TaskSeq.toArrayAsync
// awaiting already-completed tasks does not re-run them
count |> should equal 3
arr1 |> should equal [| 1; 2; 3 |]
let! arr2 = ts |> TaskSeq.toArrayAsync
// the second enumeration re-awaits the same cached task results
count |> should equal 3
arr2 |> should equal arr1
}