Fix end time filtering JEP-349-branch
authoregahlin
Wed, 23 Oct 2019 23:23:18 +0200
branchJEP-349-branch
changeset 58770 481870c3d258
parent 58769 db30b6219779
child 58771 e6feb2874fa6
Fix end time filtering
src/jdk.jfr/share/classes/jdk/jfr/consumer/RecordingStream.java
test/jdk/jdk/jfr/api/consumer/recordingstream/TestSetEndTime.java
test/jdk/jdk/jfr/api/consumer/recordingstream/TestSetStartTime.java
--- a/src/jdk.jfr/share/classes/jdk/jfr/consumer/RecordingStream.java	Wed Oct 23 23:22:27 2019 +0200
+++ b/src/jdk.jfr/share/classes/jdk/jfr/consumer/RecordingStream.java	Wed Oct 23 23:23:18 2019 +0200
@@ -297,7 +297,7 @@
 
     @Override
     public void setEndTime(Instant endTime) {
-        directoryStream.setStartTime(endTime);
+        directoryStream.setEndTime(endTime);
     }
 
     @Override
--- a/test/jdk/jdk/jfr/api/consumer/recordingstream/TestSetEndTime.java	Wed Oct 23 23:22:27 2019 +0200
+++ b/test/jdk/jdk/jfr/api/consumer/recordingstream/TestSetEndTime.java	Wed Oct 23 23:23:18 2019 +0200
@@ -30,13 +30,14 @@
 import java.nio.file.Paths;
 import java.time.Duration;
 import java.time.Instant;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import jdk.jfr.Event;
 import jdk.jfr.Name;
 import jdk.jfr.Recording;
 import jdk.jfr.StackTrace;
-import jdk.jfr.Timestamp;
 import jdk.jfr.consumer.EventStream;
 import jdk.jfr.consumer.RecordedEvent;
 import jdk.jfr.consumer.RecordingFile;
@@ -59,37 +60,44 @@
     }
 
     public static void main(String... args) throws Exception {
-        testEventStream();
+     //   testEventStream();
         testRecordingStream();
     }
 
-    @StackTrace(false)
-    public final static class Now extends Event {
-        @Timestamp(Timestamp.MILLISECONDS_SINCE_EPOCH)
-        public long timestamp;
-    }
-
     private static void testRecordingStream() throws Exception {
-        try (RecordingStream rs = new RecordingStream()) {
-            AtomicBoolean fail = new AtomicBoolean();
-            Instant endTime = Instant.now().plus(Duration.ofSeconds(1));
-            rs.setReuse(false);
-            rs.onEvent(e -> {
-                if (e.getEndTime().isAfter(endTime)) {
-                    fail.set(true);
+        while (true) {
+            CountDownLatch closed = new CountDownLatch(1);
+            AtomicInteger count = new AtomicInteger();
+            try (RecordingStream rs = new RecordingStream()) {
+                rs.setFlushInterval(Duration.ofSeconds(1));
+                rs.onEvent(e -> {
+                    count.incrementAndGet();
+                });
+                // when end is reached stream is closed
+                rs.onClose(() -> {
+                    closed.countDown();
+                });
+                Instant endTime = Instant.now().plus(Duration.ofMillis(10_000));
+                System.out.println("Setting end time: " + endTime);
+                rs.setEndTime(endTime);
+                rs.startAsync();
+                for (int i = 0; i < 50; i++) {
+                    Mark m = new Mark();
+                    m.commit();
+                    Thread.sleep(10);
                 }
-            });
-            rs.setEndTime(endTime);
-            rs.startAsync();
-            for (int i = 0; i < 100; i++) {
-                Now m = new Now();
-                m.commit();
-                Thread.sleep(10);
+                closed.await();
+                System.out.println("Found events: " + count.get());
+                if (count.get() < 50) {
+                    return;
+                }
+                System.out.println("Found 50 events. Retrying");
+                System.out.println();
             }
         }
     }
 
-    private static void testEventStream() throws InterruptedException, IOException, Exception {
+    static void testEventStream() throws InterruptedException, IOException, Exception {
         try (Recording r = new Recording()) {
             r.setFlushInterval(Duration.ofSeconds(1));
             r.start();
--- a/test/jdk/jdk/jfr/api/consumer/recordingstream/TestSetStartTime.java	Wed Oct 23 23:22:27 2019 +0200
+++ b/test/jdk/jdk/jfr/api/consumer/recordingstream/TestSetStartTime.java	Wed Oct 23 23:23:18 2019 +0200
@@ -114,7 +114,6 @@
                 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);