/*
 * Copyright (C) 2016 Selerity, Inc. (support@seleritycorp.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 com.seleritycorp.common.base.time;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Before;
import org.junit.Test;

public class ClockImplTest {
  private long delta;

  @Before
  public void setUp() {
    delta = 2000; // 2 seconds
  }

  @Test
  public void testGetMillisEpochRange() {
    ClockImpl clock = new ClockImpl();

    long expected = System.currentTimeMillis();
    long actual = clock.getMillisEpoch();

    assertThat(actual).isBetween(expected - delta, expected + delta);
  }

  @Test
  public void testGetMillisEpochIncreasing() throws InterruptedException {
    ClockImpl clock = new ClockImpl();

    long ts1 = clock.getMillisEpoch();

    long end = System.currentTimeMillis() + 100;
    while (System.currentTimeMillis() < end) {
      try {
        Thread.sleep(100);
      } catch (InterruptedException e) {
        throw e;
      }
    }

    long ts2 = clock.getMillisEpoch();

    assertThat(ts2 - ts1).isBetween(100L, 100L + delta);
  }

  @Test
  public void testGetNanosEpochRange() {
    ClockImpl clock = new ClockImpl();

    long lowMark = System.currentTimeMillis();
    long actual = clock.getNanosEpoch();
    long highMark = System.currentTimeMillis();

    assertThat(actual / 1000000L).isBetween(lowMark, highMark);
  }

  @Test
  public void testGetNanosEpochResolution() {
    ClockImpl clock = new ClockImpl();

    long ts1 = clock.getNanosEpoch();
    long ts2 = clock.getNanosEpoch();

    assertThat(ts1).isLessThan(ts2);
  }

  @Test
  public void testReadRange() {
    ClockImpl clock = new ClockImpl();

    long lowMark = System.currentTimeMillis();
    long actual = clock.read();
    long highMark = System.currentTimeMillis();

    assertThat(actual / 1000000L).isBetween(lowMark, highMark);
  }

}

@@ -726,33 +726,8 @@
 t;%0A%0A
-import org.junit.Before;%0A
 impo
@@ -779,101 +779,8 @@
 t %7B%0A
-  private long delta;%0A%0A  @Before%0A  public void setUp() %7B%0A    delta = 2000; // 2 seconds%0A  %7D%0A%0A
   @T
@@ -817,32 +817,61 @@
 sEpochRange() %7B%0A
+    long deltaMillis = 2000;%0A
     ClockImpl cl
@@ -911,16 +911,22 @@
 expected
+Millis
  = Syste
@@ -955,32 +955,38 @@
 %0A    long actual
+Millis
  = clock.getMill
@@ -1005,45 +1005,33 @@
     
-assertThat(actual).isBetween(
+long lowMillis = 
 expected
  - d
@@ -1030,16 +1030,22 @@
 cted
+Millis
  - delta
 , ex
@@ -1040,17 +1040,45 @@
  - delta
-,
+Millis;%0A    long highMillis =
  expecte
@@ -1078,24 +1078,30 @@
 expected
+Millis
  + delta
 );%0A  %7D%0A%0A
@@ -1084,32 +1084,100 @@
 edMillis + delta
+Millis;%0A    assertThat(actualMillis).isBetween(lowMillis, highMillis
 );%0A  %7D%0A%0A  @Test%0A
@@ -1251,16 +1251,45 @@
 ption %7B%0A
+    long deltaMillis = 2000;%0A
     Cloc
@@ -1321,35 +1321,44 @@
 pl();%0A%0A    long 
-ts1
+millisBefore
  = clock.getMill
@@ -1578,35 +1578,43 @@
     %7D%0A%0A    long 
-ts2
+millisAfter
  = clock.getMill
@@ -1644,17 +1644,34 @@
 hat(
-ts2 - ts1
+millisAfter - millisBefore
 ).is
@@ -1696,16 +1696,22 @@
  + delta
+Millis
 );%0A  %7D%0A%0A
@@ -1751,32 +1751,59 @@
 sEpochRange() %7B%0A
+    long deltaMillis = 10;%0A
     ClockImpl cl
@@ -1831,35 +1831,37 @@
 ;%0A%0A    long lowM
-ark
+illis
  = System.curren
@@ -1865,32 +1865,46 @@
 rentTimeMillis()
+ - deltaMillis
 ;%0A    long actua
@@ -1896,32 +1896,37 @@
 %0A    long actual
+Nanos
  = clock.getNano
@@ -1941,35 +1941,37 @@
 ;%0A    long highM
-ark
+illis
  = System.curren
@@ -1975,32 +1975,46 @@
 rentTimeMillis()
+ + deltaMillis
 ;%0A%0A    assertTha
@@ -2013,32 +2013,37 @@
 ssertThat(actual
+Nanos
  / 1000000L).isB
@@ -2049,37 +2049,41 @@
 Between(lowM
-ark
+illis
 , highM
-ark
+illis
 );%0A  %7D%0A%0A  @T
@@ -2327,24 +2327,51 @@
 adRange() %7B%0A
+    long deltaMillis = 10;%0A
     ClockImp
@@ -2411,19 +2411,21 @@
 ong lowM
-ark
+illis
  = Syste
@@ -2437,32 +2437,46 @@
 rentTimeMillis()
+ - deltaMillis
 ;%0A    long actua
@@ -2476,16 +2476,21 @@
 g actual
+Nanos
  = clock
@@ -2512,19 +2512,21 @@
 ng highM
-ark
+illis
  = Syste
@@ -2546,16 +2546,30 @@
 Millis()
+ + deltaMillis
 ;%0A%0A    a
@@ -2584,16 +2584,21 @@
 t(actual
+Nanos
  / 10000
@@ -2620,21 +2620,25 @@
 lowM
-ark
+illis
 , highM
-ark
+illis
 );%0A 

Make Clock tests less strict