test/jdk/java/net/Socket/asyncClose/Race.java
changeset 49573 6b46983d6fbe
parent 47216 71c04702a3d5
child 55045 3a8433d967ea
equal deleted inserted replaced
49572:fcdca1973b84 49573:6b46983d6fbe
    28  */
    28  */
    29 
    29 
    30 import java.io.InputStream;
    30 import java.io.InputStream;
    31 import java.net.ServerSocket;
    31 import java.net.ServerSocket;
    32 import java.net.Socket;
    32 import java.net.Socket;
       
    33 import java.net.ConnectException;
    33 import java.net.SocketException;
    34 import java.net.SocketException;
    34 import java.util.concurrent.Phaser;
    35 import java.util.concurrent.Phaser;
    35 
    36 
    36 // Racey test, will not always fail, but if it does then we have a problem.
    37 // Racey test, will not always fail, but if it does then we have a problem.
    37 
    38 
    41     public static void main(String[] args) throws Exception {
    42     public static void main(String[] args) throws Exception {
    42         try (ServerSocket ss = new ServerSocket(0)) {
    43         try (ServerSocket ss = new ServerSocket(0)) {
    43             final int port = ss.getLocalPort();
    44             final int port = ss.getLocalPort();
    44             final Phaser phaser = new Phaser(THREADS + 1);
    45             final Phaser phaser = new Phaser(THREADS + 1);
    45             for (int i=0; i<100; i++) {
    46             for (int i=0; i<100; i++) {
    46                 final Socket s = new Socket("localhost", port);
    47                 try {
    47                 s.setSoLinger(false, 0);
    48                     final Socket s = new Socket("localhost", port);
    48                 try (Socket sa = ss.accept()) {
    49                     s.setSoLinger(false, 0);
    49                     sa.setSoLinger(false, 0);
    50                     try (Socket sa = ss.accept()) {
    50                     final InputStream is = s.getInputStream();
    51                         sa.setSoLinger(false, 0);
    51                     Thread[] threads = new Thread[THREADS];
    52                         final InputStream is = s.getInputStream();
    52                     for (int j=0; j<THREADS; j++) {
    53                         Thread[] threads = new Thread[THREADS];
    53                         threads[j] = new Thread() {
    54                         for (int j=0; j<THREADS; j++) {
    54                         public void run() {
    55                             threads[j] = new Thread() {
    55                             try {
    56                             public void run() {
    56                                 phaser.arriveAndAwaitAdvance();
    57                                 try {
    57                                 while (is.read() != -1)
    58                                     phaser.arriveAndAwaitAdvance();
    58                                     Thread.sleep(50);
    59                                     while (is.read() != -1)
    59                             } catch (Exception x) {
    60                                         Thread.sleep(50);
    60                                 if (!(x instanceof SocketException
    61                                 } catch (Exception x) {
    61                                       && x.getMessage().equalsIgnoreCase("socket closed")))
    62                                     if (!(x instanceof SocketException
    62                                     x.printStackTrace();
    63                                           && x.getMessage().equalsIgnoreCase("socket closed")))
    63                                 // ok, expect Socket closed
    64                                         x.printStackTrace();
    64                             }
    65                                     // ok, expect Socket closed
    65                         }};
    66                                 }
       
    67                             }};
       
    68                         }
       
    69                         for (int j=0; j<100; j++)
       
    70                             threads[j].start();
       
    71                         phaser.arriveAndAwaitAdvance();
       
    72                         s.close();
       
    73                         for (int j=0; j<100; j++)
       
    74                             threads[j].join();
    66                     }
    75                     }
    67                     for (int j=0; j<100; j++)
    76                 } catch (ConnectException e) {
    68                         threads[j].start();
    77                     System.err.println("Exception " + e + " Port: " + port);
    69                     phaser.arriveAndAwaitAdvance();
       
    70                     s.close();
       
    71                     for (int j=0; j<100; j++)
       
    72                         threads[j].join();
       
    73                 }
    78                 }
    74             }
    79             }
    75         }
    80         }
    76     }
    81     }
    77 }
    82 }