diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs index a102fee93075a5..1f1c6f8678ce88 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs @@ -769,7 +769,25 @@ public void TestPeakWorkingSet64() { CreateDefaultProcess(); - AssertNonZeroAllZeroDarwin(_process.PeakWorkingSet64); + if (OperatingSystem.IsMacOS()) + { + Assert.Equal(0, _process.PeakWorkingSet64); + return; + } + + // On recent Linux kernels (6.2+) working set can be zero just after the process started. + ExecuteWithRetryOnLinux(() => + { + try + { + Assert.NotEqual(0, _process.PeakWorkingSet64); + } + catch + { + _process.Refresh(); + throw; + } + }); } [Fact] @@ -822,7 +840,19 @@ public void TestWorkingSet64() return; } - Assert.InRange(_process.WorkingSet64, 1, long.MaxValue); + // On recent Linux kernels (6.2+) working set can be zero just after the process started. + ExecuteWithRetryOnLinux(() => + { + try + { + Assert.InRange(_process.WorkingSet64, 1, long.MaxValue); + } + catch + { + _process.Refresh(); + throw; + } + }); } [Fact] @@ -2014,9 +2044,29 @@ public void TestPeakWorkingSet() { CreateDefaultProcess(); + if (OperatingSystem.IsMacOS()) + { #pragma warning disable 0618 - AssertNonZeroAllZeroDarwin(_process.PeakWorkingSet); + Assert.Equal(0, _process.PeakWorkingSet); #pragma warning restore 0618 + return; + } + + // On recent Linux kernels (6.2+) working set can be zero just after the process started. + ExecuteWithRetryOnLinux(() => + { + try + { +#pragma warning disable 0618 + Assert.NotEqual(0, _process.PeakWorkingSet); +#pragma warning restore 0618 + } + catch + { + _process.Refresh(); + throw; + } + }); } [Fact] @@ -2080,9 +2130,21 @@ public void TestWorkingSet() return; } + // On recent Linux kernels (6.2+) working set can be zero just after the process started. + ExecuteWithRetryOnLinux(() => + { + try + { #pragma warning disable 0618 - Assert.InRange(_process.WorkingSet, 1, int.MaxValue); + Assert.InRange(_process.WorkingSet, 1, int.MaxValue); #pragma warning restore 0618 + } + catch + { + _process.Refresh(); + throw; + } + }); } [Fact] @@ -2677,5 +2739,17 @@ private SecureString AsSecureString(string str) return secureString; } + + private static void ExecuteWithRetryOnLinux(Action test) + { + if (OperatingSystem.IsLinux()) + { + RetryHelper.Execute(test, retryWhen: ex => ex is XunitException); + } + else + { + test(); + } + } } }