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