Skip to content

Commit c2900b5

Browse files
authored
Use polling for socket receive message instead of timeouts. (#120)
* Use polling for socket receive message instead of timeouts. In design mode client, wait for communication channel to be connected before sending version check. Fix #110. * Add test.
1 parent 8a8f528 commit c2900b5

File tree

5 files changed

+116
-109
lines changed

5 files changed

+116
-109
lines changed

src/Microsoft.TestPlatform.Client/DesignMode/DesignModeClient.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ namespace Microsoft.VisualStudio.TestPlatform.Client.DesignMode
77
using System.Threading;
88
using System.Threading.Tasks;
99

10+
using Microsoft.VisualStudio.TestPlatform.Client.RequestHelper;
11+
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
1012
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Interfaces;
1113
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel;
12-
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
13-
using Microsoft.VisualStudio.TestPlatform.Client.RequestHelper;
14-
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.Interfaces;
1514
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
16-
using System.Diagnostics;
1715

1816
/// <summary>
1917
/// The design mode client.
@@ -47,6 +45,9 @@ public DesignModeClient()
4745
/// <param name="communicationManager">
4846
/// The communication manager.
4947
/// </param>
48+
/// <param name="dataSerializer">
49+
/// The data Serializer.
50+
/// </param>
5051
internal DesignModeClient(ICommunicationManager communicationManager, IDataSerializer dataSerializer)
5152
{
5253
this.communicationManager = communicationManager;
@@ -69,16 +70,21 @@ public static void Initialize()
6970
/// <summary>
7071
/// Creates a client and waits for server to accept connection asynchronously
7172
/// </summary>
72-
/// <param name="port">port number to connect</param>
73+
/// <param name="port">
74+
/// Port number to connect
75+
/// </param>
76+
/// <param name="testRequestManager">
77+
/// The test Request Manager.
78+
/// </param>
7379
public void ConnectToClientAndProcessRequests(int port, ITestRequestManager testRequestManager)
7480
{
7581
EqtTrace.Info("Trying to connect to server on port : {0}", port);
7682
this.communicationManager.SetupClientAsync(port);
77-
this.communicationManager.SendMessage(MessageType.SessionConnected);
7883

7984
// Wait for the connection to the server and listen for requests.
8085
if (this.communicationManager.WaitForServerConnection(ClientListenTimeOut))
8186
{
87+
this.communicationManager.SendMessage(MessageType.SessionConnected);
8288
this.ProcessRequests(testRequestManager);
8389
}
8490
else
@@ -99,7 +105,9 @@ public void HandleParentProcessExit()
99105
/// <summary>
100106
/// Process Requests from the IDE
101107
/// </summary>
102-
/// <param name="handler"></param>
108+
/// <param name="testRequestManager">
109+
/// The test Request Manager.
110+
/// </param>
103111
private void ProcessRequests(ITestRequestManager testRequestManager)
104112
{
105113
var isSessionEnd = false;
@@ -202,7 +210,9 @@ private void ProcessRequests(ITestRequestManager testRequestManager)
202210
/// <summary>
203211
/// Send a custom host launch message to IDE
204212
/// </summary>
205-
/// <param name="customTestHostLaunchPayload">Payload required to launch a custom host</param>
213+
/// <param name="testProcessStartInfo">
214+
/// The test Process Start Info.
215+
/// </param>
206216
public int LaunchCustomHost(TestProcessStartInfo testProcessStartInfo)
207217
{
208218
lock (ackLockObject)

src/Microsoft.TestPlatform.CommunicationUtilities/Messages/MessageType.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,5 @@ public static class MessageType
160160
public const string AfterTestRunEndResult = "DataCollection.AfterTestRunEndResult";
161161

162162
#endregion
163-
164163
}
165164
}

src/Microsoft.TestPlatform.CommunicationUtilities/SocketCommunicationManager.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,17 @@ public class SocketCommunicationManager : ICommunicationManager
6464
/// </summary>
6565
private NetworkStream stream;
6666

67+
private Socket socket;
68+
6769
/// <summary>
68-
/// The server stream read timeout constant.
70+
/// The server stream read timeout constant (in microseconds).
6971
/// </summary>
70-
private const int StreamReadTimeout = 1000;
72+
private const int StreamReadTimeout = 1000 * 1000;
7173

72-
public SocketCommunicationManager(): this(JsonDataSerializer.Instance)
74+
/// <summary>
75+
/// Initializes a new instance of the <see cref="SocketCommunicationManager"/> class.
76+
/// </summary>
77+
public SocketCommunicationManager() : this(JsonDataSerializer.Instance)
7378
{
7479
}
7580

@@ -107,6 +112,7 @@ public async Task AcceptClientAsync()
107112
this.clientConnectedEvent.Reset();
108113

109114
var client = await this.tcpListener.AcceptTcpClientAsync();
115+
this.socket = client.Client;
110116
this.stream = client.GetStream();
111117
this.binaryReader = new BinaryReader(this.stream);
112118
this.binaryWriter = new BinaryWriter(this.stream);
@@ -150,6 +156,7 @@ public async Task SetupClientAsync(int portNumber)
150156
this.clientConnectionAcceptedEvent.Reset();
151157
EqtTrace.Info("Trying to connect to server on port : {0}", portNumber);
152158
this.tcpClient = new TcpClient();
159+
this.socket = this.tcpClient.Client;
153160
await this.tcpClient.ConnectAsync(IPAddress.Loopback, portNumber);
154161
this.stream = this.tcpClient.GetStream();
155162
this.binaryReader = new BinaryReader(this.stream);
@@ -292,13 +299,15 @@ private string TryReceiveRawMessage(CancellationToken cancellationToken)
292299
bool success = false;
293300

294301
// Set read timeout to avoid blocking receive raw message
295-
this.stream.ReadTimeout = StreamReadTimeout;
296302
while (!cancellationToken.IsCancellationRequested && !success)
297303
{
298304
try
299305
{
300-
str = this.ReceiveRawMessage();
301-
success = true;
306+
if (this.socket.Poll(StreamReadTimeout, SelectMode.SelectRead))
307+
{
308+
str = this.ReceiveRawMessage();
309+
success = true;
310+
}
302311
}
303312
catch (IOException ioException)
304313
{
@@ -327,7 +336,6 @@ private string TryReceiveRawMessage(CancellationToken cancellationToken)
327336
}
328337
}
329338

330-
this.stream.ReadTimeout = -1;
331339
return str;
332340
}
333341

src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleRequestSender.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public void StartTestRunWithCustomHost(
198198
/// </summary>
199199
public void CancelTestRun()
200200
{
201-
this.communicationManager.SendMessage(MessageType.CancelTestRun);
201+
this.communicationManager.SendMessage(MessageType.CancelTestRun);
202202
}
203203

204204
/// <summary>
@@ -365,7 +365,7 @@ private void SendMessageAndListenAndReportTestResults(string messageType, object
365365
eventHandler.HandleLogMessage(TestMessageLevel.Error, Resources.AbortedTestsRun);
366366
var completeArgs = new TestRunCompleteEventArgs(null, false, true, exception, null, TimeSpan.Zero);
367367
eventHandler.HandleTestRunComplete(completeArgs, null, null, null);
368-
CleanupCommunicationIfProcessExit();
368+
this.CleanupCommunicationIfProcessExit();
369369
}
370370
}
371371

@@ -377,6 +377,7 @@ private void CleanupCommunicationIfProcessExit()
377377
this.communicationManager.StopServer();
378378
}
379379
}
380+
380381
private Message TryReceiveMessage()
381382
{
382383
Message message = null;

0 commit comments

Comments
 (0)