forked from microsoft/testfx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommonTestHost.cs
274 lines (230 loc) · 11.6 KB
/
CommonTestHost.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Testing.Platform.Extensions.TestFramework;
using Microsoft.Testing.Platform.Extensions.TestHost;
using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.Logging;
using Microsoft.Testing.Platform.Messages;
using Microsoft.Testing.Platform.OutputDevice;
using Microsoft.Testing.Platform.ServerMode;
using Microsoft.Testing.Platform.Services;
using Microsoft.Testing.Platform.Telemetry;
using Microsoft.Testing.Platform.TestHost;
namespace Microsoft.Testing.Platform.Hosts;
internal abstract class CommonTestHost(ServiceProvider serviceProvider) : ITestHost
{
public ServiceProvider ServiceProvider => serviceProvider;
protected IPushOnlyProtocol? PushOnlyProtocol => ServiceProvider.GetService<IPushOnlyProtocol>();
protected abstract bool RunTestApplicationLifeCycleCallbacks { get; }
public async Task<int> RunAsync()
{
CancellationToken testApplicationCancellationToken = ServiceProvider.GetTestApplicationCancellationTokenSource().CancellationToken;
int exitCode = ExitCodes.GenericFailure;
try
{
if (PushOnlyProtocol is null || PushOnlyProtocol?.IsServerMode == false)
{
exitCode = await RunTestAppAsync(testApplicationCancellationToken);
if (testApplicationCancellationToken.IsCancellationRequested)
{
exitCode = ExitCodes.TestSessionAborted;
}
return exitCode;
}
try
{
RoslynDebug.Assert(PushOnlyProtocol is not null);
ITestApplicationModuleInfo testApplicationModuleInfo = serviceProvider.GetTestApplicationModuleInfo();
bool isValidProtocol = await PushOnlyProtocol.IsCompatibleProtocolAsync(GetHostType());
exitCode = isValidProtocol
? await RunTestAppAsync(testApplicationCancellationToken)
: ExitCodes.IncompatibleProtocolVersion;
}
finally
{
if (PushOnlyProtocol is not null)
{
await PushOnlyProtocol.OnExitAsync();
}
}
}
catch (OperationCanceledException) when (testApplicationCancellationToken.IsCancellationRequested)
{
// We do nothing we're canceling
}
finally
{
await DisposeServiceProviderAsync(ServiceProvider, isProcessShutdown: true);
await DisposeHelper.DisposeAsync(ServiceProvider.GetService<FileLoggerProvider>());
await DisposeHelper.DisposeAsync(PushOnlyProtocol);
// This is intentional that we are not disposing the CTS.
// An unobserved task exception could be raised after the dispose, and we want to use OutputDevice there
// which needs CTS down the path.
// await DisposeHelper.DisposeAsync(ServiceProvider.GetTestApplicationCancellationTokenSource());
}
if (testApplicationCancellationToken.IsCancellationRequested)
{
exitCode = ExitCodes.TestSessionAborted;
}
return exitCode;
}
private string GetHostType()
{
// For now, we don't inherit TestHostOrchestratorHost from CommonTestHost one so we don't connect when we orchestrate
string hostType = this switch
{
ConsoleTestHost => "TestHost",
TestHostControllersTestHost => "TestHostController",
_ => throw new InvalidOperationException("Unknown host type"),
};
return hostType;
}
private async Task<int> RunTestAppAsync(CancellationToken testApplicationCancellationToken)
{
if (RunTestApplicationLifeCycleCallbacks)
{
// Get the test application lifecycle callbacks to be able to call the before run
foreach (ITestApplicationLifecycleCallbacks testApplicationLifecycleCallbacks in ServiceProvider.GetServicesInternal<ITestApplicationLifecycleCallbacks>())
{
await testApplicationLifecycleCallbacks.BeforeRunAsync(testApplicationCancellationToken);
}
}
int exitCode = await InternalRunAsync();
if (RunTestApplicationLifeCycleCallbacks)
{
foreach (ITestApplicationLifecycleCallbacks testApplicationLifecycleCallbacks in ServiceProvider.GetServicesInternal<ITestApplicationLifecycleCallbacks>())
{
await testApplicationLifecycleCallbacks.AfterRunAsync(exitCode, testApplicationCancellationToken);
await DisposeHelper.DisposeAsync(testApplicationLifecycleCallbacks);
}
}
return exitCode;
}
protected abstract Task<int> InternalRunAsync();
protected static async Task ExecuteRequestAsync(ProxyOutputDevice outputDevice, ITestSessionContext testSessionInfo,
ServiceProvider serviceProvider, BaseMessageBus baseMessageBus, ITestFramework testFramework, TestHost.ClientInfo client)
{
CancellationToken testSessionCancellationToken = serviceProvider.GetTestSessionContext().CancellationToken;
await DisplayBeforeSessionStartAsync(outputDevice, testSessionInfo, testSessionCancellationToken);
try
{
await NotifyTestSessionStartAsync(testSessionInfo.SessionId, baseMessageBus, serviceProvider, testSessionCancellationToken);
await serviceProvider.GetTestAdapterInvoker().ExecuteAsync(testFramework, client, testSessionCancellationToken);
await NotifyTestSessionEndAsync(testSessionInfo.SessionId, baseMessageBus, serviceProvider, testSessionCancellationToken);
}
catch (OperationCanceledException) when (testSessionCancellationToken.IsCancellationRequested)
{
// Do nothing we're canceled
}
// We keep the display after session out of the OperationCanceledException catch because we want to notify the IPlatformOutputDevice
// also in case of cancellation. Most likely it needs to notify users that the session was canceled.
await DisplayAfterSessionEndRunAsync(outputDevice, testSessionInfo, testSessionCancellationToken);
}
private static async Task DisplayBeforeSessionStartAsync(ProxyOutputDevice outputDevice, ITestSessionContext sessionInfo, CancellationToken cancellationToken)
{
// Display before session start
await outputDevice.DisplayBeforeSessionStartAsync();
if (outputDevice.OriginalOutputDevice is ITestSessionLifetimeHandler testSessionLifetimeHandler)
{
await testSessionLifetimeHandler.OnTestSessionStartingAsync(
sessionInfo.SessionId,
cancellationToken);
}
}
private static async Task DisplayAfterSessionEndRunAsync(ProxyOutputDevice outputDevice, ITestSessionContext sessionInfo, CancellationToken cancellationToken)
{
// Display after session end
await outputDevice.DisplayAfterSessionEndRunAsync();
// We want to ensure that the output service is the last one to run
if (outputDevice.OriginalOutputDevice is ITestSessionLifetimeHandler testSessionLifetimeHandlerFinishing)
{
await testSessionLifetimeHandlerFinishing.OnTestSessionFinishingAsync(
sessionInfo.SessionId,
cancellationToken);
}
}
private static async Task NotifyTestSessionStartAsync(SessionUid sessionUid, BaseMessageBus baseMessageBus, ServiceProvider serviceProvider, CancellationToken cancellationToken)
{
TestSessionLifetimeHandlersContainer? testSessionLifetimeHandlersContainer = serviceProvider.GetService<TestSessionLifetimeHandlersContainer>();
if (testSessionLifetimeHandlersContainer is null)
{
return;
}
foreach (ITestSessionLifetimeHandler testSessionLifetimeHandler in testSessionLifetimeHandlersContainer.TestSessionLifetimeHandlers)
{
await testSessionLifetimeHandler.OnTestSessionStartingAsync(sessionUid, cancellationToken);
}
// Drain messages generated by the session start notification before to start test execution.
await baseMessageBus.DrainDataAsync();
}
private static async Task NotifyTestSessionEndAsync(SessionUid sessionUid, BaseMessageBus baseMessageBus, ServiceProvider serviceProvider, CancellationToken cancellationToken)
{
// Drain messages generated by the test session execution before to process the session end notification.
await baseMessageBus.DrainDataAsync();
TestSessionLifetimeHandlersContainer? testSessionLifetimeHandlersContainer = serviceProvider.GetService<TestSessionLifetimeHandlersContainer>();
if (testSessionLifetimeHandlersContainer is null)
{
return;
}
foreach (ITestSessionLifetimeHandler testSessionLifetimeHandler in serviceProvider.GetRequiredService<TestSessionLifetimeHandlersContainer>().TestSessionLifetimeHandlers)
{
await testSessionLifetimeHandler.OnTestSessionFinishingAsync(sessionUid, cancellationToken);
// OnTestSessionFinishingAsync could produce information that needs to be handled by others.
await baseMessageBus.DrainDataAsync();
}
// We disable after the drain because it's possible that the drain will produce more messages
await baseMessageBus.DrainDataAsync();
await baseMessageBus.DisableAsync();
}
protected static async Task DisposeServiceProviderAsync(ServiceProvider serviceProvider, Func<object, bool>? filter = null, List<object>? alreadyDisposed = null, bool isProcessShutdown = false)
{
alreadyDisposed ??= [];
foreach (object service in serviceProvider.Services)
{
// Logger is the most special service and we dispose it manually as last one, we want to be able to
// collect logs till the end of the process.
if (service is FileLoggerProvider)
{
continue;
}
// The ITestApplicationCancellationTokenSource contains the cancellation token and can be used by other services during the shutdown
// we will collect manually in the correct moment.
if (service is ITestApplicationCancellationTokenSource)
{
continue;
}
if (filter is not null && !filter(service))
{
continue;
}
// We need to ensure that we won't dispose special services till the shutdown
if (!isProcessShutdown &&
service is ITelemetryCollector or
ITestApplicationLifecycleCallbacks or
IPushOnlyProtocol)
{
continue;
}
if (!alreadyDisposed.Contains(service))
{
await DisposeHelper.DisposeAsync(service);
alreadyDisposed.Add(service);
}
if (service is BaseMessageBus messageBus)
{
foreach (IDataConsumer dataConsumer in messageBus.DataConsumerServices)
{
if (filter is not null && !filter(dataConsumer))
{
continue;
}
if (!alreadyDisposed.Contains(dataConsumer))
{
await DisposeHelper.DisposeAsync(dataConsumer);
alreadyDisposed.Add(service);
}
}
}
}
}
}