### Question:
CompositeTimer extends CompositeMeter<Timer> implements Timer { @Override public void record(long amount, TimeUnit unit) { for (Timer t : meters) { t.record(amount, unit); } } CompositeTimer(Id id, Clock clock, Collection<Timer> timers); @Override void record(long amount, TimeUnit unit); @Override T record(Callable<T> f); @Override void record(Runnable f); @Override long count(); @Override long totalTime();  }### Answer:
@Test public void empty() { Timer t = new CompositeTimer(NoopId.INSTANCE, clock, Collections.emptyList()); assertCountEquals(t, 0L); assertTotalEquals(t, 0L); t.record(1L, TimeUnit.SECONDS); assertCountEquals(t, 0L); assertTotalEquals(t, 0L); }
@Test public void testRecord() { Timer t = newTimer(); t.record(42, TimeUnit.MILLISECONDS); assertCountEquals(t, 1L); assertTotalEquals(t, 42000000L); }
@Test public void testRecordCallable() throws Exception { Timer t = newTimer(); clock.setMonotonicTime(100L); int v = t.record(() -> { clock.setMonotonicTime(500L); return 42; }); Assertions.assertEquals(v, 42); assertCountEquals(t, 1L); assertTotalEquals(t, 400L); }
@Test public void testRecordCallableException() throws Exception { Timer t = newTimer(); clock.setMonotonicTime(100L); boolean seen = false; try { t.record((Callable<Integer>) () -> { clock.setMonotonicTime(500L); throw new RuntimeException("foo"); }); } catch (Exception e) { seen = true; } Assertions.assertTrue(seen); assertCountEquals(t, 1L); assertTotalEquals(t, 400L); }
@Test public void testRecordRunnable() throws Exception { Timer t = newTimer(); clock.setMonotonicTime(100L); t.record(() -> clock.setMonotonicTime(500L)); assertCountEquals(t, 1L); assertTotalEquals(t, 400L); }
@Test public void testRecordRunnableException() throws Exception { Timer t = newTimer(); clock.setMonotonicTime(100L); boolean seen = false; try { t.record((Runnable) () -> { clock.setMonotonicTime(500L); throw new RuntimeException("foo"); }); } catch (Exception e) { seen = true; } Assertions.assertTrue(seen); assertCountEquals(t, 1L); assertTotalEquals(t, 400L); }
@Test public void testMeasure() { Timer t = newTimer(); t.record(42, TimeUnit.MILLISECONDS); clock.setWallTime(3712345L); for (Measurement m : t.measure()) { Assertions.assertEquals(m.timestamp(), 3712345L); if (m.id().equals(t.id().withTag(Statistic.count))) { Assertions.assertEquals(m.value(), 1.0, 0.1e-12); } else if (m.id().equals(t.id().withTag(Statistic.totalTime))) { Assertions.assertEquals(m.value(), 42e6, 0.1e-12); } else { Assertions.fail("unexpected id: " + m.id()); } } }