8215438: jshell tool: Ctrl-D causes EOF
authorjlahoda
Tue, 08 Jan 2019 16:31:27 +0100
changeset 53215 299fe76c25c7
parent 53214 386df79e2011
child 53216 df97e2c0f9ae
8215438: jshell tool: Ctrl-D causes EOF Summary: Properly handling EndOfFileException so that jshell can be closed with Ctrl-D. Reviewed-by: rfield
src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java
test/langtools/jdk/jshell/ReplToolTesting.java
test/langtools/jdk/jshell/ToolBasicTest.java
--- a/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java	Tue Jan 08 09:29:36 2019 +0100
+++ b/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java	Tue Jan 08 16:31:27 2019 +0100
@@ -59,6 +59,7 @@
 import jdk.internal.org.jline.keymap.KeyMap;
 import jdk.internal.org.jline.reader.Binding;
 import jdk.internal.org.jline.reader.EOFError;
+import jdk.internal.org.jline.reader.EndOfFileException;
 import jdk.internal.org.jline.reader.History;
 import jdk.internal.org.jline.reader.LineReader;
 import jdk.internal.org.jline.reader.LineReader.Option;
@@ -200,6 +201,8 @@
             return in.readLine(firstLinePrompt);
         } catch (UserInterruptException ex) {
             throw (InputInterruptedException) new InputInterruptedException().initCause(ex);
+        } catch (EndOfFileException ex) {
+            return null;
         }
     }
 
@@ -1212,6 +1215,7 @@
                     while ((r = input.read()) != (-1)) {
                         processInputByte(r);
                     }
+                    slaveInput.close();
                 } catch (IOException ex) {
                     throw new IllegalStateException(ex);
                 }
--- a/test/langtools/jdk/jshell/ReplToolTesting.java	Tue Jan 08 09:29:36 2019 +0100
+++ b/test/langtools/jdk/jshell/ReplToolTesting.java	Tue Jan 08 16:31:27 2019 +0100
@@ -22,6 +22,7 @@
  */
 
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.OutputStream;
 import java.io.PrintStream;
 import java.util.ArrayList;
@@ -126,6 +127,14 @@
         cmdin.setInput(s);
     }
 
+    public void closeCommandInput() {
+        try {
+            cmdin.close();
+        } catch (IOException ex) {
+            throw new IllegalStateException(ex);
+        }
+    }
+
     public final static Pattern idPattern = Pattern.compile("^\\s+(\\d+)");
     public Consumer<String> assertList() {
         return s -> {
@@ -755,6 +764,8 @@
 
     class WaitingTestingInputStream extends TestingInputStream {
 
+        private boolean closed;
+
         @Override
         synchronized void setInput(String s) {
             super.setInput(s);
@@ -764,7 +775,7 @@
         synchronized void waitForInput() {
             boolean interrupted = false;
             try {
-                while (available() == 0) {
+                while (available() == 0 && !closed) {
                     try {
                         wait();
                     } catch (InterruptedException e) {
@@ -790,6 +801,12 @@
             waitForInput();
             return super.read(b, off, len);
         }
+
+        @Override
+        public synchronized void close() throws IOException {
+            closed = true;
+            notify();
+        }
     }
 
     class PromptedCommandOutputStream extends OutputStream {
--- a/test/langtools/jdk/jshell/ToolBasicTest.java	Tue Jan 08 09:29:36 2019 +0100
+++ b/test/langtools/jdk/jshell/ToolBasicTest.java	Tue Jan 08 16:31:27 2019 +0100
@@ -130,6 +130,18 @@
         }
     }
 
+    public void testCtrlD() {
+        test(false, new String[]{"--no-startup"},
+                a -> {
+                    if (!a) {
+                        closeCommandInput();
+                    } else {
+                        throw new IllegalStateException();
+                    }
+                }
+        );
+    }
+
     private final Object lock = new Object();
     private PrintWriter out;
     private boolean isStopped;