CompoundMeterBinder implements CloseableMeterBinder { @Override public void close() { meterBinders.stream() .filter(AutoCloseable.class::isInstance) .map(AutoCloseable.class::cast) .forEach(this::close); } CompoundMeterBinder(Collection<MeterBinder> binders); @VisibleForTesting  CompoundMeterBinder(Logger logger, Collection<MeterBinder> binders); @Override void bindTo(MeterRegistry registry); @Override void close();  }
@Test public void closesAllCloseableBinders() { Set<MeterBinder> nonCloseableBinders = setOf(4, MeterBinder.class); Set<CloseableMeterBinder> closeableBinders = setOf(9, CloseableMeterBinder.class); Collection<MeterBinder> allBinders = new HashSet<>(); allBinders.addAll(nonCloseableBinders); allBinders.addAll(closeableBinders); CompoundMeterBinder composedBinder = new CompoundMeterBinder(allBinders); composedBinder.close(); closeableBinders.forEach(CompoundMeterBinderTest::verifyClosed); }
@Test public void logsWarning_ifBinderThrowsWhileClosing() throws Exception { Logger logger = mock(Logger.class); RuntimeException thrownByBinder = new RuntimeException("thrown for test purposes"); CloseableMeterBinder throwingBinder = mock(CloseableMeterBinder.class); doThrow(thrownByBinder).when(throwingBinder).close(); CompoundMeterBinder composedBinder = new CompoundMeterBinder(logger, singleton(throwingBinder)); composedBinder.close(); verify(logger).warn(any(String.class), same(thrownByBinder)); }