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