0N/A/*
5501N/A * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @summary Test SocketChannel.finishConnect
0N/A * @library ..
0N/A */
0N/A
0N/Aimport java.net.*;
0N/Aimport java.nio.*;
0N/Aimport java.nio.channels.*;
0N/Aimport java.nio.channels.spi.SelectorProvider;
0N/Aimport java.nio.charset.*;
0N/Aimport java.util.*;
0N/A
0N/A
0N/Apublic class FinishConnect {
0N/A
0N/A public static void main(String[] args) throws Exception {
5501N/A try (TestServers.DayTimeServer dayTimeServer
5501N/A = TestServers.DayTimeServer.startNewServer(100)) {
5501N/A test1(dayTimeServer, true, true);
5501N/A test1(dayTimeServer, true, false);
5501N/A test1(dayTimeServer, false, true);
5501N/A test1(dayTimeServer, false, false);
5501N/A test2(dayTimeServer);
5501N/A }
0N/A }
0N/A
5501N/A static void test1(TestServers.DayTimeServer daytimeServer,
5501N/A boolean select,
5501N/A boolean setBlocking)
5501N/A throws Exception
5501N/A {
0N/A InetSocketAddress isa
5501N/A = new InetSocketAddress(daytimeServer.getAddress(),
5501N/A daytimeServer.getPort());
0N/A SocketChannel sc = SocketChannel.open();
0N/A sc.configureBlocking(false);
0N/A boolean connected = sc.connect(isa);
0N/A int attempts = 0;
0N/A
0N/A try {
0N/A sc.connect(isa);
0N/A throw new RuntimeException("Allowed another connect call");
0N/A } catch (IllegalStateException ise) {
0N/A // Correct behavior
0N/A }
0N/A
0N/A if (setBlocking)
0N/A sc.configureBlocking(true);
0N/A
0N/A if (!connected && select && !setBlocking) {
0N/A Selector selector = SelectorProvider.provider().openSelector();
0N/A sc.register(selector, SelectionKey.OP_CONNECT);
0N/A while (!connected) {
0N/A int keysAdded = selector.select(100);
0N/A if (keysAdded > 0) {
0N/A Set readyKeys = selector.selectedKeys();
0N/A Iterator i = readyKeys.iterator();
0N/A while (i.hasNext()) {
0N/A SelectionKey sk = (SelectionKey)i.next();
0N/A SocketChannel nextReady =
0N/A (SocketChannel)sk.channel();
0N/A connected = sc.finishConnect();
0N/A }
0N/A }
0N/A }
0N/A selector.close();
0N/A }
0N/A
0N/A while (!connected) {
0N/A if (attempts++ > 30)
0N/A throw new RuntimeException("Failed to connect");
0N/A Thread.sleep(100);
0N/A connected = sc.finishConnect();
0N/A }
0N/A
0N/A ByteBuffer bb = ByteBuffer.allocateDirect(100);
0N/A int bytesRead = 0;
0N/A int totalRead = 0;
0N/A while (totalRead < 20) {
0N/A bytesRead = sc.read(bb);
0N/A if (bytesRead > 0)
0N/A totalRead += bytesRead;
0N/A if (bytesRead < 0)
0N/A throw new RuntimeException("Message shorter than expected");
0N/A }
0N/A bb.position(bb.position() - 2); // Drop CRLF
0N/A bb.flip();
0N/A CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
0N/A System.err.println(isa + " says: \"" + cb + "\"");
0N/A sc.close();
0N/A }
0N/A
5501N/A static void test2(TestServers.DayTimeServer daytimeServer) throws Exception {
0N/A InetSocketAddress isa
5501N/A = new InetSocketAddress(daytimeServer.getAddress(),
5501N/A daytimeServer.getPort());
0N/A boolean done = false;
0N/A int globalAttempts = 0;
5501N/A int connectSuccess = 0;
0N/A while (!done) {
5501N/A // When using a local daytime server it is not always possible
5501N/A // to get a pending connection, as sc.connect(isa) may always
5501N/A // return true.
5501N/A // So we're going to throw the exception only if there was
5501N/A // at least 1 case where we did not manage to connect.
5501N/A if (globalAttempts++ > 50) {
5501N/A if (globalAttempts == connectSuccess + 1) {
5501N/A System.out.println("Can't fully test on "
5501N/A + System.getProperty("os.name"));
5501N/A break;
5501N/A }
0N/A throw new RuntimeException("Failed to connect");
5501N/A }
0N/A SocketChannel sc = SocketChannel.open();
0N/A sc.configureBlocking(false);
0N/A boolean connected = sc.connect(isa);
0N/A int localAttempts = 0;
0N/A while (!connected) {
0N/A if (localAttempts++ > 500)
0N/A throw new RuntimeException("Failed to connect");
0N/A connected = sc.finishConnect();
0N/A if (connected) {
0N/A done = true;
0N/A break;
0N/A }
0N/A Thread.sleep(10);
0N/A }
5501N/A if (connected) {
5501N/A connectSuccess++;
5501N/A }
0N/A sc.close();
0N/A }
0N/A }
0N/A
0N/A}