Add test for the even handler leaking fix (#10768)

Add a test for the even handler leaking fix addressed in #10626 https://github.com/PowerShell/PowerShell/pull/10626
The test is added to verify that PowerShell unregister the 'AppDomain.ProcessExit' event handler when a Runspace closes.
This commit is contained in:
Dongbo Wang
2019-10-11 23:34:53 +05:00
committed by Ilya
parent 1fa73e360f
commit 60a930c9ba
+25 -1
View File
@@ -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 @@ namespace PSTests.Sequential
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");
}
}
}