test/jdk/jdk/jfr/api/consumer/recordingstream/TestSetEndTime.java
branchJEP-349-branch
changeset 57690 9316d02dd4a5
child 57848 bd961c64ecce
equal deleted inserted replaced
57641:5fb8ececb9e6 57690:9316d02dd4a5
       
     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 EventStream::setEndTime
       
    40  * @key jfr
       
    41  * @requires vm.hasJFR
       
    42  * @library /test/lib
       
    43  * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestSetEndTime
       
    44  */
       
    45 public final class TestSetEndTime {
       
    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             Instant start = Instant.now();
       
    58             System.out.println("Instant.start() = " + start);
       
    59             Thread.sleep(2000);
       
    60             Mark event1 = new Mark();
       
    61             event1.before = true;
       
    62             event1.commit();
       
    63             Thread.sleep(2000);
       
    64             Instant end = Instant.now();
       
    65             System.out.println("Instant.end() = " + end);
       
    66             Thread.sleep(2000);
       
    67             Mark event2 = new Mark();
       
    68             event2.before = false;
       
    69             event2.commit();
       
    70             AtomicBoolean error = new AtomicBoolean(true);
       
    71             try (EventStream d = EventStream.openRepository()) {
       
    72                 d.setStartTime(start); // needed so we don't start after end time
       
    73                 d.setEndTime(end);
       
    74                 d.onEvent(e -> {
       
    75                     System.out.println(e);
       
    76                     boolean before = e.getBoolean("before");
       
    77                     if (before) {
       
    78                         error.set(false);
       
    79                     } else {
       
    80                         error.set(true);
       
    81                     }
       
    82                 });
       
    83                 d.start();
       
    84                 if (error.get()) {
       
    85                     throw new Exception("Found unexpected event!");
       
    86                 }
       
    87             }
       
    88         }
       
    89     }
       
    90 }