diff --git a/src/Microsoft.TestPlatform.CrossPlatEngine/MultiTestRunFinalization/MultiTestRunFinalizationManager.cs b/src/Microsoft.TestPlatform.CrossPlatEngine/MultiTestRunFinalization/MultiTestRunFinalizationManager.cs index 23bb2d463b..f3a0188771 100644 --- a/src/Microsoft.TestPlatform.CrossPlatEngine/MultiTestRunFinalization/MultiTestRunFinalizationManager.cs +++ b/src/Microsoft.TestPlatform.CrossPlatEngine/MultiTestRunFinalization/MultiTestRunFinalizationManager.cs @@ -35,7 +35,7 @@ public MultiTestRunFinalizationManager(ITestPlatformEventSource testPlatformEven /// public async Task FinalizeMultiTestRunAsync(ICollection attachments, IMultiTestRunFinalizationEventsHandler eventHandler, CancellationToken cancellationToken) { - await InternalFinalizeMultiTestRunAsync(new Collection(attachments.ToList()), eventHandler, cancellationToken); + await InternalFinalizeMultiTestRunAsync(new Collection(attachments.ToList()), eventHandler, cancellationToken).ConfigureAwait(false); } /// @@ -53,30 +53,28 @@ private async Task> InternalFinalizeMultiTestRunAsync( cancellationToken.ThrowIfCancellationRequested(); var taskCompletionSource = new TaskCompletionSource(); - cancellationToken.Register(() => + using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled())) { - taskCompletionSource.TrySetCanceled(); - }); - - Task task = Task.Run(() => - { - HandleAttachments(attachments, cancellationToken); - }); + Task task = Task.Run(() => + { + HandleAttachments(attachments, cancellationToken); + }); - var completedTask = await Task.WhenAny(task, taskCompletionSource.Task); + var completedTask = await Task.WhenAny(task, taskCompletionSource.Task).ConfigureAwait(false); - if (completedTask == task) - { - await task; - eventHandler?.HandleMultiTestRunFinalizationComplete(attachments); - testPlatformEventSource.MultiTestRunFinalizationStop(attachments.Count); - return attachments; - } - else - { - eventHandler?.HandleLogMessage(ObjectModel.Logging.TestMessageLevel.Informational, "Finalization was cancelled."); - eventHandler?.HandleMultiTestRunFinalizationComplete(null); - testPlatformEventSource.MultiTestRunFinalizationStop(0); + if (completedTask == task) + { + await task; + eventHandler?.HandleMultiTestRunFinalizationComplete(attachments); + testPlatformEventSource.MultiTestRunFinalizationStop(attachments.Count); + return attachments; + } + else + { + eventHandler?.HandleLogMessage(ObjectModel.Logging.TestMessageLevel.Informational, "Finalization was cancelled."); + eventHandler?.HandleMultiTestRunFinalizationComplete(null); + testPlatformEventSource.MultiTestRunFinalizationStop(0); + } } return null; diff --git a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleRequestSender.cs b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleRequestSender.cs index c32e28bfa5..69589fe187 100644 --- a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleRequestSender.cs +++ b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleRequestSender.cs @@ -742,31 +742,32 @@ private async Task SendMessageAndListenAndReportFinalizationResultAsync(ICollect this.communicationManager.SendMessage(MessageType.MultiTestRunFinalizationStart, payload); var isMultiTestRunFinalizationComplete = false; - cancellationToken.Register(() => this.communicationManager.SendMessage(MessageType.MultiTestRunFinalizationCancel)); - - // Cycle through the messages that the vstest.console sends. - // Currently each of the operations are not separate tasks since they should not each take much time. - // This is just a notification. - while (!isMultiTestRunFinalizationComplete) + using (cancellationToken.Register(() => this.communicationManager.SendMessage(MessageType.MultiTestRunFinalizationCancel))) { - var message = await this.TryReceiveMessageAsync(); - - if (string.Equals(MessageType.MultiTestRunFinalizationComplete, message.MessageType)) + // Cycle through the messages that the vstest.console sends. + // Currently each of the operations are not separate tasks since they should not each take much time. + // This is just a notification. + while (!isMultiTestRunFinalizationComplete) { - if (EqtTrace.IsInfoEnabled) + var message = await this.TryReceiveMessageAsync().ConfigureAwait(false); + + if (string.Equals(MessageType.MultiTestRunFinalizationComplete, message.MessageType)) { - EqtTrace.Info("VsTestConsoleRequestSender.SendMessageAndListenAndReportAttachments: Process complete."); - } + if (EqtTrace.IsInfoEnabled) + { + EqtTrace.Info("VsTestConsoleRequestSender.SendMessageAndListenAndReportAttachments: Process complete."); + } - var multiTestRunFinalizationCompletePayload = this.dataSerializer.DeserializePayload(message); + var multiTestRunFinalizationCompletePayload = this.dataSerializer.DeserializePayload(message); - eventHandler.HandleMultiTestRunFinalizationComplete(multiTestRunFinalizationCompletePayload.Attachments); - isMultiTestRunFinalizationComplete = true; - } - else if (string.Equals(MessageType.TestMessage, message.MessageType)) - { - var testMessagePayload = this.dataSerializer.DeserializePayload(message); - eventHandler.HandleLogMessage(testMessagePayload.MessageLevel, testMessagePayload.Message); + eventHandler.HandleMultiTestRunFinalizationComplete(multiTestRunFinalizationCompletePayload.Attachments); + isMultiTestRunFinalizationComplete = true; + } + else if (string.Equals(MessageType.TestMessage, message.MessageType)) + { + var testMessagePayload = this.dataSerializer.DeserializePayload(message); + eventHandler.HandleLogMessage(testMessagePayload.MessageLevel, testMessagePayload.Message); + } } } } diff --git a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapperAsync.cs b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapperAsync.cs index fc0f9a40fe..0a0aaf829f 100644 --- a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapperAsync.cs +++ b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapperAsync.cs @@ -228,8 +228,8 @@ public async Task FinalizeMultiTestRunAsync(ICollection attachmen { this.testPlatformEventSource.TranslationLayerMultiTestRunFinalizationStart(); - await this.EnsureInitializedAsync(); - await requestSender.FinalizeMultiTestRunAsync(attachments, testSessionEventsHandler, cancellationToken); + await this.EnsureInitializedAsync().ConfigureAwait(false); + await requestSender.FinalizeMultiTestRunAsync(attachments, testSessionEventsHandler, cancellationToken).ConfigureAwait(false); } ///