/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.jackrabbit.oak.stats;

import static junit.framework.Assert.assertTrue;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import org.junit.Test;

public class ClockTest {

    @Test
    public void testClockDrift() throws InterruptedException {
        ScheduledExecutorService executor =
                Executors.newSingleThreadScheduledExecutor();
        try {
            Clock[] clocks = new Clock[] {
                    Clock.SIMPLE,
                    Clock.ACCURATE,
                    new Clock.Fast(executor)
            };

            for (Clock clock : clocks) {
                long drift = clock.getTime() - System.currentTimeMillis();
                assertTrue(Math.abs(drift) < 20); // Windows can have 15ms gaps
            }

            Thread.sleep(100);

            for (Clock clock : clocks) {
                long drift = clock.getTime() - System.currentTimeMillis();
                assertTrue(Math.abs(drift) < 20);
            }
        } finally {
            executor.shutdown();
        }
    }

    @Test
    public void testClockIncreasing() throws InterruptedException {
        ScheduledExecutorService executor =
                Executors.newSingleThreadScheduledExecutor();
        try {
            Clock[] clocks = new Clock[] {
                    Clock.SIMPLE,
                    Clock.ACCURATE,
                    new Clock.Fast(executor)
            };

            long[] time = new long[clocks.length];
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < clocks.length; j++) {
                    long now = clocks[j].getTimeIncreasing();
                    assertTrue(time[j] < now);
                    time[j] = now;
                }
            }
        } finally {
            executor.shutdown();
        }
    }

}
@@ -1203,32 +1203,63 @@
 uledExecutor();%0A
+        final long limit = 20;%0A
         try %7B%0A  
@@ -1248,32 +1248,32 @@
 ;%0A        try %7B%0A
-
             Cloc
@@ -1565,32 +1565,98 @@
      assertTrue(
+%22unexpected drift: %22 + Math.abs(drift) + %22 (limit %22 + limit +%22)%22, 
 Math.abs(drift) 
@@ -1657,18 +1657,21 @@
 rift) %3C 
-20
+limit
 ); // Wi
@@ -1885,16 +1885,82 @@
 ertTrue(
+%22unexpected drift: %22 + Math.abs(drift) + %22 (limit %22 + limit +%22)%22, 
 Math.abs
@@ -1969,18 +1969,21 @@
 rift) %3C 
-20
+limit
 );%0A     

improve test to report actual drift on failure