keith-turner commented on code in PR #4796:
URL: https://github.com/apache/accumulo/pull/4796#discussion_r1723924982
##########
core/src/test/java/org/apache/accumulo/core/util/CountDownTimerTest.java:
##########
@@ -30,29 +31,61 @@
public class CountDownTimerTest {
@Test
public void testCountDownTimer() throws Exception {
+
+ long start = System.nanoTime();
+
var timer1 = CountDownTimer.startNew(Duration.ofMillis(100));
Thread.sleep(10);
- var timer2 = CountDownTimer.startNew(Duration.ofMillis(100));
+ var timer2 = CountDownTimer.startNew(100, TimeUnit.MILLISECONDS);
Thread.sleep(10);
var timer3 = CountDownTimer.startNew(Duration.ofMillis(100));
Thread.sleep(10);
+ boolean expired1 = timer1.isExpired();
+ boolean expired2 = timer1.isExpired();
+ boolean expired3 = timer1.isExpired();
+
var left3 = timer3.timeLeft(TimeUnit.MILLISECONDS);
var left2 = timer2.timeLeft(TimeUnit.MILLISECONDS);
var left1 = timer1.timeLeft(TimeUnit.MILLISECONDS);
+ long elapsed = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
+
assertTrue(left3 <= 90);
assertTrue(left2 <= 80);
assertTrue(left1 <= 70);
- assertTrue(left3 >= left2);
- assertTrue(left2 >= left1);
+ assertTrue(Math.max(left3 - 10, 0) >= left2);
+ assertTrue(Math.max(left2 - 10, 0) >= left1);
+ assertTrue(left1 >= 100 - elapsed);
Review Comment:
That was interesting, was passing locally when I ran it a few times. Ran it
a bunch of times and it failed. I think the problem was that inside
CountDownTimer it subtracts two nanos and then convers to millis. In the test
code it was converting to millis and then subtracting. I suspect the different
order lead to a loss of precision, the order CountDownTimer is doing the
operations is more precise and the way the test was doing things was less
precise.
I converted the test code to use Timer so that I could get a duration. I
did subtraction using duration which should make the test more precise. Made
a lot of changes in b13f932 but
this snippet `Duration.ofMillis(100).minus(elapsed).toMillis()` should fix
the problem you saw. Let me know if still seeing the problem.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]