author | jbhateja |
Tue, 26 Nov 2019 16:09:25 +0300 | |
changeset 59278 | 8375560db76b |
parent 58899 | 5573a7098439 |
permissions | -rw-r--r-- |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
1 |
/* |
54086
ccb4a50bee06
8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
4 |
* |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
8 |
* |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
13 |
* accompanied this code). |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
14 |
* |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
18 |
* |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
21 |
* questions. |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
22 |
*/ |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
23 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
24 |
/* @test |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
54086
diff
changeset
|
25 |
* @bug 7184932 8232673 |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
26 |
* @summary Test asynchronous close and interrupt of timed socket adapter methods |
36678
4a684dceb848
8152749: Mark AdaptorCloseAndInterrupt.java as intermittently failing
amlu
parents:
30046
diff
changeset
|
27 |
* @key randomness intermittent |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
28 |
*/ |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
29 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
30 |
import java.io.*; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
31 |
import java.nio.*; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
32 |
import java.nio.channels.*; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
33 |
import java.nio.channels.spi.AbstractSelectableChannel; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
34 |
import java.net.*; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
35 |
import java.util.concurrent.Callable; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
36 |
import java.util.concurrent.Executors; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
37 |
import java.util.concurrent.ScheduledExecutorService; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
38 |
import java.util.concurrent.TimeUnit; |
44548
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
39 |
import java.util.concurrent.atomic.AtomicBoolean; |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
40 |
import java.util.Random; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
41 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
42 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
43 |
public class AdaptorCloseAndInterrupt { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
44 |
private static final ScheduledExecutorService pool = |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
45 |
Executors.newScheduledThreadPool(1); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
46 |
final ServerSocketChannel listener; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
47 |
final DatagramChannel peer; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
48 |
final int port; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
49 |
|
44548
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
50 |
final AtomicBoolean isClosed = new AtomicBoolean(); |
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
51 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
52 |
|
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
53 |
public AdaptorCloseAndInterrupt() { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
54 |
listener = null; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
55 |
peer = null; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
56 |
port = -1; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
57 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
58 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
59 |
public AdaptorCloseAndInterrupt(ServerSocketChannel listener) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
60 |
this.listener = listener; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
61 |
this.port = listener.socket().getLocalPort(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
62 |
this.peer = null; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
63 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
64 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
65 |
public AdaptorCloseAndInterrupt(DatagramChannel listener) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
66 |
this.peer = listener; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
67 |
this.port = peer.socket().getLocalPort(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
68 |
this.listener = null; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
69 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
70 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
71 |
public static void main(String args[]) throws Exception { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
72 |
try { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
73 |
try (ServerSocketChannel listener = ServerSocketChannel.open()) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
74 |
listener.socket().bind(null); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
75 |
new AdaptorCloseAndInterrupt(listener).scReadAsyncClose(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
76 |
new AdaptorCloseAndInterrupt(listener).scReadAsyncInterrupt(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
77 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
78 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
79 |
try (DatagramChannel peer = DatagramChannel.open()) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
80 |
peer.socket().bind(null); |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
54086
diff
changeset
|
81 |
new AdaptorCloseAndInterrupt(peer).dcReceiveAsyncClose(0); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
54086
diff
changeset
|
82 |
new AdaptorCloseAndInterrupt(peer).dcReceiveAsyncClose(30_000); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
54086
diff
changeset
|
83 |
new AdaptorCloseAndInterrupt(peer).dcReceiveAsyncInterrupt(0); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
54086
diff
changeset
|
84 |
new AdaptorCloseAndInterrupt(peer).dcReceiveAsyncInterrupt(30_000); |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
85 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
86 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
87 |
new AdaptorCloseAndInterrupt().ssAcceptAsyncClose(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
88 |
new AdaptorCloseAndInterrupt().ssAcceptAsyncInterrupt(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
89 |
} finally { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
90 |
pool.shutdown(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
91 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
92 |
System.out.println("Test Passed"); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
93 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
94 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
95 |
void scReadAsyncClose() throws IOException { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
96 |
try { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
97 |
SocketChannel sc = SocketChannel.open(new InetSocketAddress( |
54086
ccb4a50bee06
8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents:
47216
diff
changeset
|
98 |
InetAddress.getLoopbackAddress(), port)); |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
99 |
sc.socket().setSoTimeout(30*1000); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
100 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
101 |
doAsyncClose(sc); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
102 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
103 |
try { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
104 |
sc.socket().getInputStream().read(new byte[100]); |
44548
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
105 |
System.err.format("close() was invoked: %s%n", isClosed.get()); |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
106 |
throw new RuntimeException("read should not have completed"); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
107 |
} catch (ClosedChannelException expected) {} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
108 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
109 |
if (!sc.socket().isClosed()) |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
110 |
throw new RuntimeException("socket is not closed"); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
111 |
} finally { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
112 |
// accept connection and close it. |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
113 |
listener.accept().close(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
114 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
115 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
116 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
117 |
void scReadAsyncInterrupt() throws IOException { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
118 |
try { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
119 |
final SocketChannel sc = SocketChannel.open(new InetSocketAddress( |
54086
ccb4a50bee06
8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents:
47216
diff
changeset
|
120 |
InetAddress.getLoopbackAddress(), port)); |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
121 |
sc.socket().setSoTimeout(30*1000); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
122 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
123 |
doAsyncInterrupt(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
124 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
125 |
try { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
126 |
sc.socket().getInputStream().read(new byte[100]); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
127 |
throw new RuntimeException("read should not have completed"); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
128 |
} catch (ClosedByInterruptException expected) { |
44548
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
129 |
System.out.format("interrupt() was invoked: %s%n", |
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
130 |
isInterrupted.get()); |
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
131 |
System.out.format("scReadAsyncInterrupt was interrupted: %s%n", |
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
132 |
Thread.currentThread().interrupted()); |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
133 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
134 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
135 |
if (!sc.socket().isClosed()) |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
136 |
throw new RuntimeException("socket is not closed"); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
137 |
} finally { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
138 |
// accept connection and close it. |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
139 |
listener.accept().close(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
140 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
141 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
142 |
|
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
54086
diff
changeset
|
143 |
void dcReceiveAsyncClose(int timeout) throws IOException { |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
144 |
DatagramChannel dc = DatagramChannel.open(); |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
54086
diff
changeset
|
145 |
dc.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), port)); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
54086
diff
changeset
|
146 |
dc.socket().setSoTimeout(timeout); |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
147 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
148 |
doAsyncClose(dc); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
149 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
150 |
try { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
151 |
dc.socket().receive(new DatagramPacket(new byte[100], 100)); |
44548
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
152 |
System.err.format("close() was invoked: %s%n", isClosed.get()); |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
153 |
throw new RuntimeException("receive should not have completed"); |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
54086
diff
changeset
|
154 |
} catch (SocketException expected) { } |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
155 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
156 |
if (!dc.socket().isClosed()) |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
157 |
throw new RuntimeException("socket is not closed"); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
158 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
159 |
|
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
54086
diff
changeset
|
160 |
void dcReceiveAsyncInterrupt(int timeout) throws IOException { |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
161 |
DatagramChannel dc = DatagramChannel.open(); |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
54086
diff
changeset
|
162 |
dc.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), port)); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
54086
diff
changeset
|
163 |
dc.socket().setSoTimeout(timeout); |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
164 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
165 |
doAsyncInterrupt(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
166 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
167 |
try { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
168 |
dc.socket().receive(new DatagramPacket(new byte[100], 100)); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
169 |
throw new RuntimeException("receive should not have completed"); |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
54086
diff
changeset
|
170 |
} catch (SocketException expected) { |
44548
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
171 |
System.out.format("interrupt() was invoked: %s%n", |
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
172 |
isInterrupted.get()); |
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
173 |
System.out.format("dcReceiveAsyncInterrupt was interrupted: %s%n", |
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
174 |
Thread.currentThread().interrupted()); |
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
175 |
} catch (SocketTimeoutException unexpected) { |
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
176 |
System.err.format("Receive thread interrupt invoked: %s%n", |
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
177 |
isInterrupted.get()); |
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
178 |
System.err.format("Receive thread was interrupted: %s%n", |
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
179 |
Thread.currentThread().isInterrupted()); |
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
180 |
throw unexpected; |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
181 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
182 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
183 |
if (!dc.socket().isClosed()) |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
184 |
throw new RuntimeException("socket is not closed"); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
185 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
186 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
187 |
void ssAcceptAsyncClose() throws IOException { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
188 |
ServerSocketChannel ssc = ServerSocketChannel.open(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
189 |
ssc.socket().bind(null); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
190 |
ssc.socket().setSoTimeout(30*1000); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
191 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
192 |
doAsyncClose(ssc); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
193 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
194 |
try { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
195 |
ssc.socket().accept(); |
44548
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
196 |
System.err.format("close() was invoked: %s%n", isClosed.get()); |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
197 |
throw new RuntimeException("accept should not have completed"); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
198 |
} catch (ClosedChannelException expected) {} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
199 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
200 |
if (!ssc.socket().isClosed()) |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
201 |
throw new RuntimeException("socket is not closed"); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
202 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
203 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
204 |
void ssAcceptAsyncInterrupt() throws IOException { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
205 |
ServerSocketChannel ssc = ServerSocketChannel.open(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
206 |
ssc.socket().bind(null); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
207 |
ssc.socket().setSoTimeout(30*1000); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
208 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
209 |
doAsyncInterrupt(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
210 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
211 |
try { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
212 |
ssc.socket().accept(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
213 |
throw new RuntimeException("accept should not have completed"); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
214 |
} catch (ClosedByInterruptException expected) { |
44548
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
215 |
System.out.format("interrupt() was invoked: %s%n", |
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
216 |
isInterrupted.get()); |
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
217 |
System.out.format("ssAcceptAsyncInterrupt was interrupted: %s%n", |
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
218 |
Thread.currentThread().interrupted()); |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
219 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
220 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
221 |
if (!ssc.socket().isClosed()) |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
222 |
throw new RuntimeException("socket is not closed"); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
223 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
224 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
225 |
void doAsyncClose(final AbstractSelectableChannel sc) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
226 |
AdaptorCloseAndInterrupt.pool.schedule(new Callable<Void>() { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
227 |
public Void call() throws Exception { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
228 |
sc.close(); |
44548
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
229 |
isClosed.set(true); |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
230 |
return null; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
231 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
232 |
}, new Random().nextInt(1000), TimeUnit.MILLISECONDS); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
233 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
234 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
235 |
void doAsyncInterrupt() { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
236 |
final Thread current = Thread.currentThread(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
237 |
AdaptorCloseAndInterrupt.pool.schedule(new Callable<Void>() { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
238 |
public Void call() throws Exception { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
239 |
current.interrupt(); |
44548
632ed240ed22
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents:
36678
diff
changeset
|
240 |
isInterrupted.set(true); |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
241 |
return null; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
242 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
243 |
}, new Random().nextInt(1000), TimeUnit.MILLISECONDS); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
244 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
245 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff
changeset
|
246 |
} |