author | prr |
Fri, 25 May 2018 12:12:24 -0700 | |
changeset 50347 | b2f046ae8eb6 |
parent 49255 | acdb8531cc8b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49255
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2002, 2018, 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 |
|
36963
1558f7600497
8037360: java/nio/channels/SocketChannel/Connect.java fails intermittently
alanb
parents:
14415
diff
changeset
|
25 |
* @bug 4650679 8037360 |
2 | 26 |
* @summary Unit test for socket channels |
49255
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
27 |
* @library .. /test/lib |
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
28 |
* @build jdk.test.lib.Utils TestServers |
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
29 |
* @run main Connect |
2 | 30 |
*/ |
31 |
||
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
32 |
import java.net.*; |
2 | 33 |
import java.nio.*; |
34 |
import java.nio.channels.*; |
|
35 |
import java.util.*; |
|
36 |
||
37 |
public class Connect { |
|
38 |
||
39 |
private static final long INCREMENTAL_DELAY = 30L * 1000L; |
|
40 |
||
41 |
public static void main(String args[]) throws Exception { |
|
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
42 |
try (TestServers.EchoServer echoServer |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
43 |
= TestServers.EchoServer.startNewServer(1000)) { |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
44 |
test1(echoServer); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
45 |
} |
2 | 46 |
try { |
36963
1558f7600497
8037360: java/nio/channels/SocketChannel/Connect.java fails intermittently
alanb
parents:
14415
diff
changeset
|
47 |
test1(TestServers.RefusingServer.newRefusingServer()); |
2 | 48 |
throw new Exception("Refused connection throws no exception"); |
49 |
} catch (ConnectException ce) { |
|
50 |
// Correct result |
|
51 |
} |
|
52 |
} |
|
53 |
||
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
54 |
static void test1(TestServers.AbstractServer server) throws Exception { |
2 | 55 |
Selector selector; |
56 |
SocketChannel sc; |
|
57 |
SelectionKey sk; |
|
58 |
InetSocketAddress isa = new InetSocketAddress( |
|
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
5506
diff
changeset
|
59 |
server.getAddress(), server.getPort()); |
2 | 60 |
sc = SocketChannel.open(); |
61 |
sc.configureBlocking(false); |
|
62 |
||
63 |
selector = Selector.open(); |
|
64 |
sk = sc.register(selector, SelectionKey.OP_CONNECT); |
|
65 |
if (sc.connect(isa)) { |
|
66 |
System.err.println("Connected immediately!"); |
|
67 |
sc.close(); |
|
68 |
selector.close(); |
|
69 |
return; |
|
70 |
} else { |
|
71 |
ByteBuffer buf = ByteBuffer.allocateDirect(100); |
|
72 |
buf.asCharBuffer().put(new String( |
|
73 |
"The quick brown fox jumped over the lazy dog." |
|
74 |
).toCharArray()); |
|
75 |
buf.flip(); |
|
76 |
long startTime = System.currentTimeMillis(); |
|
77 |
while(true) { |
|
78 |
selector.select(INCREMENTAL_DELAY); |
|
79 |
Set selectedKeys = selector.selectedKeys(); |
|
80 |
||
81 |
if(selectedKeys.isEmpty()) { |
|
82 |
System.err.println("Elapsed time without response: " + |
|
83 |
(System.currentTimeMillis() - |
|
84 |
startTime) / 1000L + " seconds."); |
|
85 |
} |
|
86 |
else if(!selectedKeys.contains(sk)) |
|
87 |
{ |
|
88 |
System.err.println("Got wrong event about selection key."); |
|
89 |
} else { |
|
90 |
System.err.println("Got event for our selection key."); |
|
91 |
if(sk.isConnectable()) { |
|
92 |
if(sc.finishConnect()) { |
|
93 |
if(sc.isConnected()) { |
|
94 |
System.err.println("Successful connect."); |
|
95 |
sk.interestOps(SelectionKey.OP_WRITE); |
|
96 |
sc.write(buf); |
|
97 |
} else { |
|
98 |
System.err.println( |
|
99 |
"Finish connect completed incorrectly."); |
|
100 |
} |
|
101 |
} else { |
|
102 |
System.err.println( |
|
103 |
"key incorrectly indicated socket channel connectable."); |
|
104 |
} |
|
105 |
} |
|
106 |
if(sk.isWritable() && (buf.remaining() > 0)) { |
|
107 |
sc.write(buf); |
|
108 |
} |
|
109 |
if(buf.remaining() == 0) { |
|
110 |
System.err.println( |
|
111 |
"SUCCESS! buffer contents were sent."); |
|
112 |
sc.close(); |
|
113 |
selector.close(); |
|
114 |
return; |
|
115 |
} |
|
116 |
} |
|
117 |
selectedKeys.clear(); |
|
118 |
} |
|
119 |
} |
|
120 |
} |
|
121 |
} |