1 /*
2 * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
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 *
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.
22 */
25 *
26 *
27 * Tests that the channel returned by System.inheritedChannel()
28 * is in blocking mode, bound, and in the case of a SocketChannel
29 * connected to a peer.
30 *
31 * The test works by launching a test service (called StateTestService) so
32 * that it inherits each type of channel. The test service checks the
33 * socket state and replies back to this class via an out-of-band
34 * channel.
35 */
36 import java.io.IOException;
37 import java.net.InetSocketAddress;
38 import java.nio.ByteBuffer;
39 import java.nio.channels.DatagramChannel;
40 import java.nio.channels.SelectionKey;
41 import java.nio.channels.Selector;
42 import java.nio.channels.ServerSocketChannel;
43 import java.nio.channels.SocketChannel;
44
45 public class StateTest {
46
47 private static int failures = 0;
48
49 private static String TEST_SERVICE = "StateTestService";
50
51 /*
52 * Reads the test result from the "out-of-band" connection to the test service.
53 *
54 * The out-of-band connection is just a TCP connection from the service to
55 * this class. waitForTestResult just waits (with timeout) for the service
56 * to connect. Once connected it waits (with timeout) for the test result.
57 * The test result is examined.
58 */
59 private static void waitForTestResult(ServerSocketChannel ssc, boolean expectFail) throws IOException {
60 Selector sel = ssc.provider().openSelector();
61 SelectionKey sk;
62 SocketChannel sc;
63
64 /*
65 * Wait for service to connect
66 */
67 ssc.configureBlocking(false);
68 sk = ssc.register(sel, SelectionKey.OP_ACCEPT);
69 long to = 15*1000;
70 sc = null;
71 for (;;) {
72 long st = System.currentTimeMillis();
73 sel.select(to);
74 if (sk.isAcceptable() && ((sc = ssc.accept()) != null)) {
75 // connection established
76 break;
77 }
78 sel.selectedKeys().remove(sk);
79 to -= System.currentTimeMillis() - st;
80 if (to <= 0) {
81 throw new IOException("Timed out waiting for service to report test result");
82 }
83 }
84 sk.cancel();
85 ssc.configureBlocking(false);
86
87 /*
88 * Wait for service to report test result
89 */
90 sc.configureBlocking(false);
91 sk = sc.register(sel, SelectionKey.OP_READ);
92 to = 5000;
93 ByteBuffer bb = ByteBuffer.allocateDirect(20);
94 for (;;) {
95 long st = System.currentTimeMillis();
96 sel.select(to);
97 if (sk.isReadable()) {
98 int n = sc.read(bb);
99 if (n > 0) {
100 break;
101 }
102 if (n < 0) {
103 throw new IOException("Premature EOF - no test result from service");
104 }
105 }
106 sel.selectedKeys().remove(sk);
107 to -= System.currentTimeMillis() - st;
108 if (to <= 0) {
109 throw new IOException("Timed out waiting for service to report test result");
110 }
111 }
112 sk.cancel();
|
1 /*
2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
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 *
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.
22 */
25 *
26 *
27 * Tests that the channel returned by System.inheritedChannel()
28 * is in blocking mode, bound, and in the case of a SocketChannel
29 * connected to a peer.
30 *
31 * The test works by launching a test service (called StateTestService) so
32 * that it inherits each type of channel. The test service checks the
33 * socket state and replies back to this class via an out-of-band
34 * channel.
35 */
36 import java.io.IOException;
37 import java.net.InetSocketAddress;
38 import java.nio.ByteBuffer;
39 import java.nio.channels.DatagramChannel;
40 import java.nio.channels.SelectionKey;
41 import java.nio.channels.Selector;
42 import java.nio.channels.ServerSocketChannel;
43 import java.nio.channels.SocketChannel;
44
45 import jdk.test.lib.Utils;
46
47 public class StateTest {
48
49 private static int failures = 0;
50
51 private static String TEST_SERVICE = "StateTestService";
52
53 /*
54 * Reads the test result from the "out-of-band" connection to the test service.
55 *
56 * The out-of-band connection is just a TCP connection from the service to
57 * this class. waitForTestResult just waits (with timeout) for the service
58 * to connect. Once connected it waits (with timeout) for the test result.
59 * The test result is examined.
60 */
61 private static void waitForTestResult(ServerSocketChannel ssc, boolean expectFail) throws IOException {
62 Selector sel = ssc.provider().openSelector();
63 SelectionKey sk;
64 SocketChannel sc;
65
66 /*
67 * Wait for service to connect
68 */
69 ssc.configureBlocking(false);
70 sk = ssc.register(sel, SelectionKey.OP_ACCEPT);
71 long to = Utils.adjustTimeout(15*1000);
72 sc = null;
73 for (;;) {
74 long st = System.currentTimeMillis();
75 sel.select(to);
76 if (sk.isAcceptable() && ((sc = ssc.accept()) != null)) {
77 // connection established
78 break;
79 }
80 sel.selectedKeys().remove(sk);
81 to -= System.currentTimeMillis() - st;
82 if (to <= 0) {
83 throw new IOException("Timed out waiting for service to report test result");
84 }
85 }
86 sk.cancel();
87 ssc.configureBlocking(false);
88
89 /*
90 * Wait for service to report test result
91 */
92 sc.configureBlocking(false);
93 sk = sc.register(sel, SelectionKey.OP_READ);
94 to = Utils.adjustTimeout(5000);
95 ByteBuffer bb = ByteBuffer.allocateDirect(20);
96 for (;;) {
97 long st = System.currentTimeMillis();
98 sel.select(to);
99 if (sk.isReadable()) {
100 int n = sc.read(bb);
101 if (n > 0) {
102 break;
103 }
104 if (n < 0) {
105 throw new IOException("Premature EOF - no test result from service");
106 }
107 }
108 sel.selectedKeys().remove(sk);
109 to -= System.currentTimeMillis() - st;
110 if (to <= 0) {
111 throw new IOException("Timed out waiting for service to report test result");
112 }
113 }
114 sk.cancel();
|