Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Prev Previous commit
Next Next commit
Add COM unloadability tests
Add two tests to test COM unloadability:
* One for using native COM server from managed COM client
* One for using managed COM objects from native client
  • Loading branch information
janvorli committed Nov 29, 2018
commit b379ecbc700bbd3c851bc1c27477a51e9bbdfd5d
60 changes: 60 additions & 0 deletions tests/src/Common/CoreCLRTestLibrary/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using System.Security;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -303,5 +304,64 @@ public static extern int RegGetValueW(

public static IntPtr HKEY_LOCAL_MACHINE => new IntPtr(unchecked((int)0x80000002));
}

class TestAssemblyLoadContext : AssemblyLoadContext
{
public TestAssemblyLoadContext() : base(isCollectible: true)
{

}

protected override Assembly Load(AssemblyName assemblyName)
{
return null;
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
static int ExecuteAndUnloadInternal(string assemblyPath, string[] args, Action<AssemblyLoadContext> unloadingCallback, out WeakReference alcWeakRef)
{
TestAssemblyLoadContext alc = new TestAssemblyLoadContext();
if (unloadingCallback != null)
{
alc.Unloading += unloadingCallback;
}
alcWeakRef = new WeakReference(alc);

Assembly a = alc.LoadFromAssemblyPath(assemblyPath);

object[] argsObjArray = (a.EntryPoint.GetParameters().Length != 0) ? new object[] { args } : null;
object res = a.EntryPoint.Invoke(null, argsObjArray);

alc.Unload();

return (a.EntryPoint.ReturnType == typeof(void)) ? Environment.ExitCode : Convert.ToInt32(res);
}

public static int ExecuteAndUnload(string assemblyPath, string[] args, Action<AssemblyLoadContext> unloadingCallback = null)
{
WeakReference alcWeakRef;
int exitCode;

exitCode = ExecuteAndUnloadInternal(assemblyPath, args, unloadingCallback, out alcWeakRef);

for (int i = 0; i < 8 && alcWeakRef.IsAlive; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}

if (alcWeakRef.IsAlive)
{
exitCode += 100;
Console.WriteLine("Unload failed");
}
else
{
Console.WriteLine("Unload succeeded");
}

return exitCode;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<AssemblyName>NETClientPrimitivesInALC</AssemblyName>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{85C57688-DA98-4DE3-AC9B-526E4747434C}</ProjectGuid>
<OutputType>Exe</OutputType>
<ProjectTypeGuids>{209912F9-0DA1-4184-9CC1-8D583BAF4A28};{87799F5D-CEBD-499D-BDBA-B2C6105CD766}</ProjectTypeGuids>
<ApplicationManifest>App.manifest</ApplicationManifest>

<!-- Blocked on ILAsm supporting embedding resources. See https://github.com/dotnet/coreclr/issues/20819 -->
<IlrtTestKind>BuildOnly</IlrtTestKind>

<!-- Blocked on CrossGen.exe supporting embedding resources. See https://github.com/dotnet/coreclr/issues/21006 -->
<CrossGenTest>false</CrossGenTest>

<!-- Test unsupported outside of windows -->
<TestUnsupportedOutsideWindows>true</TestUnsupportedOutsideWindows>
<DisableProjectBuild Condition="'$(TargetsUnix)' == 'true'">true</DisableProjectBuild>
</PropertyGroup>
<!-- Default configurations to help VS understand the configurations -->
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
</PropertyGroup>
<ItemGroup>
<Compile Include="TestInALC.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../../../Common/CoreCLRTestLibrary/CoreCLRTestLibrary.csproj" />
<ProjectReference Include="NetClientPrimitives.csproj" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
19 changes: 19 additions & 0 deletions tests/src/Interop/COM/NETClients/Primitives/TestInALC.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.Reflection;

namespace TestInALC
{
class Test
{
static int Main(string[] args)
{
string currentAssemblyDirectory = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath);
string testAssemblyFullPath = Path.Combine(currentAssemblyDirectory, "NETClientPrimitives.exe");
return TestLibrary.Utilities.ExecuteAndUnload(testAssemblyFullPath, args);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<AssemblyName>DefaultTestInALC</AssemblyName>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{43531C46-AFE2-4254-93C6-7F17E30D750C}</ProjectGuid>
<OutputType>Exe</OutputType>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\..\</SolutionDir>
<DefineConstants>$(DefineConstants);STATIC</DefineConstants>
<!-- Test unsupported outside of windows -->
<TestUnsupportedOutsideWindows>true</TestUnsupportedOutsideWindows>
<DisableProjectBuild Condition="'$(TargetsUnix)' == 'true'">true</DisableProjectBuild>
</PropertyGroup>
<!-- Default configurations to help VS understand the configurations -->
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"></PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"></PropertyGroup>
<ItemGroup>
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
<Visible>False</Visible>
</CodeAnalysisDependentAssemblyPaths>
</ItemGroup>
<ItemGroup>
<Compile Include="TestInALC.cs" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CMakeLists.txt" />
<ProjectReference Include="DefaultTest.csproj" />
</ItemGroup>
<Import Project="../../../Interop.settings.targets" />
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.Reflection;

namespace TestInALC
{
class Test
{
static int Main(string[] args)
{
string currentAssemblyDirectory = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath);
string testAssemblyFullPath = Path.Combine(currentAssemblyDirectory, "DefaultTest.exe");
return TestLibrary.Utilities.ExecuteAndUnload(testAssemblyFullPath, args);
}
}
}