Path: blob/master/test/jdk/java/net/Socket/InheritTimeout.java
41149 views
/*1* Copyright (c) 2001, 2019, 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 450814926* @library /test/lib27* @summary Setting ServerSocket.setSoTimeout shouldn't cause28* the timeout to be inherited by accepted connections29* @run main InheritTimeout30* @run main/othervm -Djava.net.preferIPv4Stack=true InheritTimeout31*/3233import java.net.*;34import java.io.InputStream;35import jdk.test.lib.net.IPSupport;3637public class InheritTimeout {3839class Reaper extends Thread {40Socket s;41int timeout;4243Reaper(Socket s, int timeout) {44this.s = s;45this.timeout = timeout;46}4748public void run() {49try {50Thread.currentThread().sleep(timeout);51s.close();52} catch (Exception e) {53}54}55}5657InheritTimeout() throws Exception {58InetAddress ia = InetAddress.getLocalHost();59ServerSocket ss = new ServerSocket();60ss.bind(new InetSocketAddress(ia, 0));61ss.setSoTimeout(1000);6263InetSocketAddress isa =64new InetSocketAddress(ia, ss.getLocalPort());6566// client establishes the connection67Socket s1 = new Socket();68s1.connect(isa);6970// receive the connection71Socket s2 = ss.accept();7273// schedule reaper to close the socket in 5 seconds74Reaper r = new Reaper(s2, 5000);75r.start();7677boolean readTimedOut = false;78try {79s2.getInputStream().read();80} catch (SocketTimeoutException te) {81readTimedOut = true;82} catch (SocketException e) {83if (!s2.isClosed()) {84throw e;85}86}8788s1.close();89ss.close();9091if (readTimedOut) {92throw new Exception("Unexpected SocketTimeoutException throw!");93}94}9596public static void main(String args[]) throws Exception {97IPSupport.throwSkippedExceptionIfNonOperational();98new InheritTimeout();99}100}101102103