langtools/test/jdk/jshell/StopExecutionTest.java
changeset 43135 0c7c13fa7bee
parent 35359 f04501964016
equal deleted inserted replaced
43134:006808ae5f6e 43135:0c7c13fa7bee
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
       
    26  * @bug 8171385
    26  * @summary Test JShell#stop
    27  * @summary Test JShell#stop
    27  * @modules jdk.jshell/jdk.internal.jshell.tool
    28  * @modules jdk.jshell/jdk.internal.jshell.tool
    28  * @build KullaTesting TestingInputStream
    29  * @build KullaTesting TestingInputStream
    29  * @run testng StopExecutionTest
    30  * @run testng StopExecutionTest
    30  */
    31  */
    31 
    32 
    32 import java.io.IOException;
    33 import java.io.IOException;
       
    34 import java.io.PipedInputStream;
       
    35 import java.io.PipedOutputStream;
    33 import java.io.PrintWriter;
    36 import java.io.PrintWriter;
    34 import java.io.StringWriter;
    37 import java.io.StringWriter;
    35 import java.util.Random;
    38 import java.util.Random;
       
    39 import java.util.concurrent.CountDownLatch;
       
    40 import java.util.function.Consumer;
    36 
    41 
    37 import jdk.internal.jshell.tool.StopDetectingInputStream;
    42 import jdk.internal.jshell.tool.StopDetectingInputStream;
    38 import jdk.internal.jshell.tool.StopDetectingInputStream.State;
    43 import jdk.internal.jshell.tool.StopDetectingInputStream.State;
    39 import jdk.jshell.JShell;
    44 import jdk.jshell.JShell;
    40 import org.testng.annotations.Test;
    45 import org.testng.annotations.Test;
   126 
   131 
   127             assertEquals(read, c);
   132             assertEquals(read, c);
   128         }
   133         }
   129     }
   134     }
   130 
   135 
       
   136     public void testStopDetectingInputBufferWaitStop() throws Exception {
       
   137         Runnable shouldNotHappenRun =
       
   138                 () -> { throw new AssertionError("Should not happen."); };
       
   139         Consumer<Exception> shouldNotHappenExc =
       
   140                 exc -> { throw new AssertionError("Should not happen.", exc); };
       
   141         StopDetectingInputStream sd = new StopDetectingInputStream(shouldNotHappenRun, shouldNotHappenExc);
       
   142         CountDownLatch reading = new CountDownLatch(1);
       
   143         PipedInputStream is = new PipedInputStream() {
       
   144             @Override
       
   145             public int read() throws IOException {
       
   146                 reading.countDown();
       
   147                 return super.read();
       
   148             }
       
   149         };
       
   150         PipedOutputStream os = new PipedOutputStream(is);
       
   151 
       
   152         sd.setInputStream(is);
       
   153         sd.setState(State.BUFFER);
       
   154         reading.await();
       
   155         sd.setState(State.WAIT);
       
   156         os.write(3);
       
   157         int value = sd.read();
       
   158 
       
   159         if (value != 3) {
       
   160             throw new AssertionError();
       
   161         }
       
   162     }
   131 }
   163 }