Path: blob/master/test/jdk/java/nio/channels/SocketChannel/CloseAfterConnect.java
41154 views
/*1* Copyright (c) 2006, 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* @bug 638009125*/26import java.nio.channels.SocketChannel;27import java.nio.channels.ServerSocketChannel;28import java.net.InetAddress;29import java.net.InetSocketAddress;30import java.io.IOException;3132public class CloseAfterConnect {33public static void main(String[] args) throws Exception {34ServerSocketChannel ssc = ServerSocketChannel.open();35ssc.socket().bind(new InetSocketAddress(0));3637InetAddress lh = InetAddress.getLocalHost();38final SocketChannel sc = SocketChannel.open();39final InetSocketAddress isa =40new InetSocketAddress(lh, ssc.socket().getLocalPort());4142// establish connection in another thread43Runnable connector =44new Runnable() {45public void run() {46try {47sc.connect(isa);48} catch (IOException ioe) {49ioe.printStackTrace();50}51}52};53Thread thr = new Thread(connector);54thr.start();5556// wait for connect to be established and for thread to57// terminate58do {59try {60thr.join();61} catch (InterruptedException x) { }62} while (thr.isAlive());6364// check connection is established65if (!sc.isConnected()) {66throw new RuntimeException("SocketChannel not connected");67}6869// close channel - this triggered the bug as it attempted to signal70// a thread that no longer exists71sc.close();7273// clean-up74ssc.accept().close();75ssc.close();76}77}787980