Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/InheritTimeout.java
38812 views
/*1* Copyright (c) 2001, 2002, 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* @summary Setting ServerSocket.setSoTimeout shouldn't cause27* the timeout to be inherited by accepted connections28*/2930import java.net.*;31import java.io.InputStream;3233public class InheritTimeout {3435class Reaper extends Thread {36Socket s;37int timeout;3839Reaper(Socket s, int timeout) {40this.s = s;41this.timeout = timeout;42}4344public void run() {45try {46Thread.currentThread().sleep(timeout);47s.close();48} catch (Exception e) {49}50}51}5253InheritTimeout() throws Exception {54ServerSocket ss = new ServerSocket(0);55ss.setSoTimeout(1000);5657InetAddress ia = InetAddress.getLocalHost();58InetSocketAddress isa =59new InetSocketAddress(ia, ss.getLocalPort());6061// client establishes the connection62Socket s1 = new Socket();63s1.connect(isa);6465// receive the connection66Socket s2 = ss.accept();6768// schedule reaper to close the socket in 5 seconds69Reaper r = new Reaper(s2, 5000);70r.start();7172boolean readTimedOut = false;73try {74s2.getInputStream().read();75} catch (SocketTimeoutException te) {76readTimedOut = true;77} catch (SocketException e) {78if (!s2.isClosed()) {79throw e;80}81}8283s1.close();84ss.close();8586if (readTimedOut) {87throw new Exception("Unexpected SocketTimeoutException throw!");88}89}9091public static void main(String args[]) throws Exception {92new InheritTimeout();93}94}959697