forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Address flakey behavior testing COM lifetime cleanup. (dotnet#49266)
* Address flakey behavior testing COM lifetime cleanup. * Add new lifetime test for COM.
- Loading branch information
1 parent
4e2491d
commit 6a95503
Showing
13 changed files
with
241 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"> | ||
<assemblyIdentity | ||
type="win32" | ||
name="NetClientLifetime" | ||
version="1.0.0.0" /> | ||
|
||
<dependency> | ||
<dependentAssembly> | ||
<!-- RegFree COM --> | ||
<assemblyIdentity | ||
type="win32" | ||
name="COMNativeServer.X" | ||
version="1.0.0.0"/> | ||
</dependentAssembly> | ||
</dependency> | ||
|
||
</assembly> |
17 changes: 17 additions & 0 deletions
17
src/tests/Interop/COM/NETClients/Lifetime/NETClientLifetime.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<ApplicationManifest>App.manifest</ApplicationManifest> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="Program.cs" /> | ||
<Compile Include="../../ServerContracts/Server.CoClasses.cs" /> | ||
<Compile Include="../../ServerContracts/Server.Contracts.cs" /> | ||
<Compile Include="../../ServerContracts/ServerGuids.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="../../NativeServer/CMakeLists.txt" /> | ||
<ProjectReference Include="$(TestSourceDir)Common/CoreCLRTestLibrary/CoreCLRTestLibrary.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace NetClient | ||
{ | ||
using System; | ||
using System.Threading; | ||
using System.Runtime.InteropServices; | ||
|
||
using TestLibrary; | ||
using Server.Contract; | ||
using Server.Contract.Servers; | ||
|
||
unsafe class Program | ||
{ | ||
static delegate* unmanaged<int> GetAllocationCount; | ||
|
||
// Initialize for all tests | ||
static void Initialize() | ||
{ | ||
var inst = new TrackMyLifetimeTesting(); | ||
GetAllocationCount = (delegate* unmanaged<int>)inst.GetAllocationCountCallback(); | ||
} | ||
|
||
static int AllocateInstances(int a) | ||
{ | ||
var insts = new object[a]; | ||
for (int i = 0; i < a; ++i) | ||
{ | ||
insts[i] = new TrackMyLifetimeTesting(); | ||
} | ||
return a; | ||
} | ||
|
||
static void ForceGC() | ||
{ | ||
for (int i = 0; i < 3; ++i) | ||
{ | ||
GC.Collect(); | ||
GC.WaitForPendingFinalizers(); | ||
} | ||
} | ||
|
||
static void Validate_COMServer_CleanUp() | ||
{ | ||
Console.WriteLine($"Calling {nameof(Validate_COMServer_CleanUp)}..."); | ||
|
||
int allocated = 0; | ||
allocated += AllocateInstances(1); | ||
allocated += AllocateInstances(2); | ||
allocated += AllocateInstances(3); | ||
Assert.AreNotEqual(0, GetAllocationCount()); | ||
|
||
ForceGC(); | ||
|
||
Assert.AreEqual(0, GetAllocationCount()); | ||
} | ||
|
||
static void Validate_COMServer_DisableEagerCleanUp() | ||
{ | ||
Console.WriteLine($"Calling {nameof(Validate_COMServer_DisableEagerCleanUp)}..."); | ||
Assert.AreEqual(0, GetAllocationCount()); | ||
|
||
Thread.CurrentThread.DisableComObjectEagerCleanup(); | ||
|
||
int allocated = 0; | ||
allocated += AllocateInstances(1); | ||
allocated += AllocateInstances(2); | ||
allocated += AllocateInstances(3); | ||
Assert.AreNotEqual(0, GetAllocationCount()); | ||
|
||
ForceGC(); | ||
|
||
Assert.AreNotEqual(0, GetAllocationCount()); | ||
|
||
Marshal.CleanupUnusedObjectsInCurrentContext(); | ||
|
||
ForceGC(); | ||
|
||
Assert.AreEqual(0, GetAllocationCount()); | ||
Assert.IsFalse(Marshal.AreComObjectsAvailableForCleanup()); | ||
} | ||
|
||
[STAThread] | ||
static int Main(string[] doNotUse) | ||
{ | ||
// RegFree COM is not supported on Windows Nano | ||
if (Utilities.IsWindowsNanoServer) | ||
{ | ||
return 100; | ||
} | ||
|
||
try | ||
{ | ||
// Initialization for all future tests | ||
Initialize(); | ||
Assert.IsTrue(GetAllocationCount != null); | ||
|
||
Validate_COMServer_CleanUp(); | ||
Validate_COMServer_DisableEagerCleanUp(); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine($"Test Failure: {e}"); | ||
return 101; | ||
} | ||
|
||
return 100; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/tests/Interop/COM/NativeServer/TrackMyLifetimeTesting.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#pragma once | ||
|
||
#include "Servers.h" | ||
|
||
class TrackMyLifetimeTesting : public UnknownImpl, public ITrackMyLifetimeTesting | ||
{ | ||
static std::atomic<ULONG> _instanceCount; | ||
|
||
static ULONG GetAllocatedTypes() | ||
{ | ||
return _instanceCount; | ||
} | ||
|
||
public: | ||
TrackMyLifetimeTesting() | ||
{ | ||
_instanceCount++; | ||
} | ||
~TrackMyLifetimeTesting() | ||
{ | ||
_instanceCount--; | ||
} | ||
|
||
public: // ITrackMyLifetimeTesting | ||
DEF_FUNC(GetAllocationCountCallback)(_Outptr_ void** fptr) | ||
{ | ||
if (fptr == nullptr) | ||
return E_POINTER; | ||
|
||
*fptr = (void*)&GetAllocatedTypes; | ||
return S_OK; | ||
} | ||
|
||
public: // IUnknown | ||
STDMETHOD(QueryInterface)( | ||
/* [in] */ REFIID riid, | ||
/* [iid_is][out] */ _COM_Outptr_ void __RPC_FAR *__RPC_FAR *ppvObject) | ||
{ | ||
return DoQueryInterface(riid, ppvObject, static_cast<ITrackMyLifetimeTesting *>(this)); | ||
} | ||
|
||
DEFINE_REF_COUNTING(); | ||
}; | ||
|
||
std::atomic<ULONG> TrackMyLifetimeTesting::_instanceCount = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters