1 /*
   2  * Copyright (c) 2016, 2018, 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.event;
  26 
  27 import java.time.Duration;
  28 import java.util.List;
  29 
  30 import jdk.jfr.Recording;
  31 import jdk.jfr.consumer.RecordedEvent;
  32 import jdk.test.lib.jfr.Events;
  33 import jdk.test.lib.jfr.SimpleEvent;
  34 
  35 /**
  36  * @test
  37  * @summary Test for RecordedEvent.getDuration()
  38  * @key jfr
  39  * 
  40  * @library /lib /
  41  * @run main/othervm jdk.jfr.api.event.TestBeginEnd
  42  */
  43 public class TestBeginEnd {
  44 
  45     public static void main(String[] args) throws Exception {
  46 
  47         Recording r = new Recording();
  48         r.enable(SimpleEvent.class);
  49         r.start();
  50 
  51         // Test enabled - single commit
  52         SimpleEvent e1 = new SimpleEvent();
  53         e1.id = 1; // should be included
  54         e1.commit();
  55 
  56         // Test enabled - begin - commit
  57         SimpleEvent e2 = new SimpleEvent();
  58         e2.begin();
  59         e2.id = 2; // should be included
  60         e2.commit();
  61 
  62         // Test enabled - begin - end - commit
  63         SimpleEvent e3 = new SimpleEvent();
  64         e3.begin();
  65         e3.id = 3; // should be included
  66         e3.end();
  67         e3.commit();
  68 
  69         // Test enabled - end - commit
  70         SimpleEvent e4 = new SimpleEvent();
  71         e4.id = 4; // should be included
  72         e4.end();
  73         e4.commit();
  74 
  75         // Test half enabled - begin - commit
  76         r.disable(SimpleEvent.class);
  77         SimpleEvent e5 = new SimpleEvent();
  78         e5.begin();
  79         r.enable(SimpleEvent.class);
  80         e5.id = 5; // should be included
  81         e5.commit();
  82 
  83         // Test half enabled - begin - end - commit
  84         r.disable(SimpleEvent.class);
  85         SimpleEvent r6 = new SimpleEvent();
  86         r6.begin();
  87         r.enable(SimpleEvent.class);
  88         r6.id = 6; // should be included
  89         r6.end();
  90         r6.commit();
  91 
  92         // Test half enabled - begin - commit with high threshold
  93         r.disable(SimpleEvent.class);
  94         SimpleEvent r7 = new SimpleEvent();
  95         r7.begin();
  96         r.enable(SimpleEvent.class).withThreshold(Duration.ofDays(1));
  97         r7.id = 7; // should not be included
  98         r7.commit();
  99         r.stop();
 100 
 101         List<RecordedEvent> recordedEvents = Events.fromRecording(r);
 102         for (RecordedEvent re : recordedEvents) {
 103            Integer id =  re.getValue("id");
 104            System.out.println("Found if " + id);
 105            if (id < 1 || id > 6) {
 106                throw new Exception("Unexpected id found " + id);
 107            }
 108         }
 109         if (recordedEvents.size() != 6) {
 110             throw new Exception("Expected 6 events");
 111         }
 112     }
 113 
 114 }