author | alanb |
Wed, 16 Jun 2010 14:24:46 +0100 | |
changeset 5798 | 9cc262cd2a7a |
parent 5506 | 202f599c92aa |
child 7668 | d4a77089c587 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, 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 4854354 |
|
26 |
* @summary Test vector write faster than can be read |
|
27 |
* @library .. |
|
28 |
*/ |
|
29 |
||
30 |
import java.io.*; |
|
31 |
import java.net.*; |
|
32 |
import java.nio.*; |
|
33 |
import java.nio.channels.*; |
|
34 |
import java.util.*; |
|
35 |
import sun.misc.*; |
|
36 |
||
37 |
||
38 |
public class Write { |
|
39 |
||
40 |
static Random generator = new Random(); |
|
41 |
||
42 |
static int testSize = 15; |
|
43 |
||
44 |
public static void main(String[] args) throws Exception { |
|
45 |
WriteServer sv = new WriteServer(); |
|
46 |
sv.start(); |
|
5798
9cc262cd2a7a
6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
47 |
bufferTest(sv.port()); |
2 | 48 |
if (sv.finish(8000) == 0) |
49 |
throw new Exception("Failed" ); |
|
50 |
} |
|
51 |
||
5798
9cc262cd2a7a
6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
52 |
static void bufferTest(int port) throws Exception { |
2 | 53 |
ByteBuffer[] bufs = new ByteBuffer[testSize]; |
54 |
for(int i=0; i<testSize; i++) { |
|
55 |
String source = |
|
56 |
"a muchmuchmuchmuchmuchmuchmuchmuch larger buffer numbered " + |
|
57 |
i; |
|
58 |
bufs[i] = ByteBuffer.allocateDirect(source.length()); |
|
59 |
} |
|
60 |
||
61 |
// Get a connection to the server |
|
62 |
InetAddress lh = InetAddress.getLocalHost(); |
|
63 |
InetSocketAddress isa = new InetSocketAddress(lh, port); |
|
64 |
SocketChannel sc = SocketChannel.open(); |
|
65 |
sc.connect(isa); |
|
66 |
sc.configureBlocking(false); |
|
67 |
||
68 |
// Try to overflow the socket buffer |
|
69 |
long total = 0; |
|
70 |
for (int i=0; i<100; i++) { |
|
71 |
long bytesWritten = sc.write(bufs); |
|
72 |
if (bytesWritten > 0) |
|
73 |
total += bytesWritten; |
|
74 |
for(int j=0; j<testSize; j++) |
|
75 |
bufs[j].rewind(); |
|
76 |
} |
|
77 |
||
78 |
// Clean up |
|
79 |
sc.close(); |
|
80 |
} |
|
81 |
||
82 |
} |
|
83 |
||
84 |
||
85 |
class WriteServer extends TestThread { |
|
86 |
||
87 |
static Random generator = new Random(); |
|
88 |
||
5798
9cc262cd2a7a
6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
89 |
|
9cc262cd2a7a
6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
90 |
final ServerSocketChannel ssc; |
2 | 91 |
|
5798
9cc262cd2a7a
6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
92 |
WriteServer() throws IOException { |
2 | 93 |
super("WriteServer"); |
5798
9cc262cd2a7a
6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
94 |
this.ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0)); |
9cc262cd2a7a
6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
95 |
} |
9cc262cd2a7a
6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
96 |
|
9cc262cd2a7a
6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
97 |
int port() { |
9cc262cd2a7a
6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
98 |
return ssc.socket().getLocalPort(); |
2 | 99 |
} |
100 |
||
101 |
void go() throws Exception { |
|
102 |
bufferTest(); |
|
103 |
} |
|
104 |
||
105 |
void bufferTest() throws Exception { |
|
106 |
ByteBuffer buf = ByteBuffer.allocateDirect(5); |
|
107 |
||
108 |
// Get a connection from client |
|
109 |
SocketChannel sc = null; |
|
110 |
||
111 |
try { |
|
112 |
ssc.configureBlocking(false); |
|
113 |
||
114 |
for (;;) { |
|
115 |
sc = ssc.accept(); |
|
116 |
if (sc != null) |
|
117 |
break; |
|
118 |
Thread.sleep(50); |
|
119 |
} |
|
120 |
||
121 |
// I'm a slow reader... |
|
122 |
Thread.sleep(3000); |
|
123 |
||
124 |
} finally { |
|
125 |
// Clean up |
|
126 |
ssc.close(); |
|
127 |
if (sc != null) |
|
128 |
sc.close(); |
|
129 |
} |
|
130 |
||
131 |
} |
|
132 |
||
133 |
} |