Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
gh-10: Updated unit test to check the nanoTime method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Pitzeruse committed Aug 15, 2017
1 parent 1af4a82 commit 57cba2d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
Expand Up @@ -72,7 +72,7 @@ public final long plus(final long time, @Nonnull final TimeUnit timeUnit) {

@Override
public long currentTimeMillis () {
return TimeUnit.NANOSECONDS.toMillis(nanos.get());
return TimeUnit.NANOSECONDS.toMillis(nanoTime());
}

@Override
Expand Down
Expand Up @@ -13,12 +13,25 @@ public class TestWallClocks {
public void testWallClock() {
final WallClock wallClock = new DefaultWallClock();

final long now = System.currentTimeMillis();
final long wrappedNow = wallClock.currentTimeMillis();
{
final long now = System.currentTimeMillis();
final long wrappedNow = wallClock.currentTimeMillis();

Assert.assertTrue(
"Expected sequential calls to currentTimeMillis() to be equal, and these were more than 5ms distant.",
Math.abs(wrappedNow - now) < 5
);
}

Assert.assertTrue(
"Expected sequential calls to currentTimeMillis to be equal, and these were more than 5ms distant.",
Math.abs(wrappedNow - now) < 5);
{
final long now = System.nanoTime();
final long wrappedNow = wallClock.nanoTime();

Assert.assertTrue(
"Expected sequential calls to nanoTime() to be equal, and these were more than 5ms distant.",
Math.abs(wrappedNow - now) < TimeUnit.MILLISECONDS.toNanos(5)
);
}
}

@Test
Expand All @@ -31,16 +44,22 @@ public void testStoppedClock() throws InterruptedException {
wallClock.set(now);
Assert.assertEquals(
"Expected the wall clock time to be insensitive to system time changes",
now, wallClock.currentTimeMillis());
now, wallClock.currentTimeMillis()
);

wallClock.plus(10, TimeUnit.SECONDS);
Assert.assertEquals(
"Expected a predictable change in the reported milliseconds",
now + 10 * 1000, wallClock.currentTimeMillis());
now + (10 * 1000), wallClock.currentTimeMillis()
);

wallClock.plus(-5, TimeUnit.SECONDS);
Assert.assertEquals(
"Expected negative changes to be accepted",
now + 5 * 1000, wallClock.currentTimeMillis());
now + (5 * 1000), wallClock.currentTimeMillis()
);

final StoppedClock initializedClock = new StoppedClock(now);
Assert.assertEquals(now, initializedClock.currentTimeMillis());
}
}

0 comments on commit 57cba2d

Please sign in to comment.