38 import java.util.concurrent.Future; |
40 import java.util.concurrent.Future; |
39 import java.util.concurrent.ScheduledExecutorService; |
41 import java.util.concurrent.ScheduledExecutorService; |
40 import java.util.stream.IntStream; |
42 import java.util.stream.IntStream; |
41 import static java.util.concurrent.TimeUnit.MILLISECONDS; |
43 import static java.util.concurrent.TimeUnit.MILLISECONDS; |
42 |
44 |
|
45 import jdk.test.lib.Utils; |
|
46 |
43 public class CloseDuringConnect { |
47 public class CloseDuringConnect { |
44 |
48 |
45 // number of test iterations, needs to be 5-10 at least |
49 // number of test iterations, needs to be 5-10 at least |
46 static final int ITERATIONS = 50; |
50 static final int ITERATIONS = 50; |
47 |
51 |
48 // maximum delay before closing SocketChannel, in milliseconds |
52 // maximum delay before closing SocketChannel, in milliseconds |
49 static final int MAX_DELAY_BEFORE_CLOSE = 20; |
53 static final int MAX_DELAY_BEFORE_CLOSE = 20; |
50 |
|
51 /** |
|
52 * Returns the socket address of an endpoint that refuses connections. The |
|
53 * endpoint is an InetSocketAddress where the address is the loopback address |
|
54 * and the port is a system port (1-1023 range). |
|
55 */ |
|
56 static SocketAddress refusingEndpoint() { |
|
57 InetAddress lb = InetAddress.getLoopbackAddress(); |
|
58 int port = 1; |
|
59 while (port < 1024) { |
|
60 SocketAddress sa = new InetSocketAddress(lb, port); |
|
61 try { |
|
62 SocketChannel.open(sa).close(); |
|
63 } catch (IOException ioe) { |
|
64 return sa; |
|
65 } |
|
66 port++; |
|
67 } |
|
68 throw new RuntimeException("Unable to find system port that is refusing connections"); |
|
69 } |
|
70 |
54 |
71 /** |
55 /** |
72 * Invoked by a task in the thread pool to connect to a remote address. |
56 * Invoked by a task in the thread pool to connect to a remote address. |
73 * The connection should never be established. |
57 * The connection should never be established. |
74 */ |
58 */ |
121 throw new RuntimeException("Test failed", t); |
105 throw new RuntimeException("Test failed", t); |
122 } |
106 } |
123 } |
107 } |
124 |
108 |
125 public static void main(String[] args) throws Exception { |
109 public static void main(String[] args) throws Exception { |
126 SocketAddress refusing = refusingEndpoint(); |
110 SocketAddress refusing = Utils.refusingEndpoint(); |
127 ScheduledExecutorService pool = Executors.newScheduledThreadPool(2); |
111 ScheduledExecutorService pool = Executors.newScheduledThreadPool(2); |
128 try { |
112 try { |
129 IntStream.range(0, ITERATIONS).forEach(i -> { |
113 IntStream.range(0, ITERATIONS).forEach(i -> { |
130 System.out.format("Iteration %d ...%n", (i + 1)); |
114 System.out.format("Iteration %d ...%n", (i + 1)); |
131 |
115 |