./test/java/lang/ProcessBuilder/Basic.java

Print this page
rev 5754 : [mq]: getPid-patch

*** 671,680 **** --- 671,690 ---- @Override public InputStream getErrorStream() { return p.getErrorStream(); } + + @Override + public int getPid() { + return p.getPid(); + } + + @Override + public String getProcessName( int pid ) { + return p.getProcessName( pid ); + } } private static boolean matches(String str, String regex) { return Pattern.compile(regex).matcher(str).find(); }
*** 2130,2148 **** --- 2140,2197 ---- System.setSecurityManager(null); //---------------------------------------------------------------- // Check that Process.isAlive() & // Process.waitFor(0, TimeUnit.MILLISECONDS) work as expected. + // Also, check that Process.getPid() works properly //---------------------------------------------------------------- try { List<String> childArgs = new ArrayList<String>(javaChildArgs); childArgs.add("sleep"); + List<Integer> pidsBefore = new ArrayList(Process.getPids()); + System.out.println( java.util.Arrays.toString(pidsBefore.toArray()) ); final Process p = new ProcessBuilder(childArgs).start(); long start = System.nanoTime(); if (!p.isAlive() || p.waitFor(0, TimeUnit.MILLISECONDS)) { fail("Test failed: Process exited prematurely"); } + int pid = p.getPid(); + System.out.println( "pid = " + pid ); + if (pid < 0) { + fail("Test failed: PID of running process should be >= 0"); + } + // check for valid process name (should be 'java') + String pName = p.getProcessName( pid ); + System.out.println( "Process name = " + pName ); + if (pName == null || pName.equals("") || !pName.equals("java")) { + fail("Test failed: Process.getProcessName(" + pid + ") returned '" + pName + "' instead of 'java'" ); + } + + // Now test that getPids() contains this pid + List<Integer> pidsAfter = new ArrayList(Process.getPids()); + System.out.println( java.util.Arrays.toString(pidsAfter.toArray())); + if (!pidsAfter.contains(new Integer(pid))) { + fail("Test failed: Process.getPids() (after) does not contain pid of process that was started"); + } + if (pidsBefore.contains(new Integer(pid))) { + fail("Test failed: Process.getPids() (before) contains pid of process that was started"); + } + // should just be one matching the process we just created + if (pidsAfter.size() != (pidsBefore.size() + 1)) { + fail("Test failed: Process.getPids().size() returned " + pidsAfter.size() + " instead of " + + (pidsBefore.size() + 1) ); + } + // display all process names we've created + for (Integer aPid:pidsAfter) { + try { + pName = p.getProcessName(aPid); + } catch (IllegalArgumentException iae) { + pName = "<not found>"; + } + System.out.println( aPid + " " + pName ); + } + long end = System.nanoTime(); // give waitFor(timeout) a wide berth (100ms) if ((end - start) > 100000000) fail("Test failed: waitFor took too long");
*** 2153,2162 **** --- 2202,2216 ---- !p.waitFor(0, TimeUnit.MILLISECONDS)) { fail("Test failed: Process still alive - please terminate " + p.toString() + " manually"); } + pid = p.getPid(); + System.out.println( "pid = " + pid ); + if (pid >=0) { + fail("Test failed: PID of dead process is " + pid); + } } catch (Throwable t) { unexpected(t); } //---------------------------------------------------------------- // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS) // works as expected.