test/jdk/jdk/jfr/api/recording/dump/TestDumpState.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.recording.dump;
       
    27 
       
    28 import java.io.IOException;
       
    29 import java.nio.file.Files;
       
    30 import java.nio.file.Path;
       
    31 import java.nio.file.Paths;
       
    32 import java.util.ArrayList;
       
    33 import java.util.List;
       
    34 
       
    35 import jdk.jfr.Recording;
       
    36 import jdk.jfr.consumer.RecordedEvent;
       
    37 import jdk.jfr.consumer.RecordingFile;
       
    38 import jdk.test.lib.Asserts;
       
    39 import jdk.test.lib.jfr.CommonHelper;
       
    40 import jdk.test.lib.jfr.Events;
       
    41 import jdk.test.lib.jfr.SimpleEvent;
       
    42 import jdk.test.lib.jfr.SimpleEventHelper;
       
    43 import jdk.test.lib.jfr.VoidFunction;
       
    44 
       
    45 /*
       
    46  * @test
       
    47  * @summary call copyTo() with recording in all states.
       
    48  * @key jfr
       
    49  * @library /test/lib
       
    50  * @run main/othervm jdk.jfr.api.recording.dump.TestDumpState
       
    51  */
       
    52 public class TestDumpState {
       
    53 
       
    54     public static void main(String[] args) throws Throwable {
       
    55         Recording r = new Recording();
       
    56         SimpleEventHelper.enable(r, true);
       
    57 
       
    58         List<Integer> expectedIds = new ArrayList<>();
       
    59 
       
    60         SimpleEventHelper.createEvent(0); // Recording not started, should not be included.
       
    61         verifyIOException(()->{checkEvents(r, expectedIds);}, "No Exception when dump() not started");
       
    62 
       
    63         r.start();
       
    64         SimpleEventHelper.createEvent(1);
       
    65         expectedIds.add(1);
       
    66         checkEvents(r, expectedIds);
       
    67 
       
    68         SimpleEventHelper.createEvent(2);
       
    69         expectedIds.add(2);
       
    70         checkEvents(r, expectedIds);
       
    71 
       
    72         r.stop();
       
    73         checkEvents(r, expectedIds);
       
    74 
       
    75         SimpleEventHelper.createEvent(3); // Recording stopped, should not be included.
       
    76         checkEvents(r, expectedIds);
       
    77 
       
    78         r.close();
       
    79         SimpleEventHelper.createEvent(4);
       
    80         verifyIOException(()->{checkEvents(r, expectedIds);}, "No Exception when dump() closed");
       
    81     }
       
    82 
       
    83     private static int recordingCounter = 0;
       
    84     private static void checkEvents(Recording r, List<Integer> expectedIds) throws Exception {
       
    85         Path path = Paths.get(".", String.format("%d.jfr", recordingCounter++));
       
    86         r.dump(path);
       
    87         Asserts.assertTrue(Files.exists(path), "Recording file does not exist: " + path);
       
    88 
       
    89         int index = 0;
       
    90 
       
    91         for (RecordedEvent event : RecordingFile.readAllEvents(path)) {
       
    92             Events.isEventType(event, SimpleEvent.class.getName());
       
    93             Integer id = Events.assertField(event, "id").getValue();
       
    94             System.out.println("Got event with id " + id);
       
    95             Asserts.assertGreaterThan(expectedIds.size(), index, "Too many Events found");
       
    96             Asserts.assertEquals(id, expectedIds.get(index), "Wrong id at index " + index);
       
    97             ++index;
       
    98         }
       
    99         Asserts.assertEquals(index, expectedIds.size(), "Too few Events found");
       
   100     }
       
   101 
       
   102     private static void verifyIOException(VoidFunction f, String msg) throws Throwable {
       
   103         CommonHelper.verifyException(f, msg, IOException.class);
       
   104     }
       
   105 
       
   106 }