1 /* |
1 /* |
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
26 * @bug 8006395 8012244 |
26 * @bug 8006395 8012244 |
27 * @summary Tests racing code that reads and closes a Socket |
27 * @summary Tests racing code that reads and closes a Socket |
28 */ |
28 */ |
29 |
29 |
30 import java.io.InputStream; |
30 import java.io.InputStream; |
|
31 import java.net.InetAddress; |
|
32 import java.net.InetSocketAddress; |
31 import java.net.ServerSocket; |
33 import java.net.ServerSocket; |
32 import java.net.Socket; |
34 import java.net.Socket; |
33 import java.net.ConnectException; |
35 import java.net.ConnectException; |
34 import java.net.SocketException; |
36 import java.net.SocketException; |
35 import java.util.concurrent.Phaser; |
37 import java.util.concurrent.Phaser; |
38 |
40 |
39 public class Race { |
41 public class Race { |
40 final static int THREADS = 100; |
42 final static int THREADS = 100; |
41 |
43 |
42 public static void main(String[] args) throws Exception { |
44 public static void main(String[] args) throws Exception { |
43 try (ServerSocket ss = new ServerSocket(0)) { |
45 try (ServerSocket ss = new ServerSocket()) { |
|
46 InetAddress loopback = InetAddress.getLoopbackAddress(); |
|
47 ss.bind(new InetSocketAddress(loopback, 0)); |
44 final int port = ss.getLocalPort(); |
48 final int port = ss.getLocalPort(); |
45 final Phaser phaser = new Phaser(THREADS + 1); |
49 final Phaser phaser = new Phaser(THREADS + 1); |
46 for (int i=0; i<100; i++) { |
50 for (int i=0; i<100; i++) { |
47 try { |
51 try { |
48 final Socket s = new Socket("localhost", port); |
52 final Socket s = new Socket(loopback, port); |
49 s.setSoLinger(false, 0); |
53 s.setSoLinger(false, 0); |
50 try (Socket sa = ss.accept()) { |
54 try (Socket sa = ss.accept()) { |
51 sa.setSoLinger(false, 0); |
55 sa.setSoLinger(false, 0); |
52 final InputStream is = s.getInputStream(); |
56 final InputStream is = s.getInputStream(); |
53 Thread[] threads = new Thread[THREADS]; |
57 Thread[] threads = new Thread[THREADS]; |