-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathTaskSeq.Using.Tests.fs
More file actions
225 lines (171 loc) · 6.22 KB
/
Copy pathTaskSeq.Using.Tests.fs
File metadata and controls
225 lines (171 loc) · 6.22 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
module TaskSeq.Tests.Using
open System
open System.Threading.Tasks
open FSharp.Control
open FsUnit
open Xunit
type private OneGetter() =
member _.Get1() = 1
type private Disposable(disposed: bool ref) =
inherit OneGetter()
interface IDisposable with
member _.Dispose() = disposed.Value <- true
type private AsyncDisposable(disposed: bool ref) =
inherit OneGetter()
interface IAsyncDisposable with
member _.DisposeAsync() = ValueTask(task { do disposed.Value <- true })
type private MultiDispose(disposed: int ref) =
inherit OneGetter()
interface IDisposable with
member _.Dispose() = disposed.Value <- 1
interface IAsyncDisposable with
member _.DisposeAsync() = ValueTask(task { do disposed.Value <- -1 })
/// Tracks how many times Dispose/DisposeAsync has been called.
type private CountingDisposable(disposeCount: int ref) =
interface IDisposable with
member _.Dispose() = disposeCount.Value <- disposeCount.Value + 1
/// Tracks how many times DisposeAsync has been called.
type private CountingAsyncDisposable(disposeCount: int ref) =
interface IAsyncDisposable with
member _.DisposeAsync() =
disposeCount.Value <- disposeCount.Value + 1
ValueTask.CompletedTask
let private check = TaskSeq.length >> Task.map (should equal 1)
[<Fact>]
let ``CE taskSeq: Using when type implements IDisposable`` () =
let disposed = ref false
let ts = taskSeq {
use x = new Disposable(disposed)
yield x.Get1()
}
check ts
|> Task.map (fun _ -> disposed.Value |> should be True)
[<Fact>]
let ``CE taskSeq: Using when type implements IAsyncDisposable`` () =
let disposed = ref false
let ts = taskSeq {
use x = AsyncDisposable(disposed)
yield x.Get1()
}
check ts
|> Task.map (fun _ -> disposed.Value |> should be True)
[<Fact>]
let ``CE taskSeq: Using when type implements IDisposable and IAsyncDisposable`` () =
let disposed = ref 0
let ts = taskSeq {
use x = new MultiDispose(disposed) // Used to fail to compile (see #97)
yield x.Get1()
}
check ts
|> Task.map (fun _ -> disposed.Value |> should equal -1) // should prefer IAsyncDisposable, which returns -1
[<Fact>]
let ``CE taskSeq: Using! when type implements IDisposable`` () =
let disposed = ref false
let ts = taskSeq {
use! x = task { return new Disposable(disposed) }
yield x.Get1()
}
check ts
|> Task.map (fun _ -> disposed.Value |> should be True)
[<Fact>]
let ``CE taskSeq: Using! when type implements IAsyncDisposable`` () =
let disposed = ref false
let ts = taskSeq {
use! x = task { return AsyncDisposable(disposed) }
yield x.Get1()
}
check ts
|> Task.map (fun _ -> disposed.Value |> should be True)
[<Fact>]
let ``CE taskSeq: Using! when type implements IDisposable and IAsyncDisposable`` () =
let disposed = ref 0
let ts = taskSeq {
use! x = task { return new MultiDispose(disposed) } // Used to fail to compile (see #97)
yield x.Get1()
}
check ts
|> Task.map (fun _ -> disposed.Value |> should equal -1) // should prefer IAsyncDisposable, which returns -1
module SideEffects =
[<Fact>]
let ``CE taskSeq: use - Dispose called exactly once per full iteration`` () = task {
let disposeCount = ref 0
let ts = taskSeq {
use _ = new CountingDisposable(disposeCount)
yield 1
}
do! ts |> TaskSeq.iter ignore
disposeCount.Value |> should equal 1
}
[<Fact>]
let ``CE taskSeq: use - Dispose called on each re-iteration`` () = task {
let disposeCount = ref 0
let ts = taskSeq {
use _ = new CountingDisposable(disposeCount)
yield 1
}
do! ts |> TaskSeq.iter ignore
do! ts |> TaskSeq.iter ignore
do! ts |> TaskSeq.iter ignore
disposeCount.Value |> should equal 3
}
[<Fact>]
let ``CE taskSeq: use! - DisposeAsync called exactly once per full iteration`` () = task {
let disposeCount = ref 0
let ts = taskSeq {
use! _ = task { return new CountingAsyncDisposable(disposeCount) }
yield 1
}
do! ts |> TaskSeq.iter ignore
disposeCount.Value |> should equal 1
}
[<Fact>]
let ``CE taskSeq: use! - DisposeAsync called on each re-iteration`` () = task {
let disposeCount = ref 0
let ts = taskSeq {
use! _ = task { return new CountingAsyncDisposable(disposeCount) }
yield 1
}
do! ts |> TaskSeq.iter ignore
do! ts |> TaskSeq.iter ignore
do! ts |> TaskSeq.iter ignore
disposeCount.Value |> should equal 3
}
[<Fact>]
let ``CE taskSeq: use - Dispose called on early termination via take`` () = task {
let disposeCount = ref 0
let ts = taskSeq {
use _ = new CountingDisposable(disposeCount)
yield 1
yield 2
yield 3
}
// Only take 1 item — enumerator is disposed before the rest of the sequence runs
do! ts |> TaskSeq.take 1 |> TaskSeq.iter ignore
disposeCount.Value |> should equal 1
}
[<Fact>]
let ``CE taskSeq: use - multiple use bindings each get their own Dispose`` () = task {
let disposeCount = ref 0
let ts = taskSeq {
use _ = new CountingDisposable(disposeCount)
use _ = new CountingDisposable(disposeCount)
yield 1
}
do! ts |> TaskSeq.iter ignore
disposeCount.Value |> should equal 2
}
[<Fact>]
let ``CE taskSeq: use - each re-iteration creates and disposes a fresh resource`` () = task {
let createCount = ref 0
let ts = taskSeq {
createCount.Value <- createCount.Value + 1
use _ = new CountingDisposable(ref 0) // fresh ref each time
yield createCount.Value
}
let! first = ts |> TaskSeq.toListAsync
let! second = ts |> TaskSeq.toListAsync
// Each re-iteration re-runs the CE body and creates a new resource
first |> should equal [ 1 ]
second |> should equal [ 2 ]
createCount.Value |> should equal 2
}