Skip to content

Commit 8201be4

Browse files
Revert "Self review"
This reverts commit 5980570.
1 parent db06b60 commit 8201be4

File tree

6 files changed

+65
-31
lines changed

6 files changed

+65
-31
lines changed

src/Renci.SshNet.Tests/Classes/Connection/HttpConnectorTestBase.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ protected virtual void SetupMocks()
2626
{
2727
}
2828

29+
protected override void AfterAct()
30+
{
31+
// Give some time to process all messages
32+
Thread.Sleep(400);
33+
}
34+
2935
protected sealed override void Arrange()
3036
{
3137
CreateMocks();

src/Renci.SshNet.Tests/Classes/Connection/HttpConnectorTest_Connect_ProxyUserNameIsNull.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ protected override void TearDown()
7575
{
7676
base.TearDown();
7777

78-
if (_proxyServer != null)
79-
{
80-
_proxyServer.Dispose();
81-
}
82-
8378
if (_clientSocket != null)
8479
{
8580
_clientSocket.Shutdown(SocketShutdown.Send);
8681
_clientSocket.Close();
8782
}
83+
84+
if (_proxyServer != null)
85+
{
86+
_proxyServer.Dispose();
87+
}
8888
}
8989

9090
protected override void Act()

src/Renci.SshNet.Tests/Classes/Connection/Socks4ConnectorTestBase.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ protected sealed override void Arrange()
3434
SetupMocks();
3535
}
3636

37+
protected override void AfterAct()
38+
{
39+
// Give some time to process all messages
40+
Thread.Sleep(200);
41+
}
42+
3743
protected ConnectionInfo CreateConnectionInfo(string proxyUser, string proxyPassword)
3844
{
3945
return new ConnectionInfo(IPAddress.Loopback.ToString(),

src/Renci.SshNet.Tests/Classes/Connection/Socks5ConnectorTestBase.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ protected sealed override void Arrange()
3636
SetupMocks();
3737
}
3838

39+
protected override void AfterAct()
40+
{
41+
// Give some time to process all messages
42+
Thread.Sleep(200);
43+
}
44+
3945
protected ConnectionInfo CreateConnectionInfo(string proxyUser, string proxyPassword)
4046
{
4147
return new ConnectionInfo(IPAddress.Loopback.ToString(),

src/Renci.SshNet.Tests/Common/AsyncSocketListener.cs

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ public void Stop()
6565
{
6666
_started = false;
6767

68-
// Give some time to process all messages.The new .NET is faster, so the test establishes
69-
// a connection with the server, events start to run and at the same time the test is finished.
70-
_receiveThread.Join(400);
71-
7268
lock (_syncLock)
7369
{
7470
foreach (var connectedClient in _connectedClients)
@@ -283,28 +279,7 @@ private void ReadCallback(IAsyncResult ar)
283279
return;
284280
}
285281

286-
if (bytesRead > 0)
287-
{
288-
var bytesReceived = new byte[bytesRead];
289-
Array.Copy(state.Buffer, bytesReceived, bytesRead);
290-
SignalBytesReceived(bytesReceived, handler);
291-
292-
try
293-
{
294-
handler.BeginReceive(state.Buffer, 0, state.Buffer.Length, 0, ReadCallback, state);
295-
}
296-
catch (SocketException ex)
297-
{
298-
if (!_started)
299-
{
300-
throw new Exception("BeginReceive while stopping!", ex);
301-
}
302-
303-
throw new Exception("BeginReceive while started!: " + ex.SocketErrorCode + " " + _stackTrace, ex);
304-
}
305-
306-
}
307-
else
282+
void ConnectionDisconnected()
308283
{
309284
SignalDisconnected(handler);
310285

@@ -322,6 +297,11 @@ private void ReadCallback(IAsyncResult ar)
322297
handler.Shutdown(SocketShutdown.Send);
323298
handler.Close();
324299
}
300+
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.ConnectionReset)
301+
{
302+
// On .NET 7 we got Socker Exception with ConnectionReset from Shutdown method
303+
// when the socket is disposed
304+
}
325305
catch (SocketException ex)
326306
{
327307
throw new Exception("Exception in ReadCallback: " + ex.SocketErrorCode + " " + _stackTrace, ex);
@@ -335,6 +315,37 @@ private void ReadCallback(IAsyncResult ar)
335315
}
336316
}
337317
}
318+
319+
if (bytesRead > 0)
320+
{
321+
var bytesReceived = new byte[bytesRead];
322+
Array.Copy(state.Buffer, bytesReceived, bytesRead);
323+
SignalBytesReceived(bytesReceived, handler);
324+
325+
try
326+
{
327+
handler.BeginReceive(state.Buffer, 0, state.Buffer.Length, 0, ReadCallback, state);
328+
}
329+
catch (ObjectDisposedException ex)
330+
{
331+
// TODO On .NET 7, sometimes we get ObjectDisposedException when _started but only on appveyor, locally it works
332+
ConnectionDisconnected();
333+
}
334+
catch (SocketException ex)
335+
{
336+
if (!_started)
337+
{
338+
throw new Exception("BeginReceive while stopping!", ex);
339+
}
340+
341+
throw new Exception("BeginReceive while started!: " + ex.SocketErrorCode + " " + _stackTrace, ex);
342+
}
343+
344+
}
345+
else
346+
{
347+
ConnectionDisconnected();
348+
}
338349
}
339350

340351
private void SignalBytesReceived(byte[] bytesReceived, Socket client)

src/Renci.SshNet.Tests/Common/TripleATestBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public void Init()
99
{
1010
Arrange();
1111
Act();
12+
AfterAct();
1213
}
1314

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

2627
protected abstract void Act();
28+
29+
protected virtual void AfterAct()
30+
{
31+
}
2732
}
2833
}
2934

0 commit comments

Comments
 (0)