Skip to content

Commit

Permalink
Ensure correct exit code in case of cancellation and add OnExit pha…
Browse files Browse the repository at this point in the history
…se for for `IPushOnlyProtocol` (#3820)

Co-authored-by: Marco Rossignoli <[email protected]>
  • Loading branch information
MarcoRossignoli and Marco Rossignoli authored Sep 12, 2024
1 parent 80e4078 commit 7b65d6d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
39 changes: 30 additions & 9 deletions src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,43 @@ public async Task<int> RunAsync()
{
CancellationToken testApplicationCancellationToken = ServiceProvider.GetTestApplicationCancellationTokenSource().CancellationToken;

int exitCode;
int exitCode = ExitCodes.GenericFailure;
try
{
if (PushOnlyProtocol is null || PushOnlyProtocol?.IsServerMode == false)
{
exitCode = await RunTestAppAsync(testApplicationCancellationToken);

if (testApplicationCancellationToken.IsCancellationRequested)
{
exitCode = ExitCodes.TestSessionAborted;
}

return exitCode;
}

RoslynDebug.Assert(PushOnlyProtocol is not null);
try
{
RoslynDebug.Assert(PushOnlyProtocol is not null);

ITestApplicationModuleInfo testApplicationModuleInfo = serviceProvider.GetTestApplicationModuleInfo();
bool isValidProtocol = await PushOnlyProtocol.IsCompatibleProtocolAsync(GetHostType());
ITestApplicationModuleInfo testApplicationModuleInfo = serviceProvider.GetTestApplicationModuleInfo();
bool isValidProtocol = await PushOnlyProtocol.IsCompatibleProtocolAsync(GetHostType());

exitCode = isValidProtocol
? await RunTestAppAsync(testApplicationCancellationToken)
: ExitCodes.IncompatibleProtocolVersion;
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
exitCode = ExitCodes.TestSessionAborted;
}
finally
{
Expand All @@ -57,6 +72,11 @@ public async Task<int> RunAsync()
await DisposeHelper.DisposeAsync(ServiceProvider.GetTestApplicationCancellationTokenSource());
}

if (testApplicationCancellationToken.IsCancellationRequested)
{
exitCode = ExitCodes.TestSessionAborted;
}

return exitCode;
}

Expand Down Expand Up @@ -217,7 +237,8 @@ protected static async Task DisposeServiceProviderAsync(ServiceProvider serviceP
// We need to ensure that we won't dispose special services till the shutdown
if (!isProcessShutdown &&
service is ITelemetryCollector or
ITestApplicationLifecycleCallbacks)
ITestApplicationLifecycleCallbacks or
IPushOnlyProtocol)
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public async Task SendMessageAsync(IRequest message)
}
}

public Task OnExitAsync() => Task.CompletedTask;

public void Dispose() => _dotnetTestPipeClient?.Dispose();

#if NETCOREAPP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ internal interface IPushOnlyProtocol :
Task<bool> IsCompatibleProtocolAsync(string testHostType);

Task<IPushOnlyProtocolConsumer> GetDataConsumerAsync();

Task OnExitAsync();
}

0 comments on commit 7b65d6d

Please sign in to comment.