test/jdk/jdk/jfr/api/consumer/recordingstream/TestStart.java
branchJEP-349-branch
changeset 58569 5469bde803fe
parent 58445 1893a674db04
child 58576 96c769cba8a3
equal deleted inserted replaced
58567:e77a97d0edbb 58569:5469bde803fe
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package jdk.jfr.api.consumer.recordingstream;
    26 package jdk.jfr.api.consumer.recordingstream;
    27 
    27 
    28 import java.util.concurrent.CompletableFuture;
       
    29 import java.util.concurrent.CountDownLatch;
    28 import java.util.concurrent.CountDownLatch;
    30 import java.util.concurrent.atomic.AtomicBoolean;
    29 import java.util.concurrent.atomic.AtomicBoolean;
    31 
    30 
    32 import jdk.jfr.Event;
    31 import jdk.jfr.Event;
    33 import jdk.jfr.consumer.RecordingStream;
    32 import jdk.jfr.consumer.RecordingStream;
    35 /**
    34 /**
    36  * @test
    35  * @test
    37  * @summary Tests RecordingStream::start()
    36  * @summary Tests RecordingStream::start()
    38  * @key jfr
    37  * @key jfr
    39  * @requires vm.hasJFR
    38  * @requires vm.hasJFR
    40  * @library /test/lib
    39  * @library /test/lib /test/jdk
       
    40  * @build jdk.jfr.api.consumer.recordingstream.EventProducer.java
    41  * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestStart
    41  * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestStart
    42  */
    42  */
    43 public class TestStart {
    43 public class TestStart {
    44     static class StartEvent extends Event {
    44     static class StartEvent extends Event {
    45     }
    45     }
    46     static class EventProducer extends Thread {
       
    47         private final Object lock = new Object();
       
    48         private boolean killed = false;
       
    49         public void run() {
       
    50             while (true) {
       
    51                 StartEvent s = new StartEvent();
       
    52                 s.commit();
       
    53                 synchronized (lock) {
       
    54                     try {
       
    55                         lock.wait(10);
       
    56                         if (killed) {
       
    57                             return; // end thread
       
    58                         }
       
    59                     } catch (InterruptedException e) {
       
    60                         // ignore
       
    61                     }
       
    62                 }
       
    63             }
       
    64         }
       
    65         public void kill() {
       
    66             synchronized (lock) {
       
    67                 this.killed = true;
       
    68                 lock.notifyAll();
       
    69             }
       
    70         }
       
    71     }
       
    72 
       
    73     public static void main(String... args) throws Exception {
    46     public static void main(String... args) throws Exception {
    74         testStart();
    47         testStart();
    75         testStartOnEvent();
    48         testStartOnEvent();
    76         testStartTwice();
    49         testStartTwice();
    77         testStartClosed();
    50         testStartClosed();
    81         log("Entering testStartTwice()");
    54         log("Entering testStartTwice()");
    82         CountDownLatch started = new CountDownLatch(1);
    55         CountDownLatch started = new CountDownLatch(1);
    83         try (RecordingStream rs = new RecordingStream()) {
    56         try (RecordingStream rs = new RecordingStream()) {
    84             EventProducer t = new EventProducer();
    57             EventProducer t = new EventProducer();
    85             t.start();
    58             t.start();
    86             CompletableFuture.runAsync(() -> {
    59             Thread thread = new Thread() {
    87                 rs.start();
    60                 public void run() {
    88             });
    61                     rs.start();
       
    62                 }
       
    63             };
       
    64             thread.start();
    89             rs.onEvent(e -> {
    65             rs.onEvent(e -> {
    90                 if (started.getCount() > 0) {
    66                 if (started.getCount() > 0) {
    91                     started.countDown();
    67                     started.countDown();
    92                 }
    68                 }
    93             });
    69             });
   110             rs.onEvent(e -> {
    86             rs.onEvent(e -> {
   111                 started.countDown();
    87                 started.countDown();
   112             });
    88             });
   113             EventProducer t = new EventProducer();
    89             EventProducer t = new EventProducer();
   114             t.start();
    90             t.start();
   115             CompletableFuture.runAsync(() -> {
    91             Thread thread = new Thread() {
   116                 rs.start();
    92                 public void run() {
   117             });
    93                     rs.start();
       
    94                 }
       
    95             };
       
    96             thread.start();
   118             started.await();
    97             started.await();
   119             t.kill();
    98             t.kill();
   120         }
    99         }
   121         log("Leaving testStart()");
   100         log("Leaving testStart()");
   122     }
   101     }
   136                     }
   115                     }
   137                 }
   116                 }
   138             });
   117             });
   139             EventProducer t = new EventProducer();
   118             EventProducer t = new EventProducer();
   140             t.start();
   119             t.start();
   141             CompletableFuture.runAsync(() -> {
   120             Thread thread = new Thread() {
   142                 rs.start();
   121                 public void run() {
   143             });
   122                     rs.start();
       
   123                 }
       
   124             };
       
   125             thread.start();
   144             startedTwice.await();
   126             startedTwice.await();
   145             t.kill();
   127             t.kill();
   146             if (!ISE.get()) {
   128             if (!ISE.get()) {
   147                 throw new AssertionError("Expected IllegalStateException");
   129                 throw new AssertionError("Expected IllegalStateException");
   148             }
   130             }