Work around ServerSocketConcurrentCloseTest flakiness

The test involves 100 iterations of a client and server thread
connecting to each other and disconnecting over and over, until
the ServerSocket involved is closed at the end of the iteration.

Before this CL, each iteration took 50msec (counted from when
the threads signaled that they had started running by counting
down a latch. On recent builds on some devices, the two threads
didn't manage to establish any connection within those 50msec
but only when other tests had run previously. The exact cause
for this was not determined, but threads left over from over
from previous tests appeared to not be involved.

This CL increases the maximum time for each iteration to a maximum
of 150msec (in steps of 50msec, until at least one connection was
established), which makes the test pass (5/5 tries) on a device
where it previously consistently failed (15/16 tries failed).

Test: CtsLibcoreTestCases (on affected device)
Bug: 62859398

Change-Id: I60eb30e9aa96ab1c0d2b70a74d38757c2bb84825
diff --git a/luni/src/test/java/libcore/java/net/ServerSocketConcurrentCloseTest.java b/luni/src/test/java/libcore/java/net/ServerSocketConcurrentCloseTest.java
index 9515fcc..102c263 100644
--- a/luni/src/test/java/libcore/java/net/ServerSocketConcurrentCloseTest.java
+++ b/luni/src/test/java/libcore/java/net/ServerSocketConcurrentCloseTest.java
@@ -76,7 +76,7 @@
         int numIterations = 100;
         for (int i = 0; i < numIterations; i++) {
             checkConnectIterationAndCloseSocket("Iteration " + (i+1) + " of " + numIterations,
-                    /* msecPerIteration */ 50);
+                    /* sleepMsec */ 50, /* maxSleepsPerIteration */ 3);
         }
     }
 
@@ -84,11 +84,13 @@
      * Checks that a concurrent {@link ServerSocket#close()} reliably causes
      * {@link ServerSocket#accept()} to throw {@link SocketException}.
      *
-     * <p>Spawns a server and client thread that continuously connect to each other
-     * for {@code msecPerIteration} msec. Then, closes the {@link ServerSocket} and
-     * verifies that the server quickly shuts down.
+     * <p>Spawns a server and client thread that continuously connect to each
+     * other for up to {@code maxSleepsPerIteration * sleepMsec} msec.
+     * Then, closes the {@link ServerSocket} and verifies that the server
+     * quickly shuts down.
      */
-    private void checkConnectIterationAndCloseSocket(String iterationName, int msecPerIteration) {
+    private void checkConnectIterationAndCloseSocket(String iterationName,
+        int sleepMsec, int maxSleepsPerIteration) {
         ServerSocket serverSocket;
         try {
             serverSocket = new ServerSocket(0 /* allocate port number automatically */);
@@ -110,7 +112,12 @@
                 fail("Server prematurely shut down");
             }
             // Let server and client keep connecting for some time, then close the socket.
-            Thread.sleep(msecPerIteration);
+            for (int i = 0; i < maxSleepsPerIteration; i++) {
+              Thread.sleep(sleepMsec);
+              if (serverRunnable.numSuccessfulConnections > 0) {
+                break;
+              }
+            }
             try {
                 serverSocket.close();
             } catch (IOException e) {
@@ -129,9 +136,9 @@
             // later iterations because TCP connections cannot be closed immediately (they stay
             // in TIME_WAIT state for a few minutes) and only some number (tens of thousands?)
             // can be open at a time. If this assertion turns out flaky in future, consider
-            // reducing msecPerIteration or number of iterations.
+            // reducing the iteration time or number of iterations.
             assertTrue(String.format(Locale.US, "%s: No connections in %d msec.",
-                    iterationName, msecPerIteration),
+                    iterationName, maxSleepsPerIteration * sleepMsec),
                     serverRunnable.numSuccessfulConnections > 0);
 
             assertTrue(serverRunnable.isShutdown());