test/jdk/java/net/ServerSocket/SelectFdsLimit.java
changeset 47216 71c04702a3d5
parent 19433 2dade0de0206
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2013, 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 /*
       
    25  * @test
       
    26  * @bug 8021820
       
    27  * @summary The total number of file descriptors is limited to
       
    28  * 1024(FDSET_SIZE) on MacOSX (the size of fd array passed to select()
       
    29  * call in java.net classes is limited to this value).
       
    30  * @run main/othervm SelectFdsLimit
       
    31  * @author aleksej.efimov@oracle.com
       
    32  */
       
    33 
       
    34 import java.io.File;
       
    35 import java.io.FileInputStream;
       
    36 import java.io.FileNotFoundException;
       
    37 import java.io.IOException;
       
    38 import java.io.InputStream;
       
    39 import java.net.ServerSocket;
       
    40 import java.net.SocketTimeoutException;
       
    41 
       
    42 
       
    43 /*
       
    44  * Test must be run in othervm mode to ensure that all files
       
    45  * opened by openFiles() are closed propertly.
       
    46 */
       
    47 public class SelectFdsLimit {
       
    48     static final int FDTOOPEN = 1023;
       
    49     static final String TESTFILE = "testfile";
       
    50     static FileInputStream [] testFIS;
       
    51 
       
    52     static void prepareTestEnv() throws IOException {
       
    53             File fileToCreate = new File(TESTFILE);
       
    54             if (!fileToCreate.exists())
       
    55                 if (!fileToCreate.createNewFile())
       
    56                     throw new RuntimeException("Can't create test file");
       
    57     }
       
    58 
       
    59     //If there will be some problem (i.e. ulimits on number of opened files will fail)
       
    60     //then this method will fail with exception and test will be considered as
       
    61     //failed. But allocated fds will be released because the test is executed by
       
    62     //dedicated VM (@run main/othervm).
       
    63     static void openFiles(int fn, File f) throws FileNotFoundException, IOException {
       
    64         testFIS = new FileInputStream[FDTOOPEN];
       
    65         for (;;) {
       
    66             if (0 == fn)
       
    67                 break;
       
    68             FileInputStream fis = new FileInputStream(f);
       
    69             testFIS[--fn] = fis;
       
    70         }
       
    71     }
       
    72 
       
    73     public static void main(String [] args) throws IOException, FileNotFoundException {
       
    74 
       
    75         //The bug 8021820 is a Mac specific and because of that test will pass on all
       
    76         //other platforms
       
    77         if (!System.getProperty("os.name").contains("OS X")) {
       
    78            return;
       
    79         }
       
    80 
       
    81         //Create test directory with test files
       
    82         prepareTestEnv();
       
    83 
       
    84         //Consume FD ids for this java process to overflow the 1024
       
    85         openFiles(FDTOOPEN,new File(TESTFILE));
       
    86 
       
    87         //Wait for incoming connection and make the select() used in java.net
       
    88         //classes fail the limitation on FDSET_SIZE
       
    89         ServerSocket socket = new ServerSocket(0);
       
    90 
       
    91         //Set the minimal timeout, no one is
       
    92         //going to connect to this server socket
       
    93         socket.setSoTimeout(1);
       
    94 
       
    95         // The accept() call will throw SocketException if the
       
    96         // select() has failed due to limitation on fds size,
       
    97         // indicating test failure. A SocketTimeoutException
       
    98         // is expected, so it is caught and ignored, and the test
       
    99         // passes.
       
   100         try {
       
   101            socket.accept();
       
   102         } catch (SocketTimeoutException e) { }
       
   103     }
       
   104 }