-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
DefaultLoadContextTest.cs
287 lines (227 loc) · 12.9 KB
/
DefaultLoadContextTest.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Xunit;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace System.Runtime.Loader.Tests
{
public class SecondaryLoadContext : AssemblyLoadContext
{
protected override Assembly Load(AssemblyName assemblyName)
{
return null;
}
}
public class FallbackLoadContext : AssemblyLoadContext
{
protected override Assembly Load(AssemblyName assemblyName)
{
// Return null since we expect the DefaultContext to be used to bind
// the assembly bind request.
return null;
}
}
public class OverrideDefaultLoadContext : AssemblyLoadContext
{
public bool LoadedFromContext { get; set; } = false;
protected override Assembly Load(AssemblyName assemblyName)
{
// Override the assembly that was loaded in DefaultContext.
string dirName = Path.GetDirectoryName(AssemblyPathHelper.GetAssemblyLocation(typeof(string).Assembly));
string assemblyPath = Path.Combine(dirName, assemblyName.Name + ".dll");
Assembly assembly = LoadFromAssemblyPath(assemblyPath);
LoadedFromContext = true;
return assembly;
}
}
public class DefaultLoadContextTests
{
private const string TestAssemblyName = "System.Runtime.Loader.Noop.Assembly";
private string _assemblyPath;
private string _defaultLoadDirectory;
// Since the first non-Null returning callback should stop Resolving event processing,
// this counter is used to assert the same.
private int _numNonNullResolutions = 0;
public DefaultLoadContextTests()
{
_defaultLoadDirectory = GetDefaultAssemblyLoadDirectory();
_assemblyPath = Path.Combine(_defaultLoadDirectory, "System.Runtime.Loader.Noop.Assembly_test.dll");
}
private static string GetDefaultAssemblyLoadDirectory()
{
return Path.GetDirectoryName(AssemblyPathHelper.GetAssemblyLocation(Assembly.GetExecutingAssembly()));
}
private Assembly ResolveAssembly(AssemblyLoadContext sender, AssemblyName assembly)
{
string resolvedAssemblyPath = Path.Combine(_defaultLoadDirectory, assembly.Name + "_test.dll");
_numNonNullResolutions++;
return sender.LoadFromAssemblyPath(resolvedAssemblyPath);
}
private Assembly ResolveAssemblyAgain(AssemblyLoadContext sender, AssemblyName assembly)
{
return ResolveAssembly(sender, assembly);
}
private Assembly ResolveNullAssembly(AssemblyLoadContext sender, AssemblyName assembly)
{
return null;
}
// Does not apply to Mono AOT scenarios as it is expected the name of the .aotdata file matches
// the true name of the assembly and not the physical file name.
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoAOT))]
public void LoadInDefaultContext()
{
// This will attempt to load an assembly, by path, in the Default Load context via the Resolving event
var assemblyName = new AssemblyName(TestAssemblyName);
// By default, the assembly should not be found in DefaultContext at all
Assert.Throws<FileNotFoundException>(() => Assembly.Load(assemblyName));
// Create a secondary load context and wireup its resolving event
SecondaryLoadContext slc = new SecondaryLoadContext();
slc.Resolving += ResolveAssembly;
Assert.Contains(slc, AssemblyLoadContext.All);
// Attempt to load the assembly in secondary load context
var slcLoadedAssembly = slc.LoadFromAssemblyName(assemblyName);
// We should have successfully loaded the assembly in secondary load context.
Assert.NotNull(slcLoadedAssembly);
Assert.Contains(slcLoadedAssembly, slc.Assemblies);
// And make sure the simple name matches
Assert.Equal(TestAssemblyName, slcLoadedAssembly.GetName().Name);
// We should have only invoked non-Null returning handler once
Assert.Equal(1, _numNonNullResolutions);
slc.Resolving -= ResolveAssembly;
// Reset the non-Null resolution counter
_numNonNullResolutions = 0;
// Now, wireup the Resolving event of default context to locate the assembly via multiple handlers
AssemblyLoadContext.Default.Resolving += ResolveNullAssembly;
AssemblyLoadContext.Default.Resolving += ResolveAssembly;
AssemblyLoadContext.Default.Resolving += ResolveAssemblyAgain;
// This will invoke the resolution via VM requiring to bind using the TPA binder
var assemblyExpectedFromLoad = Assembly.Load(assemblyName);
// We should have successfully loaded the assembly in default context.
Assert.NotNull(assemblyExpectedFromLoad);
Assert.Contains(assemblyExpectedFromLoad, AssemblyLoadContext.Default.Assemblies);
// We should have only invoked non-Null returning handler once
Assert.Equal(1, _numNonNullResolutions);
// And make sure the simple name matches
Assert.Equal(TestAssemblyName, assemblyExpectedFromLoad.GetName().Name);
// The assembly loaded in DefaultContext should have a different reference from the one in secondary load context
Assert.NotEqual(slcLoadedAssembly, assemblyExpectedFromLoad);
// Reset the non-Null resolution counter
_numNonNullResolutions = 0;
// Since the assembly is already loaded in TPA Binder, we will get that back without invoking any Resolving event handlers
var assemblyExpected = AssemblyLoadContext.Default.LoadFromAssemblyName(assemblyName);
Assert.Equal(0, _numNonNullResolutions);
// We should have successfully loaded the assembly in default context.
Assert.NotNull(assemblyExpected);
// What we got via Assembly.Load and LoadFromAssemblyName should be the same
Assert.Equal(assemblyExpected, assemblyExpectedFromLoad);
// And make sure the simple name matches
Assert.Equal(TestAssemblyName, assemblyExpected.GetName().Name);
// Unwire the Resolving event.
AssemblyLoadContext.Default.Resolving -= ResolveAssemblyAgain;
AssemblyLoadContext.Default.Resolving -= ResolveAssembly;
AssemblyLoadContext.Default.Resolving -= ResolveNullAssembly;
// Unwire the Resolving event and attempt to load the assembly again. This time
// it should be found in the Default Load Context.
var assemblyLoaded = Assembly.Load(new AssemblyName(TestAssemblyName));
// We should have successfully found the assembly in default context.
Assert.NotNull(assemblyLoaded);
// Ensure that we got the same assembly reference back.
Assert.Equal(assemblyExpected, assemblyLoaded);
// Run tests for binding from DefaultContext when custom load context does not have TPA overrides.
DefaultContextFallback();
// Run tests for overriding DefaultContext when custom load context has TPA overrides.
DefaultContextOverrideTPA();
}
[Fact]
public static void LoadNonExistentInDefaultContext()
{
// Now, try to load an assembly that does not exist
Assert.Throws<FileNotFoundException>(() => AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName("System.Runtime.Loader.NonExistent.Assembly")));
}
private void DefaultContextFallback()
{
var lcDefault = AssemblyLoadContext.Default;
// Load the assembly in custom load context
FallbackLoadContext flc = new FallbackLoadContext();
var asmTargetAsm = flc.LoadFromAssemblyPath(_assemblyPath);
var loadedContext = AssemblyLoadContext.GetLoadContext(asmTargetAsm);
// LoadContext of the assembly should be the custom context and not DefaultContext
Assert.NotEqual(lcDefault, flc);
Assert.Equal(flc, loadedContext);
// Get reference to the helper method that will load assemblies (actually, resolve them)
// from DefaultContext
Type type = asmTargetAsm.GetType("System.Runtime.Loader.Tests.TestClass");
var method = System.Reflection.TypeExtensions.GetMethod(type, "LoadFromDefaultContext");
// Load System.Runtime - since this is on TPA, it should get resolved from DefaultContext
// since FallbackLoadContext does not override the Load method to specify its location.
var assemblyName = "System.Runtime, Version=4.0.0.0";
Assembly asmLoaded = (Assembly)method.Invoke(null, new object[] {assemblyName});
loadedContext = AssemblyLoadContext.GetLoadContext(asmLoaded);
// Confirm assembly Loaded from DefaultContext
Assert.Equal(lcDefault, loadedContext);
Assert.NotEqual(flc, loadedContext);
// Now, do the same from an assembly that we explicitly had loaded in DefaultContext
// in the caller of this method. We should get it from FallbackLoadContext since we
// explicitly loaded it there as well.
assemblyName = TestAssemblyName;
Assembly asmLoaded2 = (Assembly)method.Invoke(null, new object[] {assemblyName});
loadedContext = AssemblyLoadContext.GetLoadContext(asmLoaded2);
Assert.NotEqual(lcDefault, loadedContext);
Assert.Equal(flc, loadedContext);
// Attempt to bind an assembly that has not been loaded in DefaultContext and is not
// present on TPA as well. Such an assembly will not trigger a load since we only consult
// the DefaultContext cache (including assemblies on TPA list) in an attempt to bind.
assemblyName = "System.Runtime.Loader.Noop.Assembly.NonExistent";
Exception ex = null;
try
{
method.Invoke(null, new object[] {assemblyName});
}
catch (TargetInvocationException tie)
{
ex = tie.InnerException;
}
Assert.Equal(typeof(FileNotFoundException), ex.GetType());
}
private void DefaultContextOverrideTPA()
{
var lcDefault = AssemblyLoadContext.Default;
// Load the assembly in custom load context
OverrideDefaultLoadContext olc = new OverrideDefaultLoadContext();
var asmTargetAsm = olc.LoadFromAssemblyPath(_assemblyPath);
var loadedContext = AssemblyLoadContext.GetLoadContext(asmTargetAsm);
olc.LoadedFromContext = false;
// LoadContext of the assembly should be the custom context and not DefaultContext
Assert.NotEqual(lcDefault, olc);
Assert.Equal(olc, loadedContext);
// Get reference to the helper method that will load assemblies (actually, resolve them)
// from DefaultContext
Type type = asmTargetAsm.GetType("System.Runtime.Loader.Tests.TestClass", true);
var method = System.Reflection.TypeExtensions.GetMethod(type, "LoadFromDefaultContext");
// Load System.Runtime - since this is on TPA, it should get resolved from our custom load context
// since the Load method has been implemented to override TPA assemblies.
var assemblyName = "System.Runtime, Version=4.0.0.0";
Assembly asmLoaded = (Assembly)method.Invoke(null, new object[] {assemblyName});
loadedContext = AssemblyLoadContext.GetLoadContext(asmLoaded);
// Confirm assembly did not load from DefaultContext
Assert.NotEqual(lcDefault, loadedContext);
Assert.Equal(olc, loadedContext);
Assert.True(olc.LoadedFromContext);
// Now, do the same for an assembly that we explicitly had loaded in DefaultContext
// in the caller of this method and ALSO loaded in the current load context. We should get it from our LoadContext,
// without invoking the Load override, since it is already loaded.
assemblyName = TestAssemblyName;
olc.LoadedFromContext = false;
Assembly asmLoaded2 = (Assembly)method.Invoke(null, new object[] {assemblyName});
loadedContext = AssemblyLoadContext.GetLoadContext(asmLoaded2);
// Confirm assembly loaded from the intended LoadContext
Assert.NotEqual(lcDefault, loadedContext);
Assert.Equal(olc, loadedContext);
Assert.False(olc.LoadedFromContext);
}
}
}