23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package jdk.jfr.api.consumer.recordingstream; |
26 package jdk.jfr.api.consumer.recordingstream; |
27 |
27 |
|
28 import java.util.List; |
|
29 import java.util.concurrent.CompletableFuture; |
28 import java.util.concurrent.CountDownLatch; |
30 import java.util.concurrent.CountDownLatch; |
29 import java.util.concurrent.atomic.AtomicBoolean; |
31 import java.util.concurrent.atomic.AtomicBoolean; |
30 |
32 |
31 import jdk.jfr.Event; |
33 import jdk.jfr.Event; |
|
34 import jdk.jfr.Recording; |
|
35 import jdk.jfr.consumer.RecordedEvent; |
32 import jdk.jfr.consumer.RecordingStream; |
36 import jdk.jfr.consumer.RecordingStream; |
|
37 import jdk.test.lib.jfr.Events; |
33 |
38 |
34 /** |
39 /** |
35 * @test |
40 * @test |
36 * @summary Tests that events are not emitted in handlers |
41 * @summary Tests that events are not emitted in handlers |
37 * @key jfr |
42 * @key jfr |
39 * @library /test/lib |
44 * @library /test/lib |
40 * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestRecursive |
45 * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestRecursive |
41 */ |
46 */ |
42 public class TestRecursive { |
47 public class TestRecursive { |
43 |
48 |
44 static class NotRecorded extends Event { |
49 public static class NotRecorded extends Event { |
45 } |
50 } |
46 |
51 |
47 static class Recorded extends Event { |
52 public static class Recorded extends Event { |
48 } |
53 } |
49 |
54 |
50 public static void main(String... args) throws Exception { |
55 public static void main(String... args) throws Exception { |
|
56 testAsync(); |
|
57 testSync(); |
|
58 } |
|
59 |
|
60 private static void testSync() throws Exception { |
|
61 try (Recording r = new Recording()) { |
|
62 r.start(); |
|
63 Recorded e1 = new Recorded(); |
|
64 e1.commit(); |
|
65 try (RecordingStream rs = new RecordingStream()) { |
|
66 rs.onEvent(e -> { |
|
67 NotRecorded e2 = new NotRecorded(); |
|
68 e2.commit(); |
|
69 }); |
|
70 CompletableFuture.runAsync(() -> { |
|
71 r.start(); |
|
72 }); |
|
73 } |
|
74 Recorded e3 = new Recorded(); |
|
75 e3.commit(); |
|
76 r.stop(); |
|
77 List<RecordedEvent> events = Events.fromRecording(r); |
|
78 if (count(events, NotRecorded.class) != 0) { |
|
79 throw new Exception("Expected 0 NotRecorded events"); |
|
80 } |
|
81 if (count(events, Recorded.class) != 2) { |
|
82 throw new Exception("Expected 2 Recorded events"); |
|
83 } |
|
84 } |
|
85 } |
|
86 |
|
87 private static int count(List<RecordedEvent> events, Class<?> eventClass) { |
|
88 int count = 0; |
|
89 for (RecordedEvent e : events) { |
|
90 if (e.getEventType().getName().equals(eventClass.getName())) { |
|
91 count++; |
|
92 } |
|
93 } |
|
94 return count; |
|
95 } |
|
96 |
|
97 private static void testAsync() throws InterruptedException, Exception { |
51 CountDownLatch latchOne = new CountDownLatch(1); |
98 CountDownLatch latchOne = new CountDownLatch(1); |
52 CountDownLatch latchTwo = new CountDownLatch(2); |
99 CountDownLatch latchTwo = new CountDownLatch(2); |
53 AtomicBoolean fail = new AtomicBoolean(); |
100 AtomicBoolean fail = new AtomicBoolean(); |
54 try (RecordingStream r = new RecordingStream()) { |
101 try (RecordingStream r = new RecordingStream()) { |
55 r.onEvent(e -> { |
102 r.onEvent(e -> { |