1
1
// Copyright (c) Microsoft. All rights reserved.
2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
+ using Microsoft . Win32 . SafeHandles ;
4
5
using System . Collections . Generic ;
5
6
using System . IO ;
6
7
using System . IO . Pipes ;
@@ -13,7 +14,7 @@ namespace System.Diagnostics.ProcessTests
13
14
{
14
15
public partial class ProcessTest : IDisposable
15
16
{
16
- private const int WaitInMS = 100 * 1000 ;
17
+ private const int WaitInMS = 100 * 1000 ;
17
18
private const string CoreRunName = "corerun" ;
18
19
private const string TestExeName = "System.Diagnostics.Process.TestConsoleApp.exe" ;
19
20
private const int SuccessExitCode = 100 ;
@@ -51,8 +52,8 @@ Process CreateProcess(string optionalArgument = ""/*String.Empty is not a consta
51
52
_processes . Add ( p ) ;
52
53
53
54
p . StartInfo . FileName = CoreRunName ;
54
- p . StartInfo . Arguments = string . IsNullOrWhiteSpace ( optionalArgument ) ?
55
- TestExeName :
55
+ p . StartInfo . Arguments = string . IsNullOrWhiteSpace ( optionalArgument ) ?
56
+ TestExeName :
56
57
TestExeName + " " + optionalArgument ;
57
58
58
59
// Profilers / code coverage tools doing coverage of the test process set environment
@@ -79,25 +80,45 @@ public void SetAndCheckBasePriority(ProcessPriorityClass exPriorityClass, int pr
79
80
Assert . Equal ( priority , _process . BasePriority ) ;
80
81
}
81
82
82
- [ Fact ]
83
- public void Process_BasePriority ( )
83
+ [ Fact , PlatformSpecific ( PlatformID . Windows ) ]
84
+ public void Process_BasePriorityWindows ( )
84
85
{
85
86
ProcessPriorityClass originalPriority = _process . PriorityClass ;
86
87
Assert . Equal ( ProcessPriorityClass . Normal , originalPriority ) ;
87
88
88
- if ( global :: Interop . IsWindows )
89
+ try
89
90
{
90
- try
91
- {
92
- //SetAndCheckBasePriority(ProcessPriorityClass.RealTime, 24);
93
- SetAndCheckBasePriority ( ProcessPriorityClass . High , 13 ) ;
94
- SetAndCheckBasePriority ( ProcessPriorityClass . Idle , 4 ) ;
95
- SetAndCheckBasePriority ( ProcessPriorityClass . Normal , 8 ) ;
96
- }
97
- finally
98
- {
99
- _process . PriorityClass = originalPriority ;
100
- }
91
+ // We are not checking for RealTime case here, as RealTime priority process can
92
+ // preempt the threads of all other processes, including operating system processes
93
+ // performing important tasks, which may cause the machine to be unresponsive.
94
+
95
+ //SetAndCheckBasePriority(ProcessPriorityClass.RealTime, 24);
96
+
97
+ SetAndCheckBasePriority ( ProcessPriorityClass . High , 13 ) ;
98
+ SetAndCheckBasePriority ( ProcessPriorityClass . Idle , 4 ) ;
99
+ SetAndCheckBasePriority ( ProcessPriorityClass . Normal , 8 ) ;
100
+ }
101
+ finally
102
+ {
103
+ _process . PriorityClass = originalPriority ;
104
+ }
105
+ }
106
+
107
+ [ Fact , PlatformSpecific ( PlatformID . AnyUnix ) , OuterLoop ] // This test requires admin elevation on Unix
108
+ public void Process_BasePriorityUnix ( )
109
+ {
110
+ ProcessPriorityClass originalPriority = _process . PriorityClass ;
111
+ Assert . Equal ( ProcessPriorityClass . Normal , originalPriority ) ;
112
+
113
+ try
114
+ {
115
+ SetAndCheckBasePriority ( ProcessPriorityClass . High , - 11 ) ;
116
+ SetAndCheckBasePriority ( ProcessPriorityClass . Idle , 19 ) ;
117
+ SetAndCheckBasePriority ( ProcessPriorityClass . Normal , 0 ) ;
118
+ }
119
+ finally
120
+ {
121
+ _process . PriorityClass = originalPriority ;
101
122
}
102
123
}
103
124
@@ -184,10 +205,17 @@ public void Process_ExitTime()
184
205
185
206
186
207
[ Fact ]
187
- [ PlatformSpecific ( PlatformID . Windows ) ]
188
- public void Process_GetHandle ( )
208
+ public void Process_Id ( )
189
209
{
190
- Assert . Equal ( _process . Id , Interop . GetProcessId ( _process . SafeHandle ) ) ;
210
+ if ( global ::Interop . IsWindows )
211
+ {
212
+ Assert . Equal ( _process . Id , Interop . GetProcessId ( _process . SafeHandle ) ) ;
213
+ }
214
+ else
215
+ {
216
+ IEnumerable < int > testProcessIds = Process . GetProcessesByName ( CoreRunName ) . Select ( p => p . Id ) ;
217
+ Assert . Contains ( _process . Id , testProcessIds ) ;
218
+ }
191
219
}
192
220
193
221
[ Fact ]
@@ -225,7 +253,6 @@ public void Process_MachineName()
225
253
}
226
254
227
255
[ Fact ]
228
- [ ActiveIssue ( 1896 , PlatformID . Windows ) ]
229
256
public void Process_MainModule ( )
230
257
{
231
258
// Get MainModule property from a Process object
@@ -246,7 +273,7 @@ public void Process_MainModule()
246
273
{
247
274
foreach ( ProcessModule pModule in _process . Modules )
248
275
{
249
- if ( String . Equals ( Path . GetFileNameWithoutExtension ( mainModule . ModuleName ) , CoreRunName , StringComparison . OrdinalIgnoreCase ) )
276
+ if ( String . Equals ( Path . GetFileNameWithoutExtension ( pModule . ModuleName ) , CoreRunName , StringComparison . OrdinalIgnoreCase ) )
250
277
{
251
278
foundMainModule = true ;
252
279
break ;
@@ -443,7 +470,26 @@ public void Process_PriorityBoostEnabled()
443
470
}
444
471
}
445
472
446
- public void Process_PriorityClass ( )
473
+ [ Fact , PlatformSpecific ( PlatformID . AnyUnix ) , OuterLoop ] // This test requires admin elevation on Unix
474
+ public void Process_PriorityClassUnix ( )
475
+ {
476
+ ProcessPriorityClass priorityClass = _process . PriorityClass ;
477
+ try
478
+ {
479
+ _process . PriorityClass = ProcessPriorityClass . High ;
480
+ Assert . Equal ( _process . PriorityClass , ProcessPriorityClass . High ) ;
481
+
482
+ _process . PriorityClass = ProcessPriorityClass . Normal ;
483
+ Assert . Equal ( _process . PriorityClass , ProcessPriorityClass . Normal ) ;
484
+ }
485
+ finally
486
+ {
487
+ _process . PriorityClass = priorityClass ;
488
+ }
489
+ }
490
+
491
+ [ Fact , PlatformSpecific ( PlatformID . Windows ) ]
492
+ public void Process_PriorityClassWindows ( )
447
493
{
448
494
ProcessPriorityClass priorityClass = _process . PriorityClass ;
449
495
try
@@ -473,13 +519,40 @@ public void ProcessProcessName()
473
519
Assert . Equal ( _process . ProcessName , CoreRunName , StringComparer . OrdinalIgnoreCase ) ;
474
520
}
475
521
522
+ [ Fact ]
523
+ public void Process_SafeHandle ( )
524
+ {
525
+ Assert . False ( _process . SafeHandle . IsInvalid ) ;
526
+ }
527
+
528
+ [ Fact ]
529
+ public void Process_SessionId ( )
530
+ {
531
+ uint sessionId ;
532
+ if ( global ::Interop . IsWindows )
533
+ {
534
+ ProcessIdToSessionId ( ( uint ) _process . Id , out sessionId ) ;
535
+ }
536
+ else
537
+ {
538
+ sessionId = ( uint ) getsid ( _process . Id ) ;
539
+ }
540
+
541
+ Assert . Equal ( sessionId , ( uint ) _process . SessionId ) ;
542
+ }
476
543
477
544
[ DllImport ( "api-ms-win-core-processthreads-l1-1-0.dll" ) ]
478
545
internal static extern int GetCurrentProcessId ( ) ;
479
546
480
547
[ DllImport ( "libc" ) ]
481
548
internal static extern int getpid ( ) ;
482
549
550
+ [ DllImport ( "libc" ) ]
551
+ internal static extern int getsid ( int pid ) ;
552
+
553
+ [ DllImport ( "api-ms-win-core-processthreads-l1-1-2.dll" ) ]
554
+ internal static extern bool ProcessIdToSessionId ( uint dwProcessId , out uint pSessionId ) ;
555
+
483
556
[ Fact ]
484
557
public void Process_GetCurrentProcess ( )
485
558
{
@@ -501,14 +574,14 @@ public void Process_GetProcesses()
501
574
Process currentProcess = Process . GetCurrentProcess ( ) ;
502
575
503
576
var foundCurrentProcess = ( from p in Process . GetProcesses ( )
504
- where ( p . Id == currentProcess . Id ) && ( p . ProcessName . Equals ( currentProcess . ProcessName ) )
505
- select p ) . Any ( ) ;
577
+ where ( p . Id == currentProcess . Id ) && ( p . ProcessName . Equals ( currentProcess . ProcessName ) )
578
+ select p ) . Any ( ) ;
506
579
507
580
Assert . True ( foundCurrentProcess , "Process_GetProcesses001 failed" ) ;
508
581
509
582
foundCurrentProcess = ( from p in Process . GetProcesses ( currentProcess . MachineName )
510
- where ( p . Id == currentProcess . Id ) && ( p . ProcessName . Equals ( currentProcess . ProcessName ) )
511
- select p ) . Any ( ) ;
583
+ where ( p . Id == currentProcess . Id ) && ( p . ProcessName . Equals ( currentProcess . ProcessName ) )
584
+ select p ) . Any ( ) ;
512
585
Assert . True ( foundCurrentProcess , "Process_GetProcesses002 failed" ) ;
513
586
}
514
587
@@ -765,7 +838,7 @@ public void Process_IPC()
765
838
p . Start ( ) ;
766
839
outbound . DisposeLocalCopyOfClientHandle ( ) ;
767
840
inbound . DisposeLocalCopyOfClientHandle ( ) ;
768
-
841
+
769
842
for ( byte i = 0 ; i < 10 ; i ++ )
770
843
{
771
844
outbound . WriteByte ( i ) ;
0 commit comments