-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTaskSeq.Dynamic.Tests.CE.fs
More file actions
327 lines (267 loc) · 7.31 KB
/
TaskSeq.Dynamic.Tests.CE.fs
File metadata and controls
327 lines (267 loc) · 7.31 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
module TaskSeq.Tests.``taskSeqDynamic Computation Expression``
open System
open System.Threading
open Xunit
open FsUnit.Xunit
open FSharp.Control
// -------------------------------------------------------
// Basic sanity tests for the taskSeqDynamic CE builder.
// taskSeqDynamic uses the same static path as taskSeq when
// compiled normally, but falls back to the dynamic
// (ResumptionDynamicInfo-based) path in FSI / when the
// F# compiler cannot emit static resumable code.
// -------------------------------------------------------
[<Fact>]
let ``CE taskSeqDynamic empty sequence`` () = task {
let ts = taskSeqDynamic { () }
let! data = ts |> TaskSeq.toListAsync
data |> should be Empty
}
[<Fact>]
let ``CE taskSeqDynamic single yield`` () = task {
let ts = taskSeqDynamic { yield 42 }
let! data = ts |> TaskSeq.toListAsync
data |> should equal [ 42 ]
}
[<Fact>]
let ``CE taskSeqDynamic multiple yields`` () = task {
let ts = taskSeqDynamic {
yield 1
yield 2
yield 3
}
let! data = ts |> TaskSeq.toListAsync
data |> should equal [ 1; 2; 3 ]
}
[<Fact>]
let ``CE taskSeqDynamic yield with for loop`` () = task {
let ts = taskSeqDynamic {
for i in 1..5 do
yield i
}
let! data = ts |> TaskSeq.toListAsync
data |> should equal [ 1; 2; 3; 4; 5 ]
}
[<Fact>]
let ``CE taskSeqDynamic yield with async task bind`` () = task {
let ts = taskSeqDynamic {
let! x = task { return 10 }
yield x
let! y = task { return 20 }
yield y
}
let! data = ts |> TaskSeq.toListAsync
data |> should equal [ 10; 20 ]
}
[<Fact>]
let ``CE taskSeqDynamic yield from seq`` () = task {
let ts = taskSeqDynamic { yield! [ 1; 2; 3 ] }
let! data = ts |> TaskSeq.toListAsync
data |> should equal [ 1; 2; 3 ]
}
[<Fact>]
let ``CE taskSeqDynamic yield from another taskSeqDynamic`` () = task {
let inner = taskSeqDynamic {
yield 1
yield 2
}
let ts = taskSeqDynamic {
yield! inner
yield 3
}
let! data = ts |> TaskSeq.toListAsync
data |> should equal [ 1; 2; 3 ]
}
[<Fact>]
let ``CE taskSeqDynamic yield from taskSeq`` () = task {
let inner = taskSeq {
yield 1
yield 2
}
let ts = taskSeqDynamic { yield! inner }
let! data = ts |> TaskSeq.toListAsync
data |> should equal [ 1; 2 ]
}
[<Fact>]
let ``CE taskSeqDynamic with tryWith`` () = task {
let ts = taskSeqDynamic {
try
yield 1
yield 2
with _ ->
yield -1
}
let! data = ts |> TaskSeq.toListAsync
data |> should equal [ 1; 2 ]
}
[<Fact>]
let ``CE taskSeqDynamic with tryWith catching exception`` () = task {
let ts = taskSeqDynamic {
try
yield 1
raise (InvalidOperationException "test")
yield 2
with :? InvalidOperationException ->
yield 99
}
let! data = ts |> TaskSeq.toListAsync
data |> should equal [ 1; 99 ]
}
[<Fact>]
let ``CE taskSeqDynamic with tryFinally`` () = task {
let mutable finallyCalled = false
let ts = taskSeqDynamic {
try
yield 1
yield 2
finally
finallyCalled <- true
}
let! data = ts |> TaskSeq.toListAsync
data |> should equal [ 1; 2 ]
finallyCalled |> should equal true
}
[<Fact>]
let ``CE taskSeqDynamic with use`` () = task {
let mutable disposed = false
let mkDisposable () =
{ new IDisposable with
member _.Dispose() = disposed <- true
}
let ts = taskSeqDynamic {
use _d = mkDisposable ()
yield 42
}
let! data = ts |> TaskSeq.toListAsync
data |> should equal [ 42 ]
disposed |> should equal true
}
[<Fact>]
let ``CE taskSeqDynamic supports re-enumeration`` () = task {
let ts = taskSeqDynamic {
yield 1
yield 2
yield 3
}
let! data1 = ts |> TaskSeq.toListAsync
let! data2 = ts |> TaskSeq.toListAsync
data1 |> should equal [ 1; 2; 3 ]
data2 |> should equal [ 1; 2; 3 ]
}
[<Fact>]
let ``CE taskSeqDynamic multiple re-enumerations produce same result`` () = task {
let ts = taskSeqDynamic {
for i in 1..10 do
yield i
}
for _ in 1..5 do
let! data = ts |> TaskSeq.toListAsync
data |> should equal [ 1..10 ]
}
[<Fact>]
let ``CE taskSeqDynamic with cancellation`` () = task {
use cts = new CancellationTokenSource()
cts.Cancel()
let ts = taskSeqDynamic {
yield 1
yield 2
yield 3
}
let enumerator = ts.GetAsyncEnumerator(cts.Token)
let mutable threw = false
try
let! _ = enumerator.MoveNextAsync()
()
with :? OperationCanceledException ->
threw <- true
threw |> should equal true
do! enumerator.DisposeAsync()
}
[<Fact>]
let ``CE taskSeqDynamic with large for loop`` () = task {
let ts = taskSeqDynamic {
for i in 1..1000 do
yield i
}
let! data = ts |> TaskSeq.toListAsync
data |> should equal [ 1..1000 ]
data |> should haveLength 1000
}
[<Fact>]
let ``CE taskSeqDynamic with nested for loops`` () = task {
let ts = taskSeqDynamic {
for i in 1..3 do
for j in 1..3 do
yield i * 10 + j
}
let expected = [
for i in 1..3 do
for j in 1..3 do
yield i * 10 + j
]
let! data = ts |> TaskSeq.toListAsync
data |> should equal expected
}
[<Fact>]
let ``CE taskSeqDynamic with async value task bind`` () = task {
let ts = taskSeqDynamic {
let! x = System.Threading.Tasks.ValueTask.FromResult(7)
yield x
}
let! data = ts |> TaskSeq.toListAsync
data |> should equal [ 7 ]
}
[<Fact>]
let ``CE taskSeqDynamic with Async bind`` () = task {
let ts = taskSeqDynamic {
let! x = async { return 99 }
yield x
}
let! data = ts |> TaskSeq.toListAsync
data |> should equal [ 99 ]
}
[<Fact>]
let ``CE taskSeqDynamic is IAsyncEnumerable`` () = task {
// In compiled mode this uses the static path (returns TaskSeq<_,_>),
// in FSI it returns TaskSeqDynamic<_>. Both implement IAsyncEnumerable<int>.
let ts = taskSeqDynamic { yield 1 }
let! data = ts |> TaskSeq.toListAsync
data |> should equal [ 1 ]
}
[<Fact>]
let ``CE taskSeqDynamic empty produces no values`` () = task {
let mutable count = 0
let ts = taskSeqDynamic { () }
let e = ts.GetAsyncEnumerator(CancellationToken.None)
try
while! e.MoveNextAsync() do
count <- count + 1
finally
()
do! e.DisposeAsync()
count |> should equal 0
}
[<Fact>]
let ``CE taskSeqDynamic with while loop`` () = task {
let ts = taskSeqDynamic {
let mutable i = 0
while i < 5 do
yield i
i <- i + 1
}
let! data = ts |> TaskSeq.toListAsync
data |> should equal [ 0; 1; 2; 3; 4 ]
}
[<Fact>]
let ``CE taskSeqDynamic is same type as TaskSeq`` () =
let ts: TaskSeq<int> = taskSeqDynamic { yield 1 }
ts |> should not' (be Null)
[<Fact>]
let ``CE taskSeqDynamic with several yield!`` () = task {
let tskSeq = taskSeqDynamic {
yield! Gen.sideEffectTaskSeq 10
yield! Gen.sideEffectTaskSeq 5
}
let! data = tskSeq |> TaskSeq.toListAsync
data |> should equal (List.concat [ [ 1..10 ]; [ 1..5 ] ])
}