Skip to content

Commit 7b65d6d

Browse files
MarcoRossignoliMarco Rossignoli
andauthored
Ensure correct exit code in case of cancellation and add OnExit phase for for IPushOnlyProtocol (#3820)
Co-authored-by: Marco Rossignoli <mrossignol@microsoft.com>
1 parent 80e4078 commit 7b65d6d

3 files changed

Lines changed: 34 additions & 9 deletions

File tree

src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,43 @@ public async Task<int> RunAsync()
2626
{
2727
CancellationToken testApplicationCancellationToken = ServiceProvider.GetTestApplicationCancellationTokenSource().CancellationToken;
2828

29-
int exitCode;
29+
int exitCode = ExitCodes.GenericFailure;
3030
try
3131
{
3232
if (PushOnlyProtocol is null || PushOnlyProtocol?.IsServerMode == false)
3333
{
3434
exitCode = await RunTestAppAsync(testApplicationCancellationToken);
35+
36+
if (testApplicationCancellationToken.IsCancellationRequested)
37+
{
38+
exitCode = ExitCodes.TestSessionAborted;
39+
}
40+
3541
return exitCode;
3642
}
3743

38-
RoslynDebug.Assert(PushOnlyProtocol is not null);
44+
try
45+
{
46+
RoslynDebug.Assert(PushOnlyProtocol is not null);
3947

40-
ITestApplicationModuleInfo testApplicationModuleInfo = serviceProvider.GetTestApplicationModuleInfo();
41-
bool isValidProtocol = await PushOnlyProtocol.IsCompatibleProtocolAsync(GetHostType());
48+
ITestApplicationModuleInfo testApplicationModuleInfo = serviceProvider.GetTestApplicationModuleInfo();
49+
bool isValidProtocol = await PushOnlyProtocol.IsCompatibleProtocolAsync(GetHostType());
4250

43-
exitCode = isValidProtocol
44-
? await RunTestAppAsync(testApplicationCancellationToken)
45-
: ExitCodes.IncompatibleProtocolVersion;
51+
exitCode = isValidProtocol
52+
? await RunTestAppAsync(testApplicationCancellationToken)
53+
: ExitCodes.IncompatibleProtocolVersion;
54+
}
55+
finally
56+
{
57+
if (PushOnlyProtocol is not null)
58+
{
59+
await PushOnlyProtocol.OnExitAsync();
60+
}
61+
}
4662
}
4763
catch (OperationCanceledException) when (testApplicationCancellationToken.IsCancellationRequested)
4864
{
4965
// We do nothing we're canceling
50-
exitCode = ExitCodes.TestSessionAborted;
5166
}
5267
finally
5368
{
@@ -57,6 +72,11 @@ public async Task<int> RunAsync()
5772
await DisposeHelper.DisposeAsync(ServiceProvider.GetTestApplicationCancellationTokenSource());
5873
}
5974

75+
if (testApplicationCancellationToken.IsCancellationRequested)
76+
{
77+
exitCode = ExitCodes.TestSessionAborted;
78+
}
79+
6080
return exitCode;
6181
}
6282

@@ -217,7 +237,8 @@ protected static async Task DisposeServiceProviderAsync(ServiceProvider serviceP
217237
// We need to ensure that we won't dispose special services till the shutdown
218238
if (!isProcessShutdown &&
219239
service is ITelemetryCollector or
220-
ITestApplicationLifecycleCallbacks)
240+
ITestApplicationLifecycleCallbacks or
241+
IPushOnlyProtocol)
221242
{
222243
continue;
223244
}

src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/DotnetTestConnection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ public async Task SendMessageAsync(IRequest message)
142142
}
143143
}
144144

145+
public Task OnExitAsync() => Task.CompletedTask;
146+
145147
public void Dispose() => _dotnetTestPipeClient?.Dispose();
146148

147149
#if NETCOREAPP

src/Platform/Microsoft.Testing.Platform/ServerMode/IPushOnlyProtocol.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ internal interface IPushOnlyProtocol :
2020
Task<bool> IsCompatibleProtocolAsync(string testHostType);
2121

2222
Task<IPushOnlyProtocolConsumer> GetDataConsumerAsync();
23+
24+
Task OnExitAsync();
2325
}

0 commit comments

Comments
 (0)