8269657: Test java/nio/channels/DatagramChannel/Loopback.java failed: Unexpected message

Reviewed-by: dfuchs
This commit is contained in:
SendaoYan 2024-06-28 09:38:18 +00:00 committed by Daniel Fuchs
parent 99d2bbf767
commit c798316bc4

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -118,7 +118,8 @@ public class Loopback {
// send datagram to multicast group
System.out.format("send %s -> %s%n", dc.getLocalAddress(), target);
ByteBuffer src = ByteBuffer.wrap("hello".getBytes("UTF-8"));
String str = "hello " + System.nanoTime();
ByteBuffer src = ByteBuffer.wrap(str.getBytes("UTF-8"));
dc.send(src, target);
// receive datagram sent to multicast group
@ -142,6 +143,7 @@ public class Loopback {
System.out.format("send %s -> %s%n", dc.getLocalAddress(), target);
src.clear();
dc.send(src, target);
src.flip();
// test that we don't receive the datagram sent to multicast group
dc.configureBlocking(false);
@ -157,10 +159,16 @@ public class Loopback {
} else {
sel.selectedKeys().clear();
SocketAddress sender = dc.receive(dst);
if (src.mismatch(dst) != -1) {
System.out.println("src: " + src + "not equal to dst: " + dst);
dst.clear();
continue;
}
if (sender != null) {
System.out.format("received %s from %s%n", dst, sender);
senderPort = ((InetSocketAddress) sender).getPort();
assertTrue(senderPort != localPort, "Unexpected message");
assertTrue(senderPort != localPort,
"Unexpected message: localPort=" + localPort);
}
}
}