Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Prev Previous commit
Next Next commit
(#91958) Process: move OSX-specific tests to a separate file
  • Loading branch information
ForNeVeR authored and jeffhandley committed Mar 22, 2024
commit eaa1b6350ea4df942c8ffec71df49542ab10055e
56 changes: 56 additions & 0 deletions src/libraries/System.Diagnostics.Process/tests/ProcessTests.OSX.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Security;
using Xunit;
using Microsoft.DotNet.RemoteExecutor;
using Microsoft.DotNet.XUnitExtensions;

namespace System.Diagnostics.Tests
{
public partial class ProcessTests : ProcessTestBase
{
[Fact]
[PlatformSpecific(TestPlatforms.OSX)]
public unsafe void TestTotalProcessorTimeMacOs()
{
var rUsage = Interop.libproc.proc_pid_rusage(Environment.ProcessId);
var timeBase = new Interop.libSystem.mach_timebase_info_data_t();
Interop.libSystem.mach_timebase_info(&timeBase);

var nativeUserNs = rUsage.ri_user_time * timeBase.numer / timeBase.denom;
var nativeSystemNs = rUsage.ri_system_time * timeBase.numer / timeBase.denom;
var nativeTotalNs = nativeSystemNs + nativeUserNs;

var nativeUserTime = TimeSpan.FromMicroseconds(nativeUserNs / 1000);
var nativeSystemTime = TimeSpan.FromMicroseconds(nativeSystemNs / 1000);
var nativeTotalTime = TimeSpan.FromMicroseconds(nativeTotalNs / 1000);

var process = Process.GetCurrentProcess();
var managedUserTime = process.UserProcessorTime;
var managedSystemTime = process.PrivilegedProcessorTime;
var managedTotalTime = process.TotalProcessorTime;

AssertTime(managedUserTime, nativeUserTime, "user");
AssertTime(managedSystemTime, nativeSystemTime, "system");
AssertTime(managedTotalTime, nativeTotalTime, "total");

void AssertTime(TimeSpan managed, TimeSpan native, string label)
{
Assert.True(
managed >= native,
$"Time '{label}' returned by managed API ({managed}) should be greated or equal to the time returned by native API ({native}).");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -851,39 +851,6 @@ static void ExecuteChildProcess(string filename, string flags)
}
}

[Fact]
[PlatformSpecific(TestPlatforms.OSX)]
public unsafe void TestTotalProcessorTimeMacOs()
{
var rUsage = Interop.libproc.proc_pid_rusage(Environment.ProcessId);
var timeBase = new Interop.libSystem.mach_timebase_info_data_t();
Interop.libSystem.mach_timebase_info(&timeBase);

var nativeUserNs = rUsage.ri_user_time * timeBase.numer / timeBase.denom;
var nativeSystemNs = rUsage.ri_system_time * timeBase.numer / timeBase.denom;
var nativeTotalNs = nativeSystemNs + nativeUserNs;

var nativeUserTime = TimeSpan.FromMicroseconds(nativeUserNs / 1000);
var nativeSystemTime = TimeSpan.FromMicroseconds(nativeSystemNs / 1000);
var nativeTotalTime = TimeSpan.FromMicroseconds(nativeTotalNs / 1000);

var process = Process.GetCurrentProcess();
var managedUserTime = process.UserProcessorTime;
var managedSystemTime = process.PrivilegedProcessorTime;
var managedTotalTime = process.TotalProcessorTime;

AssertTime(managedUserTime, nativeUserTime, "user");
AssertTime(managedSystemTime, nativeSystemTime, "system");
AssertTime(managedTotalTime, nativeTotalTime, "total");

void AssertTime(TimeSpan managed, TimeSpan native, string label)
{
Assert.True(
managed >= native,
$"Time '{label}' returned by managed API ({managed}) should be greated or equal to the time returned by native API ({native}).");
}
}

[ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[InlineData(true)]
[InlineData(false)]
Expand Down Expand Up @@ -1049,13 +1016,3 @@ private static string StartAndReadToEnd(string filename, string[] arguments)
}
}
}

// TODO: Use the actual localization data in here?
namespace System
{
internal static partial class SR
{
public const string CantGetAllPids = nameof(CantGetAllPids);
public const string RUsageFailure = nameof(RUsageFailure);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-unix;$(NetCoreAppCurrent)-browser</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-unix;$(NetCoreAppCurrent)-browser;$(NetCoreAppCurrent)-osx</TargetFrameworks>
<IgnoreForCI Condition="'$(TargetOS)' == 'browser'">true</IgnoreForCI>
<EnableLibraryImportGenerator>true</EnableLibraryImportGenerator>
<StringResourcesPath>$(MSBuildProjectDirectory)\..\src\Resources\Strings.resx</StringResourcesPath>
</PropertyGroup>
<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
<PropertyGroup>
Expand Down Expand Up @@ -57,12 +58,15 @@
<!-- Helpers -->
<Compile Include="$(CommonTestPath)TestUtilities\System\WindowsTestFileShare.cs" Link="Common\TestUtilities\System\WindowsTestFileShare.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'unix' or '$(TargetPlatformIdentifier)' == 'browser'">
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'unix' or '$(TargetPlatformIdentifier)' == 'browser' or '$(TargetPlatformIdentifier)' == 'osx'">
<Compile Include="Interop.Unix.cs" />
<Compile Include="ProcessTests.Unix.cs" />
<Compile Include="ProcessThreadTests.Unix.cs" />
<Compile Include="$(CoreLibSharedDir)System\PasteArguments.Unix.cs"
Link="System\PasteArguments.Unix.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'osx'">
<Compile Include="ProcessTests.OSX.cs" />
<Compile Include="$(CommonPath)Interop\OSX\Interop.Libraries.cs"
Link="Common\Interop\OSX\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Interop\OSX\Interop.libproc.cs"
Expand Down