Extract EventProducer and update TestRecursive JEP-349-branch
authoregahlin
Fri, 11 Oct 2019 20:17:18 +0200
branchJEP-349-branch
changeset 58569 5469bde803fe
parent 58567 e77a97d0edbb
child 58570 12bf92ce7991
Extract EventProducer and update TestRecursive
test/jdk/jdk/jfr/api/consumer/recordingstream/EventProducer.java
test/jdk/jdk/jfr/api/consumer/recordingstream/TestRecursive.java
test/jdk/jdk/jfr/api/consumer/recordingstream/TestStart.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/jdk/jfr/api/consumer/recordingstream/EventProducer.java	Fri Oct 11 20:17:18 2019 +0200
@@ -0,0 +1,34 @@
+package jdk.jfr.api.consumer.recordingstream;
+
+import jdk.jfr.api.consumer.recordingstream.TestStart.StartEvent;
+
+class EventProducer extends Thread {
+    private final Object lock = new Object();
+    private boolean killed = false;
+    public void run() {
+        while (true) {
+            StartEvent s = new StartEvent();
+            s.commit();
+            synchronized (lock) {
+                try {
+                    lock.wait(10);
+                    if (killed) {
+                        return; // end thread
+                    }
+                } catch (InterruptedException e) {
+                    // ignore
+                }
+            }
+        }
+    }
+    public void kill()  {
+        synchronized (lock) {
+            this.killed = true;
+            lock.notifyAll();
+            try {
+                join();
+            } catch (InterruptedException e) {
+            }
+        }
+    }
+}
\ No newline at end of file
--- a/test/jdk/jdk/jfr/api/consumer/recordingstream/TestRecursive.java	Fri Oct 11 19:46:05 2019 +0200
+++ b/test/jdk/jdk/jfr/api/consumer/recordingstream/TestRecursive.java	Fri Oct 11 20:17:18 2019 +0200
@@ -26,6 +26,7 @@
 package jdk.jfr.api.consumer.recordingstream;
 
 import java.util.List;
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.atomic.AtomicBoolean;
 
@@ -40,7 +41,8 @@
  * @summary Tests that events are not emitted in handlers
  * @key jfr
  * @requires vm.hasJFR
- * @library /test/lib
+ * @library /test/lib /test/jdk
+ * @build jdk.jfr.api.consumer.recordingstream.EventProducer.java
  * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestRecursive
  */
 public class TestRecursive {
@@ -57,43 +59,88 @@
     public static void main(String... args) throws Exception {
         testSync();
         testAsync();
+        testStreamInStream();
     }
 
-    private static void emit(AtomicBoolean stop) {
-        Runnable r = () -> {
-            while (!stop.get()) {
-                try {
-                    Thread.sleep(1000);
-                } catch (InterruptedException e) {
-                }
-                Provoker e = new Provoker();
-                e.commit();
+    private static void testStreamInStream() throws Exception {
+        CountDownLatch latch = new CountDownLatch(1);
+        try (Recording r = new Recording()) {
+            r.start();
+            Recorded r1 = new Recorded(); // 1
+            r1.commit();
+            try (RecordingStream rs = new RecordingStream()) {
+                rs.onEvent(e1 -> {
+                    streamInStream();
+                    latch.countDown();
+                });
+                rs.startAsync();
+                Recorded r2 = new Recorded(); // 2
+                r2.commit();
+                latch.await();
+            }
+            Recorded r3 = new Recorded(); // 2
+            r3.commit();
+            r.stop();
+            List<RecordedEvent> events = Events.fromRecording(r);
+            if (count(events, NotRecorded.class) != 0) {
+                throw new Exception("Expected 0 NotRecorded events");
+            }
+            if (count(events, Recorded.class) != 3) {
+                throw new Exception("Expected 3 Recorded events");
             }
-        };
-        Thread t = new Thread(r);
-        t.start();
+        }
+    }
+
+    // No events should be recorded in this method
+    private static void streamInStream() {
+        NotRecorded nr1 = new NotRecorded();
+        nr1.commit();
+        CountDownLatch latch = new CountDownLatch(1);
+        try (RecordingStream rs2 = new RecordingStream()) {
+            rs2.onEvent(e2 -> {
+                NotRecorded nr2 = new NotRecorded();
+                nr2.commit();
+                latch.countDown();
+            });
+            NotRecorded nr3 = new NotRecorded();
+            nr3.commit();
+            rs2.startAsync();
+            // run event in separate thread
+            CompletableFuture.runAsync(() -> {
+                Provoker p = new Provoker();
+                p.commit();
+            });
+            try {
+                latch.await();
+            } catch (InterruptedException e) {
+                throw new Error("Unexpected interruption", e);
+            }
+        }
+        NotRecorded nr2 = new NotRecorded();
+        nr2.commit();
     }
 
     private static void testSync() throws Exception {
         try (Recording r = new Recording()) {
             r.start();
-            AtomicBoolean stop = new AtomicBoolean(false);
-            emit(stop);
+            AtomicBoolean first = new AtomicBoolean();
+            EventProducer p = new EventProducer();
             try (RecordingStream rs = new RecordingStream()) {
                 Recorded e1 = new Recorded();
                 e1.commit();
                 rs.onEvent(e -> {
-                    if (!stop.get()) {
+                    if (first.get()) {
                         System.out.println("Emitting NotRecorded event");
                         NotRecorded event = new NotRecorded();
                         event.commit();
                         System.out.println("Stopping event provoker");
-                        stop.set(true);
+                        p.kill();
                         System.out.println("Closing recording stream");
                         rs.close();
                         return;
                     }
                 });
+                p.start();
                 rs.start();
                 Recorded e2 = new Recorded();
                 e2.commit();
@@ -104,7 +151,7 @@
             if (count(events, NotRecorded.class) != 0) {
                 throw new Exception("Expected 0 NotRecorded events");
             }
-            if (count(events, Recorded.class) == 2) {
+            if (count(events, Recorded.class) != 2) {
                 throw new Exception("Expected 2 Recorded events");
             }
         }
--- a/test/jdk/jdk/jfr/api/consumer/recordingstream/TestStart.java	Fri Oct 11 19:46:05 2019 +0200
+++ b/test/jdk/jdk/jfr/api/consumer/recordingstream/TestStart.java	Fri Oct 11 20:17:18 2019 +0200
@@ -25,7 +25,6 @@
 
 package jdk.jfr.api.consumer.recordingstream;
 
-import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.atomic.AtomicBoolean;
 
@@ -37,39 +36,13 @@
  * @summary Tests RecordingStream::start()
  * @key jfr
  * @requires vm.hasJFR
- * @library /test/lib
+ * @library /test/lib /test/jdk
+ * @build jdk.jfr.api.consumer.recordingstream.EventProducer.java
  * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestStart
  */
 public class TestStart {
     static class StartEvent extends Event {
     }
-    static class EventProducer extends Thread {
-        private final Object lock = new Object();
-        private boolean killed = false;
-        public void run() {
-            while (true) {
-                StartEvent s = new StartEvent();
-                s.commit();
-                synchronized (lock) {
-                    try {
-                        lock.wait(10);
-                        if (killed) {
-                            return; // end thread
-                        }
-                    } catch (InterruptedException e) {
-                        // ignore
-                    }
-                }
-            }
-        }
-        public void kill() {
-            synchronized (lock) {
-                this.killed = true;
-                lock.notifyAll();
-            }
-        }
-    }
-
     public static void main(String... args) throws Exception {
         testStart();
         testStartOnEvent();
@@ -83,9 +56,12 @@
         try (RecordingStream rs = new RecordingStream()) {
             EventProducer t = new EventProducer();
             t.start();
-            CompletableFuture.runAsync(() -> {
-                rs.start();
-            });
+            Thread thread = new Thread() {
+                public void run() {
+                    rs.start();
+                }
+            };
+            thread.start();
             rs.onEvent(e -> {
                 if (started.getCount() > 0) {
                     started.countDown();
@@ -112,9 +88,12 @@
             });
             EventProducer t = new EventProducer();
             t.start();
-            CompletableFuture.runAsync(() -> {
-                rs.start();
-            });
+            Thread thread = new Thread() {
+                public void run() {
+                    rs.start();
+                }
+            };
+            thread.start();
             started.await();
             t.kill();
         }
@@ -138,9 +117,12 @@
             });
             EventProducer t = new EventProducer();
             t.start();
-            CompletableFuture.runAsync(() -> {
-                rs.start();
-            });
+            Thread thread = new Thread() {
+                public void run() {
+                    rs.start();
+                }
+            };
+            thread.start();
             startedTwice.await();
             t.kill();
             if (!ISE.get()) {