package brooklyn.util.time;

import org.testng.Assert;
import org.testng.annotations.Test;

@Test(groups="Integration")
public class CountdownTimerIntegrationTest {

    @Test
    public void testSimple() {
        final Duration SIMPLE_DURATION = Duration.seconds(1);
        
        CountdownTimer timer = SIMPLE_DURATION.countdownTimer();
        Assert.assertFalse(timer.isExpired());
        Assert.assertTrue(timer.getDurationElapsed().toMilliseconds() < SIMPLE_DURATION.half().toMilliseconds());
        Assert.assertTrue(timer.getDurationRemaining().toMilliseconds() > SIMPLE_DURATION.half().toMilliseconds());
        Time.sleep(SIMPLE_DURATION.half());
        Assert.assertFalse(timer.isExpired());
        Assert.assertTrue(timer.getDurationElapsed().toMilliseconds() > SIMPLE_DURATION.half().toMilliseconds());
        Assert.assertTrue(timer.getDurationRemaining().toMilliseconds() < SIMPLE_DURATION.half().toMilliseconds());
        Time.sleep(SIMPLE_DURATION.half().add(Duration.millis(1)));
        Assert.assertTrue(timer.isExpired());
    }
    
}

@@ -21,32 +21,39 @@
 l.time;%0A%0Aimport 
+static 
 org.testng.Asser
@@ -53,17 +53,100 @@
 g.Assert
-;
+.assertFalse;%0Aimport static org.testng.Assert.assertTrue;%0A%0Aimport java.util.Arrays;%0A
 %0Aimport 
@@ -184,30 +184,8 @@
 Test
-(groups=%22Integration%22)
 %0Apub
@@ -235,44 +235,527 @@
     
-@Test%0A    public void testSimple() %7B
+// Test failed on jenkins when using 1 second, sleeping for 500ms; %0A    // hence relaxing time constraints so is less time-sensitive at the expense of being a slower test.%0A    @Test(groups=%22Integration%22)%0A    public void testSimple() %7B%0A        final int TOTAL_TIME_MS = 5*1000;%0A        final int OVERHEAD_MS = 2000;%0A        final int EARLY_RETURN_GRACE_MS = 30;%0A        final int FIRST_SLEEP_TIME_MS = 2500;%0A        final int SECOND_SLEEP_TIME_MS = TOTAL_TIME_MS - FIRST_SLEEP_TIME_MS + EARLY_RETURN_GRACE_MS*2;%0A        
 %0A   
@@ -805,17 +805,28 @@
 ion.
-seconds(1
+millis(TOTAL_TIME_MS
 );%0A 
@@ -898,39 +898,32 @@
 imer();%0A        
-Assert.
 assertFalse(time
@@ -938,39 +938,32 @@
 red());%0A        
-Assert.
 assertTrue(timer
@@ -1002,37 +1002,66 @@
 onds() %3C
- SIMPLE_DURATION.half
+= OVERHEAD_MS, %22elapsed=%22+timer.getDurationElapsed
 ().toMil
@@ -1074,39 +1074,32 @@
 nds());%0A        
-Assert.
 assertTrue(timer
@@ -1140,37 +1140,84 @@
 onds() %3E
- SIMPLE_DURATION.half
+= TOTAL_TIME_MS - OVERHEAD_MS, %22remaining=%22+timer.getDurationElapsed
 ().toMil
@@ -1222,32 +1222,41 @@
 illiseconds());%0A
+        %0A
         Time.sle
@@ -1258,37 +1258,51 @@
 e.sleep(
-SIMPLE_DURATION.half(
+Duration.millis(FIRST_SLEEP_TIME_MS
 ));%0A    
@@ -1301,31 +1301,24 @@
 ));%0A        
-Assert.
 assertFalse(
@@ -1345,34 +1345,75 @@
         
-A
+a
 ssert
-.assertTrue(
+Ordered(FIRST_SLEEP_TIME_MS - EARLY_RETURN_GRACE_MS, 
 timer.ge
@@ -1451,50 +1451,43 @@
 ds()
- %3E SIMPLE_DURATION.half().toMilliseconds()
+, FIRST_SLEEP_TIME_MS + OVERHEAD_MS
 );%0A 
@@ -1497,124 +1497,199 @@
     
-A
+a
 ssert
-.assertTrue(timer.getDurationRemaining().toMilliseconds() %3C SIMPLE_DURATION.half().toMilliseconds()
+Ordered(TOTAL_TIME_MS - FIRST_SLEEP_TIME_MS - OVERHEAD_MS, timer.getDurationRemaining().toMilliseconds(), TOTAL_TIME_MS - FIRST_SLEEP_TIME_MS + EARLY_RETURN_GRACE_MS
 );%0A   
+ 
+    %0A
      
+   
 Time
@@ -1699,53 +1699,44 @@
 eep(
-SIMPLE_DURATION.half().add(Duration.millis(1)
+Duration.millis(SECOND_SLEEP_TIME_MS
 ));%0A
@@ -1743,23 +1743,16 @@
         
-Assert.
 assertTr
@@ -1785,10 +1785,279 @@
  %7D%0A    %0A
+    private void assertOrdered(long... vals) %7B%0A        String errmsg = %22vals=%22+Arrays.toString(vals);%0A        long prevVal = Long.MIN_VALUE;%0A        for (long val : vals) %7B%0A            assertTrue(val %3E= prevVal, errmsg);%0A            prevVal = val;%0A        %7D%0A    %7D%0A    %0A
 %7D%0A

Fix fragile CountdownTimerIntegrationTest