test/jdk/jdk/jfr/api/consumer/recordingstream/TestRemove.java
branchJEP-349-branch
changeset 57361 53dccc90a5be
child 58076 ca625d28c580
equal deleted inserted replaced
57360:5d043a159d5c 57361:53dccc90a5be
       
     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 package jdk.jfr.api.consumer.recordingstream;
       
    26 
       
    27 import java.util.concurrent.CountDownLatch;
       
    28 import java.util.concurrent.atomic.AtomicBoolean;
       
    29 import java.util.concurrent.atomic.AtomicInteger;
       
    30 import java.util.function.Consumer;
       
    31 
       
    32 import jdk.jfr.Event;
       
    33 import jdk.jfr.consumer.RecordedEvent;
       
    34 import jdk.jfr.consumer.RecordingStream;
       
    35 
       
    36 /**
       
    37  * @test
       
    38  * @summary Tests RecordingStrream::remove(...)
       
    39  * @key jfr
       
    40  * @requires vm.hasJFR
       
    41  * @library /test/lib
       
    42  * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestRemove
       
    43  */
       
    44 public class TestRemove {
       
    45 
       
    46     static class RemoveEvent extends Event {
       
    47 
       
    48     }
       
    49 
       
    50     public static void main(String... args) throws Exception {
       
    51         testRemoveNull();
       
    52         testRemoveOnFlush();
       
    53         testRemoveOnClose();
       
    54         testRemoveOnEvent();
       
    55     }
       
    56 
       
    57     private static void testRemoveNull() {
       
    58         try (RecordingStream rs = new RecordingStream()) {
       
    59            try {
       
    60                rs.remove(null);
       
    61                throw new AssertionError("Expected NullPointerException from remove(null");
       
    62            } catch (NullPointerException npe) {
       
    63                // OK; as expected
       
    64            }
       
    65         }
       
    66      }
       
    67 
       
    68     private static void testRemoveOnEvent() throws Exception {
       
    69         try (RecordingStream rs = new RecordingStream()) {
       
    70             AtomicInteger counter = new AtomicInteger(0);
       
    71             CountDownLatch events = new CountDownLatch(2);
       
    72             Consumer<RecordedEvent> c1 = e -> {
       
    73                 counter.incrementAndGet();
       
    74             };
       
    75 
       
    76             Consumer<RecordedEvent> c2 = e -> {
       
    77                 events.countDown();
       
    78             };
       
    79             rs.onEvent(c1);
       
    80             rs.onEvent(c2);
       
    81 
       
    82             rs.remove(c1);
       
    83             rs.startAsync();
       
    84             RemoveEvent r1 = new RemoveEvent();
       
    85             r1.commit();
       
    86             RemoveEvent r2 = new RemoveEvent();
       
    87             r2.commit();
       
    88             events.await();
       
    89             if (counter.get() > 0) {
       
    90                 throw new AssertionError("OnEvent handler not removed!");
       
    91             }
       
    92         }
       
    93     }
       
    94 
       
    95     private static void testRemoveOnClose() {
       
    96         try (RecordingStream rs = new RecordingStream()) {
       
    97             AtomicBoolean onClose = new AtomicBoolean(false);
       
    98             Runnable r = () -> {
       
    99                 onClose.set(true);
       
   100             };
       
   101             rs.onClose(r);
       
   102             rs.remove(r);
       
   103             rs.close();
       
   104             if (onClose.get()) {
       
   105                 throw new AssertionError("onClose handler not removed!");
       
   106             }
       
   107         }
       
   108     }
       
   109 
       
   110     private static void testRemoveOnFlush() throws Exception {
       
   111         try (RecordingStream rs = new RecordingStream()) {
       
   112             AtomicInteger flushCount = new AtomicInteger(2);
       
   113             AtomicBoolean removeExecuted = new AtomicBoolean(false);
       
   114             Runnable onFlush1 = () -> {
       
   115                 removeExecuted.set(true);
       
   116             };
       
   117             Runnable onFlush2 = () -> {
       
   118                 flushCount.incrementAndGet();
       
   119             };
       
   120 
       
   121             rs.onFlush(onFlush1);
       
   122             rs.onFlush(onFlush2);
       
   123             rs.remove(onFlush1);
       
   124             rs.startAsync();
       
   125             while (flushCount.get() < 2) {
       
   126                 RemoveEvent r = new RemoveEvent();
       
   127                 r.commit();
       
   128                 Thread.sleep(100);
       
   129             }
       
   130 
       
   131             if (removeExecuted.get()) {
       
   132                 throw new AssertionError("onFlush handler not removed!");
       
   133             }
       
   134         }
       
   135     }
       
   136 }