Skip to content

Commit

Permalink
8338281: jshell does not run shutdown hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
lahodaj committed Aug 14, 2024
1 parent 38bd8a3 commit d5f6eba
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private boolean processCommand() throws IOException {
} catch (Throwable ex) {
// JShell-core not waiting for a result, ignore
}
return true;
return false;
}
default: {
Object arg = in.readObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.sun.jdi.VirtualMachine;
import java.io.PrintStream;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import jdk.jshell.JShellConsole;
import jdk.jshell.execution.JdiDefaultExecutionControl.JdiStarter.TargetDescription;
Expand Down Expand Up @@ -267,6 +268,13 @@ public void stop() throws EngineTerminationException, InternalException {
@Override
public void close() {
super.close();
if (process != null) {
try {
process.waitFor(1, TimeUnit.SECONDS);
} catch (InterruptedException ex) {
debug(ex, "waitFor remote");
}
}
disposeVM();
}

Expand Down
8 changes: 6 additions & 2 deletions test/langtools/jdk/jshell/KullaTesting.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,12 @@ public void addToClasspath(Path path) {
}

@BeforeMethod
public void setUp() {
setUp(b -> {});
public void setUp(Method testMethod) {
setUp(testMethod, b -> {});
}

public void setUp(Method testMethod, Consumer<JShell.Builder> bc) {
setUp(bc);
}

public void setUp(Consumer<JShell.Builder> bc) {
Expand Down
41 changes: 40 additions & 1 deletion test/langtools/jdk/jshell/ShutdownTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@
* @run testng ShutdownTest
*/

import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Consumer;

import jdk.jshell.JShell;
import jdk.jshell.JShell.Subscription;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;

@Test
public class ShutdownTest extends KullaTesting {

int shutdownCount;
Expand All @@ -53,13 +58,15 @@ public void testExit() {
assertEquals(shutdownCount, 1);
}

@Test
public void testCloseCallback() {
shutdownCount = 0;
getState().onShutdown(this::shutdownCounter);
getState().close();
assertEquals(shutdownCount, 1);
}

@Test
public void testCloseUnsubscribe() {
shutdownCount = 0;
Subscription token = getState().onShutdown(this::shutdownCounter);
Expand All @@ -68,6 +75,7 @@ public void testCloseUnsubscribe() {
assertEquals(shutdownCount, 0);
}

@Test
public void testTwoShutdownListeners() {
ShutdownListener listener1 = new ShutdownListener();
ShutdownListener listener2 = new ShutdownListener();
Expand Down Expand Up @@ -118,6 +126,37 @@ public void testSubscriptionAfterShutdown() {
getState().onShutdown(e -> {});
}

@Test
public void testRunShutdownHooks() throws IOException {
Path temporary = Paths.get("temp");
Files.newOutputStream(temporary).close();
assertEval("import java.io.*;");
assertEval("import java.nio.file.*;");
assertEval("""
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
Files.delete(Paths.get("$TEMPORARY"));
} catch (IOException ex) {
//ignored
}
}))
""".replace("$TEMPORARY", temporary.toAbsolutePath().toString()));
getState().close();
assertFalse(Files.exists(temporary));
}

@Override
public void setUp(Method testMethod, Consumer<JShell.Builder> bc) {
Consumer<JShell.Builder> augmentedBuilder = switch (testMethod.getName()) {
case "testRunShutdownHooks" -> builder -> {
builder.executionEngine(Presets.TEST_STANDARD_EXECUTION);
bc.accept(builder);
};
default -> bc;
};
super.setUp(augmentedBuilder);
}

private static class ShutdownListener implements Consumer<JShell> {
private int count;

Expand Down

0 comments on commit d5f6eba

Please sign in to comment.