33362
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @summary Test JShell#stop
|
|
27 |
* @build KullaTesting TestingInputStream
|
|
28 |
* @run testng StopExecutionTest
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.io.IOException;
|
|
32 |
import java.io.PrintWriter;
|
|
33 |
import java.io.StringWriter;
|
|
34 |
import java.util.Random;
|
|
35 |
|
|
36 |
import jdk.internal.jshell.tool.StopDetectingInputStream;
|
|
37 |
import jdk.internal.jshell.tool.StopDetectingInputStream.State;
|
|
38 |
import jdk.jshell.JShell;
|
|
39 |
import org.testng.annotations.Test;
|
|
40 |
|
|
41 |
import static org.testng.Assert.assertEquals;
|
|
42 |
import static org.testng.Assert.fail;
|
|
43 |
|
|
44 |
@Test
|
|
45 |
public class StopExecutionTest extends KullaTesting {
|
|
46 |
|
|
47 |
private final Object lock = new Object();
|
|
48 |
private boolean isStopped;
|
|
49 |
|
|
50 |
@Test(enabled = false) // TODO 8129546
|
|
51 |
public void testStopLoop() throws InterruptedException {
|
|
52 |
scheduleStop("while (true) ;");
|
|
53 |
}
|
|
54 |
|
|
55 |
@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 |
}
|