1

我正在使用 maven(插件版本 1.7)和 Aspectj-1.8.3。

我的场景如下:我有一个要测试的安装程序 jar。安装程序正在使用另一个 jar,my-common.jar 库,它包装了 Apache 的实用程序 commons-exec-1.3 并使用它来执行命令,我编写的方法如下所示:

public static int execCommand(String command) throws ExecuteException, IOException, InterruptedException {
    logger.debug("About to execute command: ", command);
    CommandLine commandLine = CommandLine.parse(command);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
    executor.setWatchdog(watchdog);
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    executor.execute(commandLine, resultHandler);
    return 0;
}

问题是,由于我的测试执行另一个 jar,我的意思是安装程序,并且安装程序 jar 执行另一个(让我们将其命名为 app.jar),然后安装程序终止并且 app.jar 继续运行(首先,安装程序正在执行安装并准备环境,然后,他执行了 app.jar),当测试套件完成时,app jar 并没有被终止(这是我的意图以及它在生产环境中的假设方式)。

全局目标是杀死所有在集成测试套件下创建的进程。

我的解决方案:由于进程 id 只暴露给java.lang.UNIXProcess我想收集所有进程 id,然后在测试套件结束时手动终止它们。

我想把一个方面是这样的:

static Collection<Integer> PidsToKill = new LinkedList<Integer>();

@After("!cflow(within(IntegrationTestsAspects)) && call(* java.lang.UNIXProcess.destroyProcess(int))&&args(pid)")
public void foo3(int pid) {
    PidsToKill.add(new Integer(pid));
}

这是我在不重新设计代码的某些部分的情况下解决问题的想法。因此,对于某些人来说,我正在寻找一种方法来确保在集成测试套件下创建的所有子流程都被终止。

欢迎任何解决方案。

4

0 回答 0