Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure correct exit code in case of cancellation and add OnExit phase for for IPushOnlyProtocol #3820

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}