test/java/lang/ProcessBuilder/Basic.java

Print this page

        

*** 34,43 **** --- 34,44 ---- import java.lang.ProcessBuilder.Redirect; import static java.lang.ProcessBuilder.Redirect.*; import java.io.*; + import java.lang.reflect.Field; import java.util.*; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.security.*; import java.util.regex.Pattern;
*** 1936,1963 **** List<String> childArgs = new ArrayList<String>(javaChildArgs); childArgs.add("sleep"); final byte[] bytes = new byte[10]; final Process p = new ProcessBuilder(childArgs).start(); final CountDownLatch latch = new CountDownLatch(1); final Thread thread = new Thread() { public void run() { try { - latch.countDown(); int r; ! switch (action) { ! case 0: r = p.getInputStream().read(); break; ! case 1: r = p.getErrorStream().read(); break; ! case 2: r = p.getInputStream().read(bytes); break; ! case 3: r = p.getErrorStream().read(bytes); break; default: throw new Error(); } equal(-1, r); } catch (Throwable t) { unexpected(t); }}}; thread.start(); latch.await(); ! Thread.sleep(10); p.destroy(); thread.join(); } } catch (Throwable t) { unexpected(t); } --- 1937,1996 ---- List<String> childArgs = new ArrayList<String>(javaChildArgs); childArgs.add("sleep"); final byte[] bytes = new byte[10]; final Process p = new ProcessBuilder(childArgs).start(); final CountDownLatch latch = new CountDownLatch(1); + final InputStream s; + switch (action & 0x1) { + case 0: s = p.getInputStream(); break; + case 1: s = p.getErrorStream(); break; + default: throw new Error(); + } final Thread thread = new Thread() { public void run() { try { int r; ! latch.countDown(); ! switch (action & 0x2) { ! case 0: r = s.read(bytes); break; ! case 2: r = s.read(bytes); break; default: throw new Error(); } equal(-1, r); } catch (Throwable t) { unexpected(t); }}}; thread.start(); latch.await(); ! ! String os = System.getProperty("os.name"); ! if (os.equalsIgnoreCase("Solaris") || ! os.equalsIgnoreCase("SunOS")) ! { ! Field useCountField; ! Integer useCount = null; ! if (s.toString().startsWith( ! "java.lang.UNIXProcess$DeferredCloseInputStream")) ! { ! useCountField = s.getClass(). ! getDeclaredField("useCount"); ! useCountField.setAccessible(true); ! useCount = (Integer)useCountField.get(s); ! } else { ! Field deferredField = p.getClass(). ! getDeclaredField("stdout_inner_stream"); ! deferredField.setAccessible(true); ! Object deferred = deferredField.get(p); ! useCountField = deferred.getClass(). ! getDeclaredField("useCount"); ! useCountField.setAccessible(true); ! useCount = (Integer)useCountField.get(deferred); ! } ! while (useCount.intValue() <= 0) { ! useCount = (Integer)useCountField.get(s); ! Thread.currentThread().yield(); ! } ! } p.destroy(); thread.join(); } } catch (Throwable t) { unexpected(t); }