< prev index next >

test/jdk/jdk/jfr/event/runtime/TestThreadSleepEvent.java

Print this page
*** 25,46 ***
  
  import static jdk.test.lib.Asserts.assertTrue;
  
  import java.time.Duration;
  import java.util.List;
  
  import jdk.jfr.Recording;
  import jdk.jfr.consumer.RecordedEvent;
  import jdk.test.lib.jfr.EventNames;
  import jdk.test.lib.jfr.Events;
  
  /**
   * @test
   * @key jfr
   * @requires vm.hasJFR
   * @library /test/lib
!  * @run main/othervm jdk.jfr.event.runtime.TestThreadSleepEvent
   */
  public class TestThreadSleepEvent {
  
      private final static String EVENT_NAME = EventNames.ThreadSleep;
      // Need to set the sleep time quite high (47 ms) since the sleep
      // time on Windows has been proved unreliable.
      // See bug 6313903
      private final static Long SLEEP_TIME_MS = new Long(47);
  
      public static void main(String[] args) throws Throwable {
-         Recording recording = new Recording();
-         recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(0));
-         recording.start();
-         Thread.sleep(SLEEP_TIME_MS);
-         recording.stop();
  
!         List<RecordedEvent> events = Events.fromRecording(recording);
!         boolean isAnyFound = false;
!         for (RecordedEvent event : events) {
!             if (event.getThread().getJavaThreadId() == Thread.currentThread().getId()) {
!                 System.out.println("Event:" + event);
!                 isAnyFound = true;
!                 Events.assertField(event, "time").equal(SLEEP_TIME_MS);
              }
          }
-         assertTrue(isAnyFound, "No matching events found");
      }
- 
  }
--- 25,64 ---
  
  import static jdk.test.lib.Asserts.assertTrue;
  
  import java.time.Duration;
  import java.util.List;
+ import java.util.concurrent.ThreadFactory;
  
  import jdk.jfr.Recording;
  import jdk.jfr.consumer.RecordedEvent;
+ import jdk.test.lib.Asserts;
  import jdk.test.lib.jfr.EventNames;
  import jdk.test.lib.jfr.Events;
  
  /**
   * @test
   * @key jfr
   * @requires vm.hasJFR
   * @library /test/lib
!  * @compile --enable-preview -source ${jdk.version} TestThreadSleepEvent.java
+  * @run main/othervm --enable-preview jdk.jfr.event.runtime.TestThreadSleepEvent
   */
  public class TestThreadSleepEvent {
  
      private final static String EVENT_NAME = EventNames.ThreadSleep;
      // Need to set the sleep time quite high (47 ms) since the sleep
      // time on Windows has been proved unreliable.
      // See bug 6313903
      private final static Long SLEEP_TIME_MS = new Long(47);
  
      public static void main(String[] args) throws Throwable {
  
!         try (Recording recording = new Recording()) {
!             recording.enable(EVENT_NAME).withoutThreshold().withStackTrace();
!             recording.start();
!             Thread.sleep(SLEEP_TIME_MS);
!             Thread virtualThread  = Thread.ofVirtual().start(() -> {
!                 try {
!                     Thread.sleep(SLEEP_TIME_MS);
+                 } catch (InterruptedException ie) {
+                     throw new RuntimeException(ie);
+                 }
+             });
+             virtualThread.join();
+             recording.stop();
+ 
+             int threadCount = 2;
+             List<RecordedEvent> events = Events.fromRecording(recording);
+             Asserts.assertEquals(events.size(), threadCount);
+             for (RecordedEvent event : events) {
+                 System.out.println(event);
+                 System.out.println(event.getStackTrace());
+                 if (event.getThread().getJavaThreadId() == Thread.currentThread().getId()) {
+                     threadCount--;
+                     Events.assertDuration(event, "time", Duration.ofMillis(SLEEP_TIME_MS));
+                 }
+                 if (event.getThread().getJavaThreadId() == virtualThread.getId()) {
+                     threadCount--;
+                     Events.assertDuration(event, "time", Duration.ofMillis(SLEEP_TIME_MS));
+                 }
              }
+             Asserts.assertEquals(threadCount, 0, "Could not find all expected events");
          }
      }
  }
< prev index next >