test/jdk/jdk/jfr/api/consumer/recordingstream/TestSetStartTime.java
branchJEP-349-branch
changeset 58765 1256af493619
parent 58076 ca625d28c580
child 58770 481870c3d258
equal deleted inserted replaced
58757:df9b65cc99e8 58765:1256af493619
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package jdk.jfr.api.consumer.recordingstream;
    26 package jdk.jfr.api.consumer.recordingstream;
    27 
    27 
       
    28 import java.io.IOException;
    28 import java.time.Duration;
    29 import java.time.Duration;
    29 import java.time.Instant;
    30 import java.time.Instant;
    30 import java.util.concurrent.atomic.AtomicBoolean;
    31 import java.util.concurrent.atomic.AtomicBoolean;
    31 
    32 
    32 import jdk.jfr.Event;
    33 import jdk.jfr.Event;
    33 import jdk.jfr.Name;
    34 import jdk.jfr.Name;
    34 import jdk.jfr.Recording;
    35 import jdk.jfr.Recording;
    35 import jdk.jfr.StackTrace;
    36 import jdk.jfr.StackTrace;
    36 import jdk.jfr.consumer.EventStream;
    37 import jdk.jfr.consumer.EventStream;
       
    38 import jdk.jfr.consumer.RecordingStream;
    37 
    39 
    38 /**
    40 /**
    39  * @test
    41  * @test
    40  * @summary Tests EventStream::setStartTime
    42  * @summary Tests EventStream::setStartTime
    41  * @key jfr
    43  * @key jfr
    43  * @library /test/lib
    45  * @library /test/lib
    44  * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestSetStartTime
    46  * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestSetStartTime
    45  */
    47  */
    46 public final class TestSetStartTime {
    48 public final class TestSetStartTime {
    47 
    49 
       
    50     private final static int SLEEP_TIME_MS = 100;
       
    51 
    48     @Name("Mark")
    52     @Name("Mark")
    49     @StackTrace(false)
    53     @StackTrace(false)
    50     public final static class Mark extends Event {
    54     public final static class Mark extends Event {
    51         public boolean before;
    55         public boolean before;
    52     }
    56     }
    53 
    57 
    54     public static void main(String... args) throws Exception {
    58     public static void main(String... args) throws Exception {
    55         try (Recording r = new Recording()) {
    59         testEventStream();
    56             r.setFlushInterval(Duration.ofSeconds(1));
    60         testRecordingStream();
    57             r.start();
    61     }
    58             Mark event1 = new Mark();
    62 
    59             event1.before = true;
    63     private static void testRecordingStream() throws InterruptedException {
    60             event1.commit();
    64         AtomicBoolean exit = new AtomicBoolean();
    61             Thread.sleep(2000);
    65         int attempt = 1;
    62             Instant now = Instant.now();
    66         while (!exit.get()) {
    63             System.out.println("Instant.now() = " + now);
    67             System.out.println("Testing RecordingStream:setStartTime(...). Attempt: " + attempt++);
    64             Thread.sleep(2000);
    68             AtomicBoolean firstEvent = new AtomicBoolean(true);
    65             Mark event2 = new Mark();
    69             try (RecordingStream r2 = new RecordingStream()) {
    66             event2.before = false;
    70                 Instant t = Instant.now().plus(Duration.ofMillis(SLEEP_TIME_MS / 2));
    67             event2.commit();
    71                 System.out.println("Setting start time: " + t);
    68             AtomicBoolean error = new AtomicBoolean();
    72                 r2.setStartTime(t);
    69             try (EventStream d = EventStream.openRepository()) {
    73                 r2.onEvent(e -> {
    70                 d.setStartTime(now);
    74                     if (firstEvent.get()) {
    71                 d.onEvent(e -> {
    75                         firstEvent.set(false);
    72                     System.out.println(e);
    76                         if (!e.getBoolean("before")) {
    73                     boolean early = e.getBoolean("before");
    77                             // Skipped first event, let's exit
    74                     if (early) {
    78                             exit.set(true);
    75                         error.set(true);
    79                         }
    76                     } else {
    80                         r2.close();
    77                         // OK, as expected
       
    78                         d.close();
       
    79                     }
    81                     }
    80                 });
    82                 });
    81                 d.start();
    83                 r2.startAsync();
    82                 if (error.get()) {
    84                 Mark m1 = new Mark();
    83                     throw new Exception("Found unexpected event!");
    85                 m1.before = true;
       
    86                 m1.commit();
       
    87                 System.out.println("First event emitted: " + Instant.now());
       
    88                 Thread.sleep(SLEEP_TIME_MS);
       
    89                 Mark m2 = new Mark();
       
    90                 m2.before = false;
       
    91                 m2.commit();
       
    92                 System.out.println("Second event emitted: " + Instant.now());
       
    93                 r2.awaitTermination();
       
    94             }
       
    95             System.out.println();
       
    96         }
       
    97     }
       
    98 
       
    99     private static void testEventStream() throws InterruptedException, Exception, IOException {
       
   100         AtomicBoolean exit = new AtomicBoolean();
       
   101         int attempt = 1;
       
   102         while (!exit.get()) {
       
   103             System.out.println("Testing EventStream:setStartTime(...). Attempt: " + attempt++);
       
   104             AtomicBoolean firstEvent = new AtomicBoolean(true);
       
   105             try (Recording r = new Recording()) {
       
   106                 r.start();
       
   107                 Mark event1 = new Mark();
       
   108                 event1.before = true;
       
   109                 event1.commit();
       
   110                 Instant t = Instant.now();
       
   111                 System.out.println("First event emitted: " + t);
       
   112                 Thread.sleep(SLEEP_TIME_MS);
       
   113                 Mark event2 = new Mark();
       
   114                 event2.before = false;
       
   115                 event2.commit();
       
   116                 System.out.println("Second event emitted: " + Instant.now());
       
   117                 AtomicBoolean error = new AtomicBoolean();
       
   118                 try (EventStream es = EventStream.openRepository()) {
       
   119                     Instant startTime = t.plus(Duration.ofMillis(SLEEP_TIME_MS / 2));
       
   120                     es.setStartTime(startTime);
       
   121                     System.out.println("Setting start time: " + startTime);
       
   122                     es.onEvent(e -> {
       
   123                         if (firstEvent.get()) {
       
   124                             firstEvent.set(false);
       
   125                             if (!e.getBoolean("before")) {
       
   126                                 // Skipped first event, let's exit
       
   127                                 exit.set(true);
       
   128                             }
       
   129                             es.close();
       
   130                         }
       
   131                     });
       
   132                     es.startAsync();
       
   133                     es.awaitTermination();
    84                 }
   134                 }
    85             }
   135             }
       
   136             System.out.println();
    86         }
   137         }
    87     }
   138     }
    88 }
   139 }