Skip to content

Commit

Permalink
TcpSession closing test #55
Browse files Browse the repository at this point in the history
  • Loading branch information
basilevs committed Apr 15, 2024
1 parent f6d93c1 commit d6d7401
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 30 deletions.
@@ -1,19 +1,20 @@
package org.eclipse.rcptt.ecl.client.tcp.tests;


import java.util.function.BiFunction;
import java.util.function.Function;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.rcptt.ecl.core.Command;
import org.eclipse.rcptt.ecl.runtime.ICommandService;
import org.eclipse.rcptt.ecl.runtime.IProcess;

import com.google.common.base.Function;

public class EclInjectedCommandService implements ICommandService {
public static Function<Command, IStatus> delegate = new Function<Command, IStatus>() {
public static BiFunction<Command, IProcess, IStatus> delegate = new BiFunction<Command, IProcess, IStatus>() {
@Override
public IStatus apply(Command ignored) {
public IStatus apply(Command ignored, IProcess ignored2) {
return Status.OK_STATUS;
}
};
Expand All @@ -23,7 +24,16 @@ public EclInjectedCommandService() {

@Override
public IStatus service(Command command, IProcess context) throws InterruptedException, CoreException {
return delegate.apply(command);
return delegate.apply(command, context);
}


public static void inject(Function<Command, IStatus> injection) {
delegate = (command, process) -> injection.apply(command);
}

public static void inject(BiFunction<Command, IProcess, IStatus> injection) {
delegate = injection;
}

}
Expand Up @@ -13,9 +13,15 @@
import com.google.common.base.Function;
import com.google.common.io.Closer;

import static org.junit.Assert.assertTrue;

import java.io.Closeable;
import java.io.IOException;
import java.net.InetAddress;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
Expand Down Expand Up @@ -66,12 +72,7 @@ public void close() throws IOException {
}
}
});
EclInjectedCommandService.delegate = new Function<Command, IStatus>() {
@Override
public IStatus apply(Command ignored) {
return Status.OK_STATUS;
}
};
EclInjectedCommandService.inject(ignored -> Status.OK_STATUS);
}

@After
Expand All @@ -81,12 +82,7 @@ public void closeServer() throws CoreException, IOException {

@Test
public void simpleExecution() throws CoreException, InterruptedException {
EclInjectedCommandService.delegate = new Function<Command, IStatus>() {
@Override
public IStatus apply(Command ignored) {
return new Status(IStatus.INFO, "id", "message");
}
};
EclInjectedCommandService.inject(ignored -> new Status(IStatus.INFO, "id", "message"));
IStatus status = executeCommand();
Assert.assertFalse(status.getMessage(), status.matches(IStatus.ERROR | IStatus.WARNING | IStatus.CANCEL));
Assert.assertTrue(status.matches(IStatus.INFO));
Expand All @@ -102,12 +98,7 @@ private IStatus executeCommand() throws CoreException, InterruptedException {

@Test
public void errorIsReported() throws CoreException, InterruptedException {
EclInjectedCommandService.delegate = new Function<Command, IStatus>() {
@Override
public IStatus apply(Command ignored) {
return new Status(IStatus.ERROR, "id", "message");
}
};
EclInjectedCommandService.inject(ignored -> new Status(IStatus.ERROR, "id", "message"));
IStatus status = executeCommand();
Assert.assertTrue(status.matches(IStatus.ERROR));
Assert.assertEquals("message", status.getMessage());
Expand All @@ -121,13 +112,10 @@ private static void absurdFunctionThatThrows() {

@Test
public void istatusPropagatesTraces() throws CoreException, InterruptedException {
EclInjectedCommandService.delegate = new Function<Command, IStatus>() {
@Override
public IStatus apply(Command ignored) {
EclInjectedCommandService.inject( ignored -> {
absurdFunctionThatThrows();
return Status.OK_STATUS;
}
};
});
IStatus status = executeCommand();
Throwable e = status.getException();
while (! (e instanceof CoreException)) {
Expand Down Expand Up @@ -173,4 +161,32 @@ public void testCommandExecution001() throws Throwable {
}
TestCase.assertEquals(count1, count2);
}

@Test(timeout=10000)
public void processDiesIfSessionIsClosed() throws CoreException, InterruptedException {
AtomicBoolean isAlive = new AtomicBoolean();
CountDownLatch start = new CountDownLatch(1);
CountDownLatch stop = new CountDownLatch(1);
EclInjectedCommandService.inject((ignored, process) -> {
do {
isAlive.set(process.isAlive());
start.countDown();
} while (process.isAlive());
stop.countDown();
return Status.CANCEL_STATUS;
});

CompletableFuture.runAsync(() -> {
try {
executeCommand();
} catch (CoreException | InterruptedException e) {
throw new AssertionError(e);
}
});
assertTrue(start.await(1, TimeUnit.SECONDS));
assertTrue(isAlive.get());
session.close();
assertTrue(stop.await(1, TimeUnit.SECONDS));
Assert.assertFalse(isAlive.get());
}
}
Expand Up @@ -58,8 +58,6 @@ public void onStatusChange(Executable executable) {

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

}

});
Expand Down

0 comments on commit d6d7401

Please sign in to comment.