diff --git a/src/dotnet/commands/dotnet-test/MessageHandlers/TestRunnerTestStartedMessageHandler.cs b/src/dotnet/commands/dotnet-test/MessageHandlers/TestRunnerTestStartedMessageHandler.cs index c1271fc8e..ac08ebc6c 100644 --- a/src/dotnet/commands/dotnet-test/MessageHandlers/TestRunnerTestStartedMessageHandler.cs +++ b/src/dotnet/commands/dotnet-test/MessageHandlers/TestRunnerTestStartedMessageHandler.cs @@ -14,8 +14,14 @@ namespace Microsoft.DotNet.Tools.Test protected override bool CanHandleMessage(IDotnetTest dotnetTest, Message message) { - return dotnetTest.State == DotnetTestState.TestExecutionSentTestRunnerProcessStartInfo && + return IsAtAnAcceptableState(dotnetTest) && message.MessageType == TestMessageTypes.TestRunnerTestStarted; } + + private static bool IsAtAnAcceptableState(IDotnetTest dotnetTest) + { + return dotnetTest.State == DotnetTestState.TestExecutionSentTestRunnerProcessStartInfo || + dotnetTest.State == DotnetTestState.TestExecutionStarted; + } } } diff --git a/src/dotnet/commands/dotnet-test/ReportingChannel.cs b/src/dotnet/commands/dotnet-test/ReportingChannel.cs index 2b9468bc7..1597e2c91 100644 --- a/src/dotnet/commands/dotnet-test/ReportingChannel.cs +++ b/src/dotnet/commands/dotnet-test/ReportingChannel.cs @@ -114,6 +114,11 @@ namespace Microsoft.DotNet.Tools.Test var message = JsonConvert.DeserializeObject(rawMessage); MessageReceived?.Invoke(this, message); + + if (ShouldStopListening(message)) + { + break; + } } catch (Exception ex) { @@ -127,6 +132,12 @@ namespace Microsoft.DotNet.Tools.Test } } + private static bool ShouldStopListening(Message message) + { + return message.MessageType == TestMessageTypes.TestRunnerTestCompleted || + message.MessageType == TestMessageTypes.TestSessionTerminate; + } + public void Dispose() { Socket.Dispose(); diff --git a/test/dotnet-test.UnitTests/GivenATestRunnerTestStartedMessageHandler.cs b/test/dotnet-test.UnitTests/GivenATestRunnerTestStartedMessageHandler.cs index 0d8d2da0f..1a894baa8 100644 --- a/test/dotnet-test.UnitTests/GivenATestRunnerTestStartedMessageHandler.cs +++ b/test/dotnet-test.UnitTests/GivenATestRunnerTestStartedMessageHandler.cs @@ -36,7 +36,7 @@ namespace Microsoft.Dotnet.Tools.Test.Tests } [Fact] - public void It_returns_NoOp_if_the_dotnet_test_state_is_not_TestExecutionSentTestRunnerProcessStartInfo() + public void It_returns_NoOp_if_the_dotnet_test_state_is_not_TestExecutionSentTestRunnerProcessStartInfo_or_TestExecutionTestStarted() { var dotnetTestMock = new Mock(); dotnetTestMock.Setup(d => d.State).Returns(DotnetTestState.Terminated); @@ -59,7 +59,7 @@ namespace Microsoft.Dotnet.Tools.Test.Tests } [Fact] - public void It_returns_TestExecutionStarted_when_it_handles_the_message() + public void It_returns_TestExecutionStarted_when_it_handles_the_message_and_current_state_is_TestExecutionSentTestRunnerProcessStartInfo() { var nextState = _testRunnerTestStartedMessageHandler.HandleMessage( _dotnetTestMock.Object, @@ -68,6 +68,19 @@ namespace Microsoft.Dotnet.Tools.Test.Tests nextState.Should().Be(DotnetTestState.TestExecutionStarted); } + [Fact] + public void It_returns_TestExecutionStarted_when_it_handles_the_message_and_current_state_is_TestExecutionTestStarted() + { + var dotnetTestMock = new Mock(); + dotnetTestMock.Setup(d => d.State).Returns(DotnetTestState.TestExecutionStarted); + + var nextState = _testRunnerTestStartedMessageHandler.HandleMessage( + dotnetTestMock.Object, + _validMessage); + + nextState.Should().Be(DotnetTestState.TestExecutionStarted); + } + [Fact] public void It_sends_a_TestExecutionTestStarted_when_it_handles_the_message() {