25 |
25 |
26 package jdk.jfr.api.consumer.recordingstream; |
26 package jdk.jfr.api.consumer.recordingstream; |
27 |
27 |
28 import java.util.concurrent.CountDownLatch; |
28 import java.util.concurrent.CountDownLatch; |
29 import java.util.concurrent.atomic.AtomicBoolean; |
29 import java.util.concurrent.atomic.AtomicBoolean; |
|
30 import java.util.function.Consumer; |
30 |
31 |
31 import jdk.jfr.Event; |
32 import jdk.jfr.Event; |
32 import jdk.jfr.consumer.RecordingStream; |
33 import jdk.jfr.consumer.RecordingStream; |
33 |
34 |
34 /** |
35 /** |
46 |
47 |
47 private static class EnabledEvent extends Event { |
48 private static class EnabledEvent extends Event { |
48 } |
49 } |
49 |
50 |
50 public static void main(String... args) throws Exception { |
51 public static void main(String... args) throws Exception { |
|
52 testDisableWithClass(); |
|
53 testDisableWithEventName(); |
|
54 } |
|
55 |
|
56 private static void testDisableWithEventName() { |
|
57 test(r -> r.disable(DisabledEvent.class.getName())); |
|
58 } |
|
59 |
|
60 private static void testDisableWithClass() { |
|
61 test(r -> r.disable(DisabledEvent.class)); |
|
62 } |
|
63 |
|
64 private static void test(Consumer<RecordingStream> disablement) { |
51 CountDownLatch twoEvent = new CountDownLatch(2); |
65 CountDownLatch twoEvent = new CountDownLatch(2); |
52 AtomicBoolean fail = new AtomicBoolean(false); |
66 AtomicBoolean fail = new AtomicBoolean(false); |
53 try(RecordingStream r = new RecordingStream()) { |
67 try(RecordingStream r = new RecordingStream()) { |
54 r.onEvent(e -> { |
68 r.onEvent(e -> { |
55 if (e.getEventType().getName().equals(DisabledEvent.class.getName())) { |
69 if (e.getEventType().getName().equals(DisabledEvent.class.getName())) { |
56 fail.set(true); |
70 fail.set(true); |
57 } |
71 } |
58 twoEvent.countDown(); |
72 twoEvent.countDown(); |
59 }); |
73 }); |
60 r.disable(DisabledEvent.class.getName()); |
74 disablement.accept(r); |
61 r.startAsync(); |
75 r.startAsync(); |
62 EnabledEvent e1 = new EnabledEvent(); |
76 EnabledEvent e1 = new EnabledEvent(); |
63 e1.commit(); |
77 e1.commit(); |
64 DisabledEvent d1 = new DisabledEvent(); |
78 DisabledEvent d1 = new DisabledEvent(); |
65 d1.commit(); |
79 d1.commit(); |
66 EnabledEvent e2 = new EnabledEvent(); |
80 EnabledEvent e2 = new EnabledEvent(); |
67 e2.commit(); |
81 e2.commit(); |
68 twoEvent.await(); |
82 try { |
|
83 twoEvent.await(); |
|
84 } catch (InterruptedException ie) { |
|
85 throw new RuntimeException("Unexpexpected interruption of thread", ie); |
|
86 } |
69 if (fail.get()) { |
87 if (fail.get()) { |
70 throw new Exception("Should not receive a disabled event"); |
88 throw new RuntimeException("Should not receive a disabled event"); |
71 } |
89 } |
72 } |
90 } |
73 } |
91 } |
74 } |
92 } |