@Test public void testProgress() throws InterruptedException { final Logger logger = Mockito.mock(Logger.class); final int totalCount = 100; Thread loggerThread = null; Mockito.when(logger.isLoggable(any())).thenReturn(true); try (final Progress progress = new Progress(logger, "foo", totalCount)) { assertNotNull(progress.getLoggerThread()); loggerThread = progress.getLoggerThread(); while (loggerThread.getState() != Thread.State.WAITING) { System.out.println("Waiting for the logger thread to reach the initial wait()"); TimeUnit.MILLISECONDS.sleep(10); } for (int i = 0; i < totalCount; i++) { progress.increment(); while (loggerThread.getState() != Thread.State.WAITING) { TimeUnit.MILLISECONDS.sleep(10); } } } System.out.println("Waiting for the logger thread to terminate"); int i = 0; while (i < 3 && loggerThread.getState() != Thread.State.TERMINATED) { TimeUnit.MILLISECONDS.sleep(10); i++; } Assert.assertSame(loggerThread.getState(), Thread.State.TERMINATED); Mockito.verify(logger, times(totalCount)).log(any(), anyString(), any(Object[].class)); }
public Progress(Logger logger, String suffix, long totalCount) { this.logger = logger; this.suffix = suffix; this.totalCount = totalCount; if (totalCount > 0 && RuntimeEnvironment.getInstance().isPrintProgress()) { run = true; loggerThread = new Thread(this::logLoop, "progress-thread-" + suffix.replaceAll(" ", "_")); loggerThread.start(); } }
Progress implements AutoCloseable { public Progress(Logger logger, String suffix, long totalCount) { this.logger = logger; this.suffix = suffix; this.totalCount = totalCount; if (totalCount > 0 && RuntimeEnvironment.getInstance().isPrintProgress()) { run = true; loggerThread = new Thread(this::logLoop, "progress-thread-" + suffix.replaceAll(" ", "_")); loggerThread.start(); } } }
Progress implements AutoCloseable { public Progress(Logger logger, String suffix, long totalCount) { this.logger = logger; this.suffix = suffix; this.totalCount = totalCount; if (totalCount > 0 && RuntimeEnvironment.getInstance().isPrintProgress()) { run = true; loggerThread = new Thread(this::logLoop, "progress-thread-" + suffix.replaceAll(" ", "_")); loggerThread.start(); } } Progress(Logger logger, String suffix, long totalCount); }
Progress implements AutoCloseable { public Progress(Logger logger, String suffix, long totalCount) { this.logger = logger; this.suffix = suffix; this.totalCount = totalCount; if (totalCount > 0 && RuntimeEnvironment.getInstance().isPrintProgress()) { run = true; loggerThread = new Thread(this::logLoop, "progress-thread-" + suffix.replaceAll(" ", "_")); loggerThread.start(); } } Progress(Logger logger, String suffix, long totalCount); void increment(); @Override void close(); }
Progress implements AutoCloseable { public Progress(Logger logger, String suffix, long totalCount) { this.logger = logger; this.suffix = suffix; this.totalCount = totalCount; if (totalCount > 0 && RuntimeEnvironment.getInstance().isPrintProgress()) { run = true; loggerThread = new Thread(this::logLoop, "progress-thread-" + suffix.replaceAll(" ", "_")); loggerThread.start(); } } Progress(Logger logger, String suffix, long totalCount); void increment(); @Override void close();  }