Skip to content

Commit

Permalink
Fix SocketCommunicationManager (#2290)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoRossignoli authored and nohwnd committed Jan 13, 2020
1 parent 3ae0859 commit e8e994a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void SendTestCaseStart(TestCaseStartEventArgs e)
this.communicationManager.SendMessage(MessageType.DataCollectionTestStart, e);

var message = this.communicationManager.ReceiveMessage();
if (message.MessageType != MessageType.DataCollectionTestStartAck)
if (message != null && message.MessageType != MessageType.DataCollectionTestStartAck)
{
if (EqtTrace.IsErrorEnabled)
{
Expand All @@ -108,8 +108,7 @@ public Collection<AttachmentSet> SendTestCaseEnd(TestCaseEndEventArgs e)
this.communicationManager.SendMessage(MessageType.DataCollectionTestEnd, e);

var message = this.communicationManager.ReceiveMessage();

if (message.MessageType == MessageType.DataCollectionTestEndResult)
if (message != null && message.MessageType == MessageType.DataCollectionTestEndResult)
{
attachmentSets = this.dataSerializer.DeserializePayload<Collection<AttachmentSet>>(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,12 @@ public void SendRawMessage(string rawMessage)
public Message ReceiveMessage()
{
var rawMessage = this.ReceiveRawMessage();
return this.dataSerializer.DeserializeMessage(rawMessage);
if (!string.IsNullOrEmpty(rawMessage))
{
return this.dataSerializer.DeserializeMessage(rawMessage);
}

return null;
}

/// <summary>
Expand Down Expand Up @@ -317,7 +322,7 @@ public string ReceiveRawMessage()
lock (this.receiveSyncObject)
{
// Reading message on binaryreader is not thread-safe
return this.binaryReader.ReadString();
return this.binaryReader?.ReadString();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,22 @@ public void SocketPollShouldNotHangServerClientCommunication()
Assert.IsTrue(true);
}

[TestMethod]
public async Task ReceiveRawMessageNotConnectedSocketShouldReturnNull()
{
var peer = new SocketCommunicationManager();
Assert.IsNull(peer.ReceiveRawMessage());
Assert.IsNull(await peer.ReceiveRawMessageAsync(CancellationToken.None));
}

[TestMethod]
public async Task ReceiveMessageNotConnectedSocketShouldReturnNull()
{
var peer = new SocketCommunicationManager();
Assert.IsNull(peer.ReceiveMessage());
Assert.IsNull(await peer.ReceiveMessageAsync(CancellationToken.None));
}

private static void SendData(ICommunicationManager communicationManager)
{
// Having less than the buffer size in SocketConstants.BUFFERSIZE.
Expand Down

0 comments on commit e8e994a

Please sign in to comment.