Skip to content

Commit 1b940f3

Browse files
committed
8256146: Cleanup test/jdk/java/nio/channels/DatagramChannel/Connect.java
Backport-of: 9dbbe83
1 parent cd591e4 commit 1b940f3

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

test/jdk/java/nio/channels/DatagramChannel/Connect.java

+37-22
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public static void main(String[] args) throws Exception {
4747

4848
static void test() throws Exception {
4949
ExecutorService threadPool = Executors.newCachedThreadPool();
50-
try (Reactor r = new Reactor();
51-
Actor a = new Actor(r.getSocketAddress())
50+
try (Responder r = new Responder();
51+
Initiator a = new Initiator(r.getSocketAddress())
5252
) {
5353
invoke(threadPool, a, r);
5454
} finally {
@@ -75,12 +75,25 @@ private static void wait(CompletableFuture<?>... futures) throws CompletionExcep
7575
future.join();
7676
}
7777

78-
public static class Actor implements AutoCloseable, Runnable {
79-
final SocketAddress socketAddress;
78+
private static SocketAddress toConnectAddress(SocketAddress address) {
79+
if (address instanceof InetSocketAddress) {
80+
var inet = (InetSocketAddress) address;
81+
if (inet.getAddress().isAnyLocalAddress()) {
82+
// if the peer is bound to the wildcard address, use
83+
// the loopback address to connect.
84+
var loopback = InetAddress.getLoopbackAddress();
85+
return new InetSocketAddress(loopback, inet.getPort());
86+
}
87+
}
88+
return address;
89+
}
90+
91+
public static class Initiator implements AutoCloseable, Runnable {
92+
final SocketAddress connectSocketAddress;
8093
final DatagramChannel dc;
8194

82-
Actor(SocketAddress socketAddress) throws IOException {
83-
this.socketAddress = socketAddress;
95+
Initiator(SocketAddress peerSocketAddress) throws IOException {
96+
this.connectSocketAddress = toConnectAddress(peerSocketAddress);
8497
dc = DatagramChannel.open();
8598
}
8699

@@ -89,37 +102,38 @@ public void run() {
89102
ByteBuffer bb = ByteBuffer.allocateDirect(256);
90103
bb.put("hello".getBytes());
91104
bb.flip();
92-
dc.connect(socketAddress);
105+
log.println("Initiator connecting to " + connectSocketAddress);
106+
dc.connect(connectSocketAddress);
93107

94108
// Send a message
95-
log.println("Actor attempting to write to Reactor at " + socketAddress.toString());
109+
log.println("Initiator attempting to write to Responder at " + connectSocketAddress.toString());
96110
dc.write(bb);
97111

98112
// Try to send to some other address
99113
try {
100114
int port = dc.socket().getLocalPort();
101115
InetAddress loopback = InetAddress.getLoopbackAddress();
102116
InetSocketAddress otherAddress = new InetSocketAddress(loopback, (port == 3333 ? 3332 : 3333));
103-
log.println("Testing if Actor throws AlreadyConnectedException" + otherAddress.toString());
117+
log.println("Testing if Initiator throws AlreadyConnectedException" + otherAddress.toString());
104118
dc.send(bb, otherAddress);
105-
throw new RuntimeException("Actor allowed send to other address while already connected");
119+
throw new RuntimeException("Initiator allowed send to other address while already connected");
106120
} catch (AlreadyConnectedException ace) {
107121
// Correct behavior
108122
}
109123

110124
// Read a reply
111125
bb.flip();
112-
log.println("Actor waiting to read");
126+
log.println("Initiator waiting to read");
113127
dc.read(bb);
114128
bb.flip();
115129
CharBuffer cb = StandardCharsets.US_ASCII.
116130
newDecoder().decode(bb);
117-
log.println("Actor received from Reactor at " + socketAddress + ": " + cb);
131+
log.println("Initiator received from Responder at " + connectSocketAddress + ": " + cb);
118132
} catch (Exception ex) {
119-
log.println("Actor threw exception: " + ex);
133+
log.println("Initiator threw exception: " + ex);
120134
throw new RuntimeException(ex);
121135
} finally {
122-
log.println("Actor finished");
136+
log.println("Initiator finished");
123137
}
124138
}
125139

@@ -129,11 +143,12 @@ public void close() throws IOException {
129143
}
130144
}
131145

132-
public static class Reactor implements AutoCloseable, Runnable {
146+
public static class Responder implements AutoCloseable, Runnable {
133147
final DatagramChannel dc;
134148

135-
Reactor() throws IOException {
136-
dc = DatagramChannel.open().bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
149+
Responder() throws IOException {
150+
var address = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
151+
dc = DatagramChannel.open().bind(address);
137152
}
138153

139154
SocketAddress getSocketAddress() throws IOException {
@@ -144,23 +159,23 @@ public void run() {
144159
try {
145160
// Listen for a message
146161
ByteBuffer bb = ByteBuffer.allocateDirect(100);
147-
log.println("Reactor waiting to receive");
162+
log.println("Responder waiting to receive");
148163
SocketAddress sa = dc.receive(bb);
149164
bb.flip();
150165
CharBuffer cb = StandardCharsets.US_ASCII.
151166
newDecoder().decode(bb);
152-
log.println("Reactor received from Actor at" + sa + ": " + cb);
167+
log.println("Responder received from Initiator at" + sa + ": " + cb);
153168

154169
// Reply to sender
155170
dc.connect(sa);
156171
bb.flip();
157-
log.println("Reactor attempting to write: " + dc.getRemoteAddress().toString());
172+
log.println("Responder attempting to write: " + dc.getRemoteAddress().toString());
158173
dc.write(bb);
159174
} catch (Exception ex) {
160-
log.println("Reactor threw exception: " + ex);
175+
log.println("Responder threw exception: " + ex);
161176
throw new RuntimeException(ex);
162177
} finally {
163-
log.println("Reactor finished");
178+
log.println("Responder finished");
164179
}
165180
}
166181

0 commit comments

Comments
 (0)