author | pconcannon |
Wed, 27 Nov 2019 16:01:34 +0000 | |
changeset 59299 | 17d242844fc9 |
parent 58009 | 0daf32316b47 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54809
351da897f409
8223465: Replace wildcard address with loopback or local host in tests - part 3
aefimov
parents:
47243
diff
changeset
|
2 |
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* @bug 4361783 |
|
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
27 |
* @key intermittent |
2 | 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 |
*/ |
|
47243
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
32 |
import java.net.BindException; |
54809
351da897f409
8223465: Replace wildcard address with loopback or local host in tests - part 3
aefimov
parents:
47243
diff
changeset
|
33 |
import java.net.DatagramPacket; |
2 | 34 |
import java.net.DatagramSocket; |
54809
351da897f409
8223465: Replace wildcard address with loopback or local host in tests - part 3
aefimov
parents:
47243
diff
changeset
|
35 |
import java.net.InetAddress; |
351da897f409
8223465: Replace wildcard address with loopback or local host in tests - part 3
aefimov
parents:
47243
diff
changeset
|
36 |
import java.net.InetSocketAddress; |
2 | 37 |
|
21947
2818a9404890
8025211: Intermittent test failure: java/net/DatagramSocket/PortUnreachable.java
msheppar
parents:
5506
diff
changeset
|
38 |
public class PortUnreachable { |
2 | 39 |
|
40 |
DatagramSocket clientSock; |
|
41 |
int serverPort; |
|
42 |
int clientPort; |
|
43 |
||
21947
2818a9404890
8025211: Intermittent test failure: java/net/DatagramSocket/PortUnreachable.java
msheppar
parents:
5506
diff
changeset
|
44 |
public void serverSend() { |
2 | 45 |
try { |
46 |
InetAddress addr = InetAddress.getLocalHost(); |
|
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
47 |
Thread.sleep(1000); |
2 | 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 |
||
47243
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
55 |
DatagramSocket sock = recreateServerSocket(serverPort); |
21947
2818a9404890
8025211: Intermittent test failure: java/net/DatagramSocket/PortUnreachable.java
msheppar
parents:
5506
diff
changeset
|
56 |
b = "Greetings from the server".getBytes(); |
2 | 57 |
packet = new DatagramPacket(b, b.length, addr, clientPort); |
58 |
sock.send(packet); |
|
59 |
sock.close(); |
|
60 |
} catch (Exception e) { |
|
61 |
e.printStackTrace(); |
|
62 |
} |
|
63 |
} |
|
64 |
||
47243
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
65 |
DatagramSocket recreateServerSocket (int serverPort) throws Exception { |
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
66 |
DatagramSocket serverSocket = null; |
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
67 |
int retryCount = 0; |
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
68 |
long sleeptime = 0; |
47243
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
69 |
System.out.println("Attempting to recreate server socket with port: " + |
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
70 |
serverPort); |
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
71 |
// it's possible that this method intermittently fails, if some other |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
72 |
// process running on the machine grabs the port we want before us, |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
73 |
// and doesn't release it before the 5 * 500 ms are elapsed... |
47243
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
74 |
while (serverSocket == null) { |
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
75 |
try { |
54809
351da897f409
8223465: Replace wildcard address with loopback or local host in tests - part 3
aefimov
parents:
47243
diff
changeset
|
76 |
serverSocket = new DatagramSocket(serverPort, InetAddress.getLocalHost()); |
47243
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
77 |
} catch (BindException bEx) { |
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
78 |
if (retryCount++ < 5) { |
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
79 |
sleeptime += sleepAtLeast(500); |
47243
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
80 |
} else { |
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
81 |
System.out.println("Give up after 5 retries and " + sleeptime(sleeptime)); |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
82 |
System.out.println("Has some other process grabbed port " + serverPort + "?"); |
47243
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
83 |
throw bEx; |
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
84 |
} |
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
85 |
} |
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
86 |
} |
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
87 |
|
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
88 |
System.out.println("PortUnreachableTest.recreateServerSocket: returning socket == " |
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
89 |
+ serverSocket.getLocalAddress() + ":" + serverSocket.getLocalPort() |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
90 |
+ " obtained at " + attempt(retryCount) + " attempt with " + sleeptime(sleeptime)); |
47243
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
91 |
return serverSocket; |
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
92 |
} |
78fd493e38bb
8085875: java/net/DatagramSocket/PortUnreachable.java fails intermittently: Address already in use
xiaofeya
parents:
47216
diff
changeset
|
93 |
|
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
94 |
long sleepAtLeast(long millis) throws Exception { |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
95 |
long start = System.nanoTime(); |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
96 |
long ms = millis; |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
97 |
while (ms > 0) { |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
98 |
assert ms < Long.MAX_VALUE/1000_000L; |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
99 |
Thread.sleep(ms); |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
100 |
long elapsedms = (System.nanoTime() - start)/1000_000L; |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
101 |
ms = millis - elapsedms; |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
102 |
} |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
103 |
return millis - ms; |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
104 |
} |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
105 |
|
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
106 |
String attempt(int retry) { |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
107 |
switch (retry) { |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
108 |
case 0: return "first"; |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
109 |
case 1: return "second"; |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
110 |
case 2: return "third"; |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
111 |
default: return retry + "th"; |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
112 |
} |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
113 |
} |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
114 |
|
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
115 |
String sleeptime(long millis) { |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
116 |
if (millis == 0) return "no sleep"; |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
117 |
long sec = millis / 1000L; |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
118 |
long ms = millis % 1000L; |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
119 |
String sleeptime = ""; |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
120 |
if (millis > 0) { |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
121 |
if (sec > 0) { |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
122 |
sleeptime = "" + sec + " s" + |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
123 |
(ms > 0 ? " " : ""); |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
124 |
} |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
125 |
if (ms > 0 ) { |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
126 |
sleeptime += ms + " ms"; |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
127 |
} |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
128 |
} else sleeptime = millis + " ms"; // should not happen |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
129 |
return sleeptime + " of sleep time"; |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
130 |
} |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54809
diff
changeset
|
131 |
|
2 | 132 |
PortUnreachable() throws Exception { |
54809
351da897f409
8223465: Replace wildcard address with loopback or local host in tests - part 3
aefimov
parents:
47243
diff
changeset
|
133 |
clientSock = new DatagramSocket(new InetSocketAddress(InetAddress.getLocalHost(), 0)); |
2 | 134 |
clientPort = clientSock.getLocalPort(); |
135 |
||
21947
2818a9404890
8025211: Intermittent test failure: java/net/DatagramSocket/PortUnreachable.java
msheppar
parents:
5506
diff
changeset
|
136 |
} |
2818a9404890
8025211: Intermittent test failure: java/net/DatagramSocket/PortUnreachable.java
msheppar
parents:
5506
diff
changeset
|
137 |
|
2818a9404890
8025211: Intermittent test failure: java/net/DatagramSocket/PortUnreachable.java
msheppar
parents:
5506
diff
changeset
|
138 |
void execute () throws Exception{ |
2818a9404890
8025211: Intermittent test failure: java/net/DatagramSocket/PortUnreachable.java
msheppar
parents:
5506
diff
changeset
|
139 |
|
2 | 140 |
// pick a port for the server |
54809
351da897f409
8223465: Replace wildcard address with loopback or local host in tests - part 3
aefimov
parents:
47243
diff
changeset
|
141 |
DatagramSocket sock2 = new DatagramSocket(new InetSocketAddress(InetAddress.getLocalHost(), 0)); |
2 | 142 |
serverPort = sock2.getLocalPort(); |
143 |
||
144 |
// send a burst of packets to the unbound port - we should get back |
|
145 |
// icmp port unreachable messages |
|
146 |
// |
|
147 |
InetAddress addr = InetAddress.getLocalHost(); |
|
148 |
byte b[] = "Hello me".getBytes(); |
|
149 |
DatagramPacket packet = new DatagramPacket(b, b.length, addr, |
|
150 |
serverPort); |
|
21947
2818a9404890
8025211: Intermittent test failure: java/net/DatagramSocket/PortUnreachable.java
msheppar
parents:
5506
diff
changeset
|
151 |
//close just before sending |
2818a9404890
8025211: Intermittent test failure: java/net/DatagramSocket/PortUnreachable.java
msheppar
parents:
5506
diff
changeset
|
152 |
sock2.close(); |
2 | 153 |
for (int i=0; i<100; i++) |
154 |
clientSock.send(packet); |
|
155 |
||
21947
2818a9404890
8025211: Intermittent test failure: java/net/DatagramSocket/PortUnreachable.java
msheppar
parents:
5506
diff
changeset
|
156 |
serverSend(); |
2 | 157 |
// try to receive |
21947
2818a9404890
8025211: Intermittent test failure: java/net/DatagramSocket/PortUnreachable.java
msheppar
parents:
5506
diff
changeset
|
158 |
b = new byte[25]; |
2818a9404890
8025211: Intermittent test failure: java/net/DatagramSocket/PortUnreachable.java
msheppar
parents:
5506
diff
changeset
|
159 |
packet = new DatagramPacket(b, b.length, addr, serverPort); |
2 | 160 |
clientSock.setSoTimeout(10000); |
161 |
clientSock.receive(packet); |
|
21947
2818a9404890
8025211: Intermittent test failure: java/net/DatagramSocket/PortUnreachable.java
msheppar
parents:
5506
diff
changeset
|
162 |
System.out.println("client received data packet " + new String(packet.getData())); |
2 | 163 |
|
164 |
// done |
|
165 |
clientSock.close(); |
|
166 |
} |
|
167 |
||
168 |
public static void main(String[] args) throws Exception { |
|
21947
2818a9404890
8025211: Intermittent test failure: java/net/DatagramSocket/PortUnreachable.java
msheppar
parents:
5506
diff
changeset
|
169 |
PortUnreachable test = new PortUnreachable(); |
2818a9404890
8025211: Intermittent test failure: java/net/DatagramSocket/PortUnreachable.java
msheppar
parents:
5506
diff
changeset
|
170 |
test.execute(); |
2 | 171 |
} |
172 |
||
173 |
} |