*/
package org.sonar.core.util;
+import com.google.common.util.concurrent.Uninterruptibles;
+import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
+import org.junit.Rule;
import org.junit.Test;
-import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
+import org.sonar.api.utils.log.Loggers;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.atLeast;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.startsWith;
-import static org.mockito.Mockito.verify;
public class ProgressLoggerTest {
- @Test(timeout = 1000L)
+ @Rule
+ public LogTester logTester = new LogTester();
+
+ @Test(timeout = 5_000L)
public void log_at_fixed_intervals() throws Exception {
- Logger logger = mock(Logger.class);
AtomicLong counter = new AtomicLong(42L);
- ProgressLogger progress = new ProgressLogger("ProgressLoggerTest", counter, logger);
+ ProgressLogger progress = new ProgressLogger("ProgressLoggerTest", counter, Loggers.get(getClass()));
progress.setPeriodMs(1L);
progress.start();
- Thread.sleep(80L);
+ while (!hasInfoLog("42 rows processed")) {
+ Uninterruptibles.sleepUninterruptibly(1, TimeUnit.MILLISECONDS);
+ }
progress.stop();
- verify(logger, atLeast(1)).info(startsWith("42 rows processed"));
// ability to manual log, generally final status
counter.incrementAndGet();
progress.log();
- verify(logger).info(startsWith("43 rows processed"));
+ assertThat(hasInfoLog("43 rows processed")).isTrue();
}
@Test
progress.setPluralLabel("issues");
assertThat(progress.getPeriodMs()).isEqualTo(10L);
assertThat(progress.getPluralLabel()).isEqualTo("issues");
+ }
+ private boolean hasInfoLog(String expectedLog) {
+ return logTester.logs(LoggerLevel.INFO).stream().filter(s -> s.startsWith(expectedLog)).findFirst().isPresent();
}
}