author | mseledtsov |
Mon, 23 Sep 2019 09:16:05 -0700 | |
branch | JEP-349-branch |
changeset 58271 | e47423f1318b |
parent 58180 | 0a8943b4d0dd |
child 58445 | 1893a674db04 |
permissions | -rw-r--r-- |
57361 | 1 |
/* |
2 |
* Copyright (c) 2019, 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.jfr.api.consumer.recordingstream; |
|
27 |
||
28 |
import java.util.concurrent.CompletableFuture; |
|
29 |
import java.util.concurrent.CountDownLatch; |
|
30 |
import java.util.concurrent.atomic.AtomicBoolean; |
|
31 |
||
32 |
import jdk.jfr.Event; |
|
33 |
import jdk.jfr.consumer.RecordingStream; |
|
34 |
||
35 |
/** |
|
36 |
* @test |
|
37 |
* @summary Tests RecordingStream::start() |
|
38 |
* @key jfr |
|
39 |
* @requires vm.hasJFR |
|
40 |
* @library /test/lib |
|
41 |
* @run main/othervm jdk.jfr.api.consumer.recordingstream.TestStart |
|
42 |
*/ |
|
43 |
public class TestStart { |
|
44 |
static class StartEvent extends Event { |
|
45 |
} |
|
46 |
static class EventProducer extends Thread { |
|
47 |
private boolean killed = false; |
|
48 |
public void run() { |
|
49 |
while (true) { |
|
50 |
StartEvent s = new StartEvent(); |
|
51 |
s.commit(); |
|
52 |
synchronized (this) { |
|
53 |
try { |
|
54 |
wait(10); |
|
55 |
if (killed) { |
|
56 |
return; // end thread |
|
57 |
} |
|
58 |
} catch (InterruptedException e) { |
|
59 |
// ignore |
|
60 |
} |
|
61 |
} |
|
62 |
} |
|
63 |
} |
|
64 |
public void kill() { |
|
65 |
synchronized (this) { |
|
66 |
this.killed = true; |
|
67 |
this.notifyAll(); |
|
68 |
} |
|
69 |
} |
|
70 |
} |
|
71 |
||
72 |
public static void main(String... args) throws Exception { |
|
73 |
testStart(); |
|
74 |
testStartOnEvent(); |
|
75 |
testStartTwice(); |
|
76 |
testStartClosed(); |
|
77 |
} |
|
78 |
||
79 |
private static void testStartTwice() throws Exception { |
|
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
80 |
log("Entering testStartTwice()"); |
57361 | 81 |
CountDownLatch started = new CountDownLatch(1); |
82 |
try (RecordingStream rs = new RecordingStream()) { |
|
83 |
EventProducer t = new EventProducer(); |
|
84 |
t.start(); |
|
85 |
CompletableFuture.runAsync(() -> { |
|
86 |
rs.start(); |
|
87 |
}); |
|
88 |
rs.onEvent(e -> { |
|
89 |
if (started.getCount() > 0) { |
|
90 |
started.countDown(); |
|
91 |
} |
|
92 |
}); |
|
93 |
started.await(); |
|
94 |
t.kill(); |
|
95 |
try { |
|
96 |
rs.start(); |
|
97 |
throw new AssertionError("Expected IllegalStateException if started twice"); |
|
98 |
} catch (IllegalStateException ise) { |
|
99 |
// OK, as expected |
|
100 |
} |
|
101 |
} |
|
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
102 |
log("Leaving testStartTwice()"); |
57361 | 103 |
} |
104 |
||
105 |
static void testStart() throws Exception { |
|
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
106 |
log("Entering testStart()"); |
57361 | 107 |
CountDownLatch started = new CountDownLatch(1); |
108 |
try (RecordingStream rs = new RecordingStream()) { |
|
109 |
rs.onEvent(e -> { |
|
110 |
started.countDown(); |
|
111 |
}); |
|
112 |
EventProducer t = new EventProducer(); |
|
113 |
t.start(); |
|
114 |
CompletableFuture.runAsync(() -> { |
|
115 |
rs.start(); |
|
116 |
}); |
|
117 |
started.await(); |
|
118 |
t.kill(); |
|
119 |
} |
|
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
120 |
log("Leaving testStart()"); |
57361 | 121 |
} |
122 |
||
123 |
static void testStartOnEvent() throws Exception { |
|
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
124 |
log("Entering testStartOnEvent()"); |
57361 | 125 |
AtomicBoolean ISE = new AtomicBoolean(false); |
126 |
CountDownLatch startedTwice = new CountDownLatch(1); |
|
127 |
try (RecordingStream rs = new RecordingStream()) { |
|
128 |
rs.onEvent(e -> { |
|
129 |
try { |
|
130 |
rs.start(); // must not deadlock |
|
131 |
} catch (IllegalStateException ise) { |
|
132 |
if (!ISE.get()) { |
|
57857 | 133 |
ISE.set(true); |
57361 | 134 |
startedTwice.countDown(); |
135 |
} |
|
136 |
} |
|
137 |
}); |
|
138 |
EventProducer t = new EventProducer(); |
|
139 |
t.start(); |
|
140 |
CompletableFuture.runAsync(() -> { |
|
141 |
rs.start(); |
|
142 |
}); |
|
143 |
startedTwice.await(); |
|
144 |
t.kill(); |
|
145 |
if (!ISE.get()) { |
|
146 |
throw new AssertionError("Expected IllegalStateException"); |
|
147 |
} |
|
148 |
} |
|
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
149 |
log("Leaving testStartOnEvent()"); |
57361 | 150 |
} |
151 |
||
152 |
static void testStartClosed() { |
|
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
153 |
log("Entering testStartClosed()"); |
57361 | 154 |
RecordingStream rs = new RecordingStream(); |
155 |
rs.close(); |
|
156 |
try { |
|
157 |
rs.start(); |
|
158 |
throw new AssertionError("Expected IllegalStateException"); |
|
159 |
} catch (IllegalStateException ise) { |
|
160 |
// OK, as expected. |
|
161 |
} |
|
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
162 |
log("Leaving testStartClosed()"); |
57361 | 163 |
} |
164 |
||
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
165 |
private static void log(String msg) { |
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
166 |
System.out.println(msg); |
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
167 |
} |
58180 | 168 |
} |