CsvMetricsService extends AbstractIdleService { @Override protected void startUp() throws Exception { if (reporter != null) { LOGGER.info("already started {}, directory: {}, interval: {}", serviceName(), directory, interval); return; } LOGGER.info("starting {}, directory: {}, interval: {}", serviceName(), directory, interval); if (directory.exists()) { moveDirectory(directory, new File(directory.getAbsolutePath() + "." + currentTimeMillis())); } if (directory.mkdirs()) { reporter = CsvReporter.forRegistry(metricRegistry) .formatFor(US) .convertRatesTo(SECONDS) .convertDurationsTo(MILLISECONDS) .build(directory); reporter.start(interval, SECONDS); LOGGER.info("started {}, directory: {}, interval: {}", serviceName(), directory, interval); } else { LOGGER.error("Can't create directory: {}", directory); throw new MetricsException(format("unable to make directory: %s", directory)); } } @Inject  CsvMetricsService(@Named("metrics.csv.directory") final File directory,
                             @Named("metrics.csv.interval.seconds") final int interval,
                             final MetricRegistry metricRegistry);   }
@Test public void startUp() throws Exception { TemporaryFolder temporaryFolder = new TemporaryFolder(); temporaryFolder.create(); directory = temporaryFolder.getRoot(); csvMetricsService = createService(); temporaryFolder.delete(); }
@Test public void startUpDirectoryDoesNotExist() throws Exception { when(directory.exists()).thenReturn(false); when(directory.mkdirs()).thenReturn(true); csvMetricsService = createService(); csvMetricsService.startUp(); }
@Test public void startUpDirectoryExists() throws Exception { TemporaryFolder temporaryFolder = new TemporaryFolder(); temporaryFolder.create(); directory = temporaryFolder.getRoot(); csvMetricsService = createService(); csvMetricsService.startUp(); temporaryFolder.delete(); }
@Test(expected = MetricsException.class) public void startUpDirectoryDoesNotExistCanNotMkdir() throws Exception { when(directory.exists()).thenReturn(false); when(directory.mkdirs()).thenReturn(false); csvMetricsService = createService(); csvMetricsService.startUp(); }
@Test public void startUpAlreadyStarted() throws Exception { when(directory.exists()).thenReturn(false); when(directory.mkdirs()).thenReturn(true); csvMetricsService = createService(); csvMetricsService.startUp(); csvMetricsService.startUp(); }