Skip to content

Commit

Permalink
Revert "Self review"
Browse files Browse the repository at this point in the history
This reverts commit 5980570.
  • Loading branch information
WojciechNagorski committed Apr 17, 2023
1 parent db06b60 commit 8201be4
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ protected virtual void SetupMocks()
{
}

protected override void AfterAct()
{
// Give some time to process all messages
Thread.Sleep(400);
}

protected sealed override void Arrange()
{
CreateMocks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@ protected override void TearDown()
{
base.TearDown();

if (_proxyServer != null)
{
_proxyServer.Dispose();
}

if (_clientSocket != null)
{
_clientSocket.Shutdown(SocketShutdown.Send);
_clientSocket.Close();
}

if (_proxyServer != null)
{
_proxyServer.Dispose();
}
}

protected override void Act()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ protected sealed override void Arrange()
SetupMocks();
}

protected override void AfterAct()
{
// Give some time to process all messages
Thread.Sleep(200);
}

protected ConnectionInfo CreateConnectionInfo(string proxyUser, string proxyPassword)
{
return new ConnectionInfo(IPAddress.Loopback.ToString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ protected sealed override void Arrange()
SetupMocks();
}

protected override void AfterAct()
{
// Give some time to process all messages
Thread.Sleep(200);
}

protected ConnectionInfo CreateConnectionInfo(string proxyUser, string proxyPassword)
{
return new ConnectionInfo(IPAddress.Loopback.ToString(),
Expand Down
63 changes: 37 additions & 26 deletions src/Renci.SshNet.Tests/Common/AsyncSocketListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ public void Stop()
{
_started = false;

// Give some time to process all messages.The new .NET is faster, so the test establishes
// a connection with the server, events start to run and at the same time the test is finished.
_receiveThread.Join(400);

lock (_syncLock)
{
foreach (var connectedClient in _connectedClients)
Expand Down Expand Up @@ -283,28 +279,7 @@ private void ReadCallback(IAsyncResult ar)
return;
}

if (bytesRead > 0)
{
var bytesReceived = new byte[bytesRead];
Array.Copy(state.Buffer, bytesReceived, bytesRead);
SignalBytesReceived(bytesReceived, handler);

try
{
handler.BeginReceive(state.Buffer, 0, state.Buffer.Length, 0, ReadCallback, state);
}
catch (SocketException ex)
{
if (!_started)
{
throw new Exception("BeginReceive while stopping!", ex);
}

throw new Exception("BeginReceive while started!: " + ex.SocketErrorCode + " " + _stackTrace, ex);
}

}
else
void ConnectionDisconnected()
{
SignalDisconnected(handler);

Expand All @@ -322,6 +297,11 @@ private void ReadCallback(IAsyncResult ar)
handler.Shutdown(SocketShutdown.Send);
handler.Close();
}
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.ConnectionReset)
{
// On .NET 7 we got Socker Exception with ConnectionReset from Shutdown method
// when the socket is disposed
}
catch (SocketException ex)
{
throw new Exception("Exception in ReadCallback: " + ex.SocketErrorCode + " " + _stackTrace, ex);
Expand All @@ -335,6 +315,37 @@ private void ReadCallback(IAsyncResult ar)
}
}
}

if (bytesRead > 0)
{
var bytesReceived = new byte[bytesRead];
Array.Copy(state.Buffer, bytesReceived, bytesRead);
SignalBytesReceived(bytesReceived, handler);

try
{
handler.BeginReceive(state.Buffer, 0, state.Buffer.Length, 0, ReadCallback, state);
}
catch (ObjectDisposedException ex)
{
// TODO On .NET 7, sometimes we get ObjectDisposedException when _started but only on appveyor, locally it works
ConnectionDisconnected();
}
catch (SocketException ex)
{
if (!_started)
{
throw new Exception("BeginReceive while stopping!", ex);
}

throw new Exception("BeginReceive while started!: " + ex.SocketErrorCode + " " + _stackTrace, ex);
}

}
else
{
ConnectionDisconnected();
}
}

private void SignalBytesReceived(byte[] bytesReceived, Socket client)
Expand Down
5 changes: 5 additions & 0 deletions src/Renci.SshNet.Tests/Common/TripleATestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public void Init()
{
Arrange();
Act();
AfterAct();
}

[TestCleanup]
Expand All @@ -24,6 +25,10 @@ protected virtual void TearDown()
protected abstract void Arrange();

protected abstract void Act();

protected virtual void AfterAct()
{
}
}
}

0 comments on commit 8201be4

Please sign in to comment.