Path: blob/master/test/jdk/java/lang/Process/WaitFor.java
41149 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 822068426* @summary Process.waitFor(long, TimeUnit) can return false for a process27* that exited within the timeout28* @run main/othervm WaitFor29*/3031import java.io.InputStream;32import java.io.OutputStream;33import java.util.concurrent.TimeUnit;3435public class WaitFor {36public static void main(String[] args) throws Throwable {37int failCnt = 0;38for (int i = 0; i < 30; ++i) {39Process proc = new MyProcess(new ProcessBuilder("true").start());40boolean exited = proc.waitFor(100, TimeUnit.MILLISECONDS);41if (!exited && !proc.isAlive()) failCnt++;42}43if (failCnt > 10) {44throw new RuntimeException(failCnt + " processes were still alive"45+ " after timeout");46}47}48}4950/**51* This class uses the default implementation of java.lang.Process#waitFor(long,52* TimeUnit), and delegates all other calls to the actual implementation of53* Process.54*/55class MyProcess extends Process {56Process impl;57public MyProcess(Process impl) { this.impl = impl; }58public OutputStream getOutputStream() { return impl.getOutputStream(); }59public InputStream getInputStream() { return impl.getInputStream(); }60public InputStream getErrorStream() { return impl.getErrorStream(); }61public int waitFor() throws InterruptedException { return impl.waitFor(); }62public int exitValue() { return impl.exitValue(); }63public void destroy() { impl.destroy(); }64public ProcessHandle toHandle() { return impl.toHandle(); }65}666768