-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBrowserTestHost.cs
More file actions
233 lines (204 loc) · 7.46 KB
/
BrowserTestHost.cs
File metadata and controls
233 lines (204 loc) · 7.46 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System.Diagnostics;
namespace DotPilot.UITests;
internal static class BrowserTestHost
{
private const string DotnetExecutableName = "dotnet";
private const string RunCommand = "run";
private const string ConfigurationOption = "-c";
private const string ReleaseConfiguration = "Release";
private const string FrameworkOption = "-f";
private const string BrowserFramework = "net10.0-browserwasm";
private const string ProjectOption = "--project";
private const string NoLaunchProfileOption = "--no-launch-profile";
private const string UiAutomationProperty = "-p:IsUiAutomationMappingEnabled=True";
private const string ProjectRelativePath = "DotPilot/DotPilot.csproj";
private const string SolutionMarkerFileName = "DotPilot.slnx";
private const string HostReadyTimeoutMessage = "Timed out waiting for the WebAssembly host to become reachable.";
private static readonly TimeSpan HostStartupTimeout = TimeSpan.FromSeconds(45);
private static readonly TimeSpan HostShutdownTimeout = TimeSpan.FromSeconds(10);
private static readonly TimeSpan HostProbeInterval = TimeSpan.FromMilliseconds(250);
private static readonly HttpClient HttpClient = new()
{
Timeout = TimeSpan.FromSeconds(2),
};
private static readonly object SyncRoot = new();
private static Process? _hostProcess;
private static bool _startedHost;
private static string _lastOutput = string.Empty;
static BrowserTestHost()
{
AppDomain.CurrentDomain.ProcessExit += (_, _) => Stop();
}
public static void EnsureStarted(string hostUri)
{
lock (SyncRoot)
{
if (IsReachable(hostUri))
{
HarnessLog.Write("Browser host is already reachable.");
return;
}
if (_hostProcess is { HasExited: false })
{
HarnessLog.Write("Browser host process already exists. Waiting for readiness.");
WaitForHost(hostUri);
return;
}
var repoRoot = FindRepositoryRoot();
var projectPath = Path.Combine(repoRoot, ProjectRelativePath);
var projectDirectory = Path.GetDirectoryName(projectPath)
?? throw new InvalidOperationException($"Could not resolve project directory for '{projectPath}'.");
HarnessLog.Write("Starting browser host process.");
StartHostProcess(projectDirectory, projectPath);
WaitForHost(hostUri);
}
}
private static void StartHostProcess(string projectDirectory, string projectPath)
{
var processStartInfo = CreateStartInfo(projectDirectory);
foreach (var argument in CreateRunArguments(projectPath))
{
processStartInfo.ArgumentList.Add(argument);
}
processStartInfo.Environment["ASPNETCORE_URLS"] = BrowserTestEnvironment.WebAssemblyUrlsValue;
_hostProcess = Process.Start(processStartInfo) ?? throw new InvalidOperationException("Failed to start the WebAssembly test host.");
_hostProcess.OutputDataReceived += (_, args) => CaptureOutput(args.Data);
_hostProcess.ErrorDataReceived += (_, args) => CaptureOutput(args.Data);
_hostProcess.BeginOutputReadLine();
_hostProcess.BeginErrorReadLine();
_startedHost = true;
HarnessLog.Write($"Browser host process started with PID {_hostProcess.Id}.");
}
internal static IReadOnlyList<string> CreateRunArguments(string projectPath)
{
return
[
RunCommand,
ConfigurationOption,
ReleaseConfiguration,
FrameworkOption,
BrowserFramework,
UiAutomationProperty,
ProjectOption,
projectPath,
NoLaunchProfileOption,
];
}
private static void CaptureOutput(string? line)
{
if (!string.IsNullOrWhiteSpace(line))
{
_lastOutput = line;
}
}
private static void WaitForHost(string hostUri)
{
var timeoutAt = DateTimeOffset.UtcNow.Add(HostStartupTimeout);
while (DateTimeOffset.UtcNow < timeoutAt)
{
if (IsReachable(hostUri))
{
HarnessLog.Write("Browser host responded to readiness probe.");
return;
}
if (_hostProcess is { HasExited: true } exitedProcess)
{
throw new InvalidOperationException(
$"The WebAssembly host exited before it became reachable. Exit code: {exitedProcess.ExitCode}. Last output: {_lastOutput}");
}
Task.Delay(HostProbeInterval).GetAwaiter().GetResult();
}
throw new InvalidOperationException($"{HostReadyTimeoutMessage} Last output: {_lastOutput}");
}
private static bool IsReachable(string hostUri)
{
try
{
using var response = HttpClient.GetAsync(hostUri).GetAwaiter().GetResult();
return response.IsSuccessStatusCode;
}
catch
{
return false;
}
}
private static ProcessStartInfo CreateStartInfo(string workingDirectory)
{
return new ProcessStartInfo
{
FileName = DotnetExecutableName,
WorkingDirectory = workingDirectory,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};
}
private static string FindRepositoryRoot()
{
var currentDirectory = new DirectoryInfo(AppContext.BaseDirectory);
while (currentDirectory is not null)
{
var markerPath = Path.Combine(currentDirectory.FullName, SolutionMarkerFileName);
if (File.Exists(markerPath))
{
return currentDirectory.FullName;
}
currentDirectory = currentDirectory.Parent;
}
throw new DirectoryNotFoundException($"Could not locate repository root containing {SolutionMarkerFileName}.");
}
public static void Stop()
{
lock (SyncRoot)
{
if (!_startedHost || _hostProcess is null)
{
HarnessLog.Write("Browser host stop requested, but no owned host process is active.");
return;
}
var hostProcess = _hostProcess;
_hostProcess = null;
_startedHost = false;
_lastOutput = string.Empty;
try
{
HarnessLog.Write($"Stopping browser host process {hostProcess.Id}.");
CancelOutputReaders(hostProcess);
if (!hostProcess.HasExited)
{
hostProcess.Kill(entireProcessTree: true);
hostProcess.WaitForExit((int)HostShutdownTimeout.TotalMilliseconds);
}
HarnessLog.Write("Browser host process stopped.");
}
catch
{
// Best-effort cleanup only.
}
finally
{
hostProcess.Dispose();
}
}
}
private static void CancelOutputReaders(Process process)
{
try
{
process.CancelOutputRead();
}
catch
{
// Best-effort cleanup only.
}
try
{
process.CancelErrorRead();
}
catch
{
// Best-effort cleanup only.
}
}
}