/*
 * Copyright 2014-2020 Real Logic Limited.
 *
 * 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
 *
 * https://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.agrona.concurrent;

import org.agrona.UnsafeAccess;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

import java.util.concurrent.locks.LockSupport;

import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static org.hamcrest.MatcherAssert.assertThat;

public class OffsetEpochNanoClockTest
{
    @Test
    public void shouldFindSaneEpochTimestamp()
    {
        final OffsetEpochNanoClock clock = new OffsetEpochNanoClock();

        assertSaneEpochTimeStamp(clock);
    }

    private void assertSaneEpochTimeStamp(final OffsetEpochNanoClock clock)
    {
        final long startInMs = System.currentTimeMillis();
        UnsafeAccess.UNSAFE.fullFence();
        parkForMeasurementPrecision();
        final long nanoTime = clock.nanoTime();
        final long nanoTimeInMs = NANOSECONDS.toMillis(nanoTime);
        parkForMeasurementPrecision();
        UnsafeAccess.UNSAFE.fullFence();
        final long endInMs = System.currentTimeMillis();

        assertThat(nanoTimeInMs, Matchers.lessThanOrEqualTo(endInMs));
        assertThat(nanoTimeInMs, Matchers.greaterThanOrEqualTo(startInMs));
    }

    @Test
    public void shouldResampleSaneEpochTimestamp()
    {
        final OffsetEpochNanoClock clock = new OffsetEpochNanoClock();

        clock.sample();

        assertSaneEpochTimeStamp(clock);
    }

    private void parkForMeasurementPrecision()
    {
        LockSupport.parkNanos(1_000_000);
    }
}

@@ -645,251 +645,254 @@
 org.
-agrona.UnsafeAccess;%0Aimport org.hamcrest.Matchers;%0Aimport org.junit.jupiter.api.Test;%0A%0Aimport java.util.concurrent.locks.LockSupport;%0A%0Aimport static java.util.concurrent.TimeUnit.NANOSECONDS;%0Aimport static org.hamcrest.MatcherAssert.assertThat
+junit.jupiter.api.Test;%0A%0Aimport static java.util.concurrent.TimeUnit.NANOSECONDS;%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.Matchers.greaterThanOrEqualTo;%0Aimport static org.hamcrest.Matchers.lessThanOrEqualTo
 ;%0A%0Ap
@@ -1084,33 +1084,33 @@
 ertSaneEpochTime
-S
+s
 tamp(clock);%0A   
@@ -1145,25 +1145,25 @@
 aneEpochTime
-S
+s
 tamp(final O
@@ -1214,22 +1214,20 @@
 al long 
-startI
+begi
 nMs = Sy
@@ -1264,80 +1264,18 @@
     
-UnsafeAccess.UNSAFE.fullFence();%0A        parkForMeasurementPrecision
+sleep
 ();%0A
+%0A
     
@@ -1337,26 +1337,20 @@
 al long 
-nanoT
+t
 ime
-In
 Ms = NAN
@@ -1382,84 +1382,22 @@
 e);%0A
-        parkForMeasurementPrecision();%0A        UnsafeAccess.UNSAFE.fullFence
+%0A        sleep
 ();%0A
@@ -1418,18 +1418,16 @@
 long end
-In
 Ms = Sys
@@ -1471,39 +1471,24 @@
 ertThat(
-nanoT
+t
 ime
-In
 Ms, 
-Matchers.
 lessThan
@@ -1500,18 +1500,16 @@
 alTo(end
-In
 Ms));%0A  
@@ -1529,31 +1529,16 @@
 hat(
-nanoT
+t
 ime
-In
 Ms, 
-Matchers.
 grea
@@ -1558,14 +1558,12 @@
 lTo(
-startI
+begi
 nMs)
@@ -1763,17 +1763,17 @@
 pochTime
-S
+s
 tamp(clo
@@ -1805,38 +1805,137 @@
 oid 
-parkForMeasurementPrecision(
+sleep()%0A    %7B%0A        try%0A        %7B%0A            Thread.sleep(1);%0A        %7D%0A        catch (final InterruptedException ignore
 )%0A
+    
     
@@ -1948,41 +1948,57 @@
     
-LockSupport.parkNanos(1_000_000);
+    Thread.currentThread().interrupt();%0A        %7D
 %0A   

Rework OffsetEpochNanoClockTest.