diff options
author | Stephen Toub <stoub@microsoft.com> | 2017-02-07 19:24:25 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-07 19:24:25 +0300 |
commit | b9e51257e5409f77af2cb3d2566593e97eefba7a (patch) | |
tree | a2f27c567db9dedbc9b2e7d5bc8503d611d4a8c7 | |
parent | b49bcfbc0856482752d91ba645110cbd67e06eca (diff) | |
parent | b20f547e83899f224ccb7a5feb98a4dea24541c0 (diff) |
Merge pull request #15887 from steveharter/SocketPortCollision
Make socket tests more reliable under Unix
-rw-r--r-- | src/System.Net.Sockets/tests/FunctionalTests/DualModeSocketTest.cs | 96 |
1 files changed, 64 insertions, 32 deletions
diff --git a/src/System.Net.Sockets/tests/FunctionalTests/DualModeSocketTest.cs b/src/System.Net.Sockets/tests/FunctionalTests/DualModeSocketTest.cs index 0e069ad23d..0d763499f2 100644 --- a/src/System.Net.Sockets/tests/FunctionalTests/DualModeSocketTest.cs +++ b/src/System.Net.Sockets/tests/FunctionalTests/DualModeSocketTest.cs @@ -104,19 +104,13 @@ namespace System.Net.Sockets.Tests [Fact] public void ConnectV4IPAddressToV6Host_Fails() { - Assert.ThrowsAny<SocketException>(() => - { - DualModeConnect_IPAddressToHost_Helper(IPAddress.Loopback, IPAddress.IPv6Loopback, false); - }); + DualModeConnect_IPAddressToHost_Fails_Helper(IPAddress.Loopback, IPAddress.IPv6Loopback); } [Fact] public void ConnectV6IPAddressToV4Host_Fails() { - Assert.ThrowsAny<SocketException>(() => - { - DualModeConnect_IPAddressToHost_Helper(IPAddress.IPv6Loopback, IPAddress.Loopback, false); - }); + DualModeConnect_IPAddressToHost_Fails_Helper(IPAddress.IPv6Loopback, IPAddress.Loopback); } [Fact] @@ -141,6 +135,20 @@ namespace System.Net.Sockets.Tests Assert.True(socket.Connected); } } + + private void DualModeConnect_IPAddressToHost_Fails_Helper(IPAddress connectTo, IPAddress listenOn) + { + Assert.ThrowsAny<SocketException>(() => + { + DualModeConnect_IPAddressToHost_Helper(connectTo, listenOn, false); + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + // On Unix, socket assignment is random (not incremental) and there is a small chance the + // listening socket was created in another test currently running. Try the test one more time. + DualModeConnect_IPAddressToHost_Helper(connectTo, listenOn, false); + } + }); + } } [Trait("IPv4", "true")] @@ -186,19 +194,13 @@ namespace System.Net.Sockets.Tests [Fact] public void ConnectV4IPEndPointToV6Host_Fails() { - Assert.ThrowsAny<SocketException>(() => - { - DualModeConnect_IPEndPointToHost_Helper(IPAddress.Loopback, IPAddress.IPv6Loopback, false); - }); + DualModeConnect_IPEndPointToHost_Fails_Helper(IPAddress.Loopback, IPAddress.IPv6Loopback); } [Fact] public void ConnectV6IPEndPointToV4Host_Fails() { - Assert.ThrowsAny<SocketException>(() => - { - DualModeConnect_IPEndPointToHost_Helper(IPAddress.IPv6Loopback, IPAddress.Loopback, false); - }); + DualModeConnect_IPEndPointToHost_Fails_Helper(IPAddress.IPv6Loopback, IPAddress.Loopback); } [Fact] @@ -223,6 +225,20 @@ namespace System.Net.Sockets.Tests Assert.True(socket.Connected); } } + + private void DualModeConnect_IPEndPointToHost_Fails_Helper(IPAddress connectTo, IPAddress listenOn) + { + Assert.ThrowsAny<SocketException>(() => + { + DualModeConnect_IPEndPointToHost_Helper(connectTo, listenOn, false); + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + // On Unix, socket assignment is random (not incremental) and there is a small chance the + // listening socket was created in another test currently running. Try the test one more time. + DualModeConnect_IPEndPointToHost_Helper(connectTo, listenOn, false); + } + }); + } } [Trait("IPv4", "true")] @@ -339,19 +355,13 @@ namespace System.Net.Sockets.Tests [Fact] public void BeginConnectV4IPAddressToV6Host_Fails() { - Assert.ThrowsAny<SocketException>(() => - { - DualModeBeginConnect_IPAddressToHost_Helper(IPAddress.Loopback, IPAddress.IPv6Loopback, false); - }); + DualModeBeginConnect_IPAddressToHost_Fails_Helper(IPAddress.Loopback, IPAddress.IPv6Loopback); } [Fact] public void BeginConnectV6IPAddressToV4Host_Fails() { - Assert.ThrowsAny<SocketException>(() => - { - DualModeBeginConnect_IPAddressToHost_Helper(IPAddress.IPv6Loopback, IPAddress.Loopback, false); - }); + DualModeBeginConnect_IPAddressToHost_Fails_Helper(IPAddress.IPv6Loopback, IPAddress.Loopback); } [Fact] @@ -377,6 +387,20 @@ namespace System.Net.Sockets.Tests Assert.True(socket.Connected); } } + + private void DualModeBeginConnect_IPAddressToHost_Fails_Helper(IPAddress connectTo, IPAddress listenOn) + { + Assert.ThrowsAny<SocketException>(() => + { + DualModeBeginConnect_IPAddressToHost_Helper(connectTo, listenOn, false); + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + // On Unix, socket assignment is random (not incremental) and there is a small chance the + // listening socket was created in another test currently running. Try the test one more time. + DualModeBeginConnect_IPAddressToHost_Helper(connectTo, listenOn, false); + } + }); + } } [Trait("IPv4", "true")] @@ -513,19 +537,13 @@ namespace System.Net.Sockets.Tests [ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsSubsystemForLinux))] // https://github.com/Microsoft/BashOnWindows/issues/982 public void ConnectAsyncV4IPEndPointToV6Host_Fails() { - Assert.Throws<SocketException>(() => - { - DualModeConnectAsync_IPEndPointToHost_Helper(IPAddress.Loopback, IPAddress.IPv6Loopback, false); - }); + DualModeConnectAsync_IPEndPointToHost_Fails_Helper(IPAddress.Loopback, IPAddress.IPv6Loopback); } [ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsSubsystemForLinux))] // https://github.com/Microsoft/BashOnWindows/issues/982 public void ConnectAsyncV6IPEndPointToV4Host_Fails() { - Assert.Throws<SocketException>(() => - { - DualModeConnectAsync_IPEndPointToHost_Helper(IPAddress.IPv6Loopback, IPAddress.Loopback, false); - }); + DualModeConnectAsync_IPEndPointToHost_Fails_Helper(IPAddress.IPv6Loopback, IPAddress.Loopback); } [Fact] @@ -563,6 +581,20 @@ namespace System.Net.Sockets.Tests } } + private void DualModeConnectAsync_IPEndPointToHost_Fails_Helper(IPAddress connectTo, IPAddress listenOn) + { + Assert.ThrowsAny<SocketException>(() => + { + DualModeConnectAsync_IPEndPointToHost_Helper(connectTo, listenOn, false); + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + // On Unix, socket assignment is random (not incremental) and there is a small chance the + // listening socket was created in another test currently running. Try the test one more time. + DualModeConnectAsync_IPEndPointToHost_Helper(connectTo, listenOn, false); + } + }); + } + [Theory] [MemberData(nameof(DualMode_Connect_IPAddress_DualMode_Data))] [PlatformSpecific(TestPlatforms.Windows)] |