Skip to content
This repository has been archived by the owner on Jul 13, 2022. It is now read-only.

Commit

Permalink
Enhance 'SynchLaunch' test utility to avoid 'stuck' process to hang t…
Browse files Browse the repository at this point in the history
…est case
  • Loading branch information
kdvolder committed Sep 14, 2017
1 parent 938aa6e commit 8c5e43a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
Expand Up @@ -10,6 +10,9 @@
*******************************************************************************/
package org.springframework.ide.eclipse.boot.test.util;

import java.time.Duration;

import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchesListener;
Expand Down Expand Up @@ -37,18 +40,24 @@ private synchronized boolean isTerminated() {
return launch.isTerminated();
}

public synchronized void waitForProcessTermination() {
//TODO: need a timeout limit?
public synchronized void waitForProcessTermination(Duration timeout) {
long start = System.currentTimeMillis();
long end = start+timeout.toMillis();
while (!isTerminated()) {
if (System.currentTimeMillis()>end) {
try {
//The process may be stuck, try to kill it forcibly to avoid stuck tests
launch.terminate();
} catch (DebugException e) {
}
}
try {
wait();
wait(Math.max(500, end - System.currentTimeMillis()));
} catch (InterruptedException e) {
}
}
}



public void launchesRemoved(ILaunch[] launches) {
notifyMaybe(launches);
}
Expand All @@ -75,8 +84,8 @@ public void launchesTerminated(ILaunch[] launches) {
notifyMaybe(launches);
}

public IProcess waitForProcess() {
waitForProcessTermination();
public IProcess waitForProcess(Duration timeout) {
waitForProcessTermination(timeout);
return LaunchUtil.findProcess(launch);
}

Expand Down
Expand Up @@ -10,6 +10,8 @@
*******************************************************************************/
package org.springframework.ide.eclipse.boot.test.util;

import java.time.Duration;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
Expand All @@ -31,13 +33,17 @@
public class LaunchUtil {

public static LaunchResult synchLaunch(ILaunchConfiguration launchConf) throws CoreException {
return synchLaunch(launchConf, Duration.ofMinutes(2));
}

public static LaunchResult synchLaunch(ILaunchConfiguration launchConf, Duration timeout) throws CoreException {
ILaunch l = launchConf.launch("run", new NullProgressMonitor(), false, true);
IProcess process = findProcess(l);
IStreamsProxy streams = process.getStreamsProxy();

StringBuilder out = capture(streams.getOutputStreamMonitor());
StringBuilder err = capture(streams.getErrorStreamMonitor());
IProcess p = synchLaunch(l);
IProcess p = synchLaunch(l, timeout);
return new LaunchResult(p.getExitValue(), out.toString(), err.toString());
}

Expand All @@ -54,14 +60,14 @@ public void streamAppended(String text, IStreamMonitor monitor) {
return out;
}

private static IProcess synchLaunch(ILaunch launch) {
private static IProcess synchLaunch(ILaunch launch, Duration timeout) {
DebugPlugin mgr = DebugPlugin.getDefault();
LaunchTerminationListener listener = null;
try {
//DebugUITools.launch(launchConf, "run");
listener = new LaunchTerminationListener(launch);
mgr.getLaunchManager().addLaunchListener(listener);
return listener.waitForProcess();
return listener.waitForProcess(timeout);
} finally {
if (listener!=null) {
mgr.getLaunchManager().removeLaunchListener(listener);
Expand Down

0 comments on commit 8c5e43a

Please sign in to comment.