From a1dda7cfb1cc99bfc64a0433a760d97955c02ae8 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 13 Sep 2016 14:57:12 +0200 Subject: [PATCH] Improve stability of ProgressLoggerTest --- .../sonar/core/util/ProgressLoggerTest.java | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/sonar-core/src/test/java/org/sonar/core/util/ProgressLoggerTest.java b/sonar-core/src/test/java/org/sonar/core/util/ProgressLoggerTest.java index d7e93c41c98..57bb0de02d5 100644 --- a/sonar-core/src/test/java/org/sonar/core/util/ProgressLoggerTest.java +++ b/sonar-core/src/test/java/org/sonar/core/util/ProgressLoggerTest.java @@ -19,33 +19,37 @@ */ 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 @@ -61,6 +65,9 @@ public class ProgressLoggerTest { 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(); } } -- 2.39.5