author | bpb |
Mon, 30 Apr 2018 13:40:39 -0700 | |
changeset 49930 | 3aaaa5370999 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
31719
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
2 |
* Copyright (c) 2002, 2015, 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 |
|
31719
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
25 |
* @bug 4669040 8130394 |
2 | 26 |
* @summary Test DatagramChannel subsequent receives with no datagram ready |
27 |
* @author Mike McCloskey |
|
28 |
*/ |
|
29 |
||
31719
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
30 |
import java.io.IOException; |
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
31 |
import java.io.PrintStream; |
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
32 |
import java.net.InetAddress; |
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
33 |
import java.net.InetSocketAddress; |
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
34 |
import java.net.SocketAddress; |
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
35 |
import java.nio.ByteBuffer; |
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
36 |
import java.nio.ByteOrder; |
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
37 |
import java.nio.channels.DatagramChannel; |
2 | 38 |
|
39 |
public class Sender { |
|
40 |
||
41 |
static PrintStream log = System.err; |
|
31719
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
42 |
static volatile SocketAddress clientISA = null; |
2 | 43 |
|
44 |
public static void main(String[] args) throws Exception { |
|
45 |
test(); |
|
46 |
} |
|
47 |
||
48 |
static void test() throws Exception { |
|
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
49 |
Server server = new Server(); |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
50 |
Client client = new Client(server.port()); |
2 | 51 |
|
52 |
Thread serverThread = new Thread(server); |
|
53 |
serverThread.start(); |
|
54 |
||
55 |
Thread clientThread = new Thread(client); |
|
56 |
clientThread.start(); |
|
57 |
||
58 |
serverThread.join(); |
|
59 |
clientThread.join(); |
|
60 |
||
61 |
server.throwException(); |
|
62 |
client.throwException(); |
|
63 |
} |
|
64 |
||
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
65 |
public static class Client implements Runnable { |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
66 |
final int port; |
2 | 67 |
Exception e = null; |
68 |
||
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
69 |
Client(int port) { |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
70 |
this.port = port; |
2 | 71 |
} |
72 |
||
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
73 |
void throwException() throws Exception { |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
74 |
if (e != null) |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
75 |
throw e; |
2 | 76 |
} |
77 |
||
78 |
public void run() { |
|
79 |
try { |
|
80 |
DatagramChannel dc = DatagramChannel.open(); |
|
81 |
ByteBuffer bb = ByteBuffer.allocateDirect(12); |
|
82 |
bb.order(ByteOrder.BIG_ENDIAN); |
|
83 |
bb.putInt(1).putLong(1); |
|
84 |
bb.flip(); |
|
85 |
InetAddress address = InetAddress.getLocalHost(); |
|
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
86 |
InetSocketAddress isa = new InetSocketAddress(address, port); |
2 | 87 |
dc.connect(isa); |
31719
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
88 |
clientISA = dc.getLocalAddress(); |
2 | 89 |
dc.write(bb); |
90 |
} catch (Exception ex) { |
|
91 |
e = ex; |
|
92 |
} |
|
93 |
} |
|
94 |
} |
|
95 |
||
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
96 |
public static class Server implements Runnable { |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
97 |
final DatagramChannel dc; |
2 | 98 |
Exception e = null; |
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
99 |
|
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
100 |
Server() throws IOException { |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
101 |
dc = DatagramChannel.open().bind(new InetSocketAddress(0)); |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
102 |
} |
2 | 103 |
|
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
104 |
int port() { |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
105 |
return dc.socket().getLocalPort(); |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
106 |
} |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
107 |
|
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
108 |
void throwException() throws Exception { |
2 | 109 |
if (e != null) |
110 |
throw e; |
|
111 |
} |
|
112 |
||
113 |
void showBuffer(String s, ByteBuffer bb) { |
|
114 |
log.println(s); |
|
115 |
bb.rewind(); |
|
116 |
for (int i=0; i<bb.limit(); i++) { |
|
117 |
byte element = bb.get(); |
|
118 |
log.print(element); |
|
119 |
} |
|
120 |
log.println(); |
|
121 |
} |
|
122 |
||
123 |
public void run() { |
|
124 |
SocketAddress sa = null; |
|
125 |
||
31719
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
126 |
|
2 | 127 |
try { |
128 |
ByteBuffer bb = ByteBuffer.allocateDirect(12); |
|
129 |
bb.clear(); |
|
130 |
// Get the one valid datagram |
|
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
131 |
dc.configureBlocking(false); |
31719
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
132 |
while (sa == null) { |
2 | 133 |
sa = dc.receive(bb); |
31719
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
134 |
if (sa != null && clientISA != null && !clientISA.equals(sa)) { |
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
135 |
log.println("Ignore a possible stray diagram from " + sa); |
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
136 |
sa = null; |
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
137 |
} |
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
138 |
} |
8c4b63e942fb
8130394: DatagramChannel tests need to be hardended to ignore stray datagrams
bpb
parents:
7668
diff
changeset
|
139 |
showBuffer("Received:", bb); |
2 | 140 |
sa = null; |
141 |
for (int i=0; i<100; i++) { |
|
142 |
bb.clear(); |
|
143 |
sa = dc.receive(bb); |
|
144 |
if (sa != null) |
|
145 |
throw new RuntimeException("Test failed"); |
|
146 |
} |
|
147 |
dc.close(); |
|
148 |
} catch (Exception ex) { |
|
149 |
e = ex; |
|
150 |
} |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
} |