forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildGraphTests.fs
235 lines (181 loc) · 7.09 KB
/
BuildGraphTests.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
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace FSharp.Compiler.UnitTests
open System
open System.Threading
open System.Runtime.CompilerServices
open Xunit
open FSharp.Test
open FSharp.Test.Compiler
open FSharp.Compiler.BuildGraph
open Internal.Utilities.Library
module BuildGraphTests =
[<MethodImpl(MethodImplOptions.NoInlining)>]
let private createNode () =
let o = obj ()
GraphNode(node {
Assert.shouldBeTrue (o <> null)
return 1
}), WeakReference(o)
[<Fact>]
let ``Intialization of graph node should not have a computed value``() =
let node = GraphNode(node { return 1 })
Assert.shouldBeTrue(node.TryPeekValue().IsNone)
Assert.shouldBeFalse(node.HasValue)
[<Fact>]
let ``Two requests to get a value asynchronously should be successful``() =
let resetEvent = new ManualResetEvent(false)
let resetEventInAsync = new ManualResetEvent(false)
let graphNode =
GraphNode(node {
resetEventInAsync.Set() |> ignore
let! _ = NodeCode.AwaitWaitHandle_ForTesting(resetEvent)
return 1
})
let task1 =
node {
let! _ = graphNode.GetOrComputeValue()
()
} |> NodeCode.StartAsTask_ForTesting
let task2 =
node {
let! _ = graphNode.GetOrComputeValue()
()
} |> NodeCode.StartAsTask_ForTesting
resetEventInAsync.WaitOne() |> ignore
resetEvent.Set() |> ignore
try
task1.Wait(1000) |> ignore
task2.Wait() |> ignore
with
| :? TimeoutException -> reraise()
| _ -> ()
[<Fact>]
let ``Many requests to get a value asynchronously should only evaluate the computation once``() =
let requests = 10000
let mutable computationCount = 0
let graphNode =
GraphNode(node {
computationCount <- computationCount + 1
return 1
})
let work = Async.Parallel(Array.init requests (fun _ -> graphNode.GetOrComputeValue() |> Async.AwaitNodeCode))
Async.RunImmediate(work)
|> ignore
Assert.shouldBe 1 computationCount
[<Fact>]
let ``Many requests to get a value asynchronously should get the correct value``() =
let requests = 10000
let graphNode = GraphNode(node { return 1 })
let work = Async.Parallel(Array.init requests (fun _ -> graphNode.GetOrComputeValue() |> Async.AwaitNodeCode))
let result = Async.RunImmediate(work)
Assert.shouldNotBeEmpty result
Assert.shouldBe requests result.Length
result
|> Seq.iter (Assert.shouldBe 1)
[<Fact>]
let ``A request to get a value asynchronously should have its computation cleaned up by the GC``() =
let graphNode, weak = createNode ()
GC.Collect(2, GCCollectionMode.Forced, true)
Assert.shouldBeTrue weak.IsAlive
NodeCode.RunImmediateWithoutCancellation(graphNode.GetOrComputeValue())
|> ignore
GC.Collect(2, GCCollectionMode.Forced, true)
Assert.shouldBeFalse weak.IsAlive
[<Fact>]
let ``Many requests to get a value asynchronously should have its computation cleaned up by the GC``() =
let requests = 10000
let graphNode, weak = createNode ()
GC.Collect(2, GCCollectionMode.Forced, true)
Assert.shouldBeTrue weak.IsAlive
Async.RunImmediate(Async.Parallel(Array.init requests (fun _ -> graphNode.GetOrComputeValue() |> Async.AwaitNodeCode)))
|> ignore
GC.Collect(2, GCCollectionMode.Forced, true)
Assert.shouldBeFalse weak.IsAlive
[<Fact>]
let ``A request can cancel``() =
let graphNode =
GraphNode(node {
return 1
})
use cts = new CancellationTokenSource()
let work =
node {
cts.Cancel()
return! graphNode.GetOrComputeValue()
}
let ex =
try
NodeCode.RunImmediate(work, ct = cts.Token)
|> ignore
failwith "Should have canceled"
with
| :? OperationCanceledException as ex ->
ex
Assert.shouldBeTrue(ex <> null)
[<Fact>]
let ``A request can cancel 2``() =
let resetEvent = new ManualResetEvent(false)
let graphNode =
GraphNode(node {
let! _ = NodeCode.AwaitWaitHandle_ForTesting(resetEvent)
return 1
})
use cts = new CancellationTokenSource()
let task =
node {
cts.Cancel()
resetEvent.Set() |> ignore
}
|> NodeCode.StartAsTask_ForTesting
let ex =
try
NodeCode.RunImmediate(graphNode.GetOrComputeValue(), ct = cts.Token)
|> ignore
failwith "Should have canceled"
with
| :? OperationCanceledException as ex ->
ex
Assert.shouldBeTrue(ex <> null)
try task.Wait(1000) |> ignore with | :? TimeoutException -> reraise() | _ -> ()
[<Fact>]
let ``Many requests to get a value asynchronously might evaluate the computation more than once even when some requests get canceled``() =
let requests = 10000
let resetEvent = new ManualResetEvent(false)
let mutable computationCountBeforeSleep = 0
let mutable computationCount = 0
let graphNode =
GraphNode(node {
computationCountBeforeSleep <- computationCountBeforeSleep + 1
let! _ = NodeCode.AwaitWaitHandle_ForTesting(resetEvent)
computationCount <- computationCount + 1
return 1
})
use cts = new CancellationTokenSource()
let work =
node {
let! _ = graphNode.GetOrComputeValue()
()
}
let tasks = ResizeArray()
for i = 0 to requests - 1 do
if i % 10 = 0 then
NodeCode.StartAsTask_ForTesting(work, ct = cts.Token)
|> tasks.Add
else
NodeCode.StartAsTask_ForTesting(work)
|> tasks.Add
cts.Cancel()
resetEvent.Set() |> ignore
NodeCode.RunImmediateWithoutCancellation(work)
|> ignore
Assert.shouldBeTrue cts.IsCancellationRequested
Assert.shouldBeTrue(computationCountBeforeSleep > 0)
Assert.shouldBeTrue(computationCount >= 0)
tasks
|> Seq.iter (fun x ->
try x.Wait(1000) |> ignore with | :? TimeoutException -> reraise() | _ -> ())
[<Fact>]
let ``GraphNode created from an already computed result will return it in tryPeekValue`` () =
let graphNode = GraphNode.FromResult 1
Assert.shouldBeTrue graphNode.HasValue
Assert.shouldBe (ValueSome 1) (graphNode.TryPeekValue())