Skip to content
Permalink
Browse files
8256146: Cleanup test/jdk/java/nio/channels/DatagramChannel/Connect.java
Backport-of: 9dbbe83
  • Loading branch information
GoeLin committed Apr 5, 2022
1 parent cd591e4 commit 1b940f31b5871e39178594a5456951a200ae5da3
Showing 1 changed file with 37 additions and 22 deletions.
@@ -47,8 +47,8 @@ public static void main(String[] args) throws Exception {

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

public static class Actor implements AutoCloseable, Runnable {
final SocketAddress socketAddress;
private static SocketAddress toConnectAddress(SocketAddress address) {
if (address instanceof InetSocketAddress) {
var inet = (InetSocketAddress) address;
if (inet.getAddress().isAnyLocalAddress()) {
// if the peer is bound to the wildcard address, use
// the loopback address to connect.
var loopback = InetAddress.getLoopbackAddress();
return new InetSocketAddress(loopback, inet.getPort());
}
}
return address;
}

public static class Initiator implements AutoCloseable, Runnable {
final SocketAddress connectSocketAddress;
final DatagramChannel dc;

Actor(SocketAddress socketAddress) throws IOException {
this.socketAddress = socketAddress;
Initiator(SocketAddress peerSocketAddress) throws IOException {
this.connectSocketAddress = toConnectAddress(peerSocketAddress);
dc = DatagramChannel.open();
}

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

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

// Try to send to some other address
try {
int port = dc.socket().getLocalPort();
InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress otherAddress = new InetSocketAddress(loopback, (port == 3333 ? 3332 : 3333));
log.println("Testing if Actor throws AlreadyConnectedException" + otherAddress.toString());
log.println("Testing if Initiator throws AlreadyConnectedException" + otherAddress.toString());
dc.send(bb, otherAddress);
throw new RuntimeException("Actor allowed send to other address while already connected");
throw new RuntimeException("Initiator allowed send to other address while already connected");
} catch (AlreadyConnectedException ace) {
// Correct behavior
}

// Read a reply
bb.flip();
log.println("Actor waiting to read");
log.println("Initiator waiting to read");
dc.read(bb);
bb.flip();
CharBuffer cb = StandardCharsets.US_ASCII.
newDecoder().decode(bb);
log.println("Actor received from Reactor at " + socketAddress + ": " + cb);
log.println("Initiator received from Responder at " + connectSocketAddress + ": " + cb);
} catch (Exception ex) {
log.println("Actor threw exception: " + ex);
log.println("Initiator threw exception: " + ex);
throw new RuntimeException(ex);
} finally {
log.println("Actor finished");
log.println("Initiator finished");
}
}

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

public static class Reactor implements AutoCloseable, Runnable {
public static class Responder implements AutoCloseable, Runnable {
final DatagramChannel dc;

Reactor() throws IOException {
dc = DatagramChannel.open().bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
Responder() throws IOException {
var address = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
dc = DatagramChannel.open().bind(address);
}

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

// Reply to sender
dc.connect(sa);
bb.flip();
log.println("Reactor attempting to write: " + dc.getRemoteAddress().toString());
log.println("Responder attempting to write: " + dc.getRemoteAddress().toString());
dc.write(bb);
} catch (Exception ex) {
log.println("Reactor threw exception: " + ex);
log.println("Responder threw exception: " + ex);
throw new RuntimeException(ex);
} finally {
log.println("Reactor finished");
log.println("Responder finished");
}
}

1 comment on commit 1b940f3

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.