test/jdk/jdk/jfr/api/consumer/recordingstream/TestSetEndTime.java
branchJEP-349-branch
changeset 58862 abeb87af766e
parent 58771 e6feb2874fa6
equal deleted inserted replaced
58855:83c307e00630 58862:abeb87af766e
    54 public final class TestSetEndTime {
    54 public final class TestSetEndTime {
    55 
    55 
    56     @Name("Mark")
    56     @Name("Mark")
    57     @StackTrace(false)
    57     @StackTrace(false)
    58     public final static class Mark extends Event {
    58     public final static class Mark extends Event {
    59         public boolean before;
    59         public boolean include;
       
    60         public int id;
    60     }
    61     }
    61 
    62 
    62     public static void main(String... args) throws Exception {
    63     public static void main(String... args) throws Exception {
    63         testEventStream();
    64         testEventStream();
    64         testRecordingStream();
    65         testRecordingStream();
    96             }
    97             }
    97         }
    98         }
    98     }
    99     }
    99 
   100 
   100     static void testEventStream() throws InterruptedException, IOException, Exception {
   101     static void testEventStream() throws InterruptedException, IOException, Exception {
   101         try (Recording r = new Recording()) {
   102         while (true) {
   102             r.setFlushInterval(Duration.ofSeconds(1));
   103             try (Recording r = new Recording()) {
   103             r.start();
   104                 r.start();
   104             Mark event1 = new Mark();
       
   105             event1.begin(); // start time
       
   106             event1.before = true;
       
   107             advanceClock();
       
   108             event1.commit();
       
   109 
   105 
   110             Mark event2 = new Mark();
   106                 Mark event1 = new Mark();
   111             event2.begin(); // end time
   107                 event1.id = 1;
   112             advanceClock();
   108                 event1.include = false;
   113             Thread.sleep(100);
   109                 event1.commit(); // start time
   114             event2.before = false;
       
   115             event2.commit();
       
   116 
   110 
   117             Path p = Paths.get("recording.jfr");
   111                 nap();
   118             r.dump(p);
   112 
   119             Instant start = null;
   113                 Mark event2 = new Mark();
   120             Instant end = null;
   114                 event2.id = 2;
   121             for (RecordedEvent e : RecordingFile.readAllEvents(p)) {
   115                 event2.include = true;
   122                 if (e.getBoolean("before")) {
   116                 event2.commit();
   123                     start = e.getStartTime();
   117 
   124                     System.out.println("Start: " + start);
   118                 nap();
       
   119 
       
   120                 Mark event3 = new Mark();
       
   121                 event3.id = 3;
       
   122                 event3.include = false;
       
   123                 event3.commit(); // end time
       
   124 
       
   125                 Path p = Paths.get("recording.jfr");
       
   126                 r.dump(p);
       
   127                 Instant start = null;
       
   128                 Instant end = null;
       
   129                 System.out.println("Find start and end time as instants:");
       
   130                 for (RecordedEvent e : RecordingFile.readAllEvents(p)) {
       
   131                     if (e.getInt("id") == 1) {
       
   132                         start = e.getEndTime();
       
   133                         System.out.println("Start  : " + start);
       
   134                     }
       
   135                     if (e.getInt("id") == 2) {
       
   136                         Instant middle = e.getEndTime();
       
   137                         System.out.println("Middle : " + middle);
       
   138                     }
       
   139                     if (e.getInt("id") == 3) {
       
   140                         end = e.getEndTime();
       
   141                         System.out.println("End    : " + end);
       
   142                     }
   125                 }
   143                 }
   126                 if (!e.getBoolean("before")) {
   144                 System.out.println();
   127                     end = e.getStartTime();
   145                 System.out.println("Opening stream between " + start + " and " + end);
   128                     System.out.println("End  : " + end);
   146                 AtomicBoolean success = new AtomicBoolean(false);
   129                 }
   147                 AtomicInteger eventsCount = new AtomicInteger();
   130             }
   148                 try (EventStream d = EventStream.openRepository()) {
   131             System.out.println("===================");
   149                     d.setStartTime(start.plusNanos(1));
   132             AtomicBoolean error = new AtomicBoolean(true);
   150                     // Stream should close when end is reached
   133             try (EventStream d = EventStream.openRepository()) {
   151                     d.setEndTime(end.minusNanos(1));
   134                 d.setStartTime(start);
   152                     d.onEvent(e -> {
   135                 d.setEndTime(end);
   153                         eventsCount.incrementAndGet();
   136                 d.onEvent(e -> {
   154                         boolean include = e.getBoolean("include");
   137                     System.out.println(e);
   155                         System.out.println("Event " + e.getEndTime() + " include=" + include);
   138                     System.out.println("Event:");
   156                         if (include) {
   139                     System.out.println(e.getStartTime());
   157                             success.set(true);
   140                     System.out.println(e.getEndTime());
   158                         }
   141                     System.out.println(e.getBoolean("before"));
   159                     });
   142                     System.out.println();
   160                     d.start();
   143                     boolean before = e.getBoolean("before");
   161                     if (eventsCount.get() == 1 && success.get()) {
   144                     if (before) {
   162                         return;
   145                         error.set(false);
       
   146                     } else {
       
   147                         error.set(true);
       
   148                     }
   163                     }
   149                 });
       
   150                 d.start();
       
   151                 if (error.get()) {
       
   152                     throw new Exception("Found unexpected event!");
       
   153                 }
   164                 }
   154             }
   165             }
   155         }
   166         }
       
   167 
   156     }
   168     }
   157 
   169 
   158     private static void advanceClock() {
   170     private static void nap() throws InterruptedException {
   159         // Wait for some clock movement with
   171         // Ensure we advance at least 1 ns with fast time
   160         // java.time clock resolution.
   172         Thread.sleep(1);
   161         Instant now = Instant.now();
       
   162         while (Instant.now().equals(now)) {
       
   163         }
       
   164     }
   173     }
       
   174 
   165 }
   175 }