Skip to content

Commit

Permalink
Review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
dfuch committed Aug 1, 2024
1 parent 6695360 commit 64fccfb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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());
Expand All @@ -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());
Expand Down
7 changes: 7 additions & 0 deletions test/jdk/java/nio/channels/DatagramChannel/Connect.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions test/jdk/java/nio/channels/DatagramChannel/NotBound.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 64fccfb

Please sign in to comment.