Skip to content

Commit

Permalink
Do not set connected event if Connection is Faulted (#1728)
Browse files Browse the repository at this point in the history
* Raise Disconnected event if connection is not accpeted by Server

* do not set connected event to true if Faulted

* add test
  • Loading branch information
mayankbansal018 authored Aug 13, 2018
1 parent c7472a4 commit c0bd0b4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ConnectedEventArgs(Exception faultException)
public ICommunicationChannel Channel { get; private set; }

/// <summary>
/// Gets true if it's connected.
/// Gets a value indicating whether channel is connected or not, true if it's connected.
/// </summary>
public bool Connected { get; private set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void OnServerConnected(Task connectAsyncTask)
{
if (connectAsyncTask.IsFaulted)
{
this.Connected.SafeInvoke(this, new ConnectedEventArgs(connectAsyncTask.Exception), "SocketClient: ServerConnected");
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ 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) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -34,13 +35,13 @@ public class TestRequestSenderTests
private readonly Mock<IDataSerializer> mockDataSerializer;
private readonly Mock<ICommunicationChannel> mockChannel;

private readonly ConnectedEventArgs connectedEventArgs;
private readonly List<string> pathToAdditionalExtensions = new List<string> { "Hello", "World" };
private readonly Mock<ITestDiscoveryEventsHandler2> mockDiscoveryEventsHandler;
private readonly Mock<ITestRunEventsHandler> mockExecutionEventsHandler;
private readonly TestRunCriteriaWithSources testRunCriteriaWithSources;
private TestHostConnectionInfo connectionInfo;
private ITestRequestSender testRequestSender;
private ConnectedEventArgs connectedEventArgs;

public TestRequestSenderTests()
{
Expand Down Expand Up @@ -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<CancellationToken>());

Assert.IsFalse(connected);
}

[TestMethod]
public void WaitForRequestHandlerConnectionWithInfiniteTimeoutShouldReturnImmediatelyWhenCancellationRequested()
{
Expand Down

0 comments on commit c0bd0b4

Please sign in to comment.