test/jdk/jdk/jfr/api/consumer/recordingstream/TestClose.java
changeset 59310 72f3dd43dd28
parent 58863 c16ac7a2eba4
child 59327 2c3578aa0bdf
equal deleted inserted replaced
59309:be238525d240 59310:72f3dd43dd28
    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.time.Instant;
    28 import java.util.concurrent.CompletableFuture;
    29 import java.util.concurrent.CompletableFuture;
    29 import java.util.concurrent.CountDownLatch;
    30 import java.util.concurrent.CountDownLatch;
    30 import java.util.concurrent.atomic.AtomicLong;
    31 import java.util.concurrent.atomic.AtomicLong;
       
    32 import java.util.concurrent.atomic.AtomicReference;
    31 
    33 
    32 import jdk.jfr.Event;
    34 import jdk.jfr.Event;
       
    35 import jdk.jfr.Recording;
       
    36 import jdk.jfr.consumer.EventStream;
    33 import jdk.jfr.consumer.RecordingStream;
    37 import jdk.jfr.consumer.RecordingStream;
    34 
    38 
    35 /**
    39 /**
    36  * @test
    40  * @test
    37  * @summary Tests RecordingStream::close()
    41  * @summary Tests RecordingStream::close()
    49         testCloseUnstarted();
    53         testCloseUnstarted();
    50         testCloseStarted();
    54         testCloseStarted();
    51         testCloseTwice();
    55         testCloseTwice();
    52         testCloseStreaming();
    56         testCloseStreaming();
    53         testCloseMySelf();
    57         testCloseMySelf();
       
    58         testCloseNoEvents();
    54     }
    59     }
    55 
    60 
    56     private static void testCloseMySelf() throws Exception {
    61     private static void testCloseMySelf() throws Exception {
    57         log("Entering testCloseMySelf()");
    62         log("Entering testCloseMySelf()");
    58         CountDownLatch l1 = new CountDownLatch(1);
    63         CountDownLatch l1 = new CountDownLatch(1);
   120         r.close();
   125         r.close();
   121         r.close();
   126         r.close();
   122         log("Leaving testCloseTwice()");
   127         log("Leaving testCloseTwice()");
   123     }
   128     }
   124 
   129 
       
   130     private static void testCloseNoEvents() throws Exception {
       
   131         try (Recording r = new Recording()) {
       
   132             r.start();
       
   133             CountDownLatch finished = new CountDownLatch(2);
       
   134             AtomicReference<Thread> streamingThread = new AtomicReference<>();
       
   135             try (EventStream es = EventStream.openRepository()) {
       
   136                 es.setStartTime(Instant.EPOCH);
       
   137                 es.onFlush( () -> {
       
   138                     streamingThread.set(Thread.currentThread());
       
   139                     finished.countDown();;
       
   140                 });
       
   141                 es.startAsync();
       
   142                 finished.await();
       
   143             } // <- EventStream::close should terminate thread
       
   144             while (streamingThread.get().isAlive()) {
       
   145                 Thread.sleep(10);
       
   146             }
       
   147         }
       
   148     }
       
   149 
   125     private static void log(String msg) {
   150     private static void log(String msg) {
   126         System.out.println(msg);
   151         System.out.println(msg);
   127     }
   152     }
   128 }
   153 }