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; |
28 import java.util.List; |
29 import java.util.concurrent.CompletableFuture; |
|
30 import java.util.concurrent.CountDownLatch; |
29 import java.util.concurrent.CountDownLatch; |
31 import java.util.concurrent.atomic.AtomicBoolean; |
30 import java.util.concurrent.atomic.AtomicBoolean; |
32 |
31 |
33 import jdk.jfr.Event; |
32 import jdk.jfr.Event; |
34 import jdk.jfr.Recording; |
33 import jdk.jfr.Recording; |
50 } |
49 } |
51 |
50 |
52 public static class Recorded extends Event { |
51 public static class Recorded extends Event { |
53 } |
52 } |
54 |
53 |
|
54 public static class Provoker extends Event { |
|
55 } |
|
56 |
55 public static void main(String... args) throws Exception { |
57 public static void main(String... args) throws Exception { |
|
58 testSync(); |
56 testAsync(); |
59 testAsync(); |
57 testSync(); |
60 } |
|
61 |
|
62 private static void emit(AtomicBoolean stop) { |
|
63 Runnable r = () -> { |
|
64 while (!stop.get()) { |
|
65 try { |
|
66 Thread.sleep(1000); |
|
67 } catch (InterruptedException e) { |
|
68 } |
|
69 Provoker e = new Provoker(); |
|
70 e.commit(); |
|
71 } |
|
72 }; |
|
73 Thread t = new Thread(r); |
|
74 t.start(); |
58 } |
75 } |
59 |
76 |
60 private static void testSync() throws Exception { |
77 private static void testSync() throws Exception { |
61 try (Recording r = new Recording()) { |
78 try (Recording r = new Recording()) { |
62 r.start(); |
79 r.start(); |
63 Recorded e1 = new Recorded(); |
80 AtomicBoolean stop = new AtomicBoolean(false); |
64 e1.commit(); |
81 emit(stop); |
65 try (RecordingStream rs = new RecordingStream()) { |
82 try (RecordingStream rs = new RecordingStream()) { |
|
83 Recorded e1 = new Recorded(); |
|
84 e1.commit(); |
66 rs.onEvent(e -> { |
85 rs.onEvent(e -> { |
67 NotRecorded e2 = new NotRecorded(); |
86 if (!stop.get()) { |
68 e2.commit(); |
87 System.out.println("Emitting NotRecorded event"); |
|
88 NotRecorded event = new NotRecorded(); |
|
89 event.commit(); |
|
90 System.out.println("Stopping event provoker"); |
|
91 stop.set(true); |
|
92 System.out.println("Closing recording stream"); |
|
93 rs.close(); |
|
94 return; |
|
95 } |
69 }); |
96 }); |
70 CompletableFuture.runAsync(() -> { |
97 rs.start(); |
71 r.start(); |
98 Recorded e2 = new Recorded(); |
72 }); |
99 e2.commit(); |
73 } |
100 } |
74 Recorded e3 = new Recorded(); |
|
75 e3.commit(); |
|
76 r.stop(); |
101 r.stop(); |
77 List<RecordedEvent> events = Events.fromRecording(r); |
102 List<RecordedEvent> events = Events.fromRecording(r); |
|
103 System.out.println(events); |
78 if (count(events, NotRecorded.class) != 0) { |
104 if (count(events, NotRecorded.class) != 0) { |
79 throw new Exception("Expected 0 NotRecorded events"); |
105 throw new Exception("Expected 0 NotRecorded events"); |
80 } |
106 } |
81 if (count(events, Recorded.class) != 2) { |
107 if (count(events, Recorded.class) == 2) { |
82 throw new Exception("Expected 2 Recorded events"); |
108 throw new Exception("Expected 2 Recorded events"); |
83 } |
109 } |
84 } |
110 } |
85 } |
111 } |
86 |
112 |
89 for (RecordedEvent e : events) { |
115 for (RecordedEvent e : events) { |
90 if (e.getEventType().getName().equals(eventClass.getName())) { |
116 if (e.getEventType().getName().equals(eventClass.getName())) { |
91 count++; |
117 count++; |
92 } |
118 } |
93 } |
119 } |
|
120 System.out.println(count); |
94 return count; |
121 return count; |
95 } |
122 } |
96 |
123 |
97 private static void testAsync() throws InterruptedException, Exception { |
124 private static void testAsync() throws InterruptedException, Exception { |
98 CountDownLatch latchOne = new CountDownLatch(1); |
125 CountDownLatch latchOne = new CountDownLatch(1); |