Path: blob/master/test/jdk/java/net/ServerSocket/SelectFdsLimit.java
41149 views
/*1* Copyright (c) 2013, 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/*24* @test25* @bug 802182026* @summary The total number of file descriptors is limited to27* 1024(FDSET_SIZE) on MacOSX (the size of fd array passed to select()28* call in java.net classes is limited to this value).29* @run main/othervm SelectFdsLimit30* @author [email protected]31*/3233import java.io.File;34import java.io.FileInputStream;35import java.io.FileNotFoundException;36import java.io.IOException;37import java.io.InputStream;38import java.net.ServerSocket;39import java.net.SocketTimeoutException;404142/*43* Test must be run in othervm mode to ensure that all files44* opened by openFiles() are closed propertly.45*/46public class SelectFdsLimit {47static final int FDTOOPEN = 1023;48static final String TESTFILE = "testfile";49static FileInputStream [] testFIS;5051static void prepareTestEnv() throws IOException {52File fileToCreate = new File(TESTFILE);53if (!fileToCreate.exists())54if (!fileToCreate.createNewFile())55throw new RuntimeException("Can't create test file");56}5758//If there will be some problem (i.e. ulimits on number of opened files will fail)59//then this method will fail with exception and test will be considered as60//failed. But allocated fds will be released because the test is executed by61//dedicated VM (@run main/othervm).62static void openFiles(int fn, File f) throws FileNotFoundException, IOException {63testFIS = new FileInputStream[FDTOOPEN];64for (;;) {65if (0 == fn)66break;67FileInputStream fis = new FileInputStream(f);68testFIS[--fn] = fis;69}70}7172public static void main(String [] args) throws IOException, FileNotFoundException {7374//The bug 8021820 is a Mac specific and because of that test will pass on all75//other platforms76if (!System.getProperty("os.name").contains("OS X")) {77return;78}7980//Create test directory with test files81prepareTestEnv();8283//Consume FD ids for this java process to overflow the 102484openFiles(FDTOOPEN,new File(TESTFILE));8586//Wait for incoming connection and make the select() used in java.net87//classes fail the limitation on FDSET_SIZE88ServerSocket socket = new ServerSocket(0);8990//Set the minimal timeout, no one is91//going to connect to this server socket92socket.setSoTimeout(1);9394// The accept() call will throw SocketException if the95// select() has failed due to limitation on fds size,96// indicating test failure. A SocketTimeoutException97// is expected, so it is caught and ignored, and the test98// passes.99try {100socket.accept();101} catch (SocketTimeoutException e) { }102}103}104105106