8190353: [Testbug] test/hotspot/jtreg/gc/logging/TestPrintReferences.java can still fail
authorsjohanss
Mon, 06 Nov 2017 11:11:44 +0100
changeset 47784 1360c7949d2f
parent 47783 a11d9dbcd6c0
child 47785 5692c538ecef
8190353: [Testbug] test/hotspot/jtreg/gc/logging/TestPrintReferences.java can still fail Reviewed-by: sangheki, eosterlund
test/hotspot/jtreg/gc/logging/TestPrintReferences.java
--- a/test/hotspot/jtreg/gc/logging/TestPrintReferences.java	Mon Nov 06 09:49:30 2017 +0100
+++ b/test/hotspot/jtreg/gc/logging/TestPrintReferences.java	Mon Nov 06 11:11:44 2017 +0100
@@ -137,15 +137,21 @@
   //      Actual value:  SoftReference(5.55) = phase1(1.85) + phase2(1.85) + phase3(1.85)
   //      Log value:     SoftReference(5.6) = phase1(1.9) + phase2(1.9) + phase3(1.9)
   //      When checked:  5.6 < 5.7 (sum of phase1~3)
-  public static boolean approximatelyEqual(BigDecimal phaseTime, BigDecimal sumOfSubPhasesTime, BigDecimal tolerance) {
-    BigDecimal abs = phaseTime.subtract(sumOfSubPhasesTime).abs();
-
-    int result = abs.compareTo(tolerance);
+  // Because of this we need method to verify that our measurements and calculations are valid.
+  public static boolean greaterThanOrApproximatelyEqual(BigDecimal phaseTime, BigDecimal sumOfSubPhasesTime, BigDecimal tolerance) {
+    if (phaseTime.compareTo(sumOfSubPhasesTime) >= 0) {
+      // phaseTime is greater than or equal.
+      return true;
+    }
 
-    // result == -1, abs is less than tolerance.
-    // result == 0,  abs is equal to tolerance.
-    // result == 1,  abs is greater than tolerance.
-    return (result != 1);
+    BigDecimal diff = sumOfSubPhasesTime.subtract(phaseTime);
+    if (diff.compareTo(tolerance) <= 0) {
+      // Difference is within tolerance, so approximately equal.
+      return true;
+    }
+
+    // sumOfSubPhasesTime is greater than phaseTime and not within tolerance.
+    return false;
   }
 
   public static BigDecimal checkPhaseTime(String refType) {
@@ -160,7 +166,7 @@
 
     // If there are 3 sub-phases, we should allow 0.1 tolerance.
     final BigDecimal toleranceFor3SubPhases = BigDecimal.valueOf(0.1);
-    if (!approximatelyEqual(phaseTime, sumOfSubPhasesTime, toleranceFor3SubPhases)) {
+    if (!greaterThanOrApproximatelyEqual(phaseTime, sumOfSubPhasesTime, toleranceFor3SubPhases)) {
       throw new RuntimeException(refType +" time(" + phaseTime +
                                  "ms) is less than the sum(" + sumOfSubPhasesTime + "ms) of each phases");
     }
@@ -181,7 +187,7 @@
 
     // If there are 4 sub-phases, we should allow 0.2 tolerance.
     final BigDecimal toleranceFor4SubPhases = BigDecimal.valueOf(0.2);
-    if (!approximatelyEqual(refProcTime, sumOfSubPhasesTime, toleranceFor4SubPhases)) {
+    if (!greaterThanOrApproximatelyEqual(refProcTime, sumOfSubPhasesTime, toleranceFor4SubPhases)) {
       throw new RuntimeException("Reference Processing time(" + refProcTime + "ms) is less than the sum("
                                  + sumOfSubPhasesTime + "ms) of each phases");
     }