Skip to content

Commit 911c359

Browse files
Andrew LuGoeLin
Andrew Lu
authored andcommitted
8249812: java/net/DatagramSocket/PortUnreachable.java still fails intermittently 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
1 parent bd7420a commit 911c359

File tree

1 file changed

+44
-29
lines changed

1 file changed

+44
-29
lines changed

test/jdk/java/net/DatagramSocket/PortUnreachable.java

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
* @test
2626
* @bug 4361783
2727
* @key intermittent
28-
* @summary Test to see if ICMP Port Unreachable on non-connected
29-
* DatagramSocket causes a SocketException "socket closed"
30-
* exception on Windows 2000.
28+
* @summary Test to see if ICMP Port Unreachable on non-connected
29+
* DatagramSocket causes a SocketException "socket closed"
30+
* exception on Windows 2000.
31+
* @run main/othervm PortUnreachable
3132
*/
33+
3234
import java.net.BindException;
3335
import java.net.DatagramPacket;
3436
import java.net.DatagramSocket;
@@ -41,25 +43,22 @@ public class PortUnreachable {
4143
int serverPort;
4244
int clientPort;
4345

44-
public void serverSend() {
45-
try {
46-
InetAddress addr = InetAddress.getLocalHost();
47-
Thread.sleep(1000);
48-
// send a delayed packet which should mean a delayed icmp
49-
// port unreachable
50-
byte b[] = "A late msg".getBytes();
51-
DatagramPacket packet = new DatagramPacket(b, b.length, addr,
52-
serverPort);
53-
clientSock.send(packet);
54-
55-
DatagramSocket sock = recreateServerSocket(serverPort);
56-
b = "Greetings from the server".getBytes();
57-
packet = new DatagramPacket(b, b.length, addr, clientPort);
58-
sock.send(packet);
59-
sock.close();
60-
} catch (Exception e) {
61-
e.printStackTrace();
62-
}
46+
public void serverSend() throws Exception {
47+
InetAddress addr = InetAddress.getLocalHost();
48+
Thread.sleep(1000);
49+
// send a delayed packet which should mean a delayed icmp
50+
// port unreachable
51+
byte b[] = "A late msg".getBytes();
52+
DatagramPacket packet = new DatagramPacket(b, b.length, addr,
53+
serverPort);
54+
clientSock.send(packet);
55+
56+
DatagramSocket sock = recreateServerSocket(serverPort);
57+
b = "Greetings from the server".getBytes();
58+
packet = new DatagramPacket(b, b.length, addr, clientPort);
59+
sock.send(packet);
60+
Thread.sleep(500); // give time to the kernel to send packet
61+
sock.close();
6362
}
6463

6564
DatagramSocket recreateServerSocket (int serverPort) throws Exception {
@@ -70,15 +69,15 @@ DatagramSocket recreateServerSocket (int serverPort) throws Exception {
7069
serverPort);
7170
// it's possible that this method intermittently fails, if some other
7271
// process running on the machine grabs the port we want before us,
73-
// and doesn't release it before the 5 * 500 ms are elapsed...
72+
// and doesn't release it before the 10 * 500 ms are elapsed...
7473
while (serverSocket == null) {
7574
try {
7675
serverSocket = new DatagramSocket(serverPort, InetAddress.getLocalHost());
7776
} catch (BindException bEx) {
78-
if (retryCount++ < 5) {
79-
sleeptime += sleepAtLeast(500);
77+
if (retryCount++ < 10) {
78+
sleeptime += sleepAtLeast(500);
8079
} else {
81-
System.out.println("Give up after 5 retries and " + sleeptime(sleeptime));
80+
System.out.println("Give up after 10 retries and " + sleeptime(sleeptime));
8281
System.out.println("Has some other process grabbed port " + serverPort + "?");
8382
throw bEx;
8483
}
@@ -154,6 +153,7 @@ void execute () throws Exception{
154153
clientSock.send(packet);
155154

156155
serverSend();
156+
157157
// try to receive
158158
b = new byte[25];
159159
packet = new DatagramPacket(b, b.length, addr, serverPort);
@@ -166,8 +166,23 @@ void execute () throws Exception{
166166
}
167167

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

173+
while (true) {
174+
try {
175+
PortUnreachable test = new PortUnreachable();
176+
test.execute();
177+
return;
178+
} catch (BindException bEx) {
179+
System.out.println("Failed to bind server: " + bEx);
180+
if (++catchCount > 3) {
181+
System.out.printf("Max retry count exceeded (%d)%n", catchCount);
182+
throw bEx;
183+
}
184+
System.out.printf("Retrying; retry count: %d%n", catchCount);
185+
}
186+
}
187+
}
173188
}

0 commit comments

Comments
 (0)