changeset 10336:4915ec1779da

8038794: java/lang/management/ThreadMXBean/SynchronizationStatistics.java fails intermittently Reviewed-by: sla
author jbachorik
date Tue, 01 Jul 2014 11:47:36 +0200
parents c173d5414a7d
children bfaeb69ca16a
files test/java/lang/management/ThreadMXBean/SynchronizationStatistics.java
diffstat 1 files changed, 88 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/lang/management/ThreadMXBean/SynchronizationStatistics.java	Tue Dec 23 14:23:43 2014 +0100
+++ b/test/java/lang/management/ThreadMXBean/SynchronizationStatistics.java	Tue Jul 01 11:47:36 2014 +0200
@@ -34,6 +34,7 @@
 
 import java.lang.management.*;
 import java.util.concurrent.Phaser;
+import java.util.function.Supplier;
 
 public class SynchronizationStatistics {
     private static class LockerThread extends Thread {
@@ -86,6 +87,8 @@
     private static void testBlockingOnSimpleMonitor() throws Exception {
         System.out.println("testBlockingOnSimpleMonitor");
         final Object lock1 = new Object();
+        System.out.println("Lock1 = " + lock1);
+
         final Phaser p = new Phaser(2);
         LockerThread lt = newLockerThread(new Runnable() {
             @Override
@@ -110,12 +113,12 @@
         }
 
         p.arriveAndAwaitAdvance(); // phase[2]
-        testBlocked(ti, mbean.getThreadInfo(tid), lockName, lock1);
+        testBlocked(ti, () -> mbean.getThreadInfo(tid), lockName, lock1);
         p.arriveAndDeregister(); // phase[3]
 
         lt.join();
 
-        System.out.println("OK");
+        printok();
     }
 
     /**
@@ -128,6 +131,9 @@
         final Object lock1 = new Object();
         final Object lock2 = new Object();
 
+        System.out.println("Lock1 = " + lock1);
+        System.out.println("Lock2 = " + lock2);
+
         final Phaser p = new Phaser(2);
         LockerThread lt = newLockerThread(new Runnable() {
             @Override
@@ -149,7 +155,6 @@
         lt.start();
         long tid = lt.getId();
         ThreadInfo ti = mbean.getThreadInfo(tid);
-        ThreadInfo ti1 = null;
         String lockName = null;
         synchronized(lock1) {
             p.arriveAndAwaitAdvance(); // phase[1]
@@ -158,9 +163,7 @@
         }
         p.arriveAndAwaitAdvance(); // phase[2]
 
-        ti1 = mbean.getThreadInfo(tid);
-        testBlocked(ti, ti1, lockName, lock1);
-        ti = ti1;
+        ti = testBlocked(ti, () -> mbean.getThreadInfo(tid), lockName, lock1);
 
         synchronized(lock2) {
             p.arriveAndAwaitAdvance(); // phase [3]
@@ -168,12 +171,12 @@
             lockName = mbean.getThreadInfo(tid).getLockName();
         }
         p.arriveAndAwaitAdvance(); // phase [4]
-        testBlocked(ti, mbean.getThreadInfo(tid), lockName, lock2);
+        testBlocked(ti, () -> mbean.getThreadInfo(tid), lockName, lock2);
         p.arriveAndDeregister();
 
         lt.join();
 
-        System.out.println("OK");
+        printok();
     }
 
     /**
@@ -209,13 +212,12 @@
         }
         p.arriveAndAwaitAdvance(); // phase[2]
 
-        ThreadInfo ti2 = mbean.getThreadInfo(lt.getId());
+        testWaited(ti1, () -> mbean.getThreadInfo(lt.getId()), 1);
         p.arriveAndDeregister(); // phase[3]
 
         lt.join();
 
-        testWaited(ti1, ti2, 1);
-        System.out.println("OK");
+        printok();
     }
 
     /**
@@ -256,12 +258,12 @@
         int phase = p.getPhase();
         while ((p.arriveAndAwaitAdvance() - phase) < 3); // phase[2-4]
 
-        ThreadInfo ti2 = mbean.getThreadInfo(lt.getId());
+        testWaited(ti1, () -> mbean.getThreadInfo(lt.getId()), 3);
         p.arriveAndDeregister(); // phase[5]
 
         lt.join();
-        testWaited(ti1, ti2, 3);
-        System.out.println("OK");
+
+        printok();
     }
 
     /**
@@ -331,44 +333,85 @@
         }
 
         p.arriveAndAwaitAdvance(); // phase[4]
-        ThreadInfo ti2 = mbean.getThreadInfo(lt.getId());
+        testWaited(ti1, () -> mbean.getThreadInfo(lt.getId()), 3);
         p.arriveAndDeregister(); // phase[5]
 
         lt.join();
-        testWaited(ti1, ti2, 3);
-        System.out.println("OK");
+        printok();
     }
 
-    private static void testWaited(ThreadInfo ti1, ThreadInfo ti2, int waited) throws Error {
-        long waitCntDiff = ti2.getWaitedCount() - ti1.getWaitedCount();
-        long waitTimeDiff = ti2.getWaitedTime() - ti1.getWaitedTime();
-        if (waitCntDiff < waited) {
-            throw new Error("Unexpected diff in waited count. Expecting at least "
-                            + waited + " , got " + waitCntDiff);
-        }
-        if (waitTimeDiff <= 0) {
-            throw new Error("Unexpected diff in waited time. Expecting increasing " +
-                            "value, got " + waitTimeDiff + "ms");
-        }
+    private static void printok() {
+        System.out.println("OK\n");
     }
 
-    private static void testBlocked(ThreadInfo ti1, ThreadInfo ti2,
+    private static void testWaited(ThreadInfo ti1, Supplier<ThreadInfo> ti2, int waited)
+    throws InterruptedException {
+        boolean error;
+        do {
+            error = false;
+            ThreadInfo ti = ti2.get();
+            long waitCntDiff = ti.getWaitedCount() - ti1.getWaitedCount();
+            long waitTimeDiff = ti.getWaitedTime() - ti1.getWaitedTime();
+            if (waitCntDiff < waited) {
+                System.err.println(
+                    "Unexpected diff in waited count. Expecting at least "
+                    + waited + " , got " + waitCntDiff
+                );
+                error = true;
+            }
+            if (waitTimeDiff <= 0) {
+                System.err.println(
+                    "Unexpected diff in waited time. Expecting increasing " +
+                    "value, got " + waitTimeDiff + "ms"
+                );
+                error = true;
+            }
+            if (error) {
+                System.err.println("Retrying in 20ms ...");
+                Thread.sleep(20);
+            }
+        } while (error);
+    }
+
+    private static ThreadInfo testBlocked(ThreadInfo ti1, Supplier<ThreadInfo> ti2,
                                     String lockName, final Object lock)
-    throws Error {
-        long blkCntDiff = ti2.getBlockedCount() - ti1.getBlockedCount();
-        long blkTimeDiff = ti2.getBlockedTime() - ti1.getBlockedTime();
-        if (blkCntDiff < 1) {
-            throw new Error("Unexpected diff in blocked count. Expecting at least 1, " +
-                            "got " + blkCntDiff);
-        }
-        if (blkTimeDiff < 0) {
-            throw new Error("Unexpected diff in blocked time. Expecting a positive " +
-                            "number, got " + blkTimeDiff);
-        }
-        if (!lockName.equals(lock.toString())) {
-            throw new Error("Unexpected blocked monitor name. Expecting " +
-                    lock.toString() + ", got " +
-                    lockName);
-        }
+    throws InterruptedException {
+        boolean error;
+        ThreadInfo ti = null;
+        do {
+            error = false;
+            ti = ti2.get();
+            long blkCntDiff = ti.getBlockedCount() - ti1.getBlockedCount();
+            long blkTimeDiff = ti.getBlockedTime() - ti1.getBlockedTime();
+
+            System.out.println("testBlocked: [" + blkCntDiff + ", " + blkTimeDiff + ", " + lockName + "]");
+
+            if (blkCntDiff < 1) {
+                System.err.println(
+                    "Unexpected diff in blocked count. Expecting at least 1, " +
+                    "got " + blkCntDiff
+                );
+                error = true;
+            }
+            if (blkTimeDiff < 0) {
+                System.err.println(
+                    "Unexpected diff in blocked time. Expecting a positive " +
+                    "number, got " + blkTimeDiff
+                );
+                error = true;
+            }
+            if (!lockName.equals(lock.toString())) {
+                System.err.println(
+                    "Unexpected blocked monitor name. Expecting " +
+                    lock.toString() + ", got " + lockName
+                );
+                error = true;
+            }
+            if (error) {
+                System.err.println("Retrying in 20ms ...");
+                Thread.sleep(20);
+            }
+        } while (error);
+        return ti;
     }
 }