test/jdk/jdk/jfr/api/consumer/recordingstream/TestClose.java
changeset 59327 2c3578aa0bdf
parent 59310 72f3dd43dd28
--- a/test/jdk/jdk/jfr/api/consumer/recordingstream/TestClose.java	Fri Nov 29 15:37:13 2019 +0000
+++ b/test/jdk/jdk/jfr/api/consumer/recordingstream/TestClose.java	Fri Nov 29 17:31:01 2019 +0100
@@ -26,9 +26,7 @@
 package jdk.jfr.api.consumer.recordingstream;
 
 import java.time.Instant;
-import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
 
 import jdk.jfr.Event;
@@ -41,7 +39,7 @@
  * @summary Tests RecordingStream::close()
  * @key jfr
  * @requires vm.hasJFR
- * @library /test/lib
+ * @library /test/lib /test/jdk
  * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestClose
  */
 public class TestClose {
@@ -58,96 +56,82 @@
         testCloseNoEvents();
     }
 
-    private static void testCloseMySelf() throws Exception {
-        log("Entering testCloseMySelf()");
-        CountDownLatch l1 = new CountDownLatch(1);
-        CountDownLatch l2 = new CountDownLatch(1);
-        RecordingStream r = new RecordingStream();
-        r.onEvent(e -> {
-            try {
-                l1.await();
-                r.close();
-                l2.countDown();
-            } catch (InterruptedException ie) {
-                throw new Error(ie);
-            }
-        });
-        r.startAsync();
-        CloseEvent c = new CloseEvent();
-        c.commit();
-        l1.countDown();
-        l2.await();
-        log("Leaving testCloseMySelf()");
+    private static void testCloseUnstarted() {
+        System.out.println("testCloseUnstarted()");
+
+        try (RecordingStream r = new RecordingStream()) {
+            r.close();
+        }
+    }
+
+    private static void testCloseStarted() {
+        System.out.println("testCloseStarted()");
+
+        try (RecordingStream r = new RecordingStream()) {
+            r.startAsync();
+        } // <- Close
+    }
+
+    private static void testCloseTwice() {
+        System.out.println("Entering testCloseTwice()");
+
+        try (RecordingStream r = new RecordingStream()) {
+            r.startAsync();
+            r.close();
+        } // <- Second close
     }
 
     private static void testCloseStreaming() throws Exception {
-        log("Entering testCloseStreaming()");
+        System.out.println("Entering testCloseStreaming()");
+
+        EventProducer p = new EventProducer();
+        p.start();
         CountDownLatch streaming = new CountDownLatch(1);
-        RecordingStream r = new RecordingStream();
-        AtomicLong count = new AtomicLong();
-        r.onEvent(e -> {
-            if (count.incrementAndGet() > 100) {
+        try (RecordingStream r = new RecordingStream()) {
+            r.onEvent(e -> {
                 streaming.countDown();
-            }
-        });
-        r.startAsync();
-        var streamingLoop = CompletableFuture.runAsync(() -> {
-            while (true) {
-                CloseEvent c = new CloseEvent();
-                c.commit();
-            }
-        });
-        streaming.await();
-        r.close();
-        streamingLoop.cancel(true);
-        log("Leaving testCloseStreaming()");
+            });
+            r.startAsync();
+            streaming.await();
+        } // <- Close
+        p.kill();
     }
 
-    private static void testCloseStarted() {
-        log("Entering testCloseStarted()");
-        RecordingStream r = new RecordingStream();
-        r.startAsync();
-        r.close();
-        log("Leaving testCloseStarted()");
-    }
+    private static void testCloseMySelf() throws Exception {
+        System.out.println("testCloseMySelf()");
 
-    private static void testCloseUnstarted() {
-        log("Entering testCloseUnstarted()");
-        RecordingStream r = new RecordingStream();
-        r.close();
-        log("Leaving testCloseUnstarted()");
-    }
-
-    private static void testCloseTwice() {
-        log("Entering testCloseTwice()");
-        RecordingStream r = new RecordingStream();
-        r.startAsync();
-        r.close();
-        r.close();
-        log("Leaving testCloseTwice()");
+        CountDownLatch closed = new CountDownLatch(1);
+        try (RecordingStream r = new RecordingStream()) {
+            r.onEvent(e -> {
+                r.close();  // <- Close
+                closed.countDown();
+            });
+            r.startAsync();
+            CloseEvent c = new CloseEvent();
+            c.commit();
+            closed.await();
+        }
     }
 
     private static void testCloseNoEvents() throws Exception {
+        System.out.println("testCloseNoEvents()");
+
         try (Recording r = new Recording()) {
             r.start();
             CountDownLatch finished = new CountDownLatch(2);
             AtomicReference<Thread> streamingThread = new AtomicReference<>();
             try (EventStream es = EventStream.openRepository()) {
                 es.setStartTime(Instant.EPOCH);
-                es.onFlush( () -> {
+                es.onFlush(() -> {
                     streamingThread.set(Thread.currentThread());
-                    finished.countDown();;
+                    finished.countDown();
                 });
                 es.startAsync();
                 finished.await();
-            } // <- EventStream::close should terminate thread
+            } // <- Close should terminate thread
             while (streamingThread.get().isAlive()) {
                 Thread.sleep(10);
             }
         }
     }
-
-    private static void log(String msg) {
-        System.out.println(msg);
-    }
 }