Add testing of RecordingStream::setStartTime JEP-349-branch
authoregahlin
Wed, 23 Oct 2019 20:11:21 +0200
branchJEP-349-branch
changeset 58765 1256af493619
parent 58757 df9b65cc99e8
child 58767 94d3a7bc8561
Add testing of RecordingStream::setStartTime
test/jdk/jdk/jfr/api/consumer/recordingstream/TestSetStartTime.java
--- a/test/jdk/jdk/jfr/api/consumer/recordingstream/TestSetStartTime.java	Wed Oct 23 17:02:32 2019 +0200
+++ b/test/jdk/jdk/jfr/api/consumer/recordingstream/TestSetStartTime.java	Wed Oct 23 20:11:21 2019 +0200
@@ -25,6 +25,7 @@
 
 package jdk.jfr.api.consumer.recordingstream;
 
+import java.io.IOException;
 import java.time.Duration;
 import java.time.Instant;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -34,6 +35,7 @@
 import jdk.jfr.Recording;
 import jdk.jfr.StackTrace;
 import jdk.jfr.consumer.EventStream;
+import jdk.jfr.consumer.RecordingStream;
 
 /**
  * @test
@@ -45,6 +47,8 @@
  */
 public final class TestSetStartTime {
 
+    private final static int SLEEP_TIME_MS = 100;
+
     @Name("Mark")
     @StackTrace(false)
     public final static class Mark extends Event {
@@ -52,37 +56,84 @@
     }
 
     public static void main(String... args) throws Exception {
-        try (Recording r = new Recording()) {
-            r.setFlushInterval(Duration.ofSeconds(1));
-            r.start();
-            Mark event1 = new Mark();
-            event1.before = true;
-            event1.commit();
-            Thread.sleep(2000);
-            Instant now = Instant.now();
-            System.out.println("Instant.now() = " + now);
-            Thread.sleep(2000);
-            Mark event2 = new Mark();
-            event2.before = false;
-            event2.commit();
-            AtomicBoolean error = new AtomicBoolean();
-            try (EventStream d = EventStream.openRepository()) {
-                d.setStartTime(now);
-                d.onEvent(e -> {
-                    System.out.println(e);
-                    boolean early = e.getBoolean("before");
-                    if (early) {
-                        error.set(true);
-                    } else {
-                        // OK, as expected
-                        d.close();
+        testEventStream();
+        testRecordingStream();
+    }
+
+    private static void testRecordingStream() throws InterruptedException {
+        AtomicBoolean exit = new AtomicBoolean();
+        int attempt = 1;
+        while (!exit.get()) {
+            System.out.println("Testing RecordingStream:setStartTime(...). Attempt: " + attempt++);
+            AtomicBoolean firstEvent = new AtomicBoolean(true);
+            try (RecordingStream r2 = new RecordingStream()) {
+                Instant t = Instant.now().plus(Duration.ofMillis(SLEEP_TIME_MS / 2));
+                System.out.println("Setting start time: " + t);
+                r2.setStartTime(t);
+                r2.onEvent(e -> {
+                    if (firstEvent.get()) {
+                        firstEvent.set(false);
+                        if (!e.getBoolean("before")) {
+                            // Skipped first event, let's exit
+                            exit.set(true);
+                        }
+                        r2.close();
                     }
                 });
-                d.start();
-                if (error.get()) {
-                    throw new Exception("Found unexpected event!");
+                r2.startAsync();
+                Mark m1 = new Mark();
+                m1.before = true;
+                m1.commit();
+                System.out.println("First event emitted: " + Instant.now());
+                Thread.sleep(SLEEP_TIME_MS);
+                Mark m2 = new Mark();
+                m2.before = false;
+                m2.commit();
+                System.out.println("Second event emitted: " + Instant.now());
+                r2.awaitTermination();
+            }
+            System.out.println();
+        }
+    }
+
+    private static void testEventStream() throws InterruptedException, Exception, IOException {
+        AtomicBoolean exit = new AtomicBoolean();
+        int attempt = 1;
+        while (!exit.get()) {
+            System.out.println("Testing EventStream:setStartTime(...). Attempt: " + attempt++);
+            AtomicBoolean firstEvent = new AtomicBoolean(true);
+            try (Recording r = new Recording()) {
+                r.start();
+                Mark event1 = new Mark();
+                event1.before = true;
+                event1.commit();
+                Instant t = Instant.now();
+                System.out.println("First event emitted: " + t);
+                Thread.sleep(SLEEP_TIME_MS);
+                Mark event2 = new Mark();
+                event2.before = false;
+                event2.commit();
+                System.out.println("Second event emitted: " + Instant.now());
+                AtomicBoolean error = new AtomicBoolean();
+                try (EventStream es = EventStream.openRepository()) {
+                    Instant startTime = t.plus(Duration.ofMillis(SLEEP_TIME_MS / 2));
+                    es.setStartTime(startTime);
+                    System.out.println("Setting start time: " + startTime);
+                    es.onEvent(e -> {
+                        if (firstEvent.get()) {
+                            firstEvent.set(false);
+                            if (!e.getBoolean("before")) {
+                                // Skipped first event, let's exit
+                                exit.set(true);
+                            }
+                            es.close();
+                        }
+                    });
+                    es.startAsync();
+                    es.awaitTermination();
                 }
             }
+            System.out.println();
         }
     }
 }