diff --git a/test/xUnit/csharp/test_Runspace.cs b/test/xUnit/csharp/test_Runspace.cs index 2042ee7d6f8..c0239e7db3f 100644 --- a/test/xUnit/csharp/test_Runspace.cs +++ b/test/xUnit/csharp/test_Runspace.cs @@ -2,9 +2,9 @@ // Licensed under the MIT License. using System; -using System.Collections.Generic; using System.Management.Automation; using System.Management.Automation.Runspaces; +using System.Reflection; using Xunit; namespace PSTests.Sequential @@ -101,5 +101,29 @@ public void TestRunspaceWithPowerShellAndInitialSessionState() runspace.Close(); } } + + [SkippableFact] + public void TestAppDomainProcessExitEvenHandlerNotLeaking() + { + Skip.IfNot(Platform.IsWindows); + + EventHandler eventHandler; + Delegate[] delegates; + FieldInfo field = typeof(AppContext).GetField("ProcessExit", BindingFlags.NonPublic | BindingFlags.Static); + + // Open runspace and invoke script. + using (var ps = PowerShell.Create()) + { + ps.AddScript("1").Invoke(); + eventHandler = (EventHandler)field.GetValue(null); + delegates = eventHandler.GetInvocationList(); + Assert.Contains(delegates, d => d.Method.Name == "CurrentDomain_ProcessExit"); + } + + // Handler registered by PowerShell should be unregistered. + eventHandler = (EventHandler)field.GetValue(null); + delegates = eventHandler.GetInvocationList(); + Assert.DoesNotContain(delegates, d => d.Method.Name == "CurrentDomain_ProcessExit"); + } } }