blob: d510bc95082ad2a4649c54ad6fbda007b78a58b9 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 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/* @test
25 * @bug 4820217
26 * @summary Ensure that pending reads on stdout and stderr streams
27 * return -1 when the process is destroyed
28 */
29
30import java.io.*;
31
32
33public class StreamsSurviveDestroy {
34
35 private static class Copier extends Thread {
36
37 String name;
38 InputStream in;
39 OutputStream out;
40 boolean wantInterrupt;
41 boolean acceptException;
42 Exception exc = null;
43
44 Copier(String name, InputStream in, OutputStream out,
45 boolean ae, boolean wi)
46 {
47 this.name = name;
48 this.in = in;
49 this.out = out;
50 this.acceptException = ae;
51 this.wantInterrupt = wi;
52 setName(name);
53 start();
54 }
55
56 private void log(String s) {
57 System.err.println(" " + name + ": " + s);
58 }
59
60 public void run() {
61 byte[] buf = new byte[4242];
62 for (;;) {
63 try {
64 int n = in.read(buf);
65 if (n < 0) {
66 System.err.println(" EOF");
67 break;
68 }
69 out.write(buf, 0, n);
70 } catch (IOException x) {
71 if (wantInterrupt) {
72 if (x instanceof InterruptedIOException) {
73 log("Interrupted as expected");
74 return;
75 }
76 exc = new Exception(name
77 + ": Not interrupted as expected");
78 return;
79 }
80 exc = x;
81 if (acceptException) {
82 log("Thrown, but okay: " + x);
83 return;
84 }
85 return;
86 }
87 }
88 }
89
90 public void check() throws Exception {
91 if (!acceptException && exc != null)
92 throw new Exception(name + ": Exception thrown", exc);
93 }
94
95 }
96
97 static void test() throws Exception {
98 System.err.println("test");
99 Process p = Runtime.getRuntime().exec("/bin/cat");
100 Copier cp1 = new Copier("out", p.getInputStream(), System.err,
101 false, false);
102 Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
103 false, false);
104 Thread.sleep(100);
105 p.destroy();
106 System.err.println(" exit: " + p.waitFor());
107 cp1.join();
108 cp1.check();
109 cp2.join();
110 cp2.check();
111 }
112
113 static void testCloseBeforeDestroy() throws Exception {
114 System.err.println("testCloseBeforeDestroy");
115 Process p = Runtime.getRuntime().exec("/bin/cat");
116 Copier cp1 = new Copier("out", p.getInputStream(), System.err,
117 true, false);
118 Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
119 true, false);
120 Thread.sleep(100);
121 p.getInputStream().close();
122 p.getErrorStream().close();
123 p.destroy();
124 System.err.println(" exit: " + p.waitFor());
125 cp1.join();
126 cp1.check();
127 cp2.join();
128 cp2.check();
129 }
130
131 static void testCloseAfterDestroy() throws Exception {
132 System.err.println("testCloseAfterDestroy");
133 Process p = Runtime.getRuntime().exec("/bin/cat");
134 Copier cp1 = new Copier("out", p.getInputStream(), System.err,
135 true, false);
136 Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
137 true, false);
138 Thread.sleep(100);
139 p.destroy();
140 p.getInputStream().close();
141 p.getErrorStream().close();
142 System.err.println(" exit: " + p.waitFor());
143 cp1.join();
144 cp1.check();
145 cp2.join();
146 cp2.check();
147 }
148
149 static void testInterrupt() throws Exception {
150 System.err.println("testInterrupt");
151 Process p = Runtime.getRuntime().exec("/bin/cat");
152 Copier cp1 = new Copier("out", p.getInputStream(), System.err,
153 false, true);
154 Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
155 false, true);
156 Thread.sleep(100);
157 cp1.interrupt();
158 cp2.interrupt();
159 Thread.sleep(100);
160 p.destroy();
161 System.err.println(" exit: " + p.waitFor());
162 cp1.join();
163 cp1.check();
164 cp2.join();
165 cp2.check();
166 }
167
168 public static void main(String[] args) throws Exception {
169
170 // Applies only to Solaris; Linux and Windows
171 // behave a little differently
172 if (!System.getProperty("os.name").equals("SunOS"))
173 return;
174
175 test();
176 testCloseBeforeDestroy();
177 testCloseAfterDestroy();
178 testInterrupt();
179
180 }
181
182}