Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion test/xUnit/csharp/test_Runspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// Licensed under the MIT License.
Comment thread
daxian-dbw marked this conversation as resolved.

using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Reflection;
using Xunit;

namespace PSTests.Sequential
Expand Down Expand Up @@ -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");
}
}
}