4
4
using System . Text ;
5
5
using System . IO ;
6
6
using Xunit ;
7
+ using System . Threading ;
7
8
8
9
namespace System . Diagnostics . ProcessTests
9
10
{
10
11
public class ProcessStreamReadTests : ProcessTestBase
11
12
{
12
- private Process CreateProcessError ( )
13
- {
14
- return CreateProcess ( "error" ) ;
15
- }
16
-
17
- private Process CreateProcessInput ( )
18
- {
19
- return CreateProcess ( "input" ) ;
20
- }
21
-
22
- private Process CreateProcessStream ( )
23
- {
24
- return CreateProcess ( "stream" ) ;
25
- }
26
-
27
- private Process CreateProcessByteAtATime ( )
28
- {
29
- return CreateProcess ( "byteAtATime" ) ;
30
- }
31
-
32
- private Process CreateProcessManyOutputLines ( )
33
- {
34
- return CreateProcess ( "manyOutputLines" ) ;
35
- }
36
-
37
13
[ Fact ]
38
14
public void TestSyncErrorStream ( )
39
15
{
40
- Process p = CreateProcessError ( ) ;
16
+ Process p = CreateProcess ( ErrorProcessBody ) ;
41
17
p . StartInfo . RedirectStandardError = true ;
42
18
p . Start ( ) ;
43
- string expected = TestExeName + " started error stream" + Environment . NewLine +
44
- TestExeName + " closed error stream" + Environment . NewLine ;
19
+ string expected = TestConsoleApp + " started error stream" + Environment . NewLine +
20
+ TestConsoleApp + " closed error stream" + Environment . NewLine ;
45
21
Assert . Equal ( expected , p . StandardError . ReadToEnd ( ) ) ;
46
22
Assert . True ( p . WaitForExit ( WaitInMS ) ) ;
47
23
}
@@ -52,7 +28,7 @@ public void TestAsyncErrorStream()
52
28
for ( int i = 0 ; i < 2 ; ++ i )
53
29
{
54
30
StringBuilder sb = new StringBuilder ( ) ;
55
- Process p = CreateProcessError ( ) ;
31
+ Process p = CreateProcess ( ErrorProcessBody ) ;
56
32
p . StartInfo . RedirectStandardError = true ;
57
33
p . ErrorDataReceived += ( s , e ) =>
58
34
{
@@ -68,20 +44,28 @@ public void TestAsyncErrorStream()
68
44
Assert . True ( p . WaitForExit ( WaitInMS ) ) ;
69
45
p . WaitForExit ( ) ; // This ensures async event handlers are finished processing.
70
46
71
- string expected = TestExeName + " started error stream" + ( i == 1 ? "" : TestExeName + " closed error stream" ) ;
47
+ string expected = TestConsoleApp + " started error stream" + ( i == 1 ? "" : TestConsoleApp + " closed error stream" ) ;
72
48
Assert . Equal ( expected , sb . ToString ( ) ) ;
73
49
}
74
50
}
75
51
52
+ private static int ErrorProcessBody ( )
53
+ {
54
+ Console . Error . WriteLine ( TestConsoleApp + " started error stream" ) ;
55
+ Console . Error . WriteLine ( TestConsoleApp + " closed error stream" ) ;
56
+ return SuccessExitCode ;
57
+ }
58
+
59
+
76
60
[ Fact ]
77
61
public void TestSyncOutputStream ( )
78
62
{
79
- Process p = CreateProcessStream ( ) ;
63
+ Process p = CreateProcess ( StreamBody ) ;
80
64
p . StartInfo . RedirectStandardOutput = true ;
81
65
p . Start ( ) ;
82
66
string s = p . StandardOutput . ReadToEnd ( ) ;
83
67
Assert . True ( p . WaitForExit ( WaitInMS ) ) ;
84
- Assert . Equal ( TestExeName + " started" + Environment . NewLine + TestExeName + " closed" + Environment . NewLine , s ) ;
68
+ Assert . Equal ( TestConsoleApp + " started" + Environment . NewLine + TestConsoleApp + " closed" + Environment . NewLine , s ) ;
85
69
}
86
70
87
71
[ Fact ]
@@ -90,7 +74,7 @@ public void TestAsyncOutputStream()
90
74
for ( int i = 0 ; i < 2 ; ++ i )
91
75
{
92
76
StringBuilder sb = new StringBuilder ( ) ;
93
- Process p = CreateProcessStream ( ) ;
77
+ Process p = CreateProcess ( StreamBody ) ;
94
78
p . StartInfo . RedirectStandardOutput = true ;
95
79
p . OutputDataReceived += ( s , e ) =>
96
80
{
@@ -105,16 +89,27 @@ public void TestAsyncOutputStream()
105
89
Assert . True ( p . WaitForExit ( WaitInMS ) ) ;
106
90
p . WaitForExit ( ) ; // This ensures async event handlers are finished processing.
107
91
108
- string expected = TestExeName + " started" + ( i == 1 ? "" : TestExeName + " closed" ) ;
92
+ string expected = TestConsoleApp + " started" + ( i == 1 ? "" : TestConsoleApp + " closed" ) ;
109
93
Assert . Equal ( expected , sb . ToString ( ) ) ;
110
94
}
111
95
}
112
96
97
+ private static int StreamBody ( )
98
+ {
99
+ Console . WriteLine ( TestConsoleApp + " started" ) ;
100
+ Console . WriteLine ( TestConsoleApp + " closed" ) ;
101
+ return SuccessExitCode ;
102
+ }
103
+
113
104
[ Fact ]
114
105
public void TestSyncStreams ( )
115
106
{
116
107
const string expected = "This string should come as output" ;
117
- Process p = CreateProcessInput ( ) ;
108
+ Process p = CreateProcess ( ( ) =>
109
+ {
110
+ Console . ReadLine ( ) ;
111
+ return SuccessExitCode ;
112
+ } ) ;
118
113
p . StartInfo . RedirectStandardInput = true ;
119
114
p . StartInfo . RedirectStandardOutput = true ;
120
115
p . OutputDataReceived += ( s , e ) => { Assert . Equal ( expected , e . Data ) ; } ;
@@ -130,7 +125,19 @@ public void TestSyncStreams()
130
125
public void TestAsyncHalfCharacterAtATime ( )
131
126
{
132
127
var receivedOutput = false ;
133
- Process p = CreateProcessByteAtATime ( ) ;
128
+ Process p = CreateProcess ( ( ) =>
129
+ {
130
+ var stdout = Console . OpenStandardOutput ( ) ;
131
+ var bytes = new byte [ ] { 97 , 0 } ; //Encoding.Unicode.GetBytes("a");
132
+
133
+ for ( int i = 0 ; i != bytes . Length ; ++ i )
134
+ {
135
+ stdout . WriteByte ( bytes [ i ] ) ;
136
+ stdout . Flush ( ) ;
137
+ Thread . Sleep ( 100 ) ;
138
+ }
139
+ return SuccessExitCode ;
140
+ } ) ;
134
141
p . StartInfo . RedirectStandardOutput = true ;
135
142
p . StartInfo . StandardOutputEncoding = Encoding . Unicode ;
136
143
p . OutputDataReceived += ( s , e ) =>
@@ -153,10 +160,19 @@ public void TestAsyncHalfCharacterAtATime()
153
160
[ Fact ]
154
161
public void TestManyOutputLines ( )
155
162
{
163
+ const int ExpectedLineCount = 144 ;
164
+
156
165
int nonWhitespaceLinesReceived = 0 ;
157
166
int totalLinesReceived = 0 ;
158
167
159
- Process p = CreateProcessManyOutputLines ( ) ;
168
+ Process p = CreateProcess ( ( ) =>
169
+ {
170
+ for ( int i = 0 ; i < ExpectedLineCount ; i ++ )
171
+ {
172
+ Console . WriteLine ( "This is line #" + i + "." ) ;
173
+ }
174
+ return SuccessExitCode ;
175
+ } ) ;
160
176
p . StartInfo . RedirectStandardOutput = true ;
161
177
p . OutputDataReceived += ( s , e ) =>
162
178
{
@@ -172,8 +188,8 @@ public void TestManyOutputLines()
172
188
Assert . True ( p . WaitForExit ( WaitInMS ) ) ;
173
189
p . WaitForExit ( ) ; // This ensures async event handlers are finished processing.
174
190
175
- Assert . Equal ( 144 , nonWhitespaceLinesReceived ) ;
176
- Assert . Equal ( 145 , totalLinesReceived ) ;
191
+ Assert . Equal ( ExpectedLineCount , nonWhitespaceLinesReceived ) ;
192
+ Assert . Equal ( ExpectedLineCount + 1 , totalLinesReceived ) ;
177
193
}
178
194
179
195
[ Fact ]
@@ -190,7 +206,7 @@ public void TestStreamNegativeTests()
190
206
}
191
207
192
208
{
193
- Process p = CreateProcessStream ( ) ;
209
+ Process p = CreateProcess ( StreamBody ) ;
194
210
p . StartInfo . RedirectStandardOutput = true ;
195
211
p . StartInfo . RedirectStandardError = true ;
196
212
p . OutputDataReceived += ( s , e ) => { } ;
@@ -206,7 +222,7 @@ public void TestStreamNegativeTests()
206
222
}
207
223
208
224
{
209
- Process p = CreateProcessStream ( ) ;
225
+ Process p = CreateProcess ( StreamBody ) ;
210
226
p . StartInfo . RedirectStandardOutput = true ;
211
227
p . StartInfo . RedirectStandardError = true ;
212
228
p . OutputDataReceived += ( s , e ) => { } ;
0 commit comments