Making the reporing channel port discovery and accept incoming connections separate. Before we were hanging when creating the reporting channel and never starting the test runner.
This commit is contained in:
parent
252eb4371f
commit
3f2b1d068d
7 changed files with 61 additions and 20 deletions
|
@ -2,6 +2,7 @@
|
|||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Testing.Abstractions;
|
||||
|
||||
namespace Microsoft.DotNet.Tools.Test
|
||||
|
@ -12,6 +13,8 @@ namespace Microsoft.DotNet.Tools.Test
|
|||
|
||||
int Port { get; }
|
||||
|
||||
void Accept();
|
||||
|
||||
void Send(Message message);
|
||||
|
||||
void SendError(string error);
|
||||
|
|
|
@ -41,6 +41,8 @@ namespace Microsoft.DotNet.Tools.Test
|
|||
|
||||
dotnetTest.StartListeningTo(testRunnerChannel);
|
||||
|
||||
testRunnerChannel.Accept();
|
||||
|
||||
var testRunner = _testRunnerFactory.CreateTestRunner(
|
||||
new RunTestsArgumentsBuilder(dotnetTest.PathToAssemblyUnderTest, testRunnerChannel.Port, message));
|
||||
|
||||
|
|
|
@ -55,6 +55,8 @@ namespace Microsoft.DotNet.Cli.Tools.Test
|
|||
|
||||
dotnetTest.StartListeningTo(testRunnerChannel);
|
||||
|
||||
testRunnerChannel.Accept();
|
||||
|
||||
var testRunner = _testRunnerFactory.CreateTestRunner(
|
||||
new DiscoverTestsArgumentsBuilder(dotnetTest.PathToAssemblyUnderTest, testRunnerChannel.Port));
|
||||
|
||||
|
|
|
@ -141,6 +141,8 @@ namespace Microsoft.DotNet.Tools.Test
|
|||
|
||||
dotnetTest.StartListeningTo(adapterChannel);
|
||||
|
||||
adapterChannel.Accept();
|
||||
|
||||
dotnetTest.StartHandlingMessages();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ using System.IO;
|
|||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Testing.Abstractions;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
@ -18,37 +19,47 @@ namespace Microsoft.DotNet.Tools.Test
|
|||
public static ReportingChannel ListenOn(int port)
|
||||
{
|
||||
// This fixes the mono incompatibility but ties it to ipv4 connections
|
||||
using (var listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
listenSocket.Bind(new IPEndPoint(IPAddress.Loopback, port));
|
||||
listenSocket.Listen(10);
|
||||
var listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
var socket = listenSocket.Accept();
|
||||
listenSocket.Bind(new IPEndPoint(IPAddress.Loopback, port));
|
||||
listenSocket.Listen(10);
|
||||
|
||||
return new ReportingChannel(socket);
|
||||
}
|
||||
return new ReportingChannel(listenSocket);
|
||||
}
|
||||
|
||||
private readonly BinaryWriter _writer;
|
||||
private readonly BinaryReader _reader;
|
||||
private BinaryWriter _writer;
|
||||
private BinaryReader _reader;
|
||||
private Socket _listenSocket;
|
||||
|
||||
private ReportingChannel(Socket socket)
|
||||
private ReportingChannel(Socket listenSocket)
|
||||
{
|
||||
Socket = socket;
|
||||
|
||||
var stream = new NetworkStream(Socket);
|
||||
_writer = new BinaryWriter(stream);
|
||||
_reader = new BinaryReader(stream);
|
||||
|
||||
// Read incoming messages on the background thread
|
||||
new Thread(ReadMessages) { IsBackground = true }.Start();
|
||||
_listenSocket = listenSocket;
|
||||
Port = ((IPEndPoint)listenSocket.LocalEndPoint).Port;
|
||||
}
|
||||
|
||||
public event EventHandler<Message> MessageReceived;
|
||||
|
||||
public Socket Socket { get; private set; }
|
||||
|
||||
public int Port => ((IPEndPoint) Socket.LocalEndPoint).Port;
|
||||
public int Port { get; }
|
||||
|
||||
public void Accept()
|
||||
{
|
||||
new Thread(() =>
|
||||
{
|
||||
using (_listenSocket)
|
||||
{
|
||||
Socket = _listenSocket.Accept();
|
||||
|
||||
var stream = new NetworkStream(Socket);
|
||||
_writer = new BinaryWriter(stream);
|
||||
_reader = new BinaryReader(stream);
|
||||
|
||||
// Read incoming messages on the background thread
|
||||
new Thread(ReadMessages) { IsBackground = true }.Start();
|
||||
}
|
||||
}) { IsBackground = true }.Start();
|
||||
}
|
||||
|
||||
public void Send(Message message)
|
||||
{
|
||||
|
@ -99,7 +110,8 @@ namespace Microsoft.DotNet.Tools.Test
|
|||
{
|
||||
try
|
||||
{
|
||||
var message = JsonConvert.DeserializeObject<Message>(_reader.ReadString());
|
||||
var rawMessage = _reader.ReadString();
|
||||
var message = JsonConvert.DeserializeObject<Message>(rawMessage);
|
||||
|
||||
MessageReceived?.Invoke(this, message);
|
||||
}
|
||||
|
|
|
@ -134,6 +134,16 @@ namespace Microsoft.Dotnet.Tools.Test.Tests
|
|||
_reportingChannelFactoryMock.Verify(r => r.CreateChannelWithAnyAvailablePort(), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void It_calls_accept_on_the_test_runner_channel()
|
||||
{
|
||||
_testDiscoveryStartMessageHandler.HandleMessage(
|
||||
_dotnetTestMock.Object,
|
||||
_validMessage);
|
||||
|
||||
_testRunnerChannelMock.Verify(t => t.Accept(), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void It_makes_dotnet_test_listen_on_the_test_runner_port_for_messages_when_it_handles_the_message()
|
||||
{
|
||||
|
|
|
@ -148,6 +148,16 @@ namespace Microsoft.Dotnet.Tools.Test.Tests
|
|||
_reportingChannelFactoryMock.Verify(r => r.CreateChannelWithAnyAvailablePort(), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void It_calls_accept_on_the_test_runner_channel()
|
||||
{
|
||||
_testGetTestRunnerProcessStartInfoMessageHandler.HandleMessage(
|
||||
_dotnetTestMock.Object,
|
||||
_validMessage);
|
||||
|
||||
_testRunnerChannelMock.Verify(t => t.Accept(), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void It_makes_dotnet_test_listen_on_the_test_runner_port_for_messages_when_it_handles_the_message()
|
||||
{
|
||||
|
|
Loading…
Add table
Reference in a new issue