Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/SocketChannel/FinishConnect.java
38828 views
/*1* Copyright (c) 2001, 2012, 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 ..26*/2728import java.net.*;29import java.nio.*;30import java.nio.channels.*;31import java.nio.channels.spi.SelectorProvider;32import java.nio.charset.*;33import java.util.*;343536public class FinishConnect {3738public static void main(String[] args) throws Exception {39try (TestServers.DayTimeServer dayTimeServer40= TestServers.DayTimeServer.startNewServer(100)) {41test1(dayTimeServer, true, true);42test1(dayTimeServer, true, false);43test1(dayTimeServer, false, true);44test1(dayTimeServer, false, false);45test2(dayTimeServer);46}47}4849static void test1(TestServers.DayTimeServer daytimeServer,50boolean select,51boolean setBlocking)52throws Exception53{54InetSocketAddress isa55= new InetSocketAddress(daytimeServer.getAddress(),56daytimeServer.getPort());57SocketChannel sc = SocketChannel.open();58sc.configureBlocking(false);59boolean connected = sc.connect(isa);60int attempts = 0;6162try {63sc.connect(isa);64throw new RuntimeException("Allowed another connect call");65} catch (IllegalStateException ise) {66// Correct behavior67}6869if (setBlocking)70sc.configureBlocking(true);7172if (!connected && select && !setBlocking) {73Selector selector = SelectorProvider.provider().openSelector();74sc.register(selector, SelectionKey.OP_CONNECT);75while (!connected) {76int keysAdded = selector.select(100);77if (keysAdded > 0) {78Set readyKeys = selector.selectedKeys();79Iterator i = readyKeys.iterator();80while (i.hasNext()) {81SelectionKey sk = (SelectionKey)i.next();82SocketChannel nextReady =83(SocketChannel)sk.channel();84connected = sc.finishConnect();85}86}87}88selector.close();89}9091while (!connected) {92if (attempts++ > 30)93throw new RuntimeException("Failed to connect");94Thread.sleep(100);95connected = sc.finishConnect();96}9798ByteBuffer bb = ByteBuffer.allocateDirect(100);99int bytesRead = 0;100int totalRead = 0;101while (totalRead < 20) {102bytesRead = sc.read(bb);103if (bytesRead > 0)104totalRead += bytesRead;105if (bytesRead < 0)106throw new RuntimeException("Message shorter than expected");107}108bb.position(bb.position() - 2); // Drop CRLF109bb.flip();110CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);111System.err.println(isa + " says: \"" + cb + "\"");112sc.close();113}114115static void test2(TestServers.DayTimeServer daytimeServer) throws Exception {116InetSocketAddress isa117= new InetSocketAddress(daytimeServer.getAddress(),118daytimeServer.getPort());119boolean done = false;120int globalAttempts = 0;121int connectSuccess = 0;122while (!done) {123// When using a local daytime server it is not always possible124// to get a pending connection, as sc.connect(isa) may always125// return true.126// So we're going to throw the exception only if there was127// at least 1 case where we did not manage to connect.128if (globalAttempts++ > 50) {129if (globalAttempts == connectSuccess + 1) {130System.out.println("Can't fully test on "131+ System.getProperty("os.name"));132break;133}134throw new RuntimeException("Failed to connect");135}136SocketChannel sc = SocketChannel.open();137sc.configureBlocking(false);138boolean connected = sc.connect(isa);139int localAttempts = 0;140while (!connected) {141if (localAttempts++ > 500)142throw new RuntimeException("Failed to connect");143connected = sc.finishConnect();144if (connected) {145done = true;146break;147}148Thread.sleep(10);149}150if (connected) {151connectSuccess++;152}153sc.close();154}155}156157}158159160