How to kill all of the sub-processes being created under my test suite?

377 Views Asked by At

I'm using maven (plugin version-1.7) and Aspectj-1.8.3.

My scenario is as follow: I have an installer jar that I want to test. The installer is using another jar, my-common.jar library that wraps Apache's utility commons-exec-1.3 and use it to execute commands, the method that I wrote looks like this:

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;
}

The problem is that since my test executes another jar,I mean, the installer, and the installer jar executes another one(lets name it app.jar) and than, the installer terminates and the app.jar keeps running (first, the installer is doing the installment and preparing the environment, and than, he executes the app.jar), the app jar is not being terminated when the testing suite is done (this was my intention and how it suppose to be in production environment).

The global target is to kill all of those processes being created under the integration tests suite.

My solution: Since the process id's are exposed only to the java.lang.UNIXProcess I thought to collect all of the processes ids and then terminate them manually at the end of the tests suite.

I thought to put an Aspect like this:

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));
}

That was my idea to solve the problem without redesign some parts of my code. So, to some it up, I'm looking for a way to make sure that all of the sub-processes being created under the integration tests suite are terminated.

Any solution is welcomed.

0

There are 0 best solutions below