test/jdk/jdk/jfr/api/consumer/recordingstream/TestRecursive.java
branchJEP-349-branch
changeset 58569 5469bde803fe
parent 58485 f40923eeb559
child 58576 96c769cba8a3
equal deleted inserted replaced
58567:e77a97d0edbb 58569:5469bde803fe
    24  */
    24  */
    25 
    25 
    26 package jdk.jfr.api.consumer.recordingstream;
    26 package jdk.jfr.api.consumer.recordingstream;
    27 
    27 
    28 import java.util.List;
    28 import java.util.List;
       
    29 import java.util.concurrent.CompletableFuture;
    29 import java.util.concurrent.CountDownLatch;
    30 import java.util.concurrent.CountDownLatch;
    30 import java.util.concurrent.atomic.AtomicBoolean;
    31 import java.util.concurrent.atomic.AtomicBoolean;
    31 
    32 
    32 import jdk.jfr.Event;
    33 import jdk.jfr.Event;
    33 import jdk.jfr.Recording;
    34 import jdk.jfr.Recording;
    38 /**
    39 /**
    39  * @test
    40  * @test
    40  * @summary Tests that events are not emitted in handlers
    41  * @summary Tests that events are not emitted in handlers
    41  * @key jfr
    42  * @key jfr
    42  * @requires vm.hasJFR
    43  * @requires vm.hasJFR
    43  * @library /test/lib
    44  * @library /test/lib /test/jdk
       
    45  * @build jdk.jfr.api.consumer.recordingstream.EventProducer.java
    44  * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestRecursive
    46  * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestRecursive
    45  */
    47  */
    46 public class TestRecursive {
    48 public class TestRecursive {
    47 
    49 
    48     public static class NotRecorded extends Event {
    50     public static class NotRecorded extends Event {
    55     }
    57     }
    56 
    58 
    57     public static void main(String... args) throws Exception {
    59     public static void main(String... args) throws Exception {
    58         testSync();
    60         testSync();
    59         testAsync();
    61         testAsync();
    60     }
    62         testStreamInStream();
    61 
    63     }
    62     private static void emit(AtomicBoolean stop) {
    64 
    63         Runnable r = () -> {
    65     private static void testStreamInStream() throws Exception {
    64             while (!stop.get()) {
    66         CountDownLatch latch = new CountDownLatch(1);
    65                 try {
    67         try (Recording r = new Recording()) {
    66                     Thread.sleep(1000);
    68             r.start();
    67                 } catch (InterruptedException e) {
    69             Recorded r1 = new Recorded(); // 1
    68                 }
    70             r1.commit();
    69                 Provoker e = new Provoker();
    71             try (RecordingStream rs = new RecordingStream()) {
    70                 e.commit();
    72                 rs.onEvent(e1 -> {
    71             }
    73                     streamInStream();
    72         };
    74                     latch.countDown();
    73         Thread t = new Thread(r);
    75                 });
    74         t.start();
    76                 rs.startAsync();
       
    77                 Recorded r2 = new Recorded(); // 2
       
    78                 r2.commit();
       
    79                 latch.await();
       
    80             }
       
    81             Recorded r3 = new Recorded(); // 2
       
    82             r3.commit();
       
    83             r.stop();
       
    84             List<RecordedEvent> events = Events.fromRecording(r);
       
    85             if (count(events, NotRecorded.class) != 0) {
       
    86                 throw new Exception("Expected 0 NotRecorded events");
       
    87             }
       
    88             if (count(events, Recorded.class) != 3) {
       
    89                 throw new Exception("Expected 3 Recorded events");
       
    90             }
       
    91         }
       
    92     }
       
    93 
       
    94     // No events should be recorded in this method
       
    95     private static void streamInStream() {
       
    96         NotRecorded nr1 = new NotRecorded();
       
    97         nr1.commit();
       
    98         CountDownLatch latch = new CountDownLatch(1);
       
    99         try (RecordingStream rs2 = new RecordingStream()) {
       
   100             rs2.onEvent(e2 -> {
       
   101                 NotRecorded nr2 = new NotRecorded();
       
   102                 nr2.commit();
       
   103                 latch.countDown();
       
   104             });
       
   105             NotRecorded nr3 = new NotRecorded();
       
   106             nr3.commit();
       
   107             rs2.startAsync();
       
   108             // run event in separate thread
       
   109             CompletableFuture.runAsync(() -> {
       
   110                 Provoker p = new Provoker();
       
   111                 p.commit();
       
   112             });
       
   113             try {
       
   114                 latch.await();
       
   115             } catch (InterruptedException e) {
       
   116                 throw new Error("Unexpected interruption", e);
       
   117             }
       
   118         }
       
   119         NotRecorded nr2 = new NotRecorded();
       
   120         nr2.commit();
    75     }
   121     }
    76 
   122 
    77     private static void testSync() throws Exception {
   123     private static void testSync() throws Exception {
    78         try (Recording r = new Recording()) {
   124         try (Recording r = new Recording()) {
    79             r.start();
   125             r.start();
    80             AtomicBoolean stop = new AtomicBoolean(false);
   126             AtomicBoolean first = new AtomicBoolean();
    81             emit(stop);
   127             EventProducer p = new EventProducer();
    82             try (RecordingStream rs = new RecordingStream()) {
   128             try (RecordingStream rs = new RecordingStream()) {
    83                 Recorded e1 = new Recorded();
   129                 Recorded e1 = new Recorded();
    84                 e1.commit();
   130                 e1.commit();
    85                 rs.onEvent(e -> {
   131                 rs.onEvent(e -> {
    86                     if (!stop.get()) {
   132                     if (first.get()) {
    87                         System.out.println("Emitting NotRecorded event");
   133                         System.out.println("Emitting NotRecorded event");
    88                         NotRecorded event = new NotRecorded();
   134                         NotRecorded event = new NotRecorded();
    89                         event.commit();
   135                         event.commit();
    90                         System.out.println("Stopping event provoker");
   136                         System.out.println("Stopping event provoker");
    91                         stop.set(true);
   137                         p.kill();
    92                         System.out.println("Closing recording stream");
   138                         System.out.println("Closing recording stream");
    93                         rs.close();
   139                         rs.close();
    94                         return;
   140                         return;
    95                     }
   141                     }
    96                 });
   142                 });
       
   143                 p.start();
    97                 rs.start();
   144                 rs.start();
    98                 Recorded e2 = new Recorded();
   145                 Recorded e2 = new Recorded();
    99                 e2.commit();
   146                 e2.commit();
   100             }
   147             }
   101             r.stop();
   148             r.stop();
   102             List<RecordedEvent> events = Events.fromRecording(r);
   149             List<RecordedEvent> events = Events.fromRecording(r);
   103             System.out.println(events);
   150             System.out.println(events);
   104             if (count(events, NotRecorded.class) != 0) {
   151             if (count(events, NotRecorded.class) != 0) {
   105                 throw new Exception("Expected 0 NotRecorded events");
   152                 throw new Exception("Expected 0 NotRecorded events");
   106             }
   153             }
   107             if (count(events, Recorded.class) == 2) {
   154             if (count(events, Recorded.class) != 2) {
   108                 throw new Exception("Expected 2 Recorded events");
   155                 throw new Exception("Expected 2 Recorded events");
   109             }
   156             }
   110         }
   157         }
   111     }
   158     }
   112 
   159