blob: 5d6c3e030259e6bbe20220e729bdf2ec60b703d3 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001-2002 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4508149
27 * @summary Setting ServerSocket.setSoTimeout shouldn't cause
28 * the timeout to be inherited by accepted connections
29 */
30
31import java.net.*;
32import java.io.InputStream;
33
34public class InheritTimeout {
35
36 class Reaper extends Thread {
37 Socket s;
38 int timeout;
39
40 Reaper(Socket s, int timeout) {
41 this.s = s;
42 this.timeout = timeout;
43 }
44
45 public void run() {
46 try {
47 Thread.currentThread().sleep(timeout);
48 s.close();
49 } catch (Exception e) {
50 }
51 }
52 }
53
54 InheritTimeout() throws Exception {
55 ServerSocket ss = new ServerSocket(0);
56 ss.setSoTimeout(1000);
57
58 InetAddress ia = InetAddress.getLocalHost();
59 InetSocketAddress isa =
60 new InetSocketAddress(ia, ss.getLocalPort());
61
62 // client establishes the connection
63 Socket s1 = new Socket();
64 s1.connect(isa);
65
66 // receive the connection
67 Socket s2 = ss.accept();
68
69 // schedule reaper to close the socket in 5 seconds
70 Reaper r = new Reaper(s2, 5000);
71 r.start();
72
73 boolean readTimedOut = false;
74 try {
75 s2.getInputStream().read();
76 } catch (SocketTimeoutException te) {
77 readTimedOut = true;
78 } catch (SocketException e) {
79 if (!s2.isClosed()) {
80 throw e;
81 }
82 }
83
84 s1.close();
85 ss.close();
86
87 if (readTimedOut) {
88 throw new Exception("Unexpected SocketTimeoutException throw!");
89 }
90 }
91
92 public static void main(String args[]) throws Exception {
93 new InheritTimeout();
94 }
95}