Path: blob/master/test/jdk/java/nio/channels/SocketChannel/FinishConnect.java
41154 views
/*1* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @summary Test SocketChannel.finishConnect25* @library .. /test/lib26* @build jdk.test.lib.Utils TestServers27* @run main FinishConnect28*/2930import java.net.*;31import java.nio.*;32import java.nio.channels.*;33import java.nio.channels.spi.SelectorProvider;34import java.nio.charset.*;35import java.util.*;363738public class FinishConnect {3940public static void main(String[] args) throws Exception {41try (TestServers.DayTimeServer dayTimeServer42= TestServers.DayTimeServer.startNewServer(100)) {43test1(dayTimeServer, true, true);44test1(dayTimeServer, true, false);45test1(dayTimeServer, false, true);46test1(dayTimeServer, false, false);47test2(dayTimeServer);48}49}5051static void test1(TestServers.DayTimeServer daytimeServer,52boolean select,53boolean setBlocking)54throws Exception55{56InetSocketAddress isa57= new InetSocketAddress(daytimeServer.getAddress(),58daytimeServer.getPort());59SocketChannel sc = SocketChannel.open();60sc.configureBlocking(false);61boolean connected = sc.connect(isa);62int attempts = 0;6364try {65sc.connect(isa);66throw new RuntimeException("Allowed another connect call");67} catch (IllegalStateException ise) {68// Correct behavior69}7071if (setBlocking)72sc.configureBlocking(true);7374if (!connected && select && !setBlocking) {75Selector selector = SelectorProvider.provider().openSelector();76sc.register(selector, SelectionKey.OP_CONNECT);77while (!connected) {78int keysAdded = selector.select(100);79if (keysAdded > 0) {80Set readyKeys = selector.selectedKeys();81Iterator i = readyKeys.iterator();82while (i.hasNext()) {83SelectionKey sk = (SelectionKey)i.next();84SocketChannel nextReady =85(SocketChannel)sk.channel();86connected = sc.finishConnect();87}88}89}90selector.close();91}9293while (!connected) {94if (attempts++ > 30)95throw new RuntimeException("Failed to connect");96Thread.sleep(100);97connected = sc.finishConnect();98}99100ByteBuffer bb = ByteBuffer.allocateDirect(100);101int bytesRead = 0;102int totalRead = 0;103while (totalRead < 20) {104bytesRead = sc.read(bb);105if (bytesRead > 0)106totalRead += bytesRead;107if (bytesRead < 0)108throw new RuntimeException("Message shorter than expected");109}110bb.position(bb.position() - 2); // Drop CRLF111bb.flip();112CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);113System.err.println(isa + " says: \"" + cb + "\"");114sc.close();115}116117static void test2(TestServers.DayTimeServer daytimeServer) throws Exception {118InetSocketAddress isa119= new InetSocketAddress(daytimeServer.getAddress(),120daytimeServer.getPort());121boolean done = false;122int globalAttempts = 0;123int connectSuccess = 0;124while (!done) {125// When using a local daytime server it is not always possible126// to get a pending connection, as sc.connect(isa) may always127// return true.128// So we're going to throw the exception only if there was129// at least 1 case where we did not manage to connect.130if (globalAttempts++ > 50) {131if (globalAttempts == connectSuccess + 1) {132System.out.println("Can't fully test on "133+ System.getProperty("os.name"));134break;135}136throw new RuntimeException("Failed to connect");137}138SocketChannel sc = SocketChannel.open();139sc.configureBlocking(false);140boolean connected = sc.connect(isa);141int localAttempts = 0;142while (!connected) {143if (localAttempts++ > 500)144throw new RuntimeException("Failed to connect");145connected = sc.finishConnect();146if (connected) {147done = true;148break;149}150Thread.sleep(10);151}152if (connected) {153connectSuccess++;154}155sc.close();156}157}158159}160161162