author | ohair |
Tue, 28 Dec 2010 15:53:50 -0800 | |
changeset 7668 | d4a77089c587 |
parent 5970 | d4e98bbfb0be |
child 31176 | db59cb6634af |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2002, 2010, 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 |
||
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 EmptyBuffer { |
|
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(); |
2 | 46 |
Thread serverThread = new Thread(server); |
47 |
serverThread.start(); |
|
48 |
DatagramChannel dc = DatagramChannel.open(); |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
49 |
try { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
50 |
ByteBuffer bb = ByteBuffer.allocateDirect(12); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
51 |
bb.order(ByteOrder.BIG_ENDIAN); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
52 |
bb.putInt(1).putLong(1); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
53 |
bb.flip(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
54 |
InetAddress address = InetAddress.getLocalHost(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
55 |
InetSocketAddress isa = new InetSocketAddress(address, server.port()); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
56 |
dc.connect(isa); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
57 |
dc.write(bb); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
58 |
bb.rewind(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
59 |
dc.write(bb); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
60 |
bb.rewind(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
61 |
dc.write(bb); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
62 |
Thread.sleep(2000); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
63 |
serverThread.interrupt(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
64 |
server.throwException(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
65 |
} finally { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
66 |
dc.close(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
67 |
} |
2 | 68 |
} |
69 |
||
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
70 |
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
|
71 |
final DatagramChannel dc; |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
72 |
Exception e = null; |
2 | 73 |
|
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
74 |
Server() throws IOException { |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
75 |
this.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
|
76 |
} |
2 | 77 |
|
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
78 |
int port() { |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
79 |
return dc.socket().getLocalPort(); |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
80 |
} |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
81 |
|
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
82 |
void throwException() throws Exception { |
2 | 83 |
if (e != null) |
84 |
throw e; |
|
85 |
} |
|
86 |
||
87 |
void showBuffer(String s, ByteBuffer bb) { |
|
88 |
log.println(s); |
|
89 |
bb.rewind(); |
|
90 |
for (int i=0; i<bb.limit(); i++) { |
|
91 |
byte element = bb.get(); |
|
92 |
log.print(element); |
|
93 |
} |
|
94 |
log.println(); |
|
95 |
} |
|
96 |
||
97 |
public void run() { |
|
98 |
SocketAddress sa = null; |
|
99 |
int numberReceived = 0; |
|
100 |
try { |
|
101 |
ByteBuffer bb = ByteBuffer.allocateDirect(12); |
|
102 |
bb.clear(); |
|
103 |
// Only one clear. The buffer will be full after |
|
104 |
// the first receive, but it should still block |
|
105 |
// and receive and discard the next two |
|
106 |
while (!Thread.interrupted()) { |
|
107 |
try { |
|
108 |
sa = dc.receive(bb); |
|
109 |
} catch (ClosedByInterruptException cbie) { |
|
110 |
// Expected |
|
111 |
log.println("Took expected exit"); |
|
112 |
break; |
|
113 |
} |
|
114 |
if (sa != null) { |
|
115 |
log.println("Client: " + sa); |
|
116 |
showBuffer("RECV", bb); |
|
117 |
sa = null; |
|
118 |
numberReceived++; |
|
119 |
if (numberReceived > 3) |
|
120 |
throw new RuntimeException("Test failed"); |
|
121 |
} |
|
122 |
} |
|
123 |
} catch (Exception ex) { |
|
124 |
e = ex; |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
125 |
} finally { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5788
diff
changeset
|
126 |
try { dc.close(); } catch (IOException ignore) { } |
2 | 127 |
} |
128 |
} |
|
129 |
} |
|
130 |
||
131 |
} |