Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit fed476a

Browse files
ViktorHoferstephentoub
authored andcommitted
Letting perfproc errors pass silently (#19495)
* Letting perfproc errors pass silently * Removed registry detection for now
1 parent 515a909 commit fed476a

File tree

1 file changed

+28
-33
lines changed

1 file changed

+28
-33
lines changed

src/System.Diagnostics.Process/tests/ProcessTests.cs

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -873,12 +873,19 @@ public void GetProcessesByName_ProcessNameMachineName_ReturnsExpected(string mac
873873
Assert.All(processes, process => Assert.Equal(machineName, process.MachineName));
874874
}
875875

876-
[ConditionalTheory(nameof(ProcessPerformanceCounterEnabled))]
877876
[MemberData(nameof(MachineName_Remote_TestData))]
878877
[PlatformSpecific(TestPlatforms.Windows)] // Accessing processes on remote machines is only supported on Windows.
879878
public void GetProcessesByName_RemoteMachineNameWindows_ReturnsExpected(string machineName)
880879
{
881-
GetProcessesByName_ProcessNameMachineName_ReturnsExpected(machineName);
880+
try
881+
{
882+
GetProcessesByName_ProcessNameMachineName_ReturnsExpected(machineName);
883+
}
884+
catch (InvalidOperationException)
885+
{
886+
// As we can't detect reliably if performance counters are enabled
887+
// we let possible InvalidOperationExceptions pass silently.
888+
}
882889
}
883890

884891
[Theory]
@@ -911,46 +918,34 @@ public void GetProcessesByName_EmptyMachineName_ThrowsArgumentException()
911918
Assert.Throws<ArgumentException>(null, () => Process.GetProcessesByName(currentProcess.ProcessName, ""));
912919
}
913920

914-
public static IEnumerable<object[]> GetTestProcess()
921+
[PlatformSpecific(TestPlatforms.Windows)] // Behavior differs on Windows and Unix
922+
[SkipOnTargetFramework(TargetFrameworkMonikers.UapAot, "https://github.com/dotnet/corefx/issues/18212")]
923+
public void TestProcessOnRemoteMachineWindows()
915924
{
916-
Process currentProcess = Process.GetCurrentProcess();
917-
yield return new object[] { currentProcess, Process.GetProcessById(currentProcess.Id, "127.0.0.1") };
918-
yield return new object[] { currentProcess, Process.GetProcessesByName(currentProcess.ProcessName, "127.0.0.1").Where(p => p.Id == currentProcess.Id).Single() };
919-
}
925+
Process currentProccess = Process.GetCurrentProcess();
926+
927+
void TestRemoteProccess(Process remoteProcess)
928+
{
929+
Assert.Equal(currentProccess.Id, remoteProcess.Id);
930+
Assert.Equal(currentProccess.BasePriority, remoteProcess.BasePriority);
931+
Assert.Equal(currentProccess.EnableRaisingEvents, remoteProcess.EnableRaisingEvents);
932+
Assert.Equal("127.0.0.1", remoteProcess.MachineName);
933+
// This property throws exception only on remote processes.
934+
Assert.Throws<NotSupportedException>(() => remoteProcess.MainModule);
935+
}
920936

921-
private static bool ProcessPerformanceCounterEnabled()
922-
{
923937
try
924938
{
925-
using (Microsoft.Win32.RegistryKey perfKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\PerfProc\Performance"))
926-
{
927-
if (perfKey == null)
928-
return false;
929-
int? value = (int?)perfKey.GetValue("Disable Performance Counters", null);
930-
return !value.HasValue || value.Value == 0;
931-
}
939+
TestRemoteProccess(Process.GetProcessById(currentProccess.Id, "127.0.0.1"));
940+
TestRemoteProccess(Process.GetProcessesByName(currentProccess.ProcessName, "127.0.0.1").Where(p => p.Id == currentProccess.Id).Single());
932941
}
933-
catch (Exception)
942+
catch (InvalidOperationException)
934943
{
935-
// Ignore exceptions, and just assume the counter is disabled.
936-
return false;
944+
// As we can't detect reliably if performance counters are enabled
945+
// we let possible InvalidOperationExceptions pass silently.
937946
}
938947
}
939948

940-
[PlatformSpecific(TestPlatforms.Windows)] // Behavior differs on Windows and Unix
941-
[ConditionalTheory(nameof(ProcessPerformanceCounterEnabled))]
942-
[SkipOnTargetFramework(TargetFrameworkMonikers.UapAot, "https://github.com/dotnet/corefx/issues/18212")]
943-
[MemberData(nameof(GetTestProcess))]
944-
public void TestProcessOnRemoteMachineWindows(Process currentProcess, Process remoteProcess)
945-
{
946-
Assert.Equal(currentProcess.Id, remoteProcess.Id);
947-
Assert.Equal(currentProcess.BasePriority, remoteProcess.BasePriority);
948-
Assert.Equal(currentProcess.EnableRaisingEvents, remoteProcess.EnableRaisingEvents);
949-
Assert.Equal("127.0.0.1", remoteProcess.MachineName);
950-
// This property throws exception only on remote processes.
951-
Assert.Throws<NotSupportedException>(() => remoteProcess.MainModule);
952-
}
953-
954949
[Fact, PlatformSpecific(TestPlatforms.AnyUnix)] // Behavior differs on Windows and Unix
955950
public void TestProcessOnRemoteMachineUnix()
956951
{

0 commit comments

Comments
 (0)