diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessThreadTests.Unix.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessThreadTests.Unix.cs index 2a0f26c1a7a3d..d7ce1abcdb556 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessThreadTests.Unix.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessThreadTests.Unix.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.ComponentModel; +using System.Runtime.InteropServices; using System.Threading; using Microsoft.DotNet.RemoteExecutor; using Xunit; @@ -30,5 +31,26 @@ public void TestPriorityLevelProperty_Unix() Assert.Throws(() => thread.PriorityLevel = level); } + + private static int GetCurrentThreadId() + { + // The magic values come from https://github.com/torvalds/linux. + int SYS_gettid = RuntimeInformation.ProcessArchitecture switch + { + Architecture.Arm => 224, + Architecture.Arm64 => 178, + Architecture.X86 => 224, + Architecture.X64 => 186, + Architecture.S390x => 236, + Architecture.Ppc64le => 207, + Architecture.RiscV64 => 178, + _ => 178, + }; + + return syscall(SYS_gettid); + } + + [DllImport("libc")] + private static extern int syscall(int nr); } } diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessThreadTests.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessThreadTests.cs index 6db4fd1a2cdee..31e07542aac42 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessThreadTests.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessThreadTests.cs @@ -7,6 +7,7 @@ using Microsoft.DotNet.RemoteExecutor; using Xunit; using System.Threading.Tasks; +using System.Runtime.InteropServices; namespace System.Diagnostics.Tests { @@ -107,8 +108,8 @@ public void TestStartTimeProperty_OSX() } } - [Fact] - [PlatformSpecific(TestPlatforms.Linux|TestPlatforms.Windows)] // OSX and FreeBSD throw PNSE from StartTime + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] + [PlatformSpecific(TestPlatforms.Linux | TestPlatforms.Windows)] // OSX and FreeBSD throw PNSE from StartTime public async Task TestStartTimeProperty() { TimeSpan allowedWindow = TimeSpan.FromSeconds(2); @@ -148,15 +149,13 @@ public async Task TestStartTimeProperty() await Task.Factory.StartNew(() => { p.Refresh(); - try - { - var newest = p.Threads.Cast().OrderBy(t => t.StartTime.ToUniversalTime()).Last(); - Assert.InRange(newest.StartTime.ToUniversalTime(), curTime - allowedWindow, DateTime.Now.ToUniversalTime() + allowedWindow); - } - catch (InvalidOperationException) - { - // A thread may have gone away between our getting its info and attempting to access its StartTime - } + + int newThreadId = GetCurrentThreadId(); + + ProcessThread[] processThreads = p.Threads.Cast().ToArray(); + ProcessThread newThread = Assert.Single(processThreads, thread => thread.Id == newThreadId); + + Assert.InRange(newThread.StartTime.ToUniversalTime(), curTime - allowedWindow, DateTime.Now.ToUniversalTime() + allowedWindow); }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); } }