StopExecutionTest.java revision 3062:15bdc18525ff
11573Srgrimes/* 21573Srgrimes * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 31573Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 41573Srgrimes * 51573Srgrimes * This code is free software; you can redistribute it and/or modify it 61573Srgrimes * under the terms of the GNU General Public License version 2 only, as 71573Srgrimes * published by the Free Software Foundation. 81573Srgrimes * 91573Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT 101573Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 111573Srgrimes * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 121573Srgrimes * version 2 for more details (a copy is included in the LICENSE file that 131573Srgrimes * accompanied this code). 141573Srgrimes * 151573Srgrimes * You should have received a copy of the GNU General Public License version 161573Srgrimes * 2 along with this work; if not, write to the Free Software Foundation, 171573Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 181573Srgrimes * 191573Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 201573Srgrimes * or visit www.oracle.com if you need additional information or have any 211573Srgrimes * questions. 221573Srgrimes */ 231573Srgrimes 241573Srgrimes/* 251573Srgrimes * @test 261573Srgrimes * @summary Test JShell#stop 271573Srgrimes * @build KullaTesting TestingInputStream 281573Srgrimes * @run testng StopExecutionTest 291573Srgrimes */ 301573Srgrimes 311573Srgrimesimport java.io.IOException; 321573Srgrimesimport java.io.PrintWriter; 331573Srgrimesimport java.io.StringWriter; 341573Srgrimesimport java.util.Random; 351573Srgrimes 361573Srgrimesimport jdk.internal.jshell.tool.StopDetectingInputStream; 371573Srgrimesimport jdk.internal.jshell.tool.StopDetectingInputStream.State; 381573Srgrimesimport jdk.jshell.JShell; 391573Srgrimesimport org.testng.annotations.Test; 401573Srgrimes 411573Srgrimesimport static org.testng.Assert.assertEquals; 421573Srgrimesimport static org.testng.Assert.fail; 431573Srgrimes 441573Srgrimes@Test 451573Srgrimespublic class StopExecutionTest extends KullaTesting { 461573Srgrimes 471573Srgrimes private final Object lock = new Object(); 481573Srgrimes private boolean isStopped; 491573Srgrimes 501573Srgrimes @Test(enabled = false) // TODO 8129546 511573Srgrimes public void testStopLoop() throws InterruptedException { 521573Srgrimes scheduleStop("while (true) ;"); 531573Srgrimes } 5417974Sbde 551573Srgrimes @Test(enabled = false) // TODO 8129546 56 public void testStopASleep() throws InterruptedException { 57 scheduleStop("while (true) { try { Thread.sleep(100); } catch (InterruptedException ex) { } }"); 58 } 59 60 @Test(enabled = false) // TODO 8129546 61 public void testScriptCatchesStop() throws Exception { 62 scheduleStop("for (int i = 0; i < 30; i++) { try { Thread.sleep(100); } catch (Throwable ex) { } }"); 63 } 64 65 private void scheduleStop(String src) throws InterruptedException { 66 JShell state = getState(); 67 isStopped = false; 68 StringWriter writer = new StringWriter(); 69 PrintWriter out = new PrintWriter(writer); 70 Thread t = new Thread(() -> { 71 int i = 1; 72 int n = 30; 73 synchronized (lock) { 74 do { 75 state.stop(); 76 if (!isStopped) { 77 out.println("Not stopped. Try again: " + i); 78 try { 79 lock.wait(1000); 80 } catch (InterruptedException ignored) { 81 } 82 } 83 } while (i++ < n && !isStopped); 84 if (!isStopped) { 85 System.err.println(writer.toString()); 86 fail("Evaluation was not stopped: '" + src + "'"); 87 } 88 } 89 }); 90 t.start(); 91 assertEval(src); 92 synchronized (lock) { 93 out.println("Evaluation was stopped successfully: '" + src + "'"); 94 isStopped = true; 95 lock.notify(); 96 } 97 // wait until the background thread finishes to prevent from calling 'stop' on closed state. 98 t.join(); 99 } 100 101 public void testStopDetectingInputRandom() throws IOException { 102 long seed = System.nanoTime(); 103 Random r = new Random(seed); 104 105 for (int m = 0; m < 10; m++) { 106 StopDetectingInputStream buffer = new StopDetectingInputStream(null, null); 107 108 buffer.setState(State.BUFFER); 109 110 for (int i = 0; i < 1000; i++) { 111 int chunkSize = r.nextInt(StopDetectingInputStream.INITIAL_SIZE * 3); 112 113 doChunk(buffer, chunkSize); 114 } 115 } 116 } 117 118 private void doChunk(StopDetectingInputStream buffer, int chunkSize) throws IOException { 119 for (int c = 0; c < chunkSize; c++) { 120 buffer.write(c); 121 } 122 123 for (int c = 0; c < chunkSize; c++) { 124 int read = buffer.read(); 125 126 assertEquals(read, c); 127 } 128 } 129 130} 131