Skip to content

Commit 45588d6

Browse files
author
Andrew Lu
committed
8299813: java/nio/channels/DatagramChannel/Disconnect.java fails with jtreg test timeout due to lost datagram
Backport-of: 49eb00da8dc66cff3ca430f06ab21357ee6180ef
1 parent 60bbd37 commit 45588d6

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

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

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -34,6 +34,7 @@
3434
import java.nio.*;
3535
import java.nio.channels.*;
3636
import java.io.IOException;
37+
3738
import jdk.test.lib.net.IPSupport;
3839

3940
public class Disconnect {
@@ -42,43 +43,61 @@ public static void main(String[] args) throws IOException {
4243

4344
// test with default protocol family
4445
try (DatagramChannel dc = DatagramChannel.open()) {
45-
test(dc);
46-
test(dc);
46+
InetAddress lo = InetAddress.getLoopbackAddress();
47+
System.out.println("Testing with default family and " + lo);
48+
test(dc, lo);
49+
test(dc, lo);
4750
}
4851

4952
if (IPSupport.hasIPv4()) {
5053
// test with IPv4 only
5154
try (DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET)) {
52-
test(dc);
53-
test(dc);
55+
InetAddress lo4 = InetAddress.ofLiteral("127.0.0.1");
56+
System.out.println("Testing with INET family and " + lo4);
57+
test(dc, lo4);
58+
test(dc, lo4);
5459
}
5560
}
5661

5762
if (IPSupport.hasIPv6()) {
5863
// test with IPv6 only
5964
try (DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET6)) {
60-
test(dc);
61-
test(dc);
65+
InetAddress lo6 = InetAddress.ofLiteral("::1");
66+
System.out.println("Testing with INET6 family and " + lo6);
67+
test(dc, lo6);
68+
test(dc, lo6);
6269
}
6370
}
6471
}
6572

73+
static int getLocalPort(DatagramChannel ch) throws IOException {
74+
return ((InetSocketAddress) ch.getLocalAddress()).getPort();
75+
}
76+
6677
/**
6778
* Connect DatagramChannel to a server, write a datagram and disconnect. Invoke
6879
* a second or subsequent time with the same DatagramChannel instance to check
6980
* that disconnect works as expected.
7081
*/
71-
static void test(DatagramChannel dc) throws IOException {
82+
static void test(DatagramChannel dc, InetAddress lo) throws IOException {
7283
try (DatagramChannel server = DatagramChannel.open()) {
73-
server.bind(new InetSocketAddress(0));
84+
server.bind(new InetSocketAddress(lo, 0));
7485

75-
InetAddress lh = InetAddress.getLocalHost();
76-
dc.connect(new InetSocketAddress(lh, server.socket().getLocalPort()));
86+
SocketAddress dcbound = dc.getLocalAddress();
87+
dc.connect(new InetSocketAddress(lo, server.socket().getLocalPort()));
88+
System.out.println("dc bound to " + dcbound + " and connected from " +
89+
dc.getLocalAddress() + " to " + dc.getRemoteAddress());
7790

7891
dc.write(ByteBuffer.wrap("hello".getBytes()));
7992

80-
ByteBuffer bb = ByteBuffer.allocate(100);
81-
server.receive(bb);
93+
if (getLocalPort(dc) != getLocalPort(server)) {
94+
ByteBuffer bb = ByteBuffer.allocate(100);
95+
server.receive(bb);
96+
} else {
97+
// some systems may allow dc and server to bind to the same port.
98+
// when that happen the datagram may never be received
99+
System.out.println("Server and clients are bound to the same port: skipping receive");
100+
}
82101

83102
dc.disconnect();
84103

0 commit comments

Comments
 (0)