8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test
authorrwestberg
Fri, 01 Jun 2018 10:15:48 +0200
changeset 50336 1b6ea6bcd21a
parent 50335 01e20d8850e3
child 50337 4d1393781fef
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test Reviewed-by: egahlin, mgronlun
test/jdk/jdk/jfr/event/runtime/TestBiasedLockRevocationEvents.java
--- a/test/jdk/jdk/jfr/event/runtime/TestBiasedLockRevocationEvents.java	Thu May 31 18:47:21 2018 -0400
+++ b/test/jdk/jdk/jfr/event/runtime/TestBiasedLockRevocationEvents.java	Fri Jun 01 10:15:48 2018 +0200
@@ -33,9 +33,11 @@
 import jdk.test.lib.jfr.Events;
 import jdk.test.lib.process.OutputAnalyzer;
 
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.concurrent.FutureTask;
+import java.util.stream.Collectors;
 
 /*
  * @test
@@ -99,6 +101,14 @@
         validateStackTrace(stackTrace, "touch");
     }
 
+    // Retrieve all biased lock revocation events related to the provided lock class, sorted by start time
+    static List<RecordedEvent> getRevocationEvents(Recording recording, String fieldName, Class<?> lockClass) throws Throwable {
+        return Events.fromRecording(recording).stream()
+                .filter(e -> ((RecordedClass)e.getValue(fieldName)).getName().equals(lockClass.getName()))
+                .sorted(Comparator.comparing(RecordedEvent::getStartTime))
+                .collect(Collectors.toList());
+    }
+
     static void testSingleRevocation() throws Throwable {
         class MyLock {};
 
@@ -110,11 +120,8 @@
         Thread biasBreaker = triggerRevocation(1, MyLock.class);
 
         recording.stop();
-
-        List<RecordedEvent> events = Events.fromRecording(recording);
-
-        // We may or may not catch a second revocation from the biasBreaker thread exiting
-        Asserts.assertGreaterThanOrEqual(events.size(), 1);
+        List<RecordedEvent> events = getRevocationEvents(recording, "lockClass", MyLock.class);
+        Asserts.assertEQ(events.size(), 1);
 
         RecordedEvent event = events.get(0);
         Events.assertEventThread(event, biasBreaker);
@@ -137,7 +144,7 @@
         Thread biasBreaker = triggerRevocation(BULK_REVOKE_THRESHOLD, MyLock.class);
 
         recording.stop();
-        List<RecordedEvent> events = Events.fromRecording(recording);
+        List<RecordedEvent> events = getRevocationEvents(recording, "revokedClass", MyLock.class);
         Asserts.assertEQ(events.size(), 1);
 
         RecordedEvent event = events.get(0);
@@ -163,8 +170,7 @@
         Thread.holdsLock(l);
 
         recording.stop();
-
-        List<RecordedEvent> events = Events.fromRecording(recording);
+        List<RecordedEvent> events = getRevocationEvents(recording, "lockClass", MyLock.class);
         Asserts.assertEQ(events.size(), 1);
 
         RecordedEvent event = events.get(0);
@@ -206,25 +212,18 @@
         touch(l);
 
         recording.stop();
-        List<RecordedEvent> events = Events.fromRecording(recording);
-        Events.hasEvents(events);
+        List<RecordedEvent> events = getRevocationEvents(recording, "lockClass", MyLock.class);
+        Asserts.assertEQ(events.size(), 1);
 
-        // Joining the locker thread can cause revocations as well, search for the interesting one
-        for (RecordedEvent event : events) {
-            RecordedClass lockClass = event.getValue("lockClass");
-            if (lockClass.getName().equals(MyLock.class.getName())) {
-                Events.assertEventThread(event, Thread.currentThread());
-                // Previous owner will usually be null, but can also be a thread that
-                // was created after the BiasLocker thread exited due to address reuse.
-                RecordedThread prevOwner = event.getValue("previousOwner");
-                if (prevOwner != null) {
-                    Asserts.assertNE(prevOwner.getJavaName(), "BiasLocker");
-                }
-                validateStackTrace(event.getStackTrace());
-                return;
-            }
+        RecordedEvent event = events.get(0);
+        Events.assertEventThread(event, Thread.currentThread());
+        // Previous owner will usually be null, but can also be a thread that
+        // was created after the BiasLocker thread exited due to address reuse.
+        RecordedThread prevOwner = event.getValue("previousOwner");
+        if (prevOwner != null) {
+            Asserts.assertNE(prevOwner.getJavaName(), "BiasLocker");
         }
-        Asserts.fail("Did not find any revocation event for MyLock");
+        validateStackTrace(event.getStackTrace());
     }
 
     static void testBulkRevocationNoRebias() throws Throwable {
@@ -239,12 +238,12 @@
         Thread biasBreaker1 = triggerRevocation(BULK_REVOKE_THRESHOLD, MyLock.class);
 
         recording.stop();
-        List<RecordedEvent> events = Events.fromRecording(recording);
+        List<RecordedEvent> events = getRevocationEvents(recording, "revokedClass", MyLock.class);
         Asserts.assertEQ(events.size(), 2);
 
         // The rebias event should occur before the noRebias one
-        RecordedEvent eventRebias = events.get(0).getStartTime().isBefore(events.get(1).getStartTime()) ? events.get(0) : events.get(1);
-        RecordedEvent eventNoRebias = events.get(0).getStartTime().isBefore(events.get(1).getStartTime()) ? events.get(1) : events.get(0);
+        RecordedEvent eventRebias = events.get(0);
+        RecordedEvent eventNoRebias = events.get(1);
 
         Events.assertEventThread(eventRebias, biasBreaker0);
         Events.assertField(eventRebias, "disableBiasing").equal(false);