test/jdk/jdk/jfr/api/consumer/filestream/TestReuse.java
changeset 58863 c16ac7a2eba4
equal deleted inserted replaced
58861:2c3cc4b01880 58863:c16ac7a2eba4
       
     1 /*
       
     2  * Copyright (c) 2019, 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.consumer.filestream;
       
    27 
       
    28 import java.io.IOException;
       
    29 import java.nio.file.Files;
       
    30 import java.nio.file.Path;
       
    31 import java.util.IdentityHashMap;
       
    32 import java.util.Map;
       
    33 import java.util.concurrent.atomic.AtomicBoolean;
       
    34 
       
    35 import jdk.jfr.Event;
       
    36 import jdk.jfr.Recording;
       
    37 import jdk.jfr.consumer.EventStream;
       
    38 import jdk.jfr.consumer.RecordedEvent;
       
    39 
       
    40 /**
       
    41  * @test
       
    42  * @summary Test EventStream::setReuse(...)
       
    43  * @key jfr
       
    44  * @requires vm.hasJFR
       
    45  * @library /test/lib
       
    46  * @run main/othervm jdk.jfr.api.consumer.filestream.TestReuse
       
    47  */
       
    48 public class TestReuse {
       
    49 
       
    50     static class ReuseEvent extends Event {
       
    51     }
       
    52 
       
    53     private static final boolean[] BOOLEAN_STATES = { false, true };
       
    54 
       
    55     public static void main(String... args) throws Exception {
       
    56         Path p = makeRecording();
       
    57 
       
    58         testSetReuseTrue(p);
       
    59         testSetReuseFalse(p);
       
    60     }
       
    61 
       
    62     private static void testSetReuseFalse(Path p) throws Exception {
       
    63         for (boolean ordered : BOOLEAN_STATES) {
       
    64             AtomicBoolean fail = new AtomicBoolean(false);
       
    65             Map<RecordedEvent, RecordedEvent> identity = new IdentityHashMap<>();
       
    66             try (EventStream es = EventStream.openFile(p)) {
       
    67                 es.setOrdered(ordered);
       
    68                 es.setReuse(false);
       
    69                 es.onEvent(e -> {
       
    70                     if (identity.containsKey(e)) {
       
    71                         fail.set(true);
       
    72                         es.close();
       
    73                     }
       
    74                     identity.put(e, e);
       
    75                 });
       
    76                 es.start();
       
    77             }
       
    78             if (fail.get()) {
       
    79                 throw new Exception("Unexpected reuse! Ordered = " + ordered);
       
    80             }
       
    81 
       
    82         }
       
    83     }
       
    84 
       
    85     private static void testSetReuseTrue(Path p) throws Exception {
       
    86         for (boolean ordered : BOOLEAN_STATES) {
       
    87             AtomicBoolean success = new AtomicBoolean(false);
       
    88             Map<RecordedEvent, RecordedEvent> events = new IdentityHashMap<>();
       
    89             try (EventStream es = EventStream.openFile(p)) {
       
    90                 es.setOrdered(ordered);
       
    91                 es.setReuse(true);
       
    92                 es.onEvent(e -> {
       
    93                     if(events.containsKey(e)) {
       
    94                         success.set(true);;
       
    95                         es.close();
       
    96                     }
       
    97                     events.put(e,e);
       
    98                 });
       
    99                 es.start();
       
   100             }
       
   101             if (!success.get()) {
       
   102                 throw new Exception("No reuse! Ordered = " + ordered);
       
   103             }
       
   104         }
       
   105 
       
   106     }
       
   107 
       
   108     private static Path makeRecording() throws IOException {
       
   109         try (Recording r = new Recording()) {
       
   110             r.start();
       
   111             for (int i = 0; i < 5; i++) {
       
   112                 ReuseEvent e = new ReuseEvent();
       
   113                 e.commit();
       
   114             }
       
   115             Recording rotation = new Recording();
       
   116             rotation.start();
       
   117             for (int i = 0; i < 5; i++) {
       
   118                 ReuseEvent e = new ReuseEvent();
       
   119                 e.commit();
       
   120             }
       
   121             r.stop();
       
   122             rotation.close();
       
   123             Path p = Files.createTempFile("recording", ".jfr");
       
   124             r.dump(p);
       
   125             return p;
       
   126         }
       
   127     }
       
   128 }