Merge cherrypicks of [2476230, 2476289, 2476272, 2476118, 2475997, 2476251, 2476119, 2476341, 2476252, 2476290, 2476232, 2476274, 2475999, 2476343, 2476275, 2476216, 2476381, 2476256, 2476000, 2476362, 2476345, 2476364, 2476234, 2476346, 2476347, 2476365, 2476276, 2476219, 2476277, 2476402, 2476348, 2476235, 2476383, 2476257, 2476278, 2476236, 2476350, 2476351, 2476307, 2476403, 2476308, 2476258] into oc-release

Change-Id: I78b42743e4b9809a9b6387038c454f60f949a48f
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());