author | mli |
Fri, 16 Mar 2018 18:15:31 +0800 | |
changeset 49255 | acdb8531cc8b |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49255
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2001, 2018, 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 |
/* @test |
|
36410
c2ef247d0b6f
8143610: (dc) java/nio/channels/DatagramChannel/AdaptDatagramSocket.java failed intermittently due to SocketTimeoutException
mli
parents:
30046
diff
changeset
|
25 |
* @bug 4313882 4981129 8143610 |
2 | 26 |
* @summary Unit test for datagram-socket-channel adaptors |
49255
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
27 |
* @library .. /test/lib |
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
28 |
* @build jdk.test.lib.Utils TestServers |
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
29 |
* @run main AdaptDatagramSocket |
30046 | 30 |
* @key randomness |
2 | 31 |
*/ |
32 |
||
33 |
import java.net.*; |
|
34 |
import java.nio.channels.*; |
|
35 |
import java.util.*; |
|
36 |
||
37 |
||
38 |
public class AdaptDatagramSocket { |
|
39 |
||
40 |
static java.io.PrintStream out = System.out; |
|
41 |
static Random rand = new Random(); |
|
42 |
||
43 |
static String toString(DatagramPacket dp) { |
|
44 |
return ("DatagramPacket[off=" + dp.getOffset() |
|
45 |
+ ", len=" + dp.getLength() |
|
46 |
+ "]"); |
|
47 |
} |
|
48 |
||
49 |
static void test(DatagramSocket ds, InetSocketAddress dst, |
|
50 |
boolean shouldTimeout) |
|
51 |
throws Exception |
|
52 |
{ |
|
53 |
DatagramPacket op = new DatagramPacket(new byte[100], 13, 42, dst); |
|
54 |
rand.nextBytes(op.getData()); |
|
55 |
DatagramPacket ip = new DatagramPacket(new byte[100], 19, 100 - 19); |
|
56 |
out.println("pre op: " + toString(op) + " ip: " + toString(ip)); |
|
57 |
||
58 |
long start = System.currentTimeMillis(); |
|
59 |
ds.send(op); |
|
60 |
||
61 |
for (;;) { |
|
62 |
try { |
|
63 |
ds.receive(ip); |
|
64 |
if (ip.getLength() == 0) { // ## Not sure why this happens |
|
65 |
ip.setLength(100 - 19); |
|
66 |
continue; |
|
67 |
} |
|
68 |
} catch (SocketTimeoutException x) { |
|
69 |
if (shouldTimeout) { |
|
70 |
out.println("Receive timed out, as expected"); |
|
71 |
return; |
|
72 |
} |
|
73 |
throw x; |
|
74 |
} |
|
75 |
break; |
|
76 |
} |
|
77 |
||
78 |
out.println("rtt: " + (System.currentTimeMillis() - start)); |
|
79 |
out.println("post op: " + toString(op) + " ip: " + toString(ip)); |
|
80 |
||
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
81 |
for (int i = 0; i < ip.getLength(); i++) { |
2 | 82 |
if (ip.getData()[ip.getOffset() + i] |
83 |
!= op.getData()[op.getOffset() + i]) |
|
84 |
throw new Exception("Incorrect data received"); |
|
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
85 |
} |
5807
d34ed576e234
4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents:
5506
diff
changeset
|
86 |
|
d34ed576e234
4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents:
5506
diff
changeset
|
87 |
if (!(ip.getSocketAddress().equals(dst))) { |
d34ed576e234
4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents:
5506
diff
changeset
|
88 |
throw new Exception("Incorrect sender address, expected: " + dst |
d34ed576e234
4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents:
5506
diff
changeset
|
89 |
+ " actual: " + ip.getSocketAddress()); |
d34ed576e234
4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents:
5506
diff
changeset
|
90 |
} |
2 | 91 |
} |
92 |
||
93 |
static void test(InetSocketAddress dst, |
|
94 |
int timeout, boolean shouldTimeout, |
|
95 |
boolean connect) |
|
96 |
throws Exception |
|
97 |
{ |
|
98 |
out.println(); |
|
99 |
out.println("dst: " + dst); |
|
100 |
||
101 |
DatagramSocket ds; |
|
102 |
if (false) { |
|
103 |
// Original |
|
104 |
ds = new DatagramSocket(); |
|
105 |
} else { |
|
106 |
DatagramChannel dc = DatagramChannel.open(); |
|
107 |
ds = dc.socket(); |
|
108 |
ds.bind(new InetSocketAddress(0)); |
|
109 |
} |
|
110 |
||
111 |
out.println("socket: " + ds); |
|
112 |
if (connect) { |
|
113 |
ds.connect(dst); |
|
114 |
out.println("connect: " + ds); |
|
115 |
} |
|
116 |
InetSocketAddress src = new InetSocketAddress(ds.getLocalAddress(), |
|
117 |
ds.getLocalPort()); |
|
118 |
out.println("src: " + src); |
|
119 |
||
120 |
if (timeout > 0) |
|
121 |
ds.setSoTimeout(timeout); |
|
122 |
out.println("timeout: " + ds.getSoTimeout()); |
|
123 |
||
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
124 |
for (int i = 0; i < 5; i++) { |
2 | 125 |
test(ds, dst, shouldTimeout); |
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
126 |
} |
2 | 127 |
|
128 |
// Leave the socket open so that we don't reuse the old src address |
|
129 |
//ds.close(); |
|
130 |
||
131 |
} |
|
132 |
||
133 |
public static void main(String[] args) throws Exception { |
|
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
134 |
// need an UDP echo server |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
135 |
try (TestServers.UdpEchoServer echoServer |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
136 |
= TestServers.UdpEchoServer.startNewServer(100)) { |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
137 |
final InetSocketAddress address |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
138 |
= new InetSocketAddress(echoServer.getAddress(), |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
139 |
echoServer.getPort()); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
140 |
test(address, 0, false, false); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
141 |
test(address, 0, false, true); |
36410
c2ef247d0b6f
8143610: (dc) java/nio/channels/DatagramChannel/AdaptDatagramSocket.java failed intermittently due to SocketTimeoutException
mli
parents:
30046
diff
changeset
|
142 |
test(address, Integer.MAX_VALUE, false, false); |
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
143 |
} |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
144 |
try (TestServers.UdpDiscardServer discardServer |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
145 |
= TestServers.UdpDiscardServer.startNewServer()) { |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
146 |
final InetSocketAddress address |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
147 |
= new InetSocketAddress(discardServer.getAddress(), |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
148 |
discardServer.getPort()); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
149 |
test(address, 10, true, false); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
150 |
} |
2 | 151 |
} |
152 |
||
153 |
} |