diff --git a/test/jdk/java/nio/channels/DatagramChannel/AdaptorMulticasting.java b/test/jdk/java/nio/channels/DatagramChannel/AdaptorMulticasting.java index 38b3051effa08..cab2bc7628562 100644 --- a/test/jdk/java/nio/channels/DatagramChannel/AdaptorMulticasting.java +++ b/test/jdk/java/nio/channels/DatagramChannel/AdaptorMulticasting.java @@ -402,10 +402,6 @@ static void testLoopbackMode(MulticastSocket s) throws IOException { () -> s.setOption((SocketOption) IP_MULTICAST_LOOP, "badValue")); } - static int getPort(SocketAddress address) { - return ((InetSocketAddress) address).getPort(); - } - /** * Send a datagram to the given multicast group and check that it is received. */ @@ -427,10 +423,10 @@ static void testSendReceive(MulticastSocket s, InetAddress group) throws IOExcep // receive message s.setSoTimeout(0); - do { + while (true) { p = new DatagramPacket(new byte[1024], 100); s.receive(p); - if (getPort(p.getSocketAddress()) == getPort(s.getLocalSocketAddress())) { + if (p.getPort() == s.getLocalPort()) { String str = new String(p.getData(), p.getOffset(), p.getLength(), "UTF-8"); if (Arrays.equals(p.getData(), p.getOffset(), p.getLength(), message, 0, message.length)) { System.out.format("Got expected message \"%s\" from %s%n", str, p.getSocketAddress()); @@ -442,7 +438,7 @@ static void testSendReceive(MulticastSocket s, InetAddress group) throws IOExcep System.out.println("Unexpected message received. Expected message from: " + s.getLocalAddress()); System.out.println("Received message sender doesn't match - skipping: " + p.getSocketAddress()); } - } while(true); + } assertTrue(p.getLength() == message.length); assertTrue(p.getPort() == s.getLocalPort()); diff --git a/test/jdk/java/nio/channels/DatagramChannel/Connect.java b/test/jdk/java/nio/channels/DatagramChannel/Connect.java index 01c83df08cae6..41c172b33284d 100644 --- a/test/jdk/java/nio/channels/DatagramChannel/Connect.java +++ b/test/jdk/java/nio/channels/DatagramChannel/Connect.java @@ -118,6 +118,13 @@ public void run() { ByteBuffer bb = ByteBuffer.allocateDirect(MAX); bb.put(bytes); bb.flip(); + // When connecting an unbound datagram channel, the underlying + // socket will first be bound to the wildcard address. On macOS, + // the system may allocate the same port on which another socket + // is already bound with a more specific address. This may prevent + // datagrams directed at the connected socket to reach it. + // To avoid this, when on macOS, we preemptively bind `dc` to the + // specific address instead of letting it bind to the wildcard. if (Platform.isOSX()) { dc.bind(new InetSocketAddress(((InetSocketAddress)connectSocketAddress).getAddress(), 0)); err.println("Initiator bound to: " + connectSocketAddress); diff --git a/test/jdk/java/nio/channels/DatagramChannel/NotBound.java b/test/jdk/java/nio/channels/DatagramChannel/NotBound.java index 3249c503c7ba7..bcd73591bf967 100644 --- a/test/jdk/java/nio/channels/DatagramChannel/NotBound.java +++ b/test/jdk/java/nio/channels/DatagramChannel/NotBound.java @@ -109,7 +109,7 @@ public static void main(String[] args) throws IOException { // connect dc = DatagramChannel.open(); try { - System.out.println("Checks that connect() binds the socket"); + System.out.println("Check that connect() binds the socket"); try (DatagramChannel peer = DatagramChannel.open()) { peer.bind(new InetSocketAddress(0)); int peerPort = ((InetSocketAddress)(peer.getLocalAddress())).getPort(); @@ -123,7 +123,7 @@ public static void main(String[] args) throws IOException { // send dc = DatagramChannel.open(); try { - System.out.println("Checks that send() binds the socket"); + System.out.println("Check that send() binds the socket"); ByteBuffer bb = ByteBuffer.wrap("NotBound: ignore this".getBytes()); SocketAddress target = new InetSocketAddress(InetAddress.getLocalHost(), 5000); @@ -136,7 +136,7 @@ public static void main(String[] args) throws IOException { // receive (blocking) dc = DatagramChannel.open(); try { - System.out.println("Checks that blocking receive() binds the socket"); + System.out.println("Check that blocking receive() binds the socket"); ByteBuffer bb = ByteBuffer.allocateDirect(128); wakeupWhenBound(dc); SocketAddress sender = dc.receive(bb); @@ -151,7 +151,7 @@ public static void main(String[] args) throws IOException { // receive (non-blocking) dc = DatagramChannel.open(); try { - System.out.println("Checks that non-blocking receive() binds the socket"); + System.out.println("Check that non-blocking receive() binds the socket"); dc.configureBlocking(false); ByteBuffer bb = ByteBuffer.allocateDirect(128); SocketAddress sender = dc.receive(bb);