8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
authormichaelm
Wed, 11 Apr 2018 10:19:37 +0100
changeset 49573 6b46983d6fbe
parent 49572 fcdca1973b84
child 49574 6a6ee36037ac
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused Reviewed-by: dfuchs
test/jdk/java/net/Socket/asyncClose/Race.java
--- a/test/jdk/java/net/Socket/asyncClose/Race.java	Tue Apr 10 20:04:32 2018 -0700
+++ b/test/jdk/java/net/Socket/asyncClose/Race.java	Wed Apr 11 10:19:37 2018 +0100
@@ -30,6 +30,7 @@
 import java.io.InputStream;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.net.ConnectException;
 import java.net.SocketException;
 import java.util.concurrent.Phaser;
 
@@ -43,33 +44,37 @@
             final int port = ss.getLocalPort();
             final Phaser phaser = new Phaser(THREADS + 1);
             for (int i=0; i<100; i++) {
-                final Socket s = new Socket("localhost", port);
-                s.setSoLinger(false, 0);
-                try (Socket sa = ss.accept()) {
-                    sa.setSoLinger(false, 0);
-                    final InputStream is = s.getInputStream();
-                    Thread[] threads = new Thread[THREADS];
-                    for (int j=0; j<THREADS; j++) {
-                        threads[j] = new Thread() {
-                        public void run() {
-                            try {
-                                phaser.arriveAndAwaitAdvance();
-                                while (is.read() != -1)
-                                    Thread.sleep(50);
-                            } catch (Exception x) {
-                                if (!(x instanceof SocketException
-                                      && x.getMessage().equalsIgnoreCase("socket closed")))
-                                    x.printStackTrace();
-                                // ok, expect Socket closed
-                            }
-                        }};
+                try {
+                    final Socket s = new Socket("localhost", port);
+                    s.setSoLinger(false, 0);
+                    try (Socket sa = ss.accept()) {
+                        sa.setSoLinger(false, 0);
+                        final InputStream is = s.getInputStream();
+                        Thread[] threads = new Thread[THREADS];
+                        for (int j=0; j<THREADS; j++) {
+                            threads[j] = new Thread() {
+                            public void run() {
+                                try {
+                                    phaser.arriveAndAwaitAdvance();
+                                    while (is.read() != -1)
+                                        Thread.sleep(50);
+                                } catch (Exception x) {
+                                    if (!(x instanceof SocketException
+                                          && x.getMessage().equalsIgnoreCase("socket closed")))
+                                        x.printStackTrace();
+                                    // ok, expect Socket closed
+                                }
+                            }};
+                        }
+                        for (int j=0; j<100; j++)
+                            threads[j].start();
+                        phaser.arriveAndAwaitAdvance();
+                        s.close();
+                        for (int j=0; j<100; j++)
+                            threads[j].join();
                     }
-                    for (int j=0; j<100; j++)
-                        threads[j].start();
-                    phaser.arriveAndAwaitAdvance();
-                    s.close();
-                    for (int j=0; j<100; j++)
-                        threads[j].join();
+                } catch (ConnectException e) {
+                    System.err.println("Exception " + e + " Port: " + port);
                 }
             }
         }