Skip to content

Commit

Permalink
Fix System.Diagnostics.Process tests for cross-platform
Browse files Browse the repository at this point in the history
Many of the tests are viable across platforms, but are currently implemented with platform-dependent functionality, e.g. P/Invokes to Win32 functions to verify results.  This commit fixes up all of those tests to be as general as possible, to use Unix functions were appropriate, and to conditionally run pieces of code.  A few tests have been annotated as platform-specific.
  • Loading branch information
stephentoub committed Apr 3, 2015
1 parent 8cd1ff1 commit 9ad827f
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,21 @@ public void SetAndCheckBasePriority(ProcessPriorityClass exPriorityClass, int pr
public void Process_BasePriority()
{
ProcessPriorityClass originalPriority = _process.PriorityClass;
Assert.Equal(ProcessPriorityClass.Normal, originalPriority);

try
{
//SetAndCheckBasePriority(ProcessPriorityClass.RealTime, 24);
SetAndCheckBasePriority(ProcessPriorityClass.High, 13);
SetAndCheckBasePriority(ProcessPriorityClass.Idle, 4);
SetAndCheckBasePriority(ProcessPriorityClass.Normal, 8);
}
finally
if (global::Interop.PlatformDetection.OperatingSystem == global::Interop.OperatingSystem.Windows)
{
_process.PriorityClass = originalPriority;
try
{
//SetAndCheckBasePriority(ProcessPriorityClass.RealTime, 24);
SetAndCheckBasePriority(ProcessPriorityClass.High, 13);
SetAndCheckBasePriority(ProcessPriorityClass.Idle, 4);
SetAndCheckBasePriority(ProcessPriorityClass.Normal, 8);
}
finally
{
_process.PriorityClass = originalPriority;
}
}
}

Expand Down Expand Up @@ -178,6 +182,7 @@ public void Process_ExitTime()


[Fact]
[PlatformSpecific(PlatformID.Windows)]
public void Process_GetHandle()
{
Assert.Equal(_process.Id, Interop.GetProcessId(_process.SafeHandle));
Expand Down Expand Up @@ -221,7 +226,16 @@ public void Process_MachineName()
public void Process_MainModule()
{
// Get MainModule property from a Process object
ProcessModule mainModule = _process.MainModule;
ProcessModule mainModule = null;
if (global::Interop.PlatformDetection.OperatingSystem == global::Interop.OperatingSystem.Windows)
{
mainModule = _process.MainModule;
}
else
{
Assert.Throws<PlatformNotSupportedException>(() => _process.MainModule);
}

if (mainModule != null)
{
Assert.Equal(CoreRunName, Path.GetFileNameWithoutExtension(mainModule.ModuleName));
Expand Down Expand Up @@ -250,41 +264,49 @@ public void Process_MaxWorkingSet()
IntPtr min, max;
uint flags;

int intCurrValue = (Int32)_process.MaxWorkingSet;
long curValue = (long)_process.MaxWorkingSet;
Assert.True(curValue >= 0);

try
if (global::Interop.PlatformDetection.OperatingSystem == global::Interop.OperatingSystem.Windows)
{
_process.MaxWorkingSet = (IntPtr)(intCurrValue + 1024);
Interop.GetProcessWorkingSetSizeEx(_process.SafeHandle, out min, out max, out flags);
intCurrValue = (int)max;
_process.Refresh();
Assert.Equal(intCurrValue, (int)_process.MaxWorkingSet);
}
finally
{
_process.MaxWorkingSet = (IntPtr)intCurrValue;
try
{
_process.MaxWorkingSet = (IntPtr)((int)curValue + 1024);
Interop.GetProcessWorkingSetSizeEx(_process.SafeHandle, out min, out max, out flags);
curValue = (int)max;
_process.Refresh();
Assert.Equal(curValue, (int)_process.MaxWorkingSet);
}
finally
{
_process.MaxWorkingSet = (IntPtr)curValue;
}
}
}

[Fact]
public void Process_MinWorkingSet()
{
int intCurrValue = (Int32)_process.MinWorkingSet;
IntPtr min;
IntPtr max;
uint flags;
long curValue = (long)_process.MinWorkingSet;
Assert.True(curValue >= 0);

try
if (global::Interop.PlatformDetection.OperatingSystem == global::Interop.OperatingSystem.Windows)
{
_process.MinWorkingSet = (IntPtr)(intCurrValue - 1024);
Interop.GetProcessWorkingSetSizeEx(_process.SafeHandle, out min, out max, out flags);
intCurrValue = (int)min;
_process.Refresh();
Assert.Equal(intCurrValue, (int)_process.MinWorkingSet);
}
finally
{
_process.MinWorkingSet = (IntPtr)intCurrValue;
try
{
_process.MinWorkingSet = (IntPtr)((int)curValue - 1024);

IntPtr min, max;
uint flags;
Interop.GetProcessWorkingSetSizeEx(_process.SafeHandle, out min, out max, out flags);
curValue = (int)min;
_process.Refresh();
Assert.Equal(curValue, (int)_process.MinWorkingSet);
}
finally
{
_process.MinWorkingSet = (IntPtr)curValue;
}
}
}

Expand All @@ -303,62 +325,67 @@ public void Process_Modules()
}
}

public void DoNothing(TimeSpan ignoreValue)
private void AssertNonZeroWindowsZeroUnix(long value)
{
// This method does nothing.
switch (global::Interop.PlatformDetection.OperatingSystem)
{
case global::Interop.OperatingSystem.Windows:
Assert.NotEqual(0, value);
break;
default:
Assert.Equal(0, value);
break;
}
}

[Fact]
public void Process_NonpagedSystemMemorySize64()
{
Assert.NotEqual(0L, _process.NonpagedSystemMemorySize64);
AssertNonZeroWindowsZeroUnix(_process.NonpagedSystemMemorySize64);
}

[Fact]
public void Process_PagedMemorySize64()
{
Assert.NotEqual(0L, _process.PagedMemorySize64);
AssertNonZeroWindowsZeroUnix(_process.PagedMemorySize64);
}

[Fact]
public void Process_PagedSystemMemorySize64()
{
Assert.NotEqual(0L, _process.PagedSystemMemorySize64);
AssertNonZeroWindowsZeroUnix(_process.PagedSystemMemorySize64);
}

[Fact]
public void Process_PeakPagedMemorySize64()
{
Assert.NotEqual(0L, _process.PeakPagedMemorySize64);
AssertNonZeroWindowsZeroUnix(_process.PeakPagedMemorySize64);
}

[Fact]
public void Process_PeakVirtualMemorySize64()
{
Assert.NotEqual(0L, _process.PeakVirtualMemorySize64);
AssertNonZeroWindowsZeroUnix(_process.PeakVirtualMemorySize64);
}

[Fact]
public void Process_PeakWorkingSet64()
{
Assert.NotEqual(0L, _process.PeakWorkingSet64);
AssertNonZeroWindowsZeroUnix(_process.PeakWorkingSet64);
}

[Fact]
public void Process_PrivateMemorySize64()
{
Assert.NotEqual(0L, _process.PrivateMemorySize64);
AssertNonZeroWindowsZeroUnix(_process.PrivateMemorySize64);
}

[Fact]
public void Process_PrivilegedProcessorTime()
{
// There is no good way to test the actual values of these
// w/o the user of Performance Counters or a ton of pinvokes, and so for now we simply check
// they do not throw exception when called.
DoNothing(_process.UserProcessorTime);
DoNothing(_process.PrivilegedProcessorTime);
DoNothing(_process.TotalProcessorTime);
Assert.True(_process.UserProcessorTime.TotalSeconds >= 0);
Assert.True(_process.PrivilegedProcessorTime.TotalSeconds >= 0);
Assert.True(_process.TotalProcessorTime.TotalSeconds >= 0);
}

[Fact]
Expand All @@ -381,7 +408,6 @@ public void Process_ProcessorAffinity()
public void Process_PriorityBoostEnabled()
{
bool isPriorityBoostEnabled = _process.PriorityBoostEnabled;

try
{
_process.PriorityBoostEnabled = true;
Expand All @@ -390,19 +416,15 @@ public void Process_PriorityBoostEnabled()
_process.PriorityBoostEnabled = false;
Assert.False(_process.PriorityBoostEnabled, "Process_PriorityBoostEnabled002 failed");
}

finally
{
_process.PriorityBoostEnabled = isPriorityBoostEnabled;
}

}

public void Process_PriorityClass()
{

ProcessPriorityClass priorityClass = _process.PriorityClass;

try
{
_process.PriorityClass = ProcessPriorityClass.High;
Expand Down Expand Up @@ -434,12 +456,20 @@ public void ProcessProcessName()
[DllImport("api-ms-win-core-processthreads-l1-1-0.dll")]
internal static extern int GetCurrentProcessId();

[DllImport("libc")]
internal static extern int getpid();

[Fact]
public void Process_GetCurrentProcess()
{
int currentProcessId = ProcessTest.GetCurrentProcessId();
Process current = Process.GetCurrentProcess();
Assert.NotNull(current);

Assert.Equal(currentProcessId, Process.GetCurrentProcess().Id);
int currentProcessId = global::Interop.PlatformDetection.OperatingSystem == global::Interop.OperatingSystem.Windows ?
GetCurrentProcessId() :
getpid();

Assert.Equal(currentProcessId, current.Id);
Assert.Equal(Process.GetProcessById(currentProcessId).ProcessName, Process.GetCurrentProcess().ProcessName);
}

Expand Down Expand Up @@ -505,7 +535,10 @@ public void Process_Environment()
Environment2.Add("NewKey", "NewValue");
Environment2.Add("NewKey2", "NewValue2");
Assert.True(Environment2.ContainsKey("NewKey"));
Assert.True(Environment2.ContainsKey("newkey")); // Windows is case-insensitive (will need to adapt this when we support tests on Unix)
if (global::Interop.PlatformDetection.OperatingSystem == global::Interop.OperatingSystem.Windows)
{
Assert.True(Environment2.ContainsKey("newkey"));
}
Assert.False(Environment2.ContainsKey("NewKey99"));

//Iterating
Expand Down Expand Up @@ -541,7 +574,10 @@ public void Process_Environment()

//Contains
Assert.True(Environment2.Contains(new System.Collections.Generic.KeyValuePair<string, string>("NewKey", "NewValue")));
Assert.True(Environment2.Contains(new System.Collections.Generic.KeyValuePair<string, string>("nEwKeY", "NewValue"))); // case-insensitive keys on Windows
if (global::Interop.PlatformDetection.OperatingSystem == global::Interop.OperatingSystem.Windows)
{
Assert.True(Environment2.Contains(new System.Collections.Generic.KeyValuePair<string, string>("nEwKeY", "NewValue")));
}
Assert.False(Environment2.Contains(new System.Collections.Generic.KeyValuePair<string, string>("NewKey99", "NewValue99")));

//Exception not thrown with invalid key
Expand All @@ -563,9 +599,12 @@ public void Process_Environment()
retval = Environment2.TryGetValue("NewKey", out stringout);
Assert.True(retval);
Assert.Equal("NewValue", stringout);
retval = Environment2.TryGetValue("NeWkEy", out stringout);
Assert.True(retval);
Assert.Equal("NewValue", stringout);
if (global::Interop.PlatformDetection.OperatingSystem == global::Interop.OperatingSystem.Windows)
{
retval = Environment2.TryGetValue("NeWkEy", out stringout);
Assert.True(retval);
Assert.Equal("NewValue", stringout);
}

stringout = null;
retval = false;
Expand Down Expand Up @@ -606,7 +645,10 @@ public void Process_Environment()
Assert.Throws<System.Collections.Generic.KeyNotFoundException>(() => { string a1 = Environment2["1bB"]; });

Assert.True(Environment2.Contains(new System.Collections.Generic.KeyValuePair<string, string>("NewKey2", "NewValue2")));
Assert.True(Environment2.Contains(new System.Collections.Generic.KeyValuePair<string, string>("NEWKeY2", "NewValue2"))); // case-insensitive keys on Windows
if (global::Interop.PlatformDetection.OperatingSystem == global::Interop.OperatingSystem.Windows)
{
Assert.True(Environment2.Contains(new System.Collections.Generic.KeyValuePair<string, string>("NEWKeY2", "NewValue2")));
}
Assert.False(Environment2.Contains(new System.Collections.Generic.KeyValuePair<string, string>("NewKey2", "newvalue2")));
Assert.False(Environment2.Contains(new System.Collections.Generic.KeyValuePair<string, string>("newkey2", "newvalue2")));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ public partial class ProcessTest
[Fact]
public void Process_EncodingBeforeProvider()
{
Action<int> run = expectedCodePage =>
{
Process p = CreateProcessInfinite();
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();

Assert.Equal(p.StandardInput.Encoding.CodePage, expectedCodePage);
Assert.Equal(p.StandardOutput.CurrentEncoding.CodePage, expectedCodePage);
Assert.Equal(p.StandardError.CurrentEncoding.CodePage, expectedCodePage);

p.Kill();
Assert.True(p.WaitForExit(WaitInMS));
};

if (global::Interop.PlatformDetection.OperatingSystem != global::Interop.OperatingSystem.Windows)
{
run(Encoding.UTF8.CodePage);
return;
}

int inputEncoding = GetConsoleCP();
int outputEncoding = GetConsoleOutputCP();
Expand All @@ -36,18 +57,7 @@ public void Process_EncodingBeforeProvider()
SetConsoleCP(s_ConsoleEncoding);
SetConsoleOutputCP(s_ConsoleEncoding);

Process p = CreateProcessInfinite();
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();

Assert.Equal(p.StandardInput.Encoding.CodePage, Encoding.UTF8.CodePage);
Assert.Equal(p.StandardOutput.CurrentEncoding.CodePage, Encoding.UTF8.CodePage);
Assert.Equal(p.StandardError.CurrentEncoding.CodePage, Encoding.UTF8.CodePage);

p.Kill();
Assert.True(p.WaitForExit(WaitInMS));
run(Encoding.UTF8.CodePage);
}

{
Expand All @@ -56,18 +66,8 @@ public void Process_EncodingBeforeProvider()

// Register the codeprovider which will ensure 437 is enabled.
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Process p = CreateProcessInfinite();
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();

Assert.Equal(p.StandardInput.Encoding.CodePage, s_ConsoleEncoding);
Assert.Equal(p.StandardOutput.CurrentEncoding.CodePage, s_ConsoleEncoding);
Assert.Equal(p.StandardError.CurrentEncoding.CodePage, s_ConsoleEncoding);

p.Kill();
Assert.True(p.WaitForExit(WaitInMS));

run(s_ConsoleEncoding);
}
}
finally
Expand All @@ -76,5 +76,6 @@ public void Process_EncodingBeforeProvider()
SetConsoleOutputCP(outputEncoding);
}
}

}
}
Loading

0 comments on commit 9ad827f

Please sign in to comment.