diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/Accept.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/Accept.cs index 7f0dc3adc14a41..78b172a16bf016 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/Accept.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/Accept.cs @@ -295,7 +295,8 @@ public async Task AcceptGetsCanceledByDispose() // We try this a couple of times to deal with a timing race: if the Dispose happens // before the operation is started, we won't see a SocketException. int msDelay = 100; - await RetryHelper.ExecuteAsync(async () => + int retries = 10; + while (true) { var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listener.Bind(new IPEndPoint(IPAddress.Loopback, 0)); @@ -330,20 +331,33 @@ await RetryHelper.ExecuteAsync(async () => disposedException = true; } - if (UsesApm) - { - Assert.Null(localSocketError); - Assert.True(disposedException); - } - else if (UsesSync) + try { - Assert.Equal(SocketError.Interrupted, localSocketError); + if (UsesApm) + { + Assert.Null(localSocketError); + Assert.True(disposedException); + } + else if (UsesSync) + { + Assert.Equal(SocketError.Interrupted, localSocketError); + } + else + { + Assert.Equal(SocketError.OperationAborted, localSocketError); + } + break; } - else + catch { - Assert.Equal(SocketError.OperationAborted, localSocketError); + if (retries-- > 0) + { + continue; + } + + throw; } - }, maxAttempts: 10); + } } [Fact] diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/Connect.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/Connect.cs index 08ecf15f3b925b..fd84bcfb664dde 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/Connect.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/Connect.cs @@ -127,7 +127,8 @@ public async Task ConnectGetsCanceledByDispose() // We try this a couple of times to deal with a timing race: if the Dispose happens // before the operation is started, we won't see a SocketException. int msDelay = 100; - await RetryHelper.ExecuteAsync(async () => + int retries = 10; + while (true) { var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); @@ -153,9 +154,6 @@ await RetryHelper.ExecuteAsync(async () => } catch (SocketException se) { - // On connection timeout, retry. - Assert.NotEqual(SocketError.TimedOut, se.SocketErrorCode); - localSocketError = se.SocketErrorCode; } catch (ObjectDisposedException) @@ -163,20 +161,36 @@ await RetryHelper.ExecuteAsync(async () => disposedException = true; } - if (UsesApm) - { - Assert.Null(localSocketError); - Assert.True(disposedException); - } - else if (UsesSync) + try { - Assert.Equal(SocketError.NotSocket, localSocketError); + // On connection timeout, retry. + Assert.NotEqual(SocketError.TimedOut, localSocketError); + + if (UsesApm) + { + Assert.Null(localSocketError); + Assert.True(disposedException); + } + else if (UsesSync) + { + Assert.Equal(SocketError.NotSocket, localSocketError); + } + else + { + Assert.Equal(SocketError.OperationAborted, localSocketError); + } + break; } - else + catch { - Assert.Equal(SocketError.OperationAborted, localSocketError); + if (retries-- > 0) + { + continue; + } + + throw; } - }, maxAttempts: 10); + } } } diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs index 3a8db0f161ee6e..31f3ccea9482d4 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs @@ -288,7 +288,8 @@ public async Task SyncSendFileGetsCanceledByDispose() // before the operation is started, the peer won't see a ConnectionReset SocketException and we won't // see a SocketException either. int msDelay = 100; - await RetryHelper.ExecuteAsync(async () => + int retries = 10; + while (true) { (Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair(); using (socket2) @@ -328,34 +329,48 @@ await RetryHelper.ExecuteAsync(async () => } catch (ObjectDisposedException) { } - Assert.Equal(SocketError.ConnectionAborted, localSocketError); - // On OSX, we're unable to unblock the on-going socket operations and - // perform an abortive close. - if (!PlatformDetection.IsOSXLike) + try { - SocketError? peerSocketError = null; - var receiveBuffer = new byte[4096]; - while (true) + Assert.Equal(SocketError.ConnectionAborted, localSocketError); + + // On OSX, we're unable to unblock the on-going socket operations and + // perform an abortive close. + if (!PlatformDetection.IsOSXLike) { - try + SocketError? peerSocketError = null; + var receiveBuffer = new byte[4096]; + while (true) { - int received = socket2.Receive(receiveBuffer); - if (received == 0) + try { + int received = socket2.Receive(receiveBuffer); + if (received == 0) + { + break; + } + } + catch (SocketException se) + { + peerSocketError = se.SocketErrorCode; break; } } - catch (SocketException se) - { - peerSocketError = se.SocketErrorCode; - break; - } + Assert.Equal(SocketError.ConnectionReset, peerSocketError); } - Assert.Equal(SocketError.ConnectionReset, peerSocketError); + break; + } + catch + { + if (retries-- > 0) + { + continue; + } + + throw; } } - }, maxAttempts: 10); + } } [OuterLoop] diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendReceive.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendReceive.cs index 50a329e044c2b0..9176b99d24a1bc 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendReceive.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendReceive.cs @@ -944,7 +944,8 @@ public async Task UdpReceiveGetsCanceledByDispose() // We try this a couple of times to deal with a timing race: if the Dispose happens // before the operation is started, we won't see a SocketException. int msDelay = 100; - await RetryHelper.ExecuteAsync(async () => + int retries = 10; + while (true) { var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.BindToAnonymousPort(IPAddress.Loopback); @@ -978,20 +979,33 @@ await RetryHelper.ExecuteAsync(async () => disposedException = true; } - if (UsesApm) - { - Assert.Null(localSocketError); - Assert.True(disposedException); - } - else if (UsesSync) + try { - Assert.Equal(SocketError.Interrupted, localSocketError); + if (UsesApm) + { + Assert.Null(localSocketError); + Assert.True(disposedException); + } + else if (UsesSync) + { + Assert.Equal(SocketError.Interrupted, localSocketError); + } + else + { + Assert.Equal(SocketError.OperationAborted, localSocketError); + } + break; } - else + catch { - Assert.Equal(SocketError.OperationAborted, localSocketError); + if (retries-- > 0) + { + continue; + } + + throw; } - }, maxAttempts: 10); + } } [Theory] @@ -1003,7 +1017,8 @@ public async Task TcpReceiveSendGetsCanceledByDispose(bool receiveOrSend) // before the operation is started, the peer won't see a ConnectionReset SocketException and we won't // see a SocketException either. int msDelay = 100; - await RetryHelper.ExecuteAsync(async () => + int retries = 10; + while (true) { (Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair(); using (socket2) @@ -1052,46 +1067,59 @@ await RetryHelper.ExecuteAsync(async () => disposedException = true; } - if (UsesApm) - { - Assert.Null(localSocketError); - Assert.True(disposedException); - } - else if (UsesSync) - { - Assert.Equal(SocketError.ConnectionAborted, localSocketError); - } - else + try { - Assert.Equal(SocketError.OperationAborted, localSocketError); - } + if (UsesApm) + { + Assert.Null(localSocketError); + Assert.True(disposedException); + } + else if (UsesSync) + { + Assert.Equal(SocketError.ConnectionAborted, localSocketError); + } + else + { + Assert.Equal(SocketError.OperationAborted, localSocketError); + } - // On OSX, we're unable to unblock the on-going socket operations and - // perform an abortive close. - if (!(UsesSync && PlatformDetection.IsOSXLike)) - { - SocketError? peerSocketError = null; - var receiveBuffer = new ArraySegment(new byte[4096]); - while (true) + // On OSX, we're unable to unblock the on-going socket operations and + // perform an abortive close. + if (!(UsesSync && PlatformDetection.IsOSXLike)) { - try + SocketError? peerSocketError = null; + var receiveBuffer = new ArraySegment(new byte[4096]); + while (true) { - int received = await ReceiveAsync(socket2, receiveBuffer); - if (received == 0) + try + { + int received = await ReceiveAsync(socket2, receiveBuffer); + if (received == 0) + { + break; + } + } + catch (SocketException se) { + peerSocketError = se.SocketErrorCode; break; } } - catch (SocketException se) - { - peerSocketError = se.SocketErrorCode; - break; - } + Assert.Equal(SocketError.ConnectionReset, peerSocketError); + } + break; + } + catch + { + if (retries-- > 0) + { + continue; } - Assert.Equal(SocketError.ConnectionReset, peerSocketError); + + throw; } } - }, maxAttempts: 10); + } } [Fact]