From 3029b0039c2419e4f8f73225fa71af112c8b2755 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Thu, 10 Oct 2019 16:15:37 -0700 Subject: [PATCH 1/4] Add test for the handler leaking fix --- test/xUnit/csharp/test_Runspace.cs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/test/xUnit/csharp/test_Runspace.cs b/test/xUnit/csharp/test_Runspace.cs index 2042ee7d6f8..7cecc10cd43 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,27 @@ public void TestRunspaceWithPowerShellAndInitialSessionState() runspace.Close(); } } + + [Fact] + public void TestAppDomainProcessExitEvenHandlerNotLeaking() + { + EventHandler eventHandler; + Delegate[] delegates; + FieldInfo field = typeof(AppContext).GetField("ProcessExit", BindingFlags.NonPublic | BindingFlags.Static); + + // Open runspace and invoke script twice. + 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"); + } } } From 547584f41816a6a1458b71e37ed0577649d05443 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Thu, 10 Oct 2019 16:17:52 -0700 Subject: [PATCH 2/4] Fix comment --- test/xUnit/csharp/test_Runspace.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/xUnit/csharp/test_Runspace.cs b/test/xUnit/csharp/test_Runspace.cs index 7cecc10cd43..dd49c6f9037 100644 --- a/test/xUnit/csharp/test_Runspace.cs +++ b/test/xUnit/csharp/test_Runspace.cs @@ -109,7 +109,7 @@ public void TestAppDomainProcessExitEvenHandlerNotLeaking() Delegate[] delegates; FieldInfo field = typeof(AppContext).GetField("ProcessExit", BindingFlags.NonPublic | BindingFlags.Static); - // Open runspace and invoke script twice. + // Open runspace and invoke script. using (var ps = PowerShell.Create()) { ps.AddScript("1").Invoke(); From 6bc7e2ac732be8d08da1f8ddd9a47447c211a725 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Thu, 10 Oct 2019 16:20:06 -0700 Subject: [PATCH 3/4] Fix CodeFactor issues --- test/xUnit/csharp/test_Runspace.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/xUnit/csharp/test_Runspace.cs b/test/xUnit/csharp/test_Runspace.cs index dd49c6f9037..bfb122a0357 100644 --- a/test/xUnit/csharp/test_Runspace.cs +++ b/test/xUnit/csharp/test_Runspace.cs @@ -113,13 +113,13 @@ public void TestAppDomainProcessExitEvenHandlerNotLeaking() using (var ps = PowerShell.Create()) { ps.AddScript("1").Invoke(); - eventHandler = (EventHandler) field.GetValue(null); + 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); + eventHandler = (EventHandler)field.GetValue(null); delegates = eventHandler.GetInvocationList(); Assert.DoesNotContain(delegates, d => d.Method.Name == "CurrentDomain_ProcessExit"); } From 0ab5c5ad6ae735504b134bfce561c904a3dee051 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Thu, 10 Oct 2019 16:39:47 -0700 Subject: [PATCH 4/4] Skip the test on non-windows --- test/xUnit/csharp/test_Runspace.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/xUnit/csharp/test_Runspace.cs b/test/xUnit/csharp/test_Runspace.cs index bfb122a0357..c0239e7db3f 100644 --- a/test/xUnit/csharp/test_Runspace.cs +++ b/test/xUnit/csharp/test_Runspace.cs @@ -102,9 +102,11 @@ public void TestRunspaceWithPowerShellAndInitialSessionState() } } - [Fact] + [SkippableFact] public void TestAppDomainProcessExitEvenHandlerNotLeaking() { + Skip.IfNot(Platform.IsWindows); + EventHandler eventHandler; Delegate[] delegates; FieldInfo field = typeof(AppContext).GetField("ProcessExit", BindingFlags.NonPublic | BindingFlags.Static);