test/jdk/jdk/jfr/api/consumer/recordingstream/TestStart.java
branchJEP-349-branch
changeset 57361 53dccc90a5be
child 57857 d957ea28ed21
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 
       
    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.AtomicBoolean;
       
    31 
       
    32 import jdk.jfr.Event;
       
    33 import jdk.jfr.consumer.RecordingStream;
       
    34 
       
    35 /**
       
    36  * @test
       
    37  * @summary Tests RecordingStream::start()
       
    38  * @key jfr
       
    39  * @requires vm.hasJFR
       
    40  * @library /test/lib
       
    41  * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestStart
       
    42  */
       
    43 public class TestStart {
       
    44     static class StartEvent extends Event {
       
    45     }
       
    46     static class EventProducer extends Thread {
       
    47         private boolean killed = false;
       
    48         public void run() {
       
    49             while (true) {
       
    50                 StartEvent s = new StartEvent();
       
    51                 s.commit();
       
    52                 synchronized (this) {
       
    53                     try {
       
    54                         wait(10);
       
    55                         if (killed) {
       
    56                             return; // end thread
       
    57                         }
       
    58                     } catch (InterruptedException e) {
       
    59                         // ignore
       
    60                     }
       
    61                 }
       
    62             }
       
    63         }
       
    64         public void kill() {
       
    65             synchronized (this) {
       
    66                 this.killed = true;
       
    67                 this.notifyAll();
       
    68             }
       
    69         }
       
    70     }
       
    71 
       
    72     public static void main(String... args) throws Exception {
       
    73         testStart();
       
    74         testStartOnEvent();
       
    75         testStartTwice();
       
    76         testStartClosed();
       
    77     }
       
    78 
       
    79     private static void testStartTwice() throws Exception {
       
    80         CountDownLatch started = new CountDownLatch(1);
       
    81         try (RecordingStream rs = new RecordingStream()) {
       
    82             EventProducer t = new EventProducer();
       
    83             t.start();
       
    84             CompletableFuture.runAsync(() -> {
       
    85                 rs.start();
       
    86             });
       
    87             rs.onEvent(e -> {
       
    88                 if (started.getCount() > 0) {
       
    89                     started.countDown();
       
    90                 }
       
    91             });
       
    92             started.await();
       
    93             t.kill();
       
    94             try {
       
    95                 rs.start();
       
    96                 throw new AssertionError("Expected IllegalStateException if started twice");
       
    97             } catch (IllegalStateException ise) {
       
    98                 // OK, as expected
       
    99             }
       
   100         }
       
   101     }
       
   102 
       
   103     static void testStart() throws Exception {
       
   104         CountDownLatch started = new CountDownLatch(1);
       
   105         try (RecordingStream rs = new RecordingStream()) {
       
   106             rs.onEvent(e -> {
       
   107                 started.countDown();
       
   108             });
       
   109             EventProducer t = new EventProducer();
       
   110             t.start();
       
   111             CompletableFuture.runAsync(() -> {
       
   112                 rs.start();
       
   113             });
       
   114             started.await();
       
   115             t.kill();
       
   116         }
       
   117     }
       
   118 
       
   119     static void testStartOnEvent() throws Exception {
       
   120         AtomicBoolean ISE = new AtomicBoolean(false);
       
   121         CountDownLatch startedTwice = new CountDownLatch(1);
       
   122         try (RecordingStream rs = new RecordingStream()) {
       
   123             rs.onEvent(e -> {
       
   124                 try {
       
   125                     rs.start(); // must not deadlock
       
   126                 } catch (IllegalStateException ise) {
       
   127                     if (!ISE.get())  {
       
   128                         startedTwice.countDown();
       
   129                         ISE.set(true);
       
   130                     }
       
   131                 }
       
   132             });
       
   133             EventProducer t = new EventProducer();
       
   134             t.start();
       
   135             CompletableFuture.runAsync(() -> {
       
   136                 rs.start();
       
   137             });
       
   138             startedTwice.await();
       
   139             t.kill();
       
   140             if (!ISE.get()) {
       
   141                 throw new AssertionError("Expected IllegalStateException");
       
   142             }
       
   143         }
       
   144     }
       
   145 
       
   146     static void testStartClosed() {
       
   147         RecordingStream rs = new RecordingStream();
       
   148         rs.close();
       
   149         try {
       
   150             rs.start();
       
   151             throw new AssertionError("Expected IllegalStateException");
       
   152         } catch (IllegalStateException ise) {
       
   153             // OK, as expected.
       
   154         }
       
   155     }
       
   156 
       
   157 }