test/jdk/jdk/jfr/api/consumer/recordingstream/TestSetStartTime.java
branchJEP-349-branch
changeset 57628 f5f590eaecf5
child 57690 9316d02dd4a5
equal deleted inserted replaced
57627:b38c7a822244 57628:f5f590eaecf5
       
     1 /*
       
     2  * Copyright (c) 2019, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package jdk.jfr.api.consumer.recordingstream;
       
    26 
       
    27 import java.time.Duration;
       
    28 import java.time.Instant;
       
    29 import java.util.concurrent.atomic.AtomicBoolean;
       
    30 
       
    31 import jdk.jfr.Event;
       
    32 import jdk.jfr.Name;
       
    33 import jdk.jfr.Recording;
       
    34 import jdk.jfr.StackTrace;
       
    35 import jdk.jfr.consumer.EventStream;
       
    36 
       
    37 /**
       
    38  * @test
       
    39  * @summary Tests RecordingStrream::setStartTime
       
    40  * @key jfr
       
    41  * @requires vm.hasJFR
       
    42  * @library /test/lib
       
    43  * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestSetStartTime
       
    44  */
       
    45 public final class TestSetStartTime {
       
    46 
       
    47     @Name("Mark")
       
    48     @StackTrace(false)
       
    49     public final static class Mark extends Event {
       
    50         public boolean before;
       
    51     }
       
    52 
       
    53     public static void main(String... args) throws Exception {
       
    54         try (Recording r = new Recording()) {
       
    55             r.setFlushInterval(Duration.ofSeconds(1));
       
    56             r.start();
       
    57             Mark event1 = new Mark();
       
    58             event1.before = true;
       
    59             event1.commit();
       
    60 
       
    61             Instant now = Instant.now();
       
    62             System.out.println("Start time was " + now);
       
    63             Mark event2 = new Mark();
       
    64             event2.before = false;
       
    65             event2.commit();
       
    66             AtomicBoolean error = new AtomicBoolean();
       
    67             try (EventStream d = EventStream.openRepository()) {
       
    68                 d.setStartTime(now);
       
    69                 d.onEvent(e -> {
       
    70                     System.out.println(e);
       
    71                     boolean early = e.getBoolean("before");
       
    72                     if (early) {
       
    73                         error.set(true);
       
    74                     } else {
       
    75                         // OK, as expected
       
    76                         d.close();
       
    77                     }
       
    78                 });
       
    79                 d.start();
       
    80                 if (error.get()) {
       
    81                     throw new Exception("Found unexpected event!");
       
    82                 }
       
    83             }
       
    84         }
       
    85     }
       
    86 }