test/jdk/java/nio/channels/Selector/TemporarySelector.java
changeset 47216 71c04702a3d5
parent 42338 a60f280f803c
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25  * @bug 6645197
       
    26  * @run main/othervm -Xmx16m TemporarySelector
       
    27  * @summary Timed read with socket adaptor throws ClosedSelectorException if temporary selector GC'ed.
       
    28  */
       
    29 import java.io.IOException;
       
    30 import java.net.InetSocketAddress;
       
    31 import java.net.ServerSocket;
       
    32 import java.net.Socket;
       
    33 import java.nio.channels.ServerSocketChannel;
       
    34 import java.nio.channels.SocketChannel;
       
    35 
       
    36 public class TemporarySelector {
       
    37 
       
    38     static volatile boolean done = false;
       
    39 
       
    40     public static void main(String[] args) throws Exception {
       
    41 
       
    42         Runnable r = new Runnable() {
       
    43             public void run() {
       
    44                 while (!done) {
       
    45                     System.gc();
       
    46                     try {
       
    47                         Thread.sleep(1000);
       
    48                     } catch (Exception e) {
       
    49                     }
       
    50                 }
       
    51             }
       
    52         };
       
    53 
       
    54         try {
       
    55             // Create a server socket that will open and accept on loopback connection
       
    56             ServerSocketChannel ssc =  ServerSocketChannel.open();
       
    57             final ServerSocket ss =  ssc.socket();
       
    58             ss.bind(new InetSocketAddress(0));
       
    59             int localPort = ss.getLocalPort();
       
    60 
       
    61             // Create a client socket that will connect and read
       
    62             System.out.println("Connecting to server socket");
       
    63             System.out.flush();
       
    64             SocketChannel channel = SocketChannel.open(new InetSocketAddress("localhost", localPort));
       
    65             System.out.println("Connected to server socket");
       
    66             System.out.flush();
       
    67 
       
    68             // Create a thread to try and cause the GC to run
       
    69             Thread t = new Thread(r);
       
    70             t.start();
       
    71             byte[] buffer = new byte[500];
       
    72             System.out.println("Reading from socket input stream");
       
    73             System.out.flush();
       
    74             Socket socket = channel.socket();
       
    75             socket.setSoTimeout(10000);  // The timeout must be set
       
    76             // to trigger this bug
       
    77             try {
       
    78                 socket.getInputStream().read(buffer);
       
    79             } catch (java.net.SocketTimeoutException ste) {
       
    80                 // no java.nio.channels.ClosedSelectorException
       
    81             }
       
    82         } finally {
       
    83             done = true;
       
    84         }
       
    85     }
       
    86 }