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