1+ /*
2+ * Copyright (c) 2025, 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.
8+ *
9+ * This code is distributed in the hope that it will be useful, but WITHOUT
10+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+ * version 2 for more details (a copy is included in the LICENSE file that
13+ * accompanied this code).
14+ *
15+ * You should have received a copy of the GNU General Public License version
16+ * 2 along with this work; if not, write to the Free Software Foundation,
17+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+ *
19+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+ * or visit www.oracle.com if you need additional information or have any
21+ * questions.
22+ */
23+ package jdk .jfr .jmx .streaming ;
24+
25+ import java .lang .management .ManagementFactory ;
26+ import java .time .Duration ;
27+ import java .util .Collections ;
28+ import java .util .HashSet ;
29+ import java .util .Set ;
30+ import java .util .function .Consumer ;
31+ import java .util .function .Predicate ;
32+
33+ import javax .management .MBeanServerConnection ;
34+
35+ import jdk .jfr .Event ;
36+ import jdk .jfr .EventSettings ;
37+ import jdk .jfr .FlightRecorder ;
38+ import jdk .jfr .Name ;
39+ import jdk .jfr .Period ;
40+ import jdk .jfr .StackTrace ;
41+ import jdk .jfr .Threshold ;
42+ import jdk .jfr .consumer .RecordedEvent ;
43+ import jdk .jfr .consumer .RecordedStackTrace ;
44+ import jdk .management .jfr .RemoteRecordingStream ;
45+
46+ /**
47+ * @test
48+ * @requires vm.flagless
49+ * @summary Tests that event settings for a RemoteRecordingStream can be changed
50+ * @requires vm.hasJFR
51+ * @library /test/lib /test/jdk
52+ * @run main/othervm jdk.jfr.jmx.streaming.TestWithers
53+ */
54+ public class TestWithers {
55+ private static final Set <String > RESULT = Collections .synchronizedSet (new HashSet <>());
56+
57+ @ Name ("AA" )
58+ @ StackTrace (false )
59+ static class A extends Event {
60+ }
61+
62+ @ Name ("BB" )
63+ @ StackTrace (true )
64+ static class B extends Event {
65+ }
66+
67+ @ Name ("CC" )
68+ @ Threshold ("10 h" )
69+ static class C extends Event {
70+ }
71+
72+ @ Name ("DD" )
73+ @ Threshold ("10 h" )
74+ static class D extends Event {
75+ }
76+
77+ @ Name ("EE" )
78+ @ StackTrace (false )
79+ static class E extends Event {
80+ }
81+
82+ @ Name ("FF" )
83+ @ Period ("10 h" )
84+ static class F extends Event {
85+ }
86+
87+ public static void main (String ... args ) throws Exception {
88+ MBeanServerConnection conn = ManagementFactory .getPlatformMBeanServer ();
89+ try (RemoteRecordingStream stream = new RemoteRecordingStream (conn )) {
90+ addCheck (stream , es -> es .withStackTrace (), "AA" , TestWithers ::hasStackTrace );
91+ addCheck (stream , es -> es .withoutStackTrace (), "BB" , e -> !hasStackTrace (e ));
92+ addCheck (stream , es -> es .withThreshold (Duration .ofMillis (0 )), "CC" , e -> true );
93+ addCheck (stream , es -> es .withoutThreshold (), "DD" , e -> true );
94+ addCheck (stream , es -> es .with ("stackTrace" , "true" ), "EE" , TestWithers ::hasStackTrace );
95+ addCheck (stream , es -> es .withPeriod (Duration .ofMillis (700 )), "FF" , e -> true );
96+ FlightRecorder .addPeriodicEvent (F .class , () -> {
97+ F f = new F ();
98+ f .commit ();
99+ });
100+ stream .onFlush (() -> {
101+ System .out .println (RESULT );
102+ if (RESULT .size () == 6 ) {
103+ stream .close ();
104+ }
105+ });
106+
107+ stream .startAsync ();
108+ A a = new A ();
109+ a .commit ();
110+
111+ B b = new B ();
112+ b .commit ();
113+
114+ C c = new C ();
115+ c .commit ();
116+
117+ D d = new D ();
118+ d .commit ();
119+
120+ E e = new E ();
121+ e .commit ();
122+
123+ stream .awaitTermination ();
124+ }
125+ }
126+
127+ private static void addCheck (RemoteRecordingStream stream , Consumer <EventSettings > es , String eventName , Predicate <RecordedEvent > validator ) {
128+ es .accept (stream .enable (eventName ));
129+ stream .onEvent (eventName , e -> {
130+ System .out .println (e );
131+ if (validator .test (e )) {
132+ RESULT .add (eventName );
133+ }
134+ });
135+ }
136+
137+ private static boolean hasStackTrace (RecordedEvent e ) {
138+ RecordedStackTrace rs = e .getStackTrace ();
139+ return rs != null && !rs .getFrames ().isEmpty ();
140+ }
141+ }
0 commit comments