2626
2727package com .oracle .svm .test .jfr ;
2828
29- import static org .junit .Assert .assertEquals ;
3029import static org .junit .Assert .assertTrue ;
3130
3231import java .lang .management .ManagementFactory ;
@@ -47,6 +46,9 @@ public class TestThreadCPULoadEvent extends JfrRecordingTest {
4746 private static final int TIMEOUT = 10000 ;
4847 private static final String THREAD_NAME_1 = "Thread-1" ;
4948 private static final String THREAD_NAME_2 = "Thread-2" ;
49+ private static final String THREAD_NAME_3 = "Thread-3" ;
50+ private static Thread thread3 = null ;
51+ private static volatile boolean finished = false ;
5052
5153 @ Test
5254 public void test () throws Throwable {
@@ -55,15 +57,21 @@ public void test() throws Throwable {
5557
5658 WeakReference <Thread > thread1 = createAndStartBusyWaitThread (THREAD_NAME_1 , 10 , 250 );
5759 WeakReference <Thread > thread2 = createAndStartBusyWaitThread (THREAD_NAME_2 , 250 , 10 );
60+ thread3 = createAndStartLongLived (THREAD_NAME_3 );
5861
5962 waitUntilCollected (thread1 );
6063 waitUntilCollected (thread2 );
6164
65+ /**
66+ * Thread 3 should be alive at the time of final chunk rotation to test whether this event
67+ * is emitted correctly upon chunk end.
68+ */
6269 stopRecording (recording , TestThreadCPULoadEvent ::validateEvents );
70+ finished = true ;
6371 }
6472
6573 private static void validateEvents (List <RecordedEvent > events ) {
66- assertEquals ( 2 , events . size ());
74+ assertTrue ( thread3 . isAlive ());
6775 Map <String , Float > userTimes = new HashMap <>();
6876 Map <String , Float > cpuTimes = new HashMap <>();
6977
@@ -73,11 +81,10 @@ private static void validateEvents(List<RecordedEvent> events) {
7381 float systemTime = e .<Float > getValue ("system" );
7482 assertTrue ("User time is outside 0..1 range" , 0.0 <= userTime && userTime <= 1.0 );
7583 assertTrue ("System time is outside 0..1 range" , 0.0 <= systemTime && systemTime <= 1.0 );
76-
7784 userTimes .put (threadName , userTime );
7885 cpuTimes .put (threadName , userTime + systemTime );
7986 }
80-
87+ assertTrue ( cpuTimes . containsKey ( THREAD_NAME_3 ));
8188 assertTrue (userTimes .get (THREAD_NAME_1 ) < userTimes .get (THREAD_NAME_2 ));
8289 assertTrue (cpuTimes .get (THREAD_NAME_1 ) < cpuTimes .get (THREAD_NAME_2 ));
8390 }
@@ -92,6 +99,21 @@ private static WeakReference<Thread> createAndStartBusyWaitThread(String name, i
9299 return new WeakReference <>(thread );
93100 }
94101
102+ private static Thread createAndStartLongLived (String name ) {
103+ Thread thread = new Thread (() -> {
104+ while (!finished ) {
105+ try {
106+ Thread .sleep (10 );
107+ } catch (InterruptedException e ) {
108+ throw new RuntimeException (e );
109+ }
110+ }
111+ });
112+ thread .setName (name );
113+ thread .start ();
114+ return thread ;
115+ }
116+
95117 private static void busyWait (long waitMs ) {
96118 ThreadMXBean mxBean = ManagementFactory .getThreadMXBean ();
97119 long timeout = System .currentTimeMillis () + TIMEOUT ;
0 commit comments