6574989: TEST_BUG: javax/sound/sampled/Clip/bug5070081.java fails sometimes
authorserb
Thu, 23 Mar 2017 17:50:10 +0300
changeset 44647 c87e29c083eb
parent 44646 df364d57eeb2
child 44648 4e8ca3dd2540
6574989: TEST_BUG: javax/sound/sampled/Clip/bug5070081.java fails sometimes Reviewed-by: prr
jdk/test/javax/sound/sampled/Clip/bug5070081.java
--- a/jdk/test/javax/sound/sampled/Clip/bug5070081.java	Tue Mar 21 11:05:26 2017 -0700
+++ b/jdk/test/javax/sound/sampled/Clip/bug5070081.java	Thu Mar 23 17:50:10 2017 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -21,6 +21,8 @@
  * questions.
  */
 
+import java.util.concurrent.TimeUnit;
+
 import javax.sound.sampled.AudioFormat;
 import javax.sound.sampled.AudioSystem;
 import javax.sound.sampled.Clip;
@@ -56,18 +58,22 @@
 
         clip.start();                               // start playing
         Thread.sleep(1000);                         // wait a sec
-        long time1 = System.currentTimeMillis();
+        long time1 = currentTimeMillis();
         long pos1 = clip.getFramePosition();        // store the position
-        System.out.println("  Position before stop: " + pos1);
         clip.stop();                                // and then stop
         long pos2 = clip.getFramePosition();        // 2nd try
-        long time2 = System.currentTimeMillis();
+        long time2 = currentTimeMillis();
+
+        System.out.println("  Position before stop: " + pos1);
         System.out.println("  Position after stop: " + pos2);
 
-        System.out.println("  d(time): " + Math.abs(time2-time1) + " ms;"
-                + "d(clip pos): " + Math.abs(pos2 - pos1) + " ms.");
+        long timeDiff = Math.abs(time2 - time1);
+        // sample rate is 22050 per second, so 22.05 per ms
+        long posDiff = (long) (Math.abs(pos2 - pos1) / 22.05);
+        System.out.println("  d(time): " + timeDiff + " ms;"
+                + "d(clip pos time): " + posDiff + " ms.");
 
-        long nDerivation = Math.abs(pos2 - pos1) - Math.abs(time2-time1);
+        long nDerivation = posDiff - timeDiff;
         // add 50 ms for deviation (delay for stopping and errors due timer precision)
         if (nDerivation > 50) {
             System.out.println("  ERROR(1): The deviation is too much: " + nDerivation + " ms");
@@ -104,4 +110,8 @@
 
         System.out.println("Test passed sucessfully");
     }
+
+    private static long currentTimeMillis() {
+        return TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
+    }
 }