25
25
* @test
26
26
* @bug 4361783
27
27
* @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
31
32
*/
33
+
32
34
import java .net .BindException ;
33
35
import java .net .DatagramPacket ;
34
36
import java .net .DatagramSocket ;
@@ -41,25 +43,22 @@ public class PortUnreachable {
41
43
int serverPort ;
42
44
int clientPort ;
43
45
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 ();
63
62
}
64
63
65
64
DatagramSocket recreateServerSocket (int serverPort ) throws Exception {
@@ -70,15 +69,15 @@ DatagramSocket recreateServerSocket (int serverPort) throws Exception {
70
69
serverPort );
71
70
// it's possible that this method intermittently fails, if some other
72
71
// 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...
74
73
while (serverSocket == null ) {
75
74
try {
76
75
serverSocket = new DatagramSocket (serverPort , InetAddress .getLocalHost ());
77
76
} catch (BindException bEx ) {
78
- if (retryCount ++ < 5 ) {
79
- sleeptime += sleepAtLeast (500 );
77
+ if (retryCount ++ < 10 ) {
78
+ sleeptime += sleepAtLeast (500 );
80
79
} 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 ));
82
81
System .out .println ("Has some other process grabbed port " + serverPort + "?" );
83
82
throw bEx ;
84
83
}
@@ -154,6 +153,7 @@ void execute () throws Exception{
154
153
clientSock .send (packet );
155
154
156
155
serverSend ();
156
+
157
157
// try to receive
158
158
b = new byte [25 ];
159
159
packet = new DatagramPacket (b , b .length , addr , serverPort );
@@ -166,8 +166,23 @@ void execute () throws Exception{
166
166
}
167
167
168
168
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 ;
172
172
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
+ }
173
188
}
0 commit comments