diff --git a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleRequestSender.cs b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleRequestSender.cs index 5516f33c0b..b263203063 100644 --- a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleRequestSender.cs +++ b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleRequestSender.cs @@ -782,7 +782,6 @@ private async Task SendMessageAndListenAndReportFinalizationResultAsync(ICollect // Also, connection termination might not kill the process which could result in files being locked by testhost. } - // TODO: do we need events? this.testPlatformEventSource.TranslationLayerMultiTestRunFinalizationStop(); } diff --git a/test/TranslationLayer.UnitTests/VsTestConsoleRequestSenderTests.cs b/test/TranslationLayer.UnitTests/VsTestConsoleRequestSenderTests.cs index 457856254d..d3d45ca6a7 100644 --- a/test/TranslationLayer.UnitTests/VsTestConsoleRequestSenderTests.cs +++ b/test/TranslationLayer.UnitTests/VsTestConsoleRequestSenderTests.cs @@ -1899,6 +1899,173 @@ public async Task StartTestRunAsyncShouldLogErrorOnProcessExited() #endregion + #region Finalization Tests + + [TestMethod] + public async Task FinalizeTestsShouldCompleteWithZeroAttachments() + { + await this.InitializeCommunicationAsync(); + + var mockHandler = new Mock(); + + var payload = new MultiTestRunFinalizationCompletePayload() { Attachments = new AttachmentSet[0] }; + var finalizationComplete = new Message() + { + MessageType = MessageType.MultiTestRunFinalizationComplete, + Payload = JToken.FromObject(payload) + }; + this.mockCommunicationManager.Setup(cm => cm.ReceiveMessageAsync(It.IsAny())).Returns(Task.FromResult(finalizationComplete)); + + await this.requestSenderAsync.FinalizeMultiTestRunAsync(new List { new AttachmentSet(new Uri("http://www.bing.com"), "a") }, mockHandler.Object, CancellationToken.None); + + mockCommunicationManager.Verify(c => c.SendMessage(MessageType.MultiTestRunFinalizationStart, It.IsAny())); + mockCommunicationManager.Verify(c => c.SendMessage(MessageType.MultiTestRunFinalizationCancel), Times.Never); + mockHandler.Verify(mh => mh.HandleMultiTestRunFinalizationComplete(It.Is>(a => a.Count == 0)), Times.Once, "Discovery Complete must be called"); + mockHandler.Verify(mh => mh.HandleLogMessage(It.IsAny(), It.IsAny()), Times.Never, "TestMessage event must not be called"); + } + + [TestMethod] + public async Task FinalizeTestsShouldCompleteWithOneAttachment() + { + await this.InitializeCommunicationAsync(); + + var mockHandler = new Mock(); + + var payload = new MultiTestRunFinalizationCompletePayload() + { + Attachments = new List { new AttachmentSet(new Uri("http://www.bing.com"), "out") } + }; + var finalizationComplete = new Message() + { + MessageType = MessageType.MultiTestRunFinalizationComplete, + Payload = JToken.FromObject(payload) + }; + this.mockCommunicationManager.Setup(cm => cm.ReceiveMessageAsync(It.IsAny())).Returns(Task.FromResult(finalizationComplete)); + + await this.requestSenderAsync.FinalizeMultiTestRunAsync(new List { new AttachmentSet(new Uri("http://www.bing.com"), "a") }, mockHandler.Object, CancellationToken.None); + + mockCommunicationManager.Verify(c => c.SendMessage(MessageType.MultiTestRunFinalizationStart, It.IsAny())); + mockCommunicationManager.Verify(c => c.SendMessage(MessageType.MultiTestRunFinalizationCancel), Times.Never); + mockHandler.Verify(mh => mh.HandleMultiTestRunFinalizationComplete(It.Is>(a => a.Count == 1)), Times.Once, "Discovery Complete must be called"); + mockHandler.Verify(mh => mh.HandleLogMessage(It.IsAny(), It.IsAny()), Times.Never, "TestMessage event must not be called"); + } + + [TestMethod] + public async Task FinalizeTestsShouldCompleteWithOneAttachmentAndTestMessage() + { + await this.InitializeCommunicationAsync(); + + var mockHandler = new Mock(); + + var payload = new MultiTestRunFinalizationCompletePayload() + { + Attachments = new List { new AttachmentSet(new Uri("http://www.bing.com"), "out") } + }; + var finalizationComplete = new Message() + { + MessageType = MessageType.MultiTestRunFinalizationComplete, + Payload = JToken.FromObject(payload) + }; + + var mpayload = new TestMessagePayload() { MessageLevel = TestMessageLevel.Informational, Message = "Hello" }; + var message = CreateMessage(MessageType.TestMessage, mpayload); + + this.mockCommunicationManager.Setup(cm => cm.ReceiveMessageAsync(It.IsAny())).Returns(Task.FromResult(message)); + mockHandler.Setup(mh => mh.HandleLogMessage(It.IsAny(), It.IsAny())).Callback( + () => this.mockCommunicationManager.Setup(cm => cm.ReceiveMessageAsync(It.IsAny())).Returns(Task.FromResult(finalizationComplete))); + + await this.requestSenderAsync.FinalizeMultiTestRunAsync(new List { new AttachmentSet(new Uri("http://www.bing.com"), "a") }, mockHandler.Object, CancellationToken.None); + + mockCommunicationManager.Verify(c => c.SendMessage(MessageType.MultiTestRunFinalizationStart, It.IsAny())); + mockCommunicationManager.Verify(c => c.SendMessage(MessageType.MultiTestRunFinalizationCancel), Times.Never); + mockHandler.Verify(mh => mh.HandleMultiTestRunFinalizationComplete(It.Is>(a => a.Count == 1)), Times.Once, "Discovery Complete must be called"); + mockHandler.Verify(mh => mh.HandleLogMessage(TestMessageLevel.Informational, "Hello"), Times.Once, "TestMessage event must be called"); + } + + [TestMethod] + public async Task FinalizeTestsShouldSendCancelMessageIfCancellationTokenCancelled() + { + await this.InitializeCommunicationAsync(); + + var cts = new CancellationTokenSource(); + + var mockHandler = new Mock(); + + var payload = new MultiTestRunFinalizationCompletePayload() + { + Attachments = new List { new AttachmentSet(new Uri("http://www.bing.com"), "out") } + }; + var finalizationComplete = new Message() + { + MessageType = MessageType.MultiTestRunFinalizationComplete, + Payload = JToken.FromObject(payload) + }; + + var mpayload = new TestMessagePayload() { MessageLevel = TestMessageLevel.Informational, Message = "Hello" }; + var message = CreateMessage(MessageType.TestMessage, mpayload); + + this.mockCommunicationManager.Setup(cm => cm.ReceiveMessageAsync(It.IsAny())).Returns(Task.FromResult(message)); + mockHandler.Setup(mh => mh.HandleLogMessage(It.IsAny(), It.IsAny())).Callback(() => + { + cts.Cancel(); + this.mockCommunicationManager.Setup(cm => cm.ReceiveMessageAsync(It.IsAny())).Returns(Task.FromResult(finalizationComplete)); + }); + + await this.requestSenderAsync.FinalizeMultiTestRunAsync(new List { new AttachmentSet(new Uri("http://www.bing.com"), "a") }, mockHandler.Object, cts.Token); + + mockCommunicationManager.Verify(c => c.SendMessage(MessageType.MultiTestRunFinalizationStart, It.IsAny())); + mockCommunicationManager.Verify(c => c.SendMessage(MessageType.MultiTestRunFinalizationCancel)); + mockHandler.Verify(mh => mh.HandleMultiTestRunFinalizationComplete(It.Is>(a => a.Count == 1)), Times.Once, "Discovery Complete must be called"); + mockHandler.Verify(mh => mh.HandleLogMessage(TestMessageLevel.Informational, "Hello"), Times.Once, "TestMessage event must be called"); + } + + [TestMethod] + public async Task FinalizeTestsShouldSendCancelMessageIfCancellationTokenCancelledAtTheBeginning() + { + await this.InitializeCommunicationAsync(); + + var cts = new CancellationTokenSource(); + cts.Cancel(); + + var mockHandler = new Mock(); + + var payload = new MultiTestRunFinalizationCompletePayload() + { + Attachments = new List { new AttachmentSet(new Uri("http://www.bing.com"), "out") } + }; + var finalizationComplete = new Message() + { + MessageType = MessageType.MultiTestRunFinalizationComplete, + Payload = JToken.FromObject(payload) + }; + + this.mockCommunicationManager.Setup(cm => cm.ReceiveMessageAsync(It.IsAny())).Returns(Task.FromResult(finalizationComplete)); + + await this.requestSenderAsync.FinalizeMultiTestRunAsync(new List { new AttachmentSet(new Uri("http://www.bing.com"), "a") }, mockHandler.Object, cts.Token); + + mockCommunicationManager.Verify(c => c.SendMessage(MessageType.MultiTestRunFinalizationStart, It.IsAny())); + mockCommunicationManager.Verify(c => c.SendMessage(MessageType.MultiTestRunFinalizationCancel)); + mockHandler.Verify(mh => mh.HandleMultiTestRunFinalizationComplete(It.Is>(a => a.Count == 1)), Times.Once, "Discovery Complete must be called"); + mockHandler.Verify(mh => mh.HandleLogMessage(TestMessageLevel.Informational, "Hello"), Times.Never, "TestMessage event must be called"); + } + + [TestMethod] + public async Task FinalizeTestsShouldAbortOnExceptionInSendMessage() + { + var mockHandler = new Mock(); + this.mockCommunicationManager.Setup(cm => cm.SendMessage(MessageType.MultiTestRunFinalizationStart, It.IsAny())).Throws(new IOException()); + + await this.requestSenderAsync.FinalizeMultiTestRunAsync(new List { new AttachmentSet(new Uri("http://www.bing.com"), "out") }, mockHandler.Object, CancellationToken.None); + + mockHandler.Verify(mh => mh.HandleMultiTestRunFinalizationComplete(null), Times.Once, "Discovery Complete must be called"); + mockHandler.Verify(mh => mh.HandleLogMessage(TestMessageLevel.Error, It.IsAny()), Times.Once, "TestMessage event must be called"); + this.mockCommunicationManager.Verify(cm => cm.StopServer(), Times.Never); + } + + #endregion + + + #region Private Methods ///