More AsyncSocketListener patchwork (#1408)

* More AsyncSocketListener patchwork

This is Whack-a-mole part 2 in diagnosing or preventing the test host from crashing in CI.

* Fix test
This commit is contained in:
Rob Hague
2024-05-19 11:11:25 +02:00
committed by GitHub
parent 1143ad3d24
commit aa70718a86
2 changed files with 17 additions and 10 deletions
@@ -130,12 +130,21 @@ namespace Renci.SshNet.Tests.Classes.Channels
})
.Returns(WaitResult.Success);
using var barrier = new Barrier(2);
var localEndpoint = new IPEndPoint(IPAddress.Loopback, 8122);
_listener = new AsyncSocketListener(localEndpoint);
_listener.Connected += socket =>
{
try
{
// We need the Connect side and the Accept side to be
// fully completed before continuing: we are implicitly
// checking that RemoteEndPoint on the Accept socket
// matches LocalEndPoint on the Connect socket when
// checking the correctness of the ChannelOpenMessage
// in the mock.
_ = barrier.SignalAndWait(TimeSpan.FromSeconds(1));
_channel = new ChannelDirectTcpip(_sessionMock.Object,
_localChannelNumber,
_localWindowSize,
@@ -156,6 +165,7 @@ namespace Renci.SshNet.Tests.Classes.Channels
_client = new Socket(localEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
_client.Connect(localEndpoint);
_ = barrier.SignalAndWait(TimeSpan.FromSeconds(1));
var clientReceiveThread = new Thread(
() =>
@@ -234,11 +234,7 @@ namespace Renci.SshNet.Tests.Common
try
{
// Read data from the client socket.
bytesRead = handler.EndReceive(ar, out var errorCode);
if (errorCode != SocketError.Success)
{
bytesRead = 0;
}
bytesRead = handler.EndReceive(ar);
}
catch (SocketException ex)
{
@@ -275,7 +271,7 @@ namespace Renci.SshNet.Tests.Common
return;
}
void ConnectionDisconnected(bool doShutdownIfApplicable)
void ConnectionDisconnected()
{
SignalDisconnected(handler);
@@ -290,11 +286,12 @@ namespace Renci.SshNet.Tests.Common
try
{
if (doShutdownIfApplicable)
if (handler.Connected)
{
handler.Shutdown(SocketShutdown.Send);
handler.Close();
}
handler.Close();
}
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.ConnectionReset)
{
@@ -328,7 +325,7 @@ namespace Renci.SshNet.Tests.Common
catch (ObjectDisposedException)
{
// TODO On .NET 7, sometimes we get ObjectDisposedException when _started but only on appveyor, locally it works
ConnectionDisconnected(doShutdownIfApplicable: false);
ConnectionDisconnected();
}
catch (SocketException ex)
{
@@ -343,7 +340,7 @@ namespace Renci.SshNet.Tests.Common
}
else
{
ConnectionDisconnected(doShutdownIfApplicable: true);
ConnectionDisconnected();
}
}