author | egahlin |
Wed, 02 Oct 2019 19:26:33 +0200 | |
branch | JEP-349-branch |
changeset 58445 | 1893a674db04 |
parent 58271 | e47423f1318b |
child 58569 | 5469bde803fe |
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 { |
|
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
58271
diff
changeset
|
47 |
private final Object lock = new Object(); |
57361 | 48 |
private boolean killed = false; |
49 |
public void run() { |
|
50 |
while (true) { |
|
51 |
StartEvent s = new StartEvent(); |
|
52 |
s.commit(); |
|
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
58271
diff
changeset
|
53 |
synchronized (lock) { |
57361 | 54 |
try { |
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
58271
diff
changeset
|
55 |
lock.wait(10); |
57361 | 56 |
if (killed) { |
57 |
return; // end thread |
|
58 |
} |
|
59 |
} catch (InterruptedException e) { |
|
60 |
// ignore |
|
61 |
} |
|
62 |
} |
|
63 |
} |
|
64 |
} |
|
65 |
public void kill() { |
|
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
58271
diff
changeset
|
66 |
synchronized (lock) { |
57361 | 67 |
this.killed = true; |
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
58271
diff
changeset
|
68 |
lock.notifyAll(); |
57361 | 69 |
} |
70 |
} |
|
71 |
} |
|
72 |
||
73 |
public static void main(String... args) throws Exception { |
|
74 |
testStart(); |
|
75 |
testStartOnEvent(); |
|
76 |
testStartTwice(); |
|
77 |
testStartClosed(); |
|
78 |
} |
|
79 |
||
80 |
private static void testStartTwice() throws Exception { |
|
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
81 |
log("Entering testStartTwice()"); |
57361 | 82 |
CountDownLatch started = new CountDownLatch(1); |
83 |
try (RecordingStream rs = new RecordingStream()) { |
|
84 |
EventProducer t = new EventProducer(); |
|
85 |
t.start(); |
|
86 |
CompletableFuture.runAsync(() -> { |
|
87 |
rs.start(); |
|
88 |
}); |
|
89 |
rs.onEvent(e -> { |
|
90 |
if (started.getCount() > 0) { |
|
91 |
started.countDown(); |
|
92 |
} |
|
93 |
}); |
|
94 |
started.await(); |
|
95 |
t.kill(); |
|
96 |
try { |
|
97 |
rs.start(); |
|
98 |
throw new AssertionError("Expected IllegalStateException if started twice"); |
|
99 |
} catch (IllegalStateException ise) { |
|
100 |
// OK, as expected |
|
101 |
} |
|
102 |
} |
|
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
103 |
log("Leaving testStartTwice()"); |
57361 | 104 |
} |
105 |
||
106 |
static void testStart() throws Exception { |
|
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
107 |
log("Entering testStart()"); |
57361 | 108 |
CountDownLatch started = new CountDownLatch(1); |
109 |
try (RecordingStream rs = new RecordingStream()) { |
|
110 |
rs.onEvent(e -> { |
|
111 |
started.countDown(); |
|
112 |
}); |
|
113 |
EventProducer t = new EventProducer(); |
|
114 |
t.start(); |
|
115 |
CompletableFuture.runAsync(() -> { |
|
116 |
rs.start(); |
|
117 |
}); |
|
118 |
started.await(); |
|
119 |
t.kill(); |
|
120 |
} |
|
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
121 |
log("Leaving testStart()"); |
57361 | 122 |
} |
123 |
||
124 |
static void testStartOnEvent() throws Exception { |
|
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
125 |
log("Entering testStartOnEvent()"); |
57361 | 126 |
AtomicBoolean ISE = new AtomicBoolean(false); |
127 |
CountDownLatch startedTwice = new CountDownLatch(1); |
|
128 |
try (RecordingStream rs = new RecordingStream()) { |
|
129 |
rs.onEvent(e -> { |
|
130 |
try { |
|
131 |
rs.start(); // must not deadlock |
|
132 |
} catch (IllegalStateException ise) { |
|
133 |
if (!ISE.get()) { |
|
57857 | 134 |
ISE.set(true); |
57361 | 135 |
startedTwice.countDown(); |
136 |
} |
|
137 |
} |
|
138 |
}); |
|
139 |
EventProducer t = new EventProducer(); |
|
140 |
t.start(); |
|
141 |
CompletableFuture.runAsync(() -> { |
|
142 |
rs.start(); |
|
143 |
}); |
|
144 |
startedTwice.await(); |
|
145 |
t.kill(); |
|
146 |
if (!ISE.get()) { |
|
147 |
throw new AssertionError("Expected IllegalStateException"); |
|
148 |
} |
|
149 |
} |
|
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
150 |
log("Leaving testStartOnEvent()"); |
57361 | 151 |
} |
152 |
||
153 |
static void testStartClosed() { |
|
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
154 |
log("Entering testStartClosed()"); |
57361 | 155 |
RecordingStream rs = new RecordingStream(); |
156 |
rs.close(); |
|
157 |
try { |
|
158 |
rs.start(); |
|
159 |
throw new AssertionError("Expected IllegalStateException"); |
|
160 |
} catch (IllegalStateException ise) { |
|
161 |
// OK, as expected. |
|
162 |
} |
|
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
163 |
log("Leaving testStartClosed()"); |
57361 | 164 |
} |
165 |
||
58271
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
166 |
private static void log(String msg) { |
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
167 |
System.out.println(msg); |
e47423f1318b
Test fixes: more logging, typos, fixes to at-run targets
mseledtsov
parents:
58180
diff
changeset
|
168 |
} |
58180 | 169 |
} |