J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2001 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 | * Tests that a thread blocked in Socket.getInputStream().read() |
| 26 | * throws a SocketException if the socket is asynchronously closed. |
| 27 | */ |
| 28 | import java.net.*; |
| 29 | import java.io.*; |
| 30 | |
| 31 | public class Socket_getInputStream_read extends AsyncCloseTest implements Runnable { |
| 32 | Socket s; |
| 33 | int timeout = 0; |
| 34 | |
| 35 | public Socket_getInputStream_read() { |
| 36 | } |
| 37 | |
| 38 | public Socket_getInputStream_read(int timeout) { |
| 39 | this.timeout = timeout; |
| 40 | } |
| 41 | |
| 42 | public String description() { |
| 43 | String s = "Socket.getInputStream().read()"; |
| 44 | if (timeout > 0) { |
| 45 | s += " (with timeout)"; |
| 46 | } |
| 47 | return s; |
| 48 | } |
| 49 | |
| 50 | public void run() { |
| 51 | InputStream in; |
| 52 | |
| 53 | try { |
| 54 | in = s.getInputStream(); |
| 55 | if (timeout > 0) { |
| 56 | s.setSoTimeout(timeout); |
| 57 | } |
| 58 | } catch (Exception e) { |
| 59 | failed(e.getMessage()); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | try { |
| 64 | int n = in.read(); |
| 65 | failed("getInptuStream().read() returned unexpectly!!"); |
| 66 | } catch (SocketException se) { |
| 67 | closed(); |
| 68 | } catch (Exception e) { |
| 69 | failed(e.getMessage()); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | public boolean go() throws Exception { |
| 74 | |
| 75 | ServerSocket ss = new ServerSocket(0); |
| 76 | |
| 77 | InetAddress lh = InetAddress.getLocalHost(); |
| 78 | s = new Socket(); |
| 79 | s.connect( new InetSocketAddress(lh, ss.getLocalPort()) ); |
| 80 | |
| 81 | Socket s2 = ss.accept(); |
| 82 | |
| 83 | Thread thr = new Thread(this); |
| 84 | thr.start(); |
| 85 | |
| 86 | Thread.currentThread().sleep(1000); |
| 87 | |
| 88 | s.close(); |
| 89 | |
| 90 | Thread.currentThread().sleep(1000); |
| 91 | |
| 92 | if (isClosed()) { |
| 93 | return true; |
| 94 | } else { |
| 95 | failed("getInputStream().read() wasn't preempted"); |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | } |