test/jdk/jdk/jfr/api/flightrecorder/TestGetSettings.java
changeset 50113 caf115bb98ad
child 51214 67736b4846a0
equal deleted inserted replaced
50112:7a2a740815b7 50113:caf115bb98ad
       
     1 /*
       
     2  * Copyright (c) 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 
       
    26 package jdk.jfr.api.flightrecorder;
       
    27 
       
    28 import static jdk.test.lib.Asserts.assertEquals;
       
    29 import static jdk.test.lib.Asserts.assertTrue;
       
    30 
       
    31 import java.time.Duration;
       
    32 import java.util.Map;
       
    33 
       
    34 import jdk.jfr.Event;
       
    35 import jdk.jfr.EventType;
       
    36 import jdk.jfr.Recording;
       
    37 
       
    38 /*
       
    39  * @test
       
    40  * @key jfr
       
    41  * @library /test/lib
       
    42  * @run main/othervm jdk.jfr.api.flightrecorder.TestGetSettings
       
    43  */
       
    44 public class TestGetSettings {
       
    45 
       
    46     public static void main(String[] args) throws Throwable {
       
    47         final long minThresholdNanos = 1000000;
       
    48         final String dummyEventPath = "mydummy/event/path";
       
    49         final String myEventSettingName = String.valueOf(EventType.getEventType(MyEvent.class).getId());
       
    50         System.out.println("myEventSettingName=" + myEventSettingName);
       
    51 
       
    52         // Settings should be merged to include the most number of events (minimum threshold).
       
    53         Recording r1 = new Recording();
       
    54         r1.enable(MyEvent.class).withThreshold(Duration.ofNanos(minThresholdNanos * 3));
       
    55         r1.enable(MyEvent.class).withThreshold(Duration.ofNanos(minThresholdNanos * 2));
       
    56         r1.enable(dummyEventPath).withThreshold(Duration.ofNanos(minThresholdNanos));
       
    57         r1.start();
       
    58 
       
    59         ExpectedSetting[] expectedR1 = {
       
    60             new ExpectedSetting(myEventSettingName, "enabled", "true"),
       
    61             new ExpectedSetting(myEventSettingName, "threshold", Long.toString(minThresholdNanos * 2) + " ns"),
       
    62             new ExpectedSetting(dummyEventPath, "enabled", "true"),
       
    63             new ExpectedSetting(dummyEventPath, "threshold", Long.toString(minThresholdNanos) + " ns"),
       
    64         };
       
    65 
       
    66         verifySettings(r1.getSettings(), expectedR1);
       
    67 
       
    68         // Start another recording. Recorder settings should be merged from both recordings.
       
    69         Recording r2 = new Recording();
       
    70         r2.enable(MyEvent.class).withThreshold(Duration.ofNanos(minThresholdNanos));
       
    71         r2.disable(dummyEventPath);
       
    72         r2.start();
       
    73 
       
    74         ExpectedSetting[] expectedR2 = {
       
    75             new ExpectedSetting(myEventSettingName, "enabled", "true"),
       
    76             new ExpectedSetting(myEventSettingName, "threshold", Long.toString(minThresholdNanos) + " ns"),
       
    77             new ExpectedSetting(dummyEventPath, "enabled", "false")
       
    78         };
       
    79 
       
    80         verifySettings(r1.getSettings(), expectedR1);
       
    81         verifySettings(r2.getSettings(), expectedR2);
       
    82 
       
    83         // Stop first recording. Recorder should use settings from r2.
       
    84         r1.stop();
       
    85         verifySettings(r2.getSettings(), expectedR2);
       
    86 
       
    87         r2.stop();
       
    88         r1.close();
       
    89         r2.close();
       
    90     }
       
    91 
       
    92     private static void verifySettings(Map<String, String> settings, ExpectedSetting ... expectedSettings) {
       
    93         for (String name : settings.keySet()) {
       
    94             System.out.printf("Settings: %s=%s%n", name, settings.get(name));
       
    95         }
       
    96         for (ExpectedSetting expected : expectedSettings) {
       
    97             boolean isFound = false;
       
    98             for (String name : settings.keySet()) {
       
    99                 if (name.contains(expected.name) && name.contains(expected.option)) {
       
   100                     final String value = settings.get(name);
       
   101                     String msg = String.format("got: %s=%s, expected: %s", name, value, expected.toString());
       
   102                     assertEquals(value, expected.value, msg);
       
   103                     System.out.println("OK: " + msg);
       
   104                     isFound = true;
       
   105                     break;
       
   106                 }
       
   107             }
       
   108             assertTrue(isFound, "Missing setting " + expected.toString());
       
   109         }
       
   110     }
       
   111 
       
   112     private static class MyEvent extends Event {
       
   113     }
       
   114 
       
   115     private static class ExpectedSetting {
       
   116         String name;
       
   117         String option;
       
   118         String value;
       
   119 
       
   120         public ExpectedSetting(String name, String option, String value) {
       
   121             this.name = name;
       
   122             this.option = option;
       
   123             this.value = value;
       
   124         }
       
   125 
       
   126         @Override
       
   127         public String toString() {
       
   128             return String.format("name=%s, option=%s, value=%s", name, option, value);
       
   129         }
       
   130     }
       
   131 }