/*
 * Copyright 2016 higherfrequencytrading.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.openhft.chronicle.core.time;

import org.jetbrains.annotations.NotNull;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/*
 * Created by Peter Lawrey on 08/12/16.
 */
public class SystemTimeProviderTest {
    @Test
    public void currentTimeMicros() throws Exception {
        @NotNull TimeProvider tp = SystemTimeProvider.INSTANCE;
        long start = System.currentTimeMillis();
        long minDiff = Long.MAX_VALUE;
        long maxDiff = Long.MIN_VALUE;
        long lastTimeMicros = 0;
        // mini-warmup.
        warmup(tp);

        while (System.currentTimeMillis() < start + 500) {
            long now0 = tp.currentTimeMillis();
            long time2 = tp.currentTimeMicros();
            long now1 = tp.currentTimeMillis();
            if (now1 - now0 > 1) {
                System.out.println("jump: " + (now1 - now0));
                continue;
            }

            long now = now1 * 1000;
            long diff = time2 - now;
            if (minDiff > diff) minDiff = diff;
            if (maxDiff < diff) maxDiff = diff;
            Thread.yield();
            assertTrue(time2 >= lastTimeMicros);
            lastTimeMicros = time2;
        }
        System.out.println("minDiff: " + minDiff + ", maxDiff: " + maxDiff);
        assertEquals(-45, minDiff, 50);
        assertEquals(1000, maxDiff, 50);
    }

    private void warmup(TimeProvider tp) {
        for (int i = 0; i < 500000; i++)
            tp.currentTimeMillis();
        Thread.yield();
    }
}
@@ -1032,42 +1032,74 @@
 ong 
-start = System.curren
+minDiff = 0;%0A        long maxDiff = 0;%0A        long las
 tTimeMi
-llis()
+cros
 ;%0A  
@@ -1101,32 +1101,90 @@
 s;%0A        long 
+start;%0A%0A        for (int i = 0; i %3C 2; i++) %7B%0A            
 minDiff = Long.M
@@ -1193,36 +1193,35 @@
 _VALUE;%0A        
-long
+   
  maxDiff = Long.
@@ -1231,36 +1231,35 @@
 _VALUE;%0A        
-long
+   
  lastTimeMicros 
@@ -1275,60 +1275,20 @@
     
-// mini-warmup.%0A        warmup(tp);%0A%0A        while (
+    start = 
 Syst
@@ -1313,26 +1313,32 @@
 is()
- %3C start + 500) %7B%0A
+;%0A%0A            do %7B%0A    
     
@@ -1389,24 +1389,28 @@
             
+ 
+   
 long time2 =
@@ -1438,32 +1438,36 @@
 ();%0A            
+    
 long now1 = tp.c
@@ -1494,24 +1494,28 @@
             
+   
+ 
 if (now1 - n
@@ -1521,24 +1521,28 @@
 now0 %3E 1) %7B%0A
+    
             
@@ -1607,16 +1607,20 @@
         
+    
 continue
@@ -1629,27 +1629,35 @@
             
+  
+  
 %7D%0A%0A
+    
             
@@ -1680,16 +1680,20 @@
 * 1000;%0A
+    
         
@@ -1729,24 +1729,28 @@
             
+    
 if (minDiff 
@@ -1765,32 +1765,36 @@
 minDiff = diff;%0A
+    
             if (
@@ -1829,32 +1829,36 @@
 ff;%0A            
+   
+ 
 Thread.yield();%0A
@@ -1853,24 +1853,28 @@
 ad.yield();%0A
+    
             
@@ -1914,32 +1914,36 @@
 s);%0A            
+    
 lastTimeMicros =
@@ -1950,16 +1950,80 @@
  time2;%0A
+            %7D while (System.currentTimeMillis() %3C start + 500);%0A
         
@@ -2024,16 +2024,17 @@
       %7D%0A
+%0A
         
@@ -2192,157 +2192,6 @@
    %7D
-%0A%0A    private void warmup(TimeProvider tp) %7B%0A        for (int i = 0; i %3C 500000; i++)%0A            tp.currentTimeMillis();%0A        Thread.yield();%0A    %7D
 %0A%7D

Make the test more robust for systems slow to warmup.