### Question:
Rate extends SampledStat<Double> { public static <T extends Number> Builder<T> of(Stat<T> input) { return new Builder<T>(input); } private  Rate(String name, Supplier<T> inputAccessor, int windowSize, double scaleFactor,
      Ticker ticker); static Builder<T> of(Stat<T> input); static Builder<Long> of(String name, Supplier<Long> input); static Builder<AtomicInteger> of(String name, AtomicInteger input); static Builder<AtomicLong> of(String name, AtomicLong input); @Override Double doSample();  }### Answer:
@Test public void testInputsRegistered() { expect(input.getName()).andReturn("test"); expectLastCall().atLeastOnce(); control.replay(); Rate.of(input); assertNotNull(Stats.getVariable("test")); }
@Test public void testVariableRateAtomicLong() throws Exception { AtomicLong value = new AtomicLong(); Rate<AtomicLong> rate = Rate.of("test", value).withTicker(ticker).build(); ticker.waitNanos(ONE_SEC); value.set(10); assertEquals(0d, rate.sample(), EPSILON); ticker.waitNanos(ONE_SEC); value.set(20); assertEquals(10d, rate.sample(), EPSILON); ticker.waitNanos(ONE_SEC); value.set(50); assertEquals(30d, rate.sample(), EPSILON); ticker.waitNanos(ONE_SEC); value.set(100); assertEquals(50d, rate.sample(), EPSILON); control.replay(); }
@Test public void testLongWindow() throws Exception { expectCalls(10, 10, 0, 10, 10); control.replay(); assertResults(Rate.of(input).withWindowSize(3).withTicker(ticker).build(), 0, -5, 0, 0); }
@Test public void testRateOfRate() throws Exception { expectCalls(10, 20, 30, 40, 50, 60); control.replay(); Rate<Integer> rate = Rate.of(input).withTicker(ticker).build(); Rate<Double> rateOfRate = Rate.of(rate).withTicker(ticker).build(); assertThat(rate.sample(), is(0d)); assertThat(rateOfRate.sample(), is(0d)); ticker.waitNanos(ONE_SEC); assertThat(rate.sample(), is(10d)); assertThat(rateOfRate.sample(), is(10d)); ticker.waitNanos(ONE_SEC); assertThat(rate.sample(), is(10d)); assertThat(rateOfRate.sample(), is(0d)); ticker.waitNanos(ONE_SEC); assertThat(rate.sample(), is(10d)); assertThat(rateOfRate.sample(), is(0d)); ticker.waitNanos(ONE_SEC); assertThat(rate.sample(), is(10d)); assertThat(rateOfRate.sample(), is(0d)); ticker.waitNanos(ONE_SEC); assertThat(rate.sample(), is(10d)); assertThat(rateOfRate.sample(), is(0d)); }
@Test public void testScaleFactor() throws Exception { expectCalls(10, 20, 30, 40); control.replay(); assertResults(Rate.of(input).withTicker(ticker).withScaleFactor(10).build(), 100, 100, 100); }
@Test public void testFractionalScaleFactor() throws Exception { expectCalls(10, 20, 30, 40); control.replay(); assertResults(Rate.of(input).withTicker(ticker).withScaleFactor(0.1).build(), 1, 1, 1); }
@Test public void testSupplier() throws Exception { Supplier<Long> supplier = new Supplier<Long>() { long value = 0; @Override public Long get() { value += 10; return value; } }; control.replay(); assertResults(Rate.of("test", supplier).withTicker(ticker).build(), 10, 10, 10); }