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.destination;
  27 
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import java.util.List;
  32 
  33 import jdk.jfr.Recording;
  34 import jdk.jfr.consumer.RecordedEvent;
  35 import jdk.jfr.consumer.RecordingFile;
  36 import jdk.test.lib.Asserts;
  37 import jdk.test.lib.jfr.Events;
  38 import jdk.test.lib.jfr.SimpleEventHelper;
  39 
  40 /*
  41  * @test
  42  * @summary Test setDestination with concurrent recordings
  43  * @key jfr
  44  * @library /test/lib
  45  * @run main/othervm -Xlog:jfr=trace jdk.jfr.api.recording.destination.TestDestMultiple
  46  */
  47 public class TestDestMultiple {
  48 
  49     public static void main(String[] args) throws Throwable {
  50         Recording rA = new Recording();
  51         Recording rB = new Recording();
  52 
  53         Path destA = Paths.get(".", "recA.jfr");
  54         Path destB = Paths.get(".", "recB.jfr");
  55         rA.setDestination(destA);
  56         rB.setDestination(destB);
  57 
  58         // Enable event in one recording and disable in the other.
  59         // Both recordings should still get the events, since we always
  60         // get the "union" of all settings.
  61         SimpleEventHelper.enable(rA, true);
  62         SimpleEventHelper.enable(rB, false);
  63 
  64         SimpleEventHelper.createEvent(0); // To no recording
  65 
  66         rA.start();
  67         SimpleEventHelper.createEvent(1); // Only to recA
  68 
  69         rB.start();
  70         SimpleEventHelper.createEvent(2); // To both recordings
  71 
  72         rA.stop();
  73 
  74         // This event will not be in recB.
  75         // The reason is that recA has stopped so event is no longer enabled.
  76         SimpleEventHelper.createEvent(3);
  77 
  78         // Enable the event and create a new event for recB
  79         SimpleEventHelper.enable(rB, true);
  80         SimpleEventHelper.createEvent(4);
  81 
  82         rB.stop();
  83         SimpleEventHelper.createEvent(5); // To no recording
  84 
  85         rB.close();
  86         rA.close();
  87 
  88         verifyRecording(destA, 1, 2);
  89         verifyRecording(destB, 2, 4);
  90     }
  91 
  92     private static void verifyRecording(Path path, int... ids) throws Exception {
  93         Asserts.assertTrue(Files.exists(path), "Recording file does not exist: " + path);
  94         int countEvent = 0;
  95         List<RecordedEvent> events = RecordingFile.readAllEvents(path);
  96         for (RecordedEvent event : events) {
  97             int id = Events.assertField(event, "id").getValue();
  98             System.out.printf("Recording '%s' id=%d%n", path, id);
  99         }
 100         for (RecordedEvent event : events) {
 101             int id = Events.assertField(event, "id").getValue();
 102             System.out.printf("Recording '%s' id=%d%n", path, id);
 103             Asserts.assertTrue(ids.length > countEvent, "Found extra event");
 104             Events.assertField(event, "id").equal(ids[countEvent]);
 105             ++countEvent;
 106         }
 107         // We expect exactly 4 events in each file. 2 events * 2 chunks
 108         Asserts.assertEquals(countEvent, ids.length, "Found too few events");
 109     }
 110 }