Skip to content

Commit

Permalink
8249812: java/net/DatagramSocket/PortUnreachable.java still fails int…
Browse files Browse the repository at this point in the history
…ermittently with SocketTimeoutException

8232513: java/net/DatagramSocket/PortUnreachable.java still fails intermittently with BindException

Fixed the test to reenable its retry logic

Reviewed-by: lucy
Backport-of: 9f23c2c
  • Loading branch information
Andrew Lu authored and GoeLin committed Nov 28, 2023
1 parent bd7420a commit 911c359
Showing 1 changed file with 44 additions and 29 deletions.
73 changes: 44 additions & 29 deletions test/jdk/java/net/DatagramSocket/PortUnreachable.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
* @test
* @bug 4361783
* @key intermittent
* @summary Test to see if ICMP Port Unreachable on non-connected
* DatagramSocket causes a SocketException "socket closed"
* exception on Windows 2000.
* @summary Test to see if ICMP Port Unreachable on non-connected
* DatagramSocket causes a SocketException "socket closed"
* exception on Windows 2000.
* @run main/othervm PortUnreachable
*/

import java.net.BindException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
Expand All @@ -41,25 +43,22 @@ public class PortUnreachable {
int serverPort;
int clientPort;

public void serverSend() {
try {
InetAddress addr = InetAddress.getLocalHost();
Thread.sleep(1000);
// send a delayed packet which should mean a delayed icmp
// port unreachable
byte b[] = "A late msg".getBytes();
DatagramPacket packet = new DatagramPacket(b, b.length, addr,
serverPort);
clientSock.send(packet);

DatagramSocket sock = recreateServerSocket(serverPort);
b = "Greetings from the server".getBytes();
packet = new DatagramPacket(b, b.length, addr, clientPort);
sock.send(packet);
sock.close();
} catch (Exception e) {
e.printStackTrace();
}
public void serverSend() throws Exception {
InetAddress addr = InetAddress.getLocalHost();
Thread.sleep(1000);
// send a delayed packet which should mean a delayed icmp
// port unreachable
byte b[] = "A late msg".getBytes();
DatagramPacket packet = new DatagramPacket(b, b.length, addr,
serverPort);
clientSock.send(packet);

DatagramSocket sock = recreateServerSocket(serverPort);
b = "Greetings from the server".getBytes();
packet = new DatagramPacket(b, b.length, addr, clientPort);
sock.send(packet);
Thread.sleep(500); // give time to the kernel to send packet
sock.close();
}

DatagramSocket recreateServerSocket (int serverPort) throws Exception {
Expand All @@ -70,15 +69,15 @@ DatagramSocket recreateServerSocket (int serverPort) throws Exception {
serverPort);
// it's possible that this method intermittently fails, if some other
// process running on the machine grabs the port we want before us,
// and doesn't release it before the 5 * 500 ms are elapsed...
// and doesn't release it before the 10 * 500 ms are elapsed...
while (serverSocket == null) {
try {
serverSocket = new DatagramSocket(serverPort, InetAddress.getLocalHost());
} catch (BindException bEx) {
if (retryCount++ < 5) {
sleeptime += sleepAtLeast(500);
if (retryCount++ < 10) {
sleeptime += sleepAtLeast(500);
} else {
System.out.println("Give up after 5 retries and " + sleeptime(sleeptime));
System.out.println("Give up after 10 retries and " + sleeptime(sleeptime));
System.out.println("Has some other process grabbed port " + serverPort + "?");
throw bEx;
}
Expand Down Expand Up @@ -154,6 +153,7 @@ void execute () throws Exception{
clientSock.send(packet);

serverSend();

// try to receive
b = new byte[25];
packet = new DatagramPacket(b, b.length, addr, serverPort);
Expand All @@ -166,8 +166,23 @@ void execute () throws Exception{
}

public static void main(String[] args) throws Exception {
PortUnreachable test = new PortUnreachable();
test.execute();
}
// A BindException might be thrown intermittently. In that case retry
// 3 times before propagating the exception to finish execution.
int catchCount = 0;

while (true) {
try {
PortUnreachable test = new PortUnreachable();
test.execute();
return;
} catch (BindException bEx) {
System.out.println("Failed to bind server: " + bEx);
if (++catchCount > 3) {
System.out.printf("Max retry count exceeded (%d)%n", catchCount);
throw bEx;
}
System.out.printf("Retrying; retry count: %d%n", catchCount);
}
}
}
}

1 comment on commit 911c359

@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.