DefaultHardwareClock implements HardwareClock { long sampleGranularity() { long start = env.nanoTime(); long end; while((end = env.nanoTime()) == start) ; return elapsedTime(start, end); } DefaultHardwareClock();  DefaultHardwareClock(Env env);  DefaultHardwareClock(Env env, int numSamples, long periodMillis); long getGranularity(); DiscreteInterval getNanoTime();  }
@Test public void canSampleImmediatelyIncrementingClock() { expect(mockEnv.nanoTime()).andReturn(4L); expect(mockEnv.nanoTime()).andReturn(5L); replay(mockEnv); assertEquals(1L, impl.sampleGranularity()); verify(mockEnv); }
@Test public void samplesClockUntilItTicks() { int i = random.nextInt(10) + 2; expect(mockEnv.nanoTime()).andReturn(4L).times(i); expect(mockEnv.nanoTime()).andReturn(10L); replay(mockEnv); assertEquals(6L, impl.sampleGranularity()); verify(mockEnv); }