ソースを参照

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
Rob Hague 1 年間 前
コミット
aa70718a86

+ 10 - 0
test/Renci.SshNet.Tests/Classes/Channels/ChannelDirectTcpipTest_Dispose_SessionIsConnectedAndChannelIsOpen.cs

@@ -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(
                 () =>

+ 7 - 10
test/Renci.SshNet.Tests/Common/AsyncSocketListener.cs

@@ -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();
             }
         }