author | bpb |
Mon, 30 Apr 2018 13:40:39 -0700 | |
changeset 49930 | 3aaaa5370999 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
31176
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
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 |
|
25 |
* @bug 4669040 |
|
26 |
* @summary Test DatagramChannel receive with empty buffer |
|
27 |
* @author Mike McCloskey |
|
28 |
*/ |
|
29 |
||
31176
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
30 |
import java.io.IOException; |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
31 |
import java.io.PrintStream; |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
32 |
import java.net.InetAddress; |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
33 |
import java.net.InetSocketAddress; |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
34 |
import java.net.SocketAddress; |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
35 |
import java.nio.ByteBuffer; |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
36 |
import java.nio.ByteOrder; |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
37 |
import java.nio.channels.ClosedByInterruptException; |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
38 |
import java.nio.channels.DatagramChannel; |
2 | 39 |
|
40 |
public class EmptyBuffer { |
|
41 |
||
31176
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
42 |
private static final PrintStream log = System.err; |
2 | 43 |
|
44 |
public static void main(String[] args) throws Exception { |
|
45 |
test(); |
|
46 |
} |
|
47 |
||
31176
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
48 |
private static void test() throws Exception { |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
49 |
DatagramChannel dc = DatagramChannel.open(); |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
50 |
InetAddress localHost = InetAddress.getLocalHost(); |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
51 |
dc.bind(new InetSocketAddress(localHost, 0)); |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
52 |
|
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
53 |
Server server = new Server(dc.getLocalAddress()); |
2 | 54 |
Thread serverThread = new Thread(server); |
55 |
serverThread.start(); |
|
31176
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
56 |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
57 |
try { |
31176
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
58 |
InetSocketAddress isa = new InetSocketAddress(localHost, server.port()); |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
59 |
dc.connect(isa); |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
60 |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
61 |
ByteBuffer bb = ByteBuffer.allocateDirect(12); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
62 |
bb.order(ByteOrder.BIG_ENDIAN); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
63 |
bb.putInt(1).putLong(1); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
64 |
bb.flip(); |
31176
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
65 |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
66 |
dc.write(bb); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
67 |
bb.rewind(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
68 |
dc.write(bb); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
69 |
bb.rewind(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
70 |
dc.write(bb); |
31176
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
71 |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
72 |
Thread.sleep(2000); |
31176
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
73 |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
74 |
serverThread.interrupt(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
75 |
server.throwException(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
76 |
} finally { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
77 |
dc.close(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
78 |
} |
2 | 79 |
} |
80 |
||
31176
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
81 |
private static class Server implements Runnable { |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
82 |
private final DatagramChannel dc; |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
83 |
private final SocketAddress clientAddress; |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
84 |
private Exception e = null; |
2 | 85 |
|
31176
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
86 |
Server(SocketAddress clientAddress) throws IOException { |
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
87 |
this.dc = DatagramChannel.open().bind(new InetSocketAddress(0)); |
31176
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
88 |
this.clientAddress = clientAddress; |
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
89 |
} |
2 | 90 |
|
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
91 |
int port() { |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
92 |
return dc.socket().getLocalPort(); |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
93 |
} |
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 |
void throwException() throws Exception { |
2 | 96 |
if (e != null) |
97 |
throw e; |
|
98 |
} |
|
99 |
||
100 |
void showBuffer(String s, ByteBuffer bb) { |
|
101 |
log.println(s); |
|
102 |
bb.rewind(); |
|
103 |
for (int i=0; i<bb.limit(); i++) { |
|
104 |
byte element = bb.get(); |
|
105 |
log.print(element); |
|
106 |
} |
|
107 |
log.println(); |
|
108 |
} |
|
109 |
||
31176
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
110 |
@Override |
2 | 111 |
public void run() { |
112 |
try { |
|
113 |
ByteBuffer bb = ByteBuffer.allocateDirect(12); |
|
114 |
bb.clear(); |
|
115 |
// Only one clear. The buffer will be full after |
|
116 |
// the first receive, but it should still block |
|
117 |
// and receive and discard the next two |
|
31176
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
118 |
int numberReceived = 0; |
2 | 119 |
while (!Thread.interrupted()) { |
31176
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
120 |
SocketAddress sa; |
2 | 121 |
try { |
122 |
sa = dc.receive(bb); |
|
123 |
} catch (ClosedByInterruptException cbie) { |
|
124 |
// Expected |
|
125 |
log.println("Took expected exit"); |
|
31176
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
126 |
// Verify that enough packets were received |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
127 |
if (numberReceived != 3) |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
128 |
throw new RuntimeException("Failed: Too few datagrams"); |
2 | 129 |
break; |
130 |
} |
|
131 |
if (sa != null) { |
|
132 |
log.println("Client: " + sa); |
|
31176
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
133 |
// Check client address so as not to count stray packets |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
134 |
if (sa.equals(clientAddress)) { |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
135 |
showBuffer("RECV", bb); |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
136 |
numberReceived++; |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
137 |
} |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
138 |
if (numberReceived > 3) |
db59cb6634af
8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents:
7668
diff
changeset
|
139 |
throw new RuntimeException("Failed: Too many datagrams"); |
2 | 140 |
sa = null; |
141 |
} |
|
142 |
} |
|
143 |
} catch (Exception ex) { |
|
144 |
e = ex; |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
145 |
} finally { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
146 |
try { dc.close(); } catch (IOException ignore) { } |
2 | 147 |
} |
148 |
} |
|
149 |
} |
|
150 |
} |