-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTaskSeq.Concat.Tests.fs
287 lines (246 loc) · 8.36 KB
/
TaskSeq.Concat.Tests.fs
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
module TaskSeq.Tests.Concat
open System
open System.Collections.Generic
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.concat - of task seqs
// TaskSeq.concat - of seqs
// TaskSeq.concat - of lists
// TaskSeq.concat - of arrays
// TaskSeq.concat - of resizable arrays
//
let validateSequence ts =
ts
|> TaskSeq.toListAsync
|> Task.map (List.map string)
|> Task.map (String.concat "")
|> Task.map (should equal "123456789101234567891012345678910")
module EmptySeq =
[<Fact>]
let ``Null source is invalid (taskseq)`` () =
assertNullArg
<| fun () -> TaskSeq.concat (null: TaskSeq<TaskSeq<_>>)
[<Fact>]
let ``Null source is invalid (seq)`` () =
assertNullArg
<| fun () -> TaskSeq.concat (null: TaskSeq<seq<_>>)
[<Fact>]
let ``Null source is invalid (array)`` () =
assertNullArg
<| fun () -> TaskSeq.concat (null: TaskSeq<array<_>>)
[<Fact>]
let ``Null source is invalid (list)`` () =
assertNullArg
<| fun () -> TaskSeq.concat (null: TaskSeq<list<_>>)
[<Fact>]
let ``Null source is invalid (resizarray)`` () =
assertNullArg
<| fun () -> TaskSeq.concat (null: TaskSeq<ResizeArray<_>>)
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-concat with nested empty task sequences`` variant =
taskSeq {
yield Gen.getEmptyVariant variant
yield Gen.getEmptyVariant variant
yield Gen.getEmptyVariant variant
}
|> TaskSeq.concat
|> verifyEmpty
[<Fact>]
let ``TaskSeq-concat with nested empty sequences`` () =
taskSeq {
yield Seq.empty<string>
yield Seq.empty<string>
yield Seq.empty<string>
}
|> TaskSeq.concat
|> verifyEmpty
[<Fact>]
let ``TaskSeq-concat with nested empty arrays`` () =
taskSeq {
yield Array.empty<int>
yield Array.empty<int>
yield Array.empty<int>
}
|> TaskSeq.concat
|> verifyEmpty
[<Fact>]
let ``TaskSeq-concat with nested empty lists`` () =
taskSeq {
yield List.empty<Guid>
yield List.empty<Guid>
yield List.empty<Guid>
}
|> TaskSeq.concat
|> verifyEmpty
[<Fact>]
let ``TaskSeq-concat with multiple nested empty resizable arrays`` () =
taskSeq {
yield ResizeArray(List.empty<byte>)
yield ResizeArray(List.empty<byte>)
yield ResizeArray(List.empty<byte>)
}
|> TaskSeq.concat
|> verifyEmpty
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-concat with empty source (taskseq)`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.box
|> TaskSeq.cast<IAsyncEnumerable<int>> // task seq is empty so this should not raise
|> TaskSeq.concat
|> verifyEmpty
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-concat with empty source (seq)`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.box
|> TaskSeq.cast<int seq> // task seq is empty so this should not raise
|> TaskSeq.concat
|> verifyEmpty
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-concat with empty source (list)`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.box
|> TaskSeq.cast<int list> // task seq is empty so this should not raise
|> TaskSeq.concat
|> verifyEmpty
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-concat with empty source (array)`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.box
|> TaskSeq.cast<int[]> // task seq is empty so this should not raise
|> TaskSeq.concat
|> verifyEmpty
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-concat with empty source (resizearray)`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.box
|> TaskSeq.cast<ResizeArray<int>> // task seq is empty so this should not raise
|> TaskSeq.concat
|> verifyEmpty
module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-concat with three sequences of sequences`` variant =
taskSeq {
yield Gen.getSeqImmutable variant
yield Gen.getSeqImmutable variant
yield Gen.getSeqImmutable variant
}
|> TaskSeq.concat
|> validateSequence
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-concat with three sequences of sequences and few empties`` variant =
taskSeq {
yield TaskSeq.empty
yield Gen.getSeqImmutable variant
yield TaskSeq.empty
yield TaskSeq.empty
yield Gen.getSeqImmutable variant
yield TaskSeq.empty
yield Gen.getSeqImmutable variant
yield TaskSeq.empty
yield TaskSeq.empty
yield TaskSeq.empty
yield TaskSeq.empty
}
|> TaskSeq.concat
|> validateSequence
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-concat throws when one of inner task sequence is null`` variant =
fun () ->
taskSeq {
yield Gen.getSeqImmutable variant
yield TaskSeq.empty
yield null
}
|> TaskSeq.concat
|> consumeTaskSeq
|> should throwAsyncExact typeof<NullReferenceException>
module SideEffect =
[<Fact>]
let ``TaskSeq-concat executes side effects of nested (taskseq)`` () =
let mutable i = 0
taskSeq {
yield Gen.getSeqImmutable SeqImmutable.ThreadSpinWait
yield Gen.getSeqImmutable SeqImmutable.ThreadSpinWait
yield taskSeq {
yield! [ 1..10 ]
i <- i + 1
}
}
|> TaskSeq.concat
|> TaskSeq.last // consume
|> Task.map (fun _ -> i |> should equal 1)
[<Fact>]
let ``TaskSeq-concat executes side effects of nested (seq)`` () =
let mutable i = 0
taskSeq {
yield seq { 1..10 }
yield seq { 1..10 }
yield seq {
yield! [ 1..10 ]
i <- i + 1
}
}
|> TaskSeq.concat
|> TaskSeq.last // consume
|> Task.map (fun _ -> i |> should equal 1)
[<Fact>]
let ``TaskSeq-concat executes side effects of nested (array)`` () =
let mutable i = 0
taskSeq {
yield [| 1..10 |]
yield [| 1..10 |]
yield [| yield! [ 1..10 ]; i <- i + 1 |]
}
|> TaskSeq.concat
|> TaskSeq.last // consume
|> Task.map (fun _ -> i |> should equal 1)
[<Fact>]
let ``TaskSeq-concat executes side effects of nested (list)`` () =
let mutable i = 0
taskSeq {
yield [ 1..10 ]
yield [ 1..10 ]
yield [ yield! [ 1..10 ]; i <- i + 1 ]
}
|> TaskSeq.concat
|> TaskSeq.last // consume
|> Task.map (fun _ -> i |> should equal 1)
[<Fact>]
let ``TaskSeq-concat executes side effects of nested (resizearray)`` () =
let mutable i = 0
taskSeq {
yield ResizeArray { 1..10 }
yield ResizeArray { 1..10 }
yield
ResizeArray(
seq {
yield! [ 1..10 ]
i <- i + 1
}
)
}
|> TaskSeq.concat
|> TaskSeq.last // consume
|> Task.map (fun _ -> i |> should equal 1)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-concat consumes side effects in empty sequences`` variant =
let mutable i = 0
taskSeq {
yield taskSeq { do i <- i + 1 }
yield Gen.getSeqImmutable variant // not yield-bang!
yield TaskSeq.empty
yield taskSeq { do i <- i + 1 }
yield Gen.getSeqImmutable variant
yield TaskSeq.empty
yield Gen.getSeqImmutable variant
yield TaskSeq.empty
yield TaskSeq.empty
yield TaskSeq.empty
yield TaskSeq.empty
yield taskSeq { do i <- i + 1 }
}
|> TaskSeq.concat
|> validateSequence
|> Task.map (fun () -> i |> should equal 3)