J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | * @bug 6380091 |
| 26 | */ |
| 27 | import java.nio.channels.SocketChannel; |
| 28 | import java.nio.channels.ServerSocketChannel; |
| 29 | import java.net.InetAddress; |
| 30 | import java.net.InetSocketAddress; |
| 31 | import java.io.IOException; |
| 32 | |
| 33 | public class CloseAfterConnect { |
| 34 | public static void main(String[] args) throws Exception { |
| 35 | ServerSocketChannel ssc = ServerSocketChannel.open(); |
| 36 | ssc.socket().bind(new InetSocketAddress(0)); |
| 37 | |
| 38 | InetAddress lh = InetAddress.getLocalHost(); |
| 39 | final SocketChannel sc = SocketChannel.open(); |
| 40 | final InetSocketAddress isa = |
| 41 | new InetSocketAddress(lh, ssc.socket().getLocalPort()); |
| 42 | |
| 43 | // establish connection in another thread |
| 44 | Runnable connector = |
| 45 | new Runnable() { |
| 46 | public void run() { |
| 47 | try { |
| 48 | sc.connect(isa); |
| 49 | } catch (IOException ioe) { |
| 50 | ioe.printStackTrace(); |
| 51 | } |
| 52 | } |
| 53 | }; |
| 54 | Thread thr = new Thread(connector); |
| 55 | thr.start(); |
| 56 | |
| 57 | // wait for connect to be established and for thread to |
| 58 | // terminate |
| 59 | do { |
| 60 | try { |
| 61 | thr.join(); |
| 62 | } catch (InterruptedException x) { } |
| 63 | } while (thr.isAlive()); |
| 64 | |
| 65 | // check connection is established |
| 66 | if (!sc.isConnected()) { |
| 67 | throw new RuntimeException("SocketChannel not connected"); |
| 68 | } |
| 69 | |
| 70 | // close channel - this triggered the bug as it attempted to signal |
| 71 | // a thread that no longer exists |
| 72 | sc.close(); |
| 73 | |
| 74 | // clean-up |
| 75 | ssc.accept().close(); |
| 76 | ssc.close(); |
| 77 | } |
| 78 | } |