-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathWebsiteProcess.cs
105 lines (88 loc) · 2.91 KB
/
WebsiteProcess.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Internal;
using Xunit.Abstractions;
namespace InteropTests.Helpers;
public class WebsiteProcess : IDisposable
{
private readonly Process _process;
private readonly ProcessEx _processEx;
private readonly TaskCompletionSource _startTcs;
private static readonly Regex NowListeningRegex = new Regex(@"^\s*Now listening on: .*:(?<port>\d*)$");
private readonly StringBuilder _output;
private readonly object _outputLock = new object();
public string ServerPort { get; private set; }
public bool IsReady => _startTcs.Task.IsCompletedSuccessfully;
public WebsiteProcess(string path, ITestOutputHelper output)
{
_output = new StringBuilder();
_process = new Process();
_process.StartInfo = new ProcessStartInfo
{
RedirectStandardOutput = true,
RedirectStandardError = true,
FileName = "dotnet",
Arguments = path
};
_process.EnableRaisingEvents = true;
_process.OutputDataReceived += Process_OutputDataReceived;
_process.ErrorDataReceived += Process_ErrorDataReceived;
output.WriteLine($"Starting process: {ProcessDebugHelper.GetDebugCommand(_process.StartInfo)}");
_process.Start();
_processEx = new ProcessEx(output, _process, Timeout.InfiniteTimeSpan);
_startTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
}
public string GetOutput()
{
lock (_outputLock)
{
return _output.ToString();
}
}
public Task WaitForReady()
{
if (_processEx.HasExited)
{
return Task.FromException(new InvalidOperationException("Server is not running."));
}
return _startTcs.Task;
}
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
var data = e.Data;
if (data != null)
{
var m = NowListeningRegex.Match(data);
if (m.Success)
{
ServerPort = m.Groups["port"].Value;
}
if (data.Contains("Application started. Press Ctrl+C to shut down."))
{
_startTcs.TrySetResult();
}
lock (_outputLock)
{
_output.AppendLine(data);
}
}
}
private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
var data = e.Data;
if (data != null)
{
lock (_outputLock)
{
_output.AppendLine("ERROR: " + data);
}
}
}
public void Dispose()
{
_processEx.Dispose();
}
}