DefaultHardwareClock implements HardwareClock { public DiscreteInterval getNanoTime() { long granularity = getGranularity(); long err = granularity / 2; if (granularity % 2 == 1) err += 1; long now = env.nanoTime() + getOffset(); return new DiscreteInterval(now - err, now + err); } DefaultHardwareClock();  DefaultHardwareClock(Env env);  DefaultHardwareClock(Env env, int numSamples, long periodMillis); long getGranularity(); DiscreteInterval getNanoTime();  }
@Test public void normalizesToPositiveReadings() { expect(mockEnv.nanoTime()).andReturn(-100L); expect(mockEnv.nanoTime()).andReturn(-90L).anyTimes(); expect(mockEnv.currentTimeMillis()).andReturn(0L).anyTimes(); replay(mockEnv); DiscreteInterval out = impl.getNanoTime(); verify(mockEnv); assertTrue(out.getMin() >= 0); assertTrue(out.getMax() >= 0); }
@Test public void canRetrieveClockReadingInterval() { impl = new DefaultHardwareClock(); assertTrue(impl.getNanoTime() instanceof DiscreteInterval); }
@Test public void errorIsHalfOfGranularityForEvenGranularity() { impl = new DefaultHardwareClock(mockEnv) { public long getGranularity() { return 6L; } }; expect(mockEnv.nanoTime()).andReturn(10L).anyTimes(); replay(mockEnv); assertEquals(7L, impl.getNanoTime().size()); verify(mockEnv); }
@Test public void errorRoundsProperlyForOddGranularity() { impl = new DefaultHardwareClock(mockEnv) { public long getGranularity() { return 5L; } }; expect(mockEnv.nanoTime()).andReturn(10L).anyTimes(); replay(mockEnv); assertEquals(7L, impl.getNanoTime().size()); verify(mockEnv); }
@Test public void canTakeMultipleReadings() { impl = new DefaultHardwareClock(); assertNotNull(impl.getNanoTime()); assertNotNull(impl.getNanoTime()); }