test/jdk/jdk/jfr/api/consumer/recordingstream/TestClose.java
changeset 58863 c16ac7a2eba4
child 59310 72f3dd43dd28
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.recordingstream;
       
    27 
       
    28 import java.util.concurrent.CompletableFuture;
       
    29 import java.util.concurrent.CountDownLatch;
       
    30 import java.util.concurrent.atomic.AtomicLong;
       
    31 
       
    32 import jdk.jfr.Event;
       
    33 import jdk.jfr.consumer.RecordingStream;
       
    34 
       
    35 /**
       
    36  * @test
       
    37  * @summary Tests RecordingStream::close()
       
    38  * @key jfr
       
    39  * @requires vm.hasJFR
       
    40  * @library /test/lib
       
    41  * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestClose
       
    42  */
       
    43 public class TestClose {
       
    44 
       
    45     private static class CloseEvent extends Event {
       
    46     }
       
    47 
       
    48     public static void main(String... args) throws Exception {
       
    49         testCloseUnstarted();
       
    50         testCloseStarted();
       
    51         testCloseTwice();
       
    52         testCloseStreaming();
       
    53         testCloseMySelf();
       
    54     }
       
    55 
       
    56     private static void testCloseMySelf() throws Exception {
       
    57         log("Entering testCloseMySelf()");
       
    58         CountDownLatch l1 = new CountDownLatch(1);
       
    59         CountDownLatch l2 = new CountDownLatch(1);
       
    60         RecordingStream r = new RecordingStream();
       
    61         r.onEvent(e -> {
       
    62             try {
       
    63                 l1.await();
       
    64                 r.close();
       
    65                 l2.countDown();
       
    66             } catch (InterruptedException ie) {
       
    67                 throw new Error(ie);
       
    68             }
       
    69         });
       
    70         r.startAsync();
       
    71         CloseEvent c = new CloseEvent();
       
    72         c.commit();
       
    73         l1.countDown();
       
    74         l2.await();
       
    75         log("Leaving testCloseMySelf()");
       
    76     }
       
    77 
       
    78     private static void testCloseStreaming() throws Exception {
       
    79         log("Entering testCloseStreaming()");
       
    80         CountDownLatch streaming = new CountDownLatch(1);
       
    81         RecordingStream r = new RecordingStream();
       
    82         AtomicLong count = new AtomicLong();
       
    83         r.onEvent(e -> {
       
    84             if (count.incrementAndGet() > 100) {
       
    85                 streaming.countDown();
       
    86             }
       
    87         });
       
    88         r.startAsync();
       
    89         var streamingLoop = CompletableFuture.runAsync(() -> {
       
    90             while (true) {
       
    91                 CloseEvent c = new CloseEvent();
       
    92                 c.commit();
       
    93             }
       
    94         });
       
    95         streaming.await();
       
    96         r.close();
       
    97         streamingLoop.cancel(true);
       
    98         log("Leaving testCloseStreaming()");
       
    99     }
       
   100 
       
   101     private static void testCloseStarted() {
       
   102         log("Entering testCloseStarted()");
       
   103         RecordingStream r = new RecordingStream();
       
   104         r.startAsync();
       
   105         r.close();
       
   106         log("Leaving testCloseStarted()");
       
   107     }
       
   108 
       
   109     private static void testCloseUnstarted() {
       
   110         log("Entering testCloseUnstarted()");
       
   111         RecordingStream r = new RecordingStream();
       
   112         r.close();
       
   113         log("Leaving testCloseUnstarted()");
       
   114     }
       
   115 
       
   116     private static void testCloseTwice() {
       
   117         log("Entering testCloseTwice()");
       
   118         RecordingStream r = new RecordingStream();
       
   119         r.startAsync();
       
   120         r.close();
       
   121         r.close();
       
   122         log("Leaving testCloseTwice()");
       
   123     }
       
   124 
       
   125     private static void log(String msg) {
       
   126         System.out.println(msg);
       
   127     }
       
   128 }