3
3
4
4
using System ;
5
5
using System . Diagnostics ;
6
+ using System . Text ;
6
7
using System . Threading ;
7
8
using System . Threading . Tasks ;
8
9
using Microsoft . AspNetCore . Internal ;
@@ -15,9 +16,13 @@ public class ClientProcess : IDisposable
15
16
private readonly Process _process ;
16
17
private readonly ProcessEx _processEx ;
17
18
private readonly TaskCompletionSource < object > _startTcs ;
19
+ private readonly StringBuilder _output ;
20
+ private readonly object _outputLock = new object ( ) ;
18
21
19
22
public ClientProcess ( ITestOutputHelper output , string path , string serverPort , string testCase )
20
23
{
24
+ _output = new StringBuilder ( ) ;
25
+
21
26
_process = new Process ( ) ;
22
27
_process . StartInfo = new ProcessStartInfo
23
28
{
@@ -28,21 +33,26 @@ public ClientProcess(ITestOutputHelper output, string path, string serverPort, s
28
33
} ;
29
34
_process . EnableRaisingEvents = true ;
30
35
_process . OutputDataReceived += Process_OutputDataReceived ;
36
+ _process . ErrorDataReceived += Process_ErrorDataReceived ;
31
37
_process . Start ( ) ;
32
38
33
39
_processEx = new ProcessEx ( output , _process , timeout : Timeout . InfiniteTimeSpan ) ;
34
40
35
41
_startTcs = new TaskCompletionSource < object > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
36
42
}
37
43
38
- public Task WaitForReady ( )
44
+ public Task WaitForReadyAsync ( ) => _startTcs . Task ;
45
+ public Task WaitForExitAsync ( ) => _processEx . Exited ;
46
+ public int ExitCode => _process . ExitCode ;
47
+
48
+ public string GetOutput ( )
39
49
{
40
- return _startTcs . Task ;
50
+ lock ( _outputLock )
51
+ {
52
+ return _output . ToString ( ) ;
53
+ }
41
54
}
42
55
43
- public int ExitCode => _process . ExitCode ;
44
- public Task Exited => _processEx . Exited ;
45
-
46
56
private void Process_OutputDataReceived ( object sender , DataReceivedEventArgs e )
47
57
{
48
58
var data = e . Data ;
@@ -52,6 +62,23 @@ private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
52
62
{
53
63
_startTcs . TrySetResult ( null ) ;
54
64
}
65
+
66
+ lock ( _outputLock )
67
+ {
68
+ _output . AppendLine ( data ) ;
69
+ }
70
+ }
71
+ }
72
+
73
+ private void Process_ErrorDataReceived ( object sender , DataReceivedEventArgs e )
74
+ {
75
+ var data = e . Data ;
76
+ if ( data != null )
77
+ {
78
+ lock ( _outputLock )
79
+ {
80
+ _output . AppendLine ( data ) ;
81
+ }
55
82
}
56
83
}
57
84
0 commit comments