blob: 68fd79a36396737827302b019e3a3c4c75605c2c [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.exec.issues;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.Executor;
import org.apache.commons.exec.TestUtil;
import org.junit.jupiter.api.Test;
/**
* EXEC-34 https://issues.apache.org/jira/browse/EXEC-34
*/
class Exec34Test {
private final Executor exec = DefaultExecutor.builder().get();
private final File testDir = new File("src/test/scripts");
private final File pingScript = TestUtil.resolveScriptFileForOS(testDir + "/ping");
/**
*
* Race condition prevent watchdog working using ExecuteStreamHandler. The test fails because when watchdog.destroyProcess() is invoked the external process
* is not bound to the watchdog yet.
*
* @throws Exception the test failed
*/
@Test
void testExec34Part1() throws Exception {
final CommandLine cmdLine = new CommandLine(pingScript);
cmdLine.addArgument("10"); // sleep 10 seconds
final ExecuteWatchdog watchdog = new ExecuteWatchdog(Integer.MAX_VALUE);
final DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
exec.setWatchdog(watchdog);
exec.execute(cmdLine, handler);
assertTrue(watchdog.isWatching());
watchdog.destroyProcess();
assertTrue(watchdog.killedProcess(), "Watchdog should have killed the process");
assertFalse(watchdog.isWatching(), "Watchdog is no longer watching the process");
}
/**
* Some user waited for an asynchronous process using watchdog.isWatching() which is now properly implemented using {@code DefaultExecuteResultHandler}.
*
* @throws Exception the test failed
*/
@Test
void testExec34Part2() throws Exception {
final CommandLine cmdLine = new CommandLine(pingScript);
cmdLine.addArgument("10"); // sleep 10 seconds
final ExecuteWatchdog watchdog = new ExecuteWatchdog(5000);
final DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
exec.setWatchdog(watchdog);
exec.execute(cmdLine, handler);
handler.waitFor();
assertTrue(handler.hasResult(), "Process has exited");
assertNotNull(handler.getException(), "Process was aborted");
assertTrue(watchdog.killedProcess(), "Watchdog should have killed the process");
assertFalse(watchdog.isWatching(), "Watchdog is no longer watching the process");
}
}