From 64bf9b0758a34136bad0698f949ff90b94ef9fb8 Mon Sep 17 00:00:00 2001 From: Mayank Bansal Date: Mon, 13 Aug 2018 18:50:57 +0530 Subject: [PATCH 1/3] Raise Disconnected event if connection is not accpeted by Server --- .../SocketClient.cs | 2 +- .../TestRequestSender.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.TestPlatform.CommunicationUtilities/SocketClient.cs b/src/Microsoft.TestPlatform.CommunicationUtilities/SocketClient.cs index 4459dc8ec2..6bd10c6f7a 100644 --- a/src/Microsoft.TestPlatform.CommunicationUtilities/SocketClient.cs +++ b/src/Microsoft.TestPlatform.CommunicationUtilities/SocketClient.cs @@ -80,7 +80,7 @@ private void OnServerConnected(Task connectAsyncTask) { if (connectAsyncTask.IsFaulted) { - this.Connected.SafeInvoke(this, new ConnectedEventArgs(connectAsyncTask.Exception), "SocketClient: ServerConnected"); + this.Disconnected.SafeInvoke(this, new DisconnectedEventArgs() { Error = connectAsyncTask.Exception }, "SocketClient: Server Failed to Connect"); if (EqtTrace.IsVerboseEnabled) { EqtTrace.Verbose("Unable to connect to server, Exception occured : {0}", connectAsyncTask.Exception); diff --git a/src/Microsoft.TestPlatform.CommunicationUtilities/TestRequestSender.cs b/src/Microsoft.TestPlatform.CommunicationUtilities/TestRequestSender.cs index 89c4b222d0..b257fd2980 100644 --- a/src/Microsoft.TestPlatform.CommunicationUtilities/TestRequestSender.cs +++ b/src/Microsoft.TestPlatform.CommunicationUtilities/TestRequestSender.cs @@ -124,8 +124,8 @@ public int InitializeCommunication() }; this.communicationEndpoint.Disconnected += (sender, args) => { - // If there's an disconnected event handler, call it - this.onDisconnected?.Invoke(args); + // throw the exception accumulated from TCP/IP stack + throw new TestPlatformException(args.Error.ToString()); }; // Server start returns the listener port From 4579ea6767fd5012beb10918c9134d1e5c91f85e Mon Sep 17 00:00:00 2001 From: Mayank Bansal Date: Mon, 13 Aug 2018 19:20:22 +0530 Subject: [PATCH 2/3] do not set connected event to true if Faulted --- .../Interfaces/Communication/ConnectedEventArgs.cs | 2 +- .../SocketClient.cs | 2 +- .../TestRequestSender.cs | 10 +++++++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.TestPlatform.CommunicationUtilities/Interfaces/Communication/ConnectedEventArgs.cs b/src/Microsoft.TestPlatform.CommunicationUtilities/Interfaces/Communication/ConnectedEventArgs.cs index 63747df454..7e80d8582a 100644 --- a/src/Microsoft.TestPlatform.CommunicationUtilities/Interfaces/Communication/ConnectedEventArgs.cs +++ b/src/Microsoft.TestPlatform.CommunicationUtilities/Interfaces/Communication/ConnectedEventArgs.cs @@ -39,7 +39,7 @@ public ConnectedEventArgs(Exception faultException) public ICommunicationChannel Channel { get; private set; } /// - /// Gets true if it's connected. + /// Gets a value indicating whether channel is connected or not true if it's connected. /// public bool Connected { get; private set; } diff --git a/src/Microsoft.TestPlatform.CommunicationUtilities/SocketClient.cs b/src/Microsoft.TestPlatform.CommunicationUtilities/SocketClient.cs index 6bd10c6f7a..e7dbdd25ec 100644 --- a/src/Microsoft.TestPlatform.CommunicationUtilities/SocketClient.cs +++ b/src/Microsoft.TestPlatform.CommunicationUtilities/SocketClient.cs @@ -80,7 +80,7 @@ private void OnServerConnected(Task connectAsyncTask) { if (connectAsyncTask.IsFaulted) { - this.Disconnected.SafeInvoke(this, new DisconnectedEventArgs() { Error = connectAsyncTask.Exception }, "SocketClient: Server Failed to Connect"); + this.Connected.SafeInvoke(this, new ConnectedEventArgs(connectAsyncTask.Exception), "SocketClient: Server Failed to Connect"); if (EqtTrace.IsVerboseEnabled) { EqtTrace.Verbose("Unable to connect to server, Exception occured : {0}", connectAsyncTask.Exception); diff --git a/src/Microsoft.TestPlatform.CommunicationUtilities/TestRequestSender.cs b/src/Microsoft.TestPlatform.CommunicationUtilities/TestRequestSender.cs index b257fd2980..e16708680e 100644 --- a/src/Microsoft.TestPlatform.CommunicationUtilities/TestRequestSender.cs +++ b/src/Microsoft.TestPlatform.CommunicationUtilities/TestRequestSender.cs @@ -120,12 +120,16 @@ public int InitializeCommunication() this.communicationEndpoint.Connected += (sender, args) => { this.channel = args.Channel; - this.connected.Set(); + + if (args.Connected && this.channel != null) + { + this.connected.Set(); + } }; this.communicationEndpoint.Disconnected += (sender, args) => { - // throw the exception accumulated from TCP/IP stack - throw new TestPlatformException(args.Error.ToString()); + // If there's an disconnected event handler, call it + this.onDisconnected?.Invoke(args); }; // Server start returns the listener port From 41eab81bfa64984ec09198d71de803bc6f37d1d4 Mon Sep 17 00:00:00 2001 From: Mayank Bansal Date: Mon, 13 Aug 2018 20:05:59 +0530 Subject: [PATCH 3/3] add test --- .../Interfaces/Communication/ConnectedEventArgs.cs | 2 +- .../TestRequestSenderTests.cs | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.TestPlatform.CommunicationUtilities/Interfaces/Communication/ConnectedEventArgs.cs b/src/Microsoft.TestPlatform.CommunicationUtilities/Interfaces/Communication/ConnectedEventArgs.cs index 7e80d8582a..ae248cca92 100644 --- a/src/Microsoft.TestPlatform.CommunicationUtilities/Interfaces/Communication/ConnectedEventArgs.cs +++ b/src/Microsoft.TestPlatform.CommunicationUtilities/Interfaces/Communication/ConnectedEventArgs.cs @@ -39,7 +39,7 @@ public ConnectedEventArgs(Exception faultException) public ICommunicationChannel Channel { get; private set; } /// - /// Gets a value indicating whether channel is connected or not true if it's connected. + /// Gets a value indicating whether channel is connected or not, true if it's connected. /// public bool Connected { get; private set; } diff --git a/test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/TestRequestSenderTests.cs b/test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/TestRequestSenderTests.cs index 9dad040c56..fea9e9156b 100644 --- a/test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/TestRequestSenderTests.cs +++ b/test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/TestRequestSenderTests.cs @@ -8,6 +8,7 @@ namespace Microsoft.TestPlatform.CommunicationUtilities.UnitTests using System.Globalization; using System.Linq; using System.Net; + using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; @@ -34,13 +35,13 @@ public class TestRequestSenderTests private readonly Mock mockDataSerializer; private readonly Mock mockChannel; - private readonly ConnectedEventArgs connectedEventArgs; private readonly List pathToAdditionalExtensions = new List { "Hello", "World" }; private readonly Mock mockDiscoveryEventsHandler; private readonly Mock mockExecutionEventsHandler; private readonly TestRunCriteriaWithSources testRunCriteriaWithSources; private TestHostConnectionInfo connectionInfo; private ITestRequestSender testRequestSender; + private ConnectedEventArgs connectedEventArgs; public TestRequestSenderTests() { @@ -85,6 +86,17 @@ public void WaitForRequestHandlerConnectionShouldWaitForClientToConnect() Assert.IsTrue(connected); } + [TestMethod] + public void WaitForRequestHandlerConnectionShouldNotConnectIfExceptionWasThrownByTcpLayer() + { + this.connectedEventArgs = new ConnectedEventArgs(new SocketException()); + this.SetupFakeCommunicationChannel(); + + var connected = this.testRequestSender.WaitForRequestHandlerConnection(1, It.IsAny()); + + Assert.IsFalse(connected); + } + [TestMethod] public void WaitForRequestHandlerConnectionWithInfiniteTimeoutShouldReturnImmediatelyWhenCancellationRequested() {