test/jdk/jdk/jfr/jcmd/TestJcmdConfigure.java
changeset 50113 caf115bb98ad
child 51214 67736b4846a0
equal deleted inserted replaced
50112:7a2a740815b7 50113:caf115bb98ad
       
     1 /*
       
     2  * Copyright (c) 2015, 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.jcmd;
       
    27 
       
    28 import java.nio.file.Files;
       
    29 import java.util.ArrayList;
       
    30 import java.util.List;
       
    31 
       
    32 import jdk.jfr.internal.Options;
       
    33 import jdk.test.lib.Asserts;
       
    34 
       
    35 /*
       
    36  * @test
       
    37  * @summary The test verifies JFR.configure command
       
    38  * @key jfr
       
    39  * @library /test/lib /test/jdk
       
    40  * @modules jdk.jfr/jdk.jfr.internal
       
    41  * @run main/othervm jdk.jfr.jcmd.TestJcmdConfigure
       
    42  */
       
    43 public class TestJcmdConfigure {
       
    44 
       
    45     private static final String DUMPPATH = "dumppath";
       
    46     private static final String STACK_DEPTH = "stackdepth";
       
    47     private static final String GLOBAL_BUFFER_COUNT = "globalbuffercount";
       
    48     private static final String GLOBAL_BUFFER_SIZE = "globalbuffersize";
       
    49     private static final String THREAD_BUFFER_SIZE = "thread_buffer_size";
       
    50     private static final String MAX_CHUNK_SIZE = "maxchunksize";
       
    51     private static final String SAMPLE_THREADS = "samplethreads";
       
    52     private static final String UNSUPPORTED_OPTION = "unsupportedoption";
       
    53 
       
    54     public static void main(String[] args) throws Exception {
       
    55         //
       
    56         // Simple sanity tests against what is available in Java,
       
    57         // before Flight Recorder is loaded. To do:
       
    58         //
       
    59         // - set values when JFR is running, check for errors.
       
    60         // - validate against output from JFR.configure
       
    61         // - where feasible, check if they are respected
       
    62         //
       
    63 
       
    64         String dumpPath = Files.createTempDirectory("dump-path").toAbsolutePath().toString();
       
    65 
       
    66         test(DUMPPATH, dumpPath);
       
    67         test(STACK_DEPTH, 15);
       
    68         test(GLOBAL_BUFFER_COUNT, 7);
       
    69         test(GLOBAL_BUFFER_SIZE, 6);
       
    70         test(THREAD_BUFFER_SIZE, 5);
       
    71         test(MAX_CHUNK_SIZE, 14 * 1000 * 1000);
       
    72         test(SAMPLE_THREADS, false);
       
    73         test(SAMPLE_THREADS, true);
       
    74         testNegative(UNSUPPORTED_OPTION, 100000);
       
    75         testNegative(MAX_CHUNK_SIZE, -500);
       
    76 
       
    77         if (!testExceptions.isEmpty()) {
       
    78             for (Exception e : testExceptions) {
       
    79                 System.out.println("Error: " + e.getMessage());
       
    80             }
       
    81             throw testExceptions.get(0);
       
    82         }
       
    83     }
       
    84 
       
    85     private static List<Exception> testExceptions = new ArrayList<>();
       
    86 
       
    87     private static void test(String configName, Object value) {
       
    88         JcmdHelper.jcmd("JFR.configure", configName + "=" + value);
       
    89         Object actualValue = getOption(configName);
       
    90         System.out.format("Test param='%s', expected='%s', actual='%s'%n", configName, value, actualValue);
       
    91         try {
       
    92             // Need convert to string to compare Integer and Long
       
    93             Asserts.assertEquals(value.toString(), actualValue.toString(), "Wrong JFR.configure " + configName);
       
    94         } catch (Exception e) {
       
    95             testExceptions.add(e);
       
    96         }
       
    97     }
       
    98 
       
    99     private static void testNegative(String configName, Object value) {
       
   100         try {
       
   101             // Syntactically invalid arguments are catched by the JCMD framework where an error code of 1 is returned.
       
   102             // Syntactically valid arguments that are semantically invalid (invalid value ranges for example) are handled by JFR code, it will always return a value of 0.
       
   103             JcmdHelper.jcmd(configName.equals(UNSUPPORTED_OPTION) ? 1 : 0, "JFR.configure", configName + "=" + value);
       
   104         } catch(Exception e) {
       
   105             testExceptions.add(e);
       
   106         }
       
   107     }
       
   108 
       
   109     private static Object getOption(String name) {
       
   110         switch (name) {
       
   111             case DUMPPATH: return Options.getDumpPath().toString();
       
   112             case STACK_DEPTH: return Options.getStackDepth();
       
   113             case GLOBAL_BUFFER_COUNT: return Options.getGlobalBufferCount();
       
   114             case GLOBAL_BUFFER_SIZE: return Options.getGlobalBufferSize();
       
   115             case THREAD_BUFFER_SIZE: return Options.getThreadBufferSize();
       
   116             case MAX_CHUNK_SIZE: return Options.getMaxChunkSize();
       
   117             case SAMPLE_THREADS: return Options.getSampleThreads();
       
   118             default: throw new RuntimeException("Unknown option " + name);
       
   119         }
       
   120     }
       
   121 }