changeset 17352:ea7b8c7a6e5c

fixed reliability of DebugTimerTest
author Doug Simon <doug.simon@oracle.com>
date Mon, 06 Oct 2014 17:32:55 +0200
parents 95663a6385cf
children 3457f147a24f
files graal/com.oracle.graal.debug.test/src/com/oracle/graal/debug/test/DebugTimerTest.java
diffstat 1 files changed, 33 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.debug.test/src/com/oracle/graal/debug/test/DebugTimerTest.java	Mon Oct 06 06:33:50 2014 -0700
+++ b/graal/com.oracle.graal.debug.test/src/com/oracle/graal/debug/test/DebugTimerTest.java	Mon Oct 06 17:32:55 2014 +0200
@@ -24,6 +24,8 @@
 
 import static org.junit.Assert.*;
 
+import java.lang.management.*;
+
 import org.junit.*;
 
 import com.oracle.graal.debug.*;
@@ -31,39 +33,46 @@
 
 public class DebugTimerTest {
 
-    private static void sleep(long millis) {
-        try {
-            Thread.sleep(millis);
-        } catch (InterruptedException e) {
-        }
-    }
+    private static final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
 
-    private static boolean runningOnWindows() {
-        return System.getProperty("os.name").toLowerCase().contains("windows");
+    /**
+     * Actively spins the current thread for at least a given number of milliseconds in such a way
+     * that timers for the current thread keep ticking over.
+     *
+     * @return the number of milliseconds actually spent spinning which is guaranteed to be >=
+     *         {@code ms}
+     */
+    private static long spin(long ms) {
+        long start = threadMXBean.getCurrentThreadCpuTime();
+        do {
+            long durationMS = (threadMXBean.getCurrentThreadCpuTime() - start) / 1000;
+            if (durationMS >= ms) {
+                return durationMS;
+            }
+        } while (true);
     }
 
     @Test
     public void test1() {
-        if (runningOnWindows()) {
-            // mx beans regression
-            return;
-        }
         DebugConfig debugConfig = Debug.fixedConfig(0, 0, false, false, true, false, null, null, System.out);
         try (DebugConfigScope dcs = new DebugConfigScope(debugConfig); Debug.Scope s = Debug.scope("DebugTimerTest")) {
 
             DebugTimer timerA = Debug.timer("TimerA");
             DebugTimer timerB = Debug.timer("TimerB");
 
+            long spinA;
+            long spinB;
+
             try (TimerCloseable a1 = timerA.start()) {
-                sleep(50);
+                spinA = spin(50);
                 try (TimerCloseable b1 = timerB.start()) {
-                    sleep(50);
+                    spinB = spin(50);
                 }
             }
 
             Assert.assertTrue(timerB.getCurrentValue() < timerA.getCurrentValue());
             if (timerA.getFlat() != null && timerB.getFlat() != null) {
-                assertTrue(timerB.getFlat().getCurrentValue() < timerA.getFlat().getCurrentValue());
+                assertTrue(spinB >= spinA || timerB.getFlat().getCurrentValue() < timerA.getFlat().getCurrentValue());
                 assertEquals(timerA.getFlat().getCurrentValue(), timerA.getCurrentValue() - timerB.getFlat().getCurrentValue(), 10D);
             }
         }
@@ -75,15 +84,15 @@
         try (DebugConfigScope dcs = new DebugConfigScope(debugConfig); Debug.Scope s = Debug.scope("DebugTimerTest")) {
             DebugTimer timerC = Debug.timer("TimerC");
             try (TimerCloseable c1 = timerC.start()) {
-                sleep(50);
+                spin(50);
                 try (TimerCloseable c2 = timerC.start()) {
-                    sleep(50);
+                    spin(50);
                     try (TimerCloseable c3 = timerC.start()) {
-                        sleep(50);
+                        spin(50);
                         try (TimerCloseable c4 = timerC.start()) {
-                            sleep(50);
+                            spin(50);
                             try (TimerCloseable c5 = timerC.start()) {
-                                sleep(50);
+                                spin(50);
                             }
                         }
                     }
@@ -97,10 +106,6 @@
 
     @Test
     public void test3() {
-        if (runningOnWindows()) {
-            // mx beans regression
-            return;
-        }
         DebugConfig debugConfig = Debug.fixedConfig(0, 0, false, false, true, false, null, null, System.out);
         try (DebugConfigScope dcs = new DebugConfigScope(debugConfig); Debug.Scope s = Debug.scope("DebugTimerTest")) {
 
@@ -108,13 +113,13 @@
             DebugTimer timerE = Debug.timer("TimerE");
 
             try (TimerCloseable d1 = timerD.start()) {
-                sleep(50);
+                spin(50);
                 try (TimerCloseable e1 = timerE.start()) {
-                    sleep(50);
+                    spin(50);
                     try (TimerCloseable d2 = timerD.start()) {
-                        sleep(50);
+                        spin(50);
                         try (TimerCloseable d3 = timerD.start()) {
-                            sleep(50);
+                            spin(50);
                         }
                     }
                 }