From d6d740128ee20aa7cdd476ccf569819003b54798 Mon Sep 17 00:00:00 2001 From: Vasili Gulevich Date: Mon, 15 Apr 2024 19:42:45 +0400 Subject: [PATCH] TcpSession closing test #55 --- .../tcp/tests/EclInjectedCommandService.java | 20 ++++-- .../client/tcp/tests/TestTcpSession.java | 62 ++++++++++++------- .../internal/launching/DataExecutable.java | 2 - 3 files changed, 54 insertions(+), 30 deletions(-) diff --git a/ecl/tests/org.eclipse.rcptt.ecl.client.tcp.tests/src/org/eclipse/rcptt/ecl/client/tcp/tests/EclInjectedCommandService.java b/ecl/tests/org.eclipse.rcptt.ecl.client.tcp.tests/src/org/eclipse/rcptt/ecl/client/tcp/tests/EclInjectedCommandService.java index 80cf8acb2..227f946e1 100644 --- a/ecl/tests/org.eclipse.rcptt.ecl.client.tcp.tests/src/org/eclipse/rcptt/ecl/client/tcp/tests/EclInjectedCommandService.java +++ b/ecl/tests/org.eclipse.rcptt.ecl.client.tcp.tests/src/org/eclipse/rcptt/ecl/client/tcp/tests/EclInjectedCommandService.java @@ -1,6 +1,9 @@ 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; @@ -8,12 +11,10 @@ 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 delegate = new Function() { + public static BiFunction delegate = new BiFunction() { @Override - public IStatus apply(Command ignored) { + public IStatus apply(Command ignored, IProcess ignored2) { return Status.OK_STATUS; } }; @@ -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 injection) { + delegate = (command, process) -> injection.apply(command); + } + + public static void inject(BiFunction injection) { + delegate = injection; } } diff --git a/ecl/tests/org.eclipse.rcptt.ecl.client.tcp.tests/src/org/eclipse/rcptt/ecl/internal/client/tcp/tests/TestTcpSession.java b/ecl/tests/org.eclipse.rcptt.ecl.client.tcp.tests/src/org/eclipse/rcptt/ecl/internal/client/tcp/tests/TestTcpSession.java index 22a45a756..7fd9ef53b 100644 --- a/ecl/tests/org.eclipse.rcptt.ecl.client.tcp.tests/src/org/eclipse/rcptt/ecl/internal/client/tcp/tests/TestTcpSession.java +++ b/ecl/tests/org.eclipse.rcptt.ecl.client.tcp.tests/src/org/eclipse/rcptt/ecl/internal/client/tcp/tests/TestTcpSession.java @@ -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; @@ -66,12 +72,7 @@ public void close() throws IOException { } } }); - EclInjectedCommandService.delegate = new Function() { - @Override - public IStatus apply(Command ignored) { - return Status.OK_STATUS; - } - }; + EclInjectedCommandService.inject(ignored -> Status.OK_STATUS); } @After @@ -81,12 +82,7 @@ public void closeServer() throws CoreException, IOException { @Test public void simpleExecution() throws CoreException, InterruptedException { - EclInjectedCommandService.delegate = new Function() { - @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)); @@ -102,12 +98,7 @@ private IStatus executeCommand() throws CoreException, InterruptedException { @Test public void errorIsReported() throws CoreException, InterruptedException { - EclInjectedCommandService.delegate = new Function() { - @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()); @@ -121,13 +112,10 @@ private static void absurdFunctionThatThrows() { @Test public void istatusPropagatesTraces() throws CoreException, InterruptedException { - EclInjectedCommandService.delegate = new Function() { - @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)) { @@ -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()); + } } diff --git a/launching/org.eclipse.rcptt.launching/src/org/eclipse/rcptt/internal/launching/DataExecutable.java b/launching/org.eclipse.rcptt.launching/src/org/eclipse/rcptt/internal/launching/DataExecutable.java index 7bd7b97eb..e6c2e9e99 100644 --- a/launching/org.eclipse.rcptt.launching/src/org/eclipse/rcptt/internal/launching/DataExecutable.java +++ b/launching/org.eclipse.rcptt.launching/src/org/eclipse/rcptt/internal/launching/DataExecutable.java @@ -58,8 +58,6 @@ public void onStatusChange(Executable executable) { @Override public void updateSessionCounters(Executable executable, IStatus status) { - // TODO Auto-generated method stub - } });