From 607824421bfdbe3eef2bfb756e8aaf4321caffc4 Mon Sep 17 00:00:00 2001 From: Steve MacLean Date: Thu, 28 Mar 2019 17:09:50 -0400 Subject: [PATCH] Amend ALC tests Add simple assertions to test: Test new public constructor Test instantiating AssemblyLoadContext as a concrete type Test All Test Name Test ToString Test Assemblies Commit migrated from https://github.com/dotnet/corefx/commit/3db3261f39945567d818b10a562acf86d334e07a --- .../System.Reflection/tests/AssemblyTests.cs | 11 ++++- .../tests/AssemblyLoadContextTest.cs | 44 ++++++++++++++++++++ .../tests/DefaultContext/DefaultLoadContextTest.cs | 10 +++-- .../tests/System/Reflection/IsCollectibleTests.cs | 47 ++++++++++++++++++++-- 4 files changed, 104 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Reflection/tests/AssemblyTests.cs b/src/libraries/System.Reflection/tests/AssemblyTests.cs index db2a25a..f394ca5 100644 --- a/src/libraries/System.Reflection/tests/AssemblyTests.cs +++ b/src/libraries/System.Reflection/tests/AssemblyTests.cs @@ -318,6 +318,15 @@ namespace System.Reflection.Tests else { Assert.NotEqual(currentAssembly, loadedAssembly1); + +#if netcoreapp + System.Runtime.Loader.AssemblyLoadContext alc = System.Runtime.Loader.AssemblyLoadContext.GetLoadContext(loadedAssembly1); + string expectedName = string.Format("Assembly.LoadFile({0})", fullRuntimeTestsPath); + Assert.Equal(expectedName, alc.Name); + Assert.Contains(fullRuntimeTestsPath, alc.Name); + Assert.Contains(expectedName, alc.ToString()); + Assert.Contains("System.Runtime.Loader.IndividualAssemblyLoadContext", alc.ToString()); +#endif } string dir = Path.GetDirectoryName(fullRuntimeTestsPath); @@ -827,4 +836,4 @@ namespace System.Reflection.Tests } } -internal class G { } \ No newline at end of file +internal class G { } diff --git a/src/libraries/System.Runtime.Loader/tests/AssemblyLoadContextTest.cs b/src/libraries/System.Runtime.Loader/tests/AssemblyLoadContextTest.cs index 01cdf0d..330c0e9 100644 --- a/src/libraries/System.Runtime.Loader/tests/AssemblyLoadContextTest.cs +++ b/src/libraries/System.Runtime.Loader/tests/AssemblyLoadContextTest.cs @@ -133,5 +133,49 @@ namespace System.Runtime.Loader.Tests Assert.NotNull(context); Assert.Same(AssemblyLoadContext.Default, context); } + + [Fact] + public static void DefaultAssemblyLoadContext_Properties() + { + AssemblyLoadContext alc = AssemblyLoadContext.Default; + + Assert.False(alc.IsCollectible); + + Assert.Equal("Default", alc.Name); + Assert.Contains("\"Default\"", alc.ToString()); + Assert.Contains("System.Runtime.Loader.DefaultAssemblyLoadContext", alc.ToString()); + Assert.Contains(alc, AssemblyLoadContext.All); + Assert.Contains(Assembly.GetCallingAssembly(), alc.Assemblies); + } + + [Fact] + public static void PublicConstructor_Default() + { + AssemblyLoadContext alc = new AssemblyLoadContext("PublicConstructor"); + + Assert.False(alc.IsCollectible); + + Assert.Equal("PublicConstructor", alc.Name); + Assert.Contains("PublicConstructor", alc.ToString()); + Assert.Contains("System.Runtime.Loader.AssemblyLoadContext", alc.ToString()); + Assert.Contains(alc, AssemblyLoadContext.All); + Assert.Empty(alc.Assemblies); + } + + [Theory] + [InlineData("AssemblyLoadContextCollectible", true)] + [InlineData("AssemblyLoadContextNonCollectible", false)] + public static void PublicConstructor_Theory(string name, bool isCollectible) + { + AssemblyLoadContext alc = new AssemblyLoadContext(name, isCollectible); + + Assert.Equal(isCollectible, alc.IsCollectible); + + Assert.Equal(name, alc.Name); + Assert.Contains(name, alc.ToString()); + Assert.Contains("System.Runtime.Loader.AssemblyLoadContext", alc.ToString()); + Assert.Contains(alc, AssemblyLoadContext.All); + Assert.Empty(alc.Assemblies); + } } } diff --git a/src/libraries/System.Runtime.Loader/tests/DefaultContext/DefaultLoadContextTest.cs b/src/libraries/System.Runtime.Loader/tests/DefaultContext/DefaultLoadContextTest.cs index 16e4c38..13aae6f 100644 --- a/src/libraries/System.Runtime.Loader/tests/DefaultContext/DefaultLoadContextTest.cs +++ b/src/libraries/System.Runtime.Loader/tests/DefaultContext/DefaultLoadContextTest.cs @@ -96,12 +96,15 @@ namespace System.Runtime.Loader.Tests // 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); @@ -118,12 +121,13 @@ namespace System.Runtime.Loader.Tests 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); diff --git a/src/libraries/System.Runtime/tests/System/Reflection/IsCollectibleTests.cs b/src/libraries/System.Runtime/tests/System/Reflection/IsCollectibleTests.cs index f73ee1e..d219bfa 100644 --- a/src/libraries/System.Runtime/tests/System/Reflection/IsCollectibleTests.cs +++ b/src/libraries/System.Runtime/tests/System/Reflection/IsCollectibleTests.cs @@ -42,15 +42,47 @@ namespace System.Reflection.Tests Assembly asm = Assembly.LoadFrom(asmPath); Assert.NotNull(asm); - + Assert.False(asm.IsCollectible); + AssemblyLoadContext alc = AssemblyLoadContext.GetLoadContext(asm); + Assert.False(alc.IsCollectible); + Assert.Equal(AssemblyLoadContext.Default, alc); + Assert.Equal("Default", alc.Name); + Assert.Contains("\"Default\"", alc.ToString()); + Assert.Contains("System.Runtime.Loader.DefaultAssemblyLoadContext", alc.ToString()); + Assert.Contains(alc, AssemblyLoadContext.All); + Assert.Contains(asm, alc.Assemblies); + return SuccessExitCode; }).Dispose(); } [Fact] - public void Assembly_IsCollectibleTrue_WhenUsingAssemblyLoadContext() + public void Assembly_IsCollectibleFalse_WhenUsingAssemblyLoadContext() + { + RemoteInvoke(() => { + AssemblyLoadContext alc = new AssemblyLoadContext("Assembly_IsCollectibleFalse_WhenUsingAssemblyLoadContext"); + + Assembly asm = alc.LoadFromAssemblyPath(asmPath); + + Assert.NotNull(asm); + + Assert.False(asm.IsCollectible); + Assert.False(alc.IsCollectible); + + Assert.Equal("Assembly_IsCollectibleFalse_WhenUsingAssemblyLoadContext", alc.Name); + Assert.Contains("Assembly_IsCollectibleFalse_WhenUsingAssemblyLoadContext", alc.ToString()); + Assert.Contains("System.Runtime.Loader.AssemblyLoadContext", alc.ToString()); + Assert.Contains(alc, AssemblyLoadContext.All); + Assert.Contains(asm, alc.Assemblies); + + return SuccessExitCode; + }).Dispose(); + } + + [Fact] + public void Assembly_IsCollectibleTrue_WhenUsingTestAssemblyLoadContext() { RemoteInvoke(() => { AssemblyLoadContext alc = new TestAssemblyLoadContext(); @@ -58,8 +90,15 @@ namespace System.Reflection.Tests Assembly asm = alc.LoadFromAssemblyPath(asmPath); Assert.NotNull(asm); - + Assert.True(asm.IsCollectible); + Assert.True(alc.IsCollectible); + + Assert.Null(alc.Name); + Assert.Contains("\"\"", alc.ToString()); + Assert.Contains("System.Reflection.Tests.TestAssemblyLoadContext", alc.ToString()); + Assert.Contains(alc, AssemblyLoadContext.All); + Assert.Contains(asm, alc.Assemblies); return SuccessExitCode; }).Dispose(); @@ -215,4 +254,4 @@ namespace System.Reflection.Tests }).Dispose(); } } -} \ No newline at end of file +} -- 2.7.4