diff --git a/src/Microsoft.TestPlatform.CommunicationUtilities/TestRequestSender.cs b/src/Microsoft.TestPlatform.CommunicationUtilities/TestRequestSender.cs
index 87204b8e3c..9cebf09b3d 100644
--- a/src/Microsoft.TestPlatform.CommunicationUtilities/TestRequestSender.cs
+++ b/src/Microsoft.TestPlatform.CommunicationUtilities/TestRequestSender.cs
@@ -320,6 +320,12 @@ public void StartTestRun(TestRunCriteriaWithTests runCriteria, ITestRunEventsHan
///
public void SendTestRunCancel()
{
+ if (this.IsOperationComplete())
+ {
+ EqtTrace.Verbose("TestRequestSender: SendTestRunCancel: Operation is already complete. Skip error message.");
+ return;
+ }
+
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestRequestSender.SendTestRunCancel: Sending test run cancel.");
@@ -331,6 +337,12 @@ public void SendTestRunCancel()
///
public void SendTestRunAbort()
{
+ if (this.IsOperationComplete())
+ {
+ EqtTrace.Verbose("TestRequestSender: SendTestRunAbort: Operation is already complete. Skip error message.");
+ return;
+ }
+
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestRequestSender.SendTestRunAbort: Sending test run abort.");
diff --git a/test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/TestRequestSenderTests.cs b/test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/TestRequestSenderTests.cs
index 9dc185016c..c911923149 100644
--- a/test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/TestRequestSenderTests.cs
+++ b/test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/TestRequestSenderTests.cs
@@ -178,6 +178,30 @@ public void EndSessionShouldNotSendSessionEndMessageIfTestHostProcessExited()
this.mockDataSerializer.Verify(ds => ds.SerializeMessage(MessageType.SessionEnd), Times.Once);
}
+ [TestMethod]
+ public void EndSessionShouldNotSendTestRunCancelMessageIfClientDisconnected()
+ {
+ this.SetupFakeCommunicationChannel();
+ this.testRequestSender.DiscoverTests(new DiscoveryCriteria(), this.mockDiscoveryEventsHandler.Object);
+ this.RaiseClientDisconnectedEvent();
+
+ this.testRequestSender.SendTestRunCancel();
+
+ this.mockChannel.Verify(mockChannel => mockChannel.Send(MessageType.CancelTestRun), Times.Never);
+ }
+
+ [TestMethod]
+ public void EndSessionShouldNotSendTestRunAbortMessageIfClientDisconnected()
+ {
+ this.SetupFakeCommunicationChannel();
+ this.testRequestSender.DiscoverTests(new DiscoveryCriteria(), this.mockDiscoveryEventsHandler.Object);
+ this.RaiseClientDisconnectedEvent();
+
+ this.testRequestSender.SendTestRunAbort();
+
+ this.mockChannel.Verify(mockChannel => mockChannel.Send(MessageType.CancelTestRun), Times.Never);
+ }
+
[DataTestMethod]
[DataRow("")]
[DataRow(" ")]