Skip to content

Commit

Permalink
Fix cancellation of running tests #55
Browse files Browse the repository at this point in the history
  • Loading branch information
basilevs committed Apr 15, 2024
1 parent c11291a commit f6d93c1
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 9 deletions.
Expand Up @@ -49,6 +49,9 @@ public IProcess execute(Command command) throws CoreException {
// public IProcess execute(final Command scriptlet, IPipe in, IPipe out)
// throws CoreException
public IProcess execute(Command scriptlet, IPipe in, IPipe out) throws CoreException {
if (isClosed()) {
throw new CoreException(Status.CANCEL_STATUS);
}
final ICommandService svc = scriptlet instanceof ProcInstance ? new ProcInstanceService()
: CorePlugin.getScriptletManager().getScriptletService(scriptlet);
final IPipe tinput = in == null ? createPipe().close(Status.OK_STATUS) : in;
Expand Down
Expand Up @@ -71,7 +71,7 @@ public synchronized IStatus waitFor(long timeout, IProgressMonitor monitor) thro
}

public synchronized boolean isAlive() {
return status == null;
return status == null && !session.isClosed();
}

public ISession getSession() {
Expand Down
Expand Up @@ -48,6 +48,9 @@ public IStatus service(Command command, IProcess process)
List<Object> contentOutput = new ArrayList<Object>();

for (int i = 0; i < times; i++) {
if (!process.isAlive()) {
throw new CoreException(Status.CANCEL_STATUS);
}
IPipe input = process.getSession().createPipe();
for (Object o : content)
input.write(o);
Expand Down
Expand Up @@ -51,6 +51,9 @@ public IStatus service(Command command, IProcess process) throws InterruptedExce
ISession session = process.getSession();
try {
for (int i = 0; i < times; i++) {
if (!process.isAlive()) {
throw new CoreException(Status.CANCEL_STATUS);
}
if (i + 1 == times && !t.isNoScreenshot()) {
session.putProperty(NO_SCREENSHOT, null);
} else {
Expand Down
Expand Up @@ -10,12 +10,16 @@
*******************************************************************************/
package org.eclipse.rcptt.ecl.server.tcp;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.rcptt.ecl.core.Command;
import org.eclipse.rcptt.ecl.internal.core.CorePlugin;
Expand All @@ -24,12 +28,16 @@
import org.eclipse.rcptt.ecl.runtime.IPipe;
import org.eclipse.rcptt.ecl.runtime.IProcess;
import org.eclipse.rcptt.ecl.runtime.ISession;
import org.osgi.framework.FrameworkUtil;

final class SessionRequestHandler implements Runnable {
private final Socket socket;
private final ISession session;
private final BufferedInputStream inputStream;
private final int defaultTimeout;
private final static ILog LOG = Platform.getLog(FrameworkUtil.getBundle(SessionRequestHandler.class));

SessionRequestHandler(Socket socket, boolean useJobs) {
SessionRequestHandler(Socket socket, boolean useJobs) throws IOException {
// super("ECL tcp session:" + socket.getPort());
this.socket = socket;
try {
Expand All @@ -38,11 +46,13 @@ final class SessionRequestHandler implements Runnable {
CorePlugin.log(e);
}
this.session = EclRuntime.createSession(useJobs);
this.inputStream = new BufferedInputStream(socket.getInputStream());
this.defaultTimeout = socket.getSoTimeout();
}

public void run() {
try {
IPipe pipe = CoreUtils.createEMFPipe(socket.getInputStream(),
IPipe pipe = CoreUtils.createEMFPipe(inputStream,
socket.getOutputStream());
while (!Thread.currentThread().isInterrupted()
&& !socket.isClosed()) {
Expand Down Expand Up @@ -116,11 +126,15 @@ private IPipe readInput(IPipe pipe) throws CoreException {
}

private IStatus writeOutput(IPipe pipe, IProcess process)
throws CoreException {
throws CoreException, IOException {
Object object;
do {
object = process.getOutput().take(Long.MAX_VALUE);
if (object instanceof IStatus) {
object = process.getOutput().take(100);
if (object == null) {
if (!isConnected()) {
throw new CoreException(Status.CANCEL_STATUS);
}
} else if (object instanceof IStatus) {
try {
return process.waitFor();
} catch (InterruptedException e) {
Expand All @@ -129,8 +143,22 @@ private IStatus writeOutput(IPipe pipe, IProcess process)
} else {
pipe.write(object);
}

} while (true);
}

private boolean isConnected() throws IOException {
inputStream.mark(10);
socket.setSoTimeout(1);
try {
return inputStream.read() >= 0 && inputStream.read() >= 0;
} catch (SocketTimeoutException e) {
return true;
} finally {
socket.setSoTimeout(defaultTimeout);
inputStream.reset();
}
}

public void recover(Socket client) {
}
Expand Down
Expand Up @@ -47,6 +47,22 @@ public DataExecutable(AutLaunch launch, IQ7NamedElement element,
}
this.launch = launch;
executionMonitor = new NullProgressMonitor();
addListener(new Listener() {

@Override
public void onStatusChange(Executable executable) {
if (getStatus() == State.COMPLETED) {
executionMonitor.setCanceled(true);
}
}

@Override
public void updateSessionCounters(Executable executable, IStatus status) {
// TODO Auto-generated method stub

}

});
}

public AutLaunch getAut() {
Expand Down
Expand Up @@ -30,7 +30,13 @@ public IStatus service(Command command, IProcess context)
int ms = wait.getMs();
if (ms < 0)
return TeslaImplPlugin.err("Negative delay is not permitted");
Thread.sleep(ms); // any exceptions will be handled by session
long stop = System.currentTimeMillis() + ms;
while (System.currentTimeMillis() < stop) {
if (!context.isAlive()) {
throw new CoreException(Status.CANCEL_STATUS);
}
Thread.sleep(100); // any exceptions will be handled by session
}
return Status.OK_STATUS;
}

Expand Down
Expand Up @@ -13,6 +13,7 @@
import java.io.IOException;
import java.net.InetAddress;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.rcptt.ecl.client.tcp.EclTcpSession;
import org.eclipse.rcptt.ecl.debug.runtime.SuspendListener;
import org.eclipse.rcptt.ecl.debug.runtime.SuspendManager;
Expand Down Expand Up @@ -66,9 +67,9 @@ public void start() {

private void testLocalEclServer() {
try {
new EclTcpSession(InetAddress.getByName("localhost"), ecl);
new EclTcpSession(InetAddress.getByName("localhost"), ecl).close();
Activator.info("Verified that local ECL server is working");
} catch (IOException e) {
} catch (IOException | CoreException e) {
Activator.err(e, "Error testing a local ECL server. Something is blocking connection");
}
}
Expand Down

0 comments on commit f6d93c1

Please sign in to comment.