]> source.dussan.org Git - sonarqube.git/commitdiff
Improve stability of ProgressLoggerTest
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 13 Sep 2016 12:57:12 +0000 (14:57 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 13 Sep 2016 14:02:02 +0000 (16:02 +0200)
sonar-core/src/test/java/org/sonar/core/util/ProgressLoggerTest.java

index d7e93c41c98a3d3370cc69890fa0d3fa3f3dc93d..57bb0de02d56802f8109a57315a64b6f3b52fcdc 100644 (file)
  */
 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();
   }
 }