Skip to content

Commit f2151fd

Browse files
committed
Improve AsyncMemoize tests
1 parent 1b50168 commit f2151fd

File tree

4 files changed

+53
-14
lines changed

4 files changed

+53
-14
lines changed

src/Compiler/Facilities/AsyncMemoize.fs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ type internal Job<'TValue> =
7474
| Failed(_, ex) -> $"Failed {ex}"
7575

7676
type internal JobEvent =
77+
| Requested
7778
| Started
79+
| Restarted
7880
| Finished
7981
| Canceled
8082
| Evicted
@@ -245,7 +247,7 @@ type internal AsyncMemoize<'TKey, 'TVersion, 'TValue when 'TKey: equality and 'T
245247

246248
let cached, otherVersions = cache.GetAll(key.Key, key.Version)
247249

248-
return
250+
let result =
249251
match msg, cached with
250252
| GetOrCompute _, Some(Completed(result, diags)) ->
251253
Interlocked.Increment &hits |> ignore
@@ -298,6 +300,9 @@ type internal AsyncMemoize<'TKey, 'TVersion, 'TValue when 'TKey: equality and 'T
298300
cts.Cancel())
299301

300302
New cts.Token
303+
304+
log (Requested, key)
305+
return result
301306
})
302307

303308
let internalError key message =
@@ -346,7 +351,7 @@ type internal AsyncMemoize<'TKey, 'TVersion, 'TValue when 'TKey: equality and 'T
346351

347352
try
348353
// TODO: Should unify starting and restarting
349-
log (Started, key)
354+
log (Restarted, key)
350355
Interlocked.Increment &restarted |> ignore
351356
System.Diagnostics.Trace.TraceInformation $"{name} Restarted {key.Label}"
352357
let currentLogger = DiagnosticsThreadStatics.DiagnosticsLogger

src/Compiler/Facilities/AsyncMemoize.fsi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ module internal Utils =
1212
val (|TaskCancelled|_|): ex: exn -> TaskCanceledException option
1313

1414
type internal JobEvent =
15+
| Requested
1516
| Started
17+
| Restarted
1618
| Finished
1719
| Canceled
1820
| Evicted

tests/FSharp.Compiler.ComponentTests/CompilerService/AsyncMemoize.fs

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ let ``Basics``() =
6161
let groups = eventLog |> Seq.groupBy snd |> Seq.toList
6262
Assert.Equal(3, groups.Length)
6363
for key, events in groups do
64-
Assert.Equal<Set<(JobEvent * int)>>(Set [ Started, key; Finished, key ], Set events)
64+
Assert.Equal<Set<(JobEvent * int)>>(Set [ Requested, key; Started, key; Finished, key ], Set events)
6565

6666
[<Fact>]
6767
let ``We can cancel a job`` () =
@@ -96,9 +96,14 @@ let ``We can cancel a job`` () =
9696
waitFor jobStarted
9797
jobStarted.Reset() |> ignore
9898

99+
let jobRequested = new ManualResetEvent(false)
100+
memoize.OnEvent(fun (e, _) -> if e = Requested then jobRequested.Set() |> ignore)
101+
99102
let _task2 = NodeCode.StartAsTask_ForTesting( memoize.Get'(key, computation ignore), ct = cts2.Token)
100103
let _task3 = NodeCode.StartAsTask_ForTesting( memoize.Get'(key, computation ignore), ct = cts3.Token)
101104

105+
waitFor jobRequested
106+
102107
cts1.Cancel()
103108
cts2.Cancel()
104109

@@ -108,7 +113,14 @@ let ``We can cancel a job`` () =
108113

109114
waitFor jobCanceled
110115

111-
Assert.Equal<(JobEvent * int) array>([| Started, key; Started, key; Canceled, key |], eventLog |> Seq.toArray )
116+
Assert.Equal<(JobEvent * int) array>([|
117+
Requested, key
118+
Started, key
119+
Requested, key
120+
Requested, key
121+
Restarted, key
122+
Canceled, key
123+
|], eventLog |> Seq.toArray )
112124
}
113125

114126
[<Fact>]
@@ -139,22 +151,31 @@ let ``Job is restarted if first requestor cancels`` () =
139151
waitFor jobStarted
140152
jobStarted.Reset() |> ignore
141153

154+
let jobRequested = new ManualResetEvent(false)
155+
memoize.OnEvent(fun (e, _) -> if e = Requested then jobRequested.Set() |> ignore)
156+
142157
let _task2 = NodeCode.StartAsTask_ForTesting( memoize.Get'(key, computation key), ct = cts2.Token)
143158
let _task3 = NodeCode.StartAsTask_ForTesting( memoize.Get'(key, computation key), ct = cts3.Token)
144159

160+
waitFor jobRequested
161+
145162
cts1.Cancel()
146163

147164
waitFor jobStarted
148165

149-
cts3.Cancel()
150-
151166
jobCanComplete.Set() |> ignore
152167

153168
let! result = _task2
154169
Assert.Equal(2, result)
155170

156171
let orderedLog = eventLog |> Seq.rev |> Seq.toList
157-
let expected = [ Started, key; Started, key; Finished, key ]
172+
let expected = [
173+
Requested, key
174+
Started, key
175+
Requested, key
176+
Requested, key
177+
Restarted, key
178+
Finished, key ]
158179

159180
Assert.Equal<_ list>(expected, orderedLog)
160181
}
@@ -184,15 +205,20 @@ let ``Job is restarted if first requestor cancels but keeps running if second re
184205

185206
let _task1 = NodeCode.StartAsTask_ForTesting( memoize.Get'(key, computation key), ct = cts1.Token)
186207

187-
jobStarted.WaitOne() |> ignore
208+
waitFor jobStarted
188209
jobStarted.Reset() |> ignore
189210

211+
let jobRequested = new ManualResetEvent(false)
212+
memoize.OnEvent(fun (e, _) -> if e = Requested then jobRequested.Set() |> ignore)
213+
190214
let _task2 = NodeCode.StartAsTask_ForTesting( memoize.Get'(key, computation key), ct = cts2.Token)
191215
let _task3 = NodeCode.StartAsTask_ForTesting( memoize.Get'(key, computation key), ct = cts3.Token)
192216

217+
waitFor jobRequested
218+
193219
cts1.Cancel()
194220

195-
jobStarted.WaitOne() |> ignore
221+
waitFor jobStarted
196222

197223
cts2.Cancel()
198224

@@ -202,7 +228,13 @@ let ``Job is restarted if first requestor cancels but keeps running if second re
202228
Assert.Equal(2, result)
203229

204230
let orderedLog = eventLog |> Seq.rev |> Seq.toList
205-
let expected = [ Started, key; Started, key; Finished, key ]
231+
let expected = [
232+
Requested, key
233+
Started, key
234+
Requested, key
235+
Requested, key
236+
Restarted, key
237+
Finished, key ]
206238

207239
Assert.Equal<_ list>(expected, orderedLog)
208240
}
@@ -228,7 +260,7 @@ let ``Stress test`` () =
228260
let keyCount = rng.Next(5, 200)
229261
let keys = [| 1 .. keyCount |]
230262

231-
let testTimeoutMs = threads * iterations * maxDuration
263+
let testTimeoutMs = threads * iterations * maxDuration * 2
232264

233265
let intenseComputation durationMs result =
234266
async {

tests/FSharp.Compiler.ComponentTests/FSharpChecker/TransparentCompiler.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ let ``File is not checked twice`` () =
237237
|> Seq.map (fun (k, g) -> k, g |> Seq.map fst |> Seq.toList)
238238
|> Map
239239

240-
Assert.Equal<JobEvent list>([Weakened; Started; Finished], intermediateTypeChecks["FileFirst.fs"])
241-
Assert.Equal<JobEvent list>([Weakened; Started; Finished], intermediateTypeChecks["FileThird.fs"])
240+
Assert.Equal<JobEvent list>([Weakened; Requested; Started; Finished], intermediateTypeChecks["FileFirst.fs"])
241+
Assert.Equal<JobEvent list>([Weakened; Requested; Started; Finished], intermediateTypeChecks["FileThird.fs"])
242242

243243
[<Fact>]
244244
let ``If a file is checked as a dependency it's not re-checked later`` () =
@@ -261,7 +261,7 @@ let ``If a file is checked as a dependency it's not re-checked later`` () =
261261
|> Seq.map (fun (k, g) -> k, g |> Seq.map fst |> Seq.toList)
262262
|> Map
263263

264-
Assert.Equal<JobEvent list>([Weakened; Started; Finished], intermediateTypeChecks["FileThird.fs"])
264+
Assert.Equal<JobEvent list>([Weakened; Requested; Started; Finished; Requested], intermediateTypeChecks["FileThird.fs"])
265265

266266

267267
// [<Fact>] TODO: differentiate complete and minimal checking requests

0 commit comments

Comments
 (0)