1 /*
   2  * Copyright (c) 2013, 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 
  26 package jdk.jfr.jmx;
  27 
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 
  31 import jdk.management.jfr.RecordingInfo;
  32 import jdk.testlibrary.Asserts;
  33 import jdk.testlibrary.jfr.JmxHelper;
  34 
  35 /*
  36  * @test
  37  * @key jfr
  38  * @library /lib/testlibrary
  39  * @run main/othervm jdk.jfr.jmx.TestGetRecordingsMultiple
  40  */
  41 public class TestGetRecordingsMultiple {
  42 
  43     private static class TestRecording {
  44         long id;
  45         boolean isClosed;
  46         public TestRecording(long id) {
  47             this.id = id;
  48             isClosed = false;
  49         }
  50     }
  51 
  52     public static void main(String[] args) throws Throwable {
  53         List<TestRecording> testRecordings = new ArrayList<>();
  54         for (int i = 0; i < 5; ++i) {
  55             verifyExistingRecordings(testRecordings);
  56             testRecordings.add(createRecording());
  57             if (i >= 1) {
  58                 startRecording(testRecordings.get(i-1));
  59             }
  60             if (i >= 2) {
  61                 stopRecording(testRecordings.get(i-2));
  62             }
  63             if (i >= 3) {
  64                 closeRecording(testRecordings.get(i-3));
  65             }
  66         }
  67         verifyExistingRecordings(testRecordings);
  68 
  69         for (TestRecording r : testRecordings) {
  70             if (!r.isClosed) {
  71                 closeRecording(r);
  72             }
  73         }
  74         verifyExistingRecordings(testRecordings);
  75     }
  76 
  77     // Verify that all active recordings are found, but no closed recordings.
  78     private static void verifyExistingRecordings(List<TestRecording> testRecordings) {
  79         for (TestRecording testRecording : testRecordings) {
  80             RecordingInfo r = findRecording(testRecording);
  81             if (r != null) {
  82                 Asserts.assertFalse(testRecording.isClosed, "Found closed recording with id " + testRecording.id);
  83                 System.out.printf("Recording %d: %s%n", r.getId(), r.getState());
  84             } else {
  85                 Asserts.assertTrue(testRecording.isClosed, "Missing recording with id " + testRecording.id);
  86                 System.out.printf("Recording %d: CLOSED%n", testRecording.id);
  87             }
  88         }
  89     }
  90 
  91     private static RecordingInfo findRecording(TestRecording testRecording) {
  92         for (RecordingInfo r : JmxHelper.getFlighteRecorderMXBean().getRecordings()) {
  93             if (r.getId() == testRecording.id) {
  94                 return r;
  95             }
  96         }
  97         return null;
  98     }
  99 
 100     private static TestRecording createRecording() {
 101         long id = JmxHelper.getFlighteRecorderMXBean().newRecording();
 102         System.out.println("created recording " + id);
 103         return new TestRecording(id);
 104     }
 105 
 106     private static void startRecording(TestRecording rec) {
 107         System.out.println("starting recording " + rec.id);
 108         JmxHelper.getFlighteRecorderMXBean().startRecording(rec.id);
 109     }
 110 
 111     private static void stopRecording(TestRecording rec) {
 112         System.out.println("stopping recording " + rec.id);
 113         JmxHelper.getFlighteRecorderMXBean().stopRecording(rec.id);
 114     }
 115 
 116     private static void closeRecording(TestRecording rec) throws Exception {
 117         System.out.println("closing recording " + rec.id);
 118         JmxHelper.getFlighteRecorderMXBean().closeRecording(rec.id);
 119         rec.isClosed = true;
 120 
 121     }
 122 }