forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleReaderCancellationTests.fs
290 lines (219 loc) · 9.49 KB
/
ModuleReaderCancellationTests.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
288
289
290
module FSharp.Compiler.Service.Tests.ModuleReaderCancellationTests
open System
open System.IO
open System.Reflection
open System.Threading
open FSharp.Compiler
open FSharp.Compiler.AbstractIL.IL
open FSharp.Compiler.AbstractIL.ILBinaryReader
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.Text
open FsUnit
open Internal.Utilities.Library
open FSharp.Compiler.Service.Tests.Common
open Xunit
let mutable private cts = new CancellationTokenSource()
let mutable private wasCancelled = false
let runCancelFirstTime f =
let mutable requestCount = 0
fun () ->
if requestCount = 0 then
cts.Cancel()
requestCount <- requestCount + 1
Cancellable.CheckAndThrow()
f ()
module ModuleReader =
let subsystemVersion = 4, 0
let useHighEntropyVA = false
let metadataVersion = String.Empty
let flags = 0
let exportedTypes = mkILExportedTypes []
let mkCtor () =
let name = ".ctor"
let methodAttrs =
MethodAttributes.Public |||
MethodAttributes.HideBySig |||
MethodAttributes.NewSlot |||
MethodAttributes.SpecialName
let callingConv = Callconv(ILThisConvention.Instance, ILArgConvention.Default)
let parameters = []
let ret = mkILReturn ILType.Void
let genericParams = []
let customAttrs = mkILCustomAttrs []
let implAttributes = MethodImplAttributes.Managed
let body = InterruptibleLazy.FromValue MethodBody.NotAvailable
let securityDecls = emptyILSecurityDecls
let isEntryPoint = false
ILMethodDef(name, methodAttrs, implAttributes, callingConv, parameters, ret, body, isEntryPoint, genericParams,
securityDecls, customAttrs)
type ModuleReader(name, typeDefs, cancelOnModuleAccess) =
let assemblyName = $"{name}.dll"
let mkModuleDef =
let mkModuleDef () =
let assemblyName = $"{name}.dll"
let moduleName = name
let isDll = true
mkILSimpleModule
assemblyName moduleName isDll
ModuleReader.subsystemVersion
ModuleReader.useHighEntropyVA
typeDefs
None None
ModuleReader.flags
ModuleReader.exportedTypes
""
if cancelOnModuleAccess then
runCancelFirstTime mkModuleDef
else
mkModuleDef
member val Timestamp = DateTime.UtcNow
member val Path = Path.Combine(Path.GetTempPath(), assemblyName)
interface ILModuleReader with
member x.ILModuleDef = mkModuleDef ()
member x.ILAssemblyRefs = []
member x.Dispose() = ()
type PreTypeDefData =
{ Name: string
Namespace: string list
HasCtor: bool
CancelOnImport: bool }
member this.TypeDef =
let name =
match this.Namespace with
| [] -> this.Name
| ns ->
let ns = ns |> String.concat "."
$"{ns}.{this.Name}"
let methodsDefs =
if this.HasCtor then
let mkCtor = runCancelFirstTime (fun _ -> [| ModuleReader.mkCtor () |])
mkILMethodsComputed mkCtor
else
mkILMethods []
let typeAttributes = TypeAttributes.Public
ILTypeDef(this.Name, typeAttributes, ILTypeDefLayout.Auto, emptyILInterfaceImpls, [],
None, methodsDefs, mkILTypeDefs [], mkILFields [], emptyILMethodImpls, mkILEvents [], mkILProperties [], ILTypeDefAdditionalFlags.None,
emptyILSecurityDecls, emptyILCustomAttrsStored)
type PreTypeDef(data: PreTypeDefData) =
let typeDef = data.TypeDef
let getTypeDef =
if data.CancelOnImport then runCancelFirstTime (fun _ -> typeDef) else (fun _ -> typeDef)
interface ILPreTypeDef with
member x.Name = data.Name
member x.Namespace = data.Namespace
member x.GetTypeDef() = getTypeDef ()
let createPreTypeDefs typeData =
typeData
|> Array.ofList
|> Array.map (fun data -> PreTypeDef data :> ILPreTypeDef)
let referenceReaderProject getPreTypeDefs (cancelOnModuleAccess: bool) (options: FSharpProjectOptions) =
let reader = new ModuleReader("Reference", mkILTypeDefsComputed getPreTypeDefs, cancelOnModuleAccess)
let project = FSharpReferencedProject.ILModuleReference(
reader.Path, (fun _ -> reader.Timestamp), (fun _ -> reader)
)
{ options with ReferencedProjects = [| project |]; OtherOptions = Array.append options.OtherOptions [| $"-r:{reader.Path}"|] }
let parseAndCheck path source options =
cts <- new CancellationTokenSource()
wasCancelled <- false
try
match Async.RunSynchronously(checker.ParseAndCheckFileInProject(path, 0, SourceText.ofString source, options), cancellationToken = cts.Token) with
| fileResults, FSharpCheckFileAnswer.Aborted -> None
| fileResults, FSharpCheckFileAnswer.Succeeded results -> Some results
with :? OperationCanceledException ->
wasCancelled <- true
None
let source1 = """
module Module
let t: T = T()
"""
let source2 = """
module Module
open Ns1.Ns2
let t: T = T()
"""
[<Fact>]
let ``Type defs 01 - assembly import`` () =
let source = source1
let getPreTypeDefs typeData = runCancelFirstTime (fun _ -> createPreTypeDefs typeData)
let typeDefs = getPreTypeDefs [ { Name = "T"; Namespace = []; HasCtor = false; CancelOnImport = false } ]
let path, options = mkTestFileAndOptions source [||]
let options = referenceReaderProject typeDefs false options
// First request, should be cancelled inside getPreTypeDefs
// The cancellation happens in side CombineImportedAssembliesTask, so background builder node fails to be evaluated
parseAndCheck path source options |> ignore
wasCancelled |> shouldEqual true
// Second request, should succeed, with complete analysis
match parseAndCheck path source options with
| Some results ->
wasCancelled |> shouldEqual false
results.Diagnostics
|> Array.map (fun e -> e.Message)
|> shouldEqual [| "No constructors are available for the type 'T'" |]
| None -> failwith "Expecting results"
// can only be run explicitly
[<Fact(Skip = "Type shouldn't be imported, see dotnet/fsharp#16166")>]
let ``Type defs 02 - assembly import`` () =
let source = source1
let typeDefs = fun _ -> createPreTypeDefs [ { Name = "T"; Namespace = ["Ns"]; HasCtor = false; CancelOnImport = true } ]
let path, options = mkTestFileAndOptions source [||]
let options = referenceReaderProject typeDefs false options
parseAndCheck path source options |> ignore
wasCancelled |> shouldEqual false
match parseAndCheck path source options with
| Some results ->
wasCancelled |> shouldEqual false
results.Diagnostics |> Array.isEmpty |> shouldEqual false
| None -> failwith "Expecting results"
[<Fact>]
let ``Type defs 03 - type import`` () =
let source = source2
let typeDefs = fun _ -> createPreTypeDefs [ { Name = "T"; Namespace = ["Ns1"; "Ns2"]; HasCtor = false; CancelOnImport = true } ]
let path, options = mkTestFileAndOptions source [||]
let options = referenceReaderProject typeDefs false options
// First request, should be cancelled inside GetTypeDef
// This shouldn't be cached due to InterruptibleLazy
parseAndCheck path source options |> ignore
wasCancelled |> shouldEqual true
// Second request, should succeed, with complete analysis
match parseAndCheck path source options with
| Some results ->
wasCancelled |> shouldEqual false
results.Diagnostics
|> Array.map (fun e -> e.Message)
|> shouldEqual [| "No constructors are available for the type 'T'" |]
| None -> failwith "Expecting results"
[<Fact>]
let ``Type defs 04 - ctor import`` () =
let source = source1
let typeDefs = fun _ -> createPreTypeDefs [ { Name = "T"; Namespace = []; HasCtor = true; CancelOnImport = false } ]
let path, options = mkTestFileAndOptions source [||]
let options = referenceReaderProject typeDefs false options
// First request, should be cancelled inside ILMethodDefs
// This shouldn't be cached due to InterruptibleLazy
parseAndCheck path source options |> ignore
wasCancelled |> shouldEqual true
// Second request, should succeed, with complete analysis
match parseAndCheck path source options with
| Some results ->
wasCancelled |> shouldEqual false
results.Diagnostics |> Array.isEmpty |> shouldEqual true
| None -> failwith "Expecting results"
[<Fact>]
let ``Module def 01 - assembly import`` () =
let source = source1
let getPreTypeDefs typeData = fun _ -> createPreTypeDefs typeData
let typeDefs = getPreTypeDefs [ { Name = "T"; Namespace = []; HasCtor = false; CancelOnImport = false } ]
let path, options = mkTestFileAndOptions source [||]
let options = referenceReaderProject typeDefs true options
// First request, should be cancelled inside getPreTypeDefs
// The cancellation happens in side CombineImportedAssembliesTask, so background builder node fails to be evaluated
parseAndCheck path source options |> ignore
wasCancelled |> shouldEqual true
// Second request, should succeed, with complete analysis
match parseAndCheck path source options with
| Some results ->
wasCancelled |> shouldEqual false
results.Diagnostics
|> Array.map _.Message
|> shouldEqual [| "No constructors are available for the type 'T'" |]
| None -> failwith "Expecting results"