[System] Fix flaky AcceptBlockingStatus test that sometimes failed on OSX
authorAlexander Köplinger <alex.koeplinger@outlook.com>
Mon, 4 Jan 2016 15:48:58 +0000 (16:48 +0100)
committerAlexander Köplinger <alex.koeplinger@outlook.com>
Mon, 4 Jan 2016 16:32:06 +0000 (17:32 +0100)
We were sometimes seeing the following failure on Jenkins on OSX:

```
Test Case Failures:
1) MonoTests.System.Net.Sockets.SocketTest.AcceptBlockingStatus : System.Net.Sockets.SocketException : Operation on non-blocking socket would block
at System.Net.Sockets.Socket.Accept () [0x00033] in /Users/builder/jenkins/workspace/test-mono-mainline/label/osx-i386/mcs/class/System/System.Net.Sockets/Socket.cs:896
at MonoTests.System.Net.Sockets.SocketTest.BlockingConnect (Boolean block) [0x0003e] in /Users/builder/jenkins/workspace/test-mono-mainline/label/osx-i386/mcs/class/System/Test/System.Net.Sockets/SocketTest.cs:129
at MonoTests.System.Net.Sockets.SocketTest.AcceptBlockingStatus () [0x0001e] in /Users/builder/jenkins/workspace/test-mono-mainline/label/osx-i386/mcs/class/System/Test/System.Net.Sockets/SocketTest.cs:147
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00038] in /Users/builder/jenkins/workspace/test-mono-mainline/label/osx-i386/mcs/class/corlib/System.Reflection/MonoMethod.cs:295
```

In non-blocking mode accept() is specified to return this error when no client connection is available right now.
It looks like on OSX we don't immediately get that connection.

To fix this issue, we try to accept in a loop for 100ms and only fail when we don't get a connection in this time.

mcs/class/System/Test/System.Net.Sockets/SocketTest.cs

index 9ee0c544769a747cdba099b137fa760cae072113..0d2dea47a73f5244dbac722c16b7ab8768e79e23 100755 (executable)
@@ -9,6 +9,7 @@
 //
 
 using System;
+using System.Diagnostics;
 using System.Linq;
 using System.Collections;
 using System.Threading;
@@ -126,7 +127,21 @@ namespace MonoTests.System.Net.Sockets
                                                  ProtocolType.Tcp);
                        conn.Connect (ep);
 
-                       Socket client = server.Accept();
+                       Socket client = null;
+                       var sw = Stopwatch.StartNew ();
+                       while (sw.ElapsedMilliseconds < 100)
+                       {
+                               try {
+                                       client = server.Accept();
+                                       break;
+                               }
+                               catch (SocketException ex) {
+                                       if (ex.SocketErrorCode == SocketError.WouldBlock)
+                                               continue;
+                                       throw;
+                               }
+                       }
+                       Assert.IsNotNull (client, "Couldn't accept a client connection within 100ms.");
                        bool client_block = client.Blocking;
 
                        client.Close();