diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectFilePreprocessor.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectFilePreprocessor.java index 40d604fb0825..0633bcc8f72c 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectFilePreprocessor.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectFilePreprocessor.java @@ -72,6 +72,8 @@ public class ProjectFilePreprocessor { private final Map> mainSourcesByModule = new HashMap<>(); private final Map> testSourcesByModule = new HashMap<>(); + private final ProgressReport progressReport = new ProgressReport("Report about progress of file preprocessing", + TimeUnit.SECONDS.toMillis(10)); private int totalFilesPreprocessed = 0; public ProjectFilePreprocessor(AnalysisWarnings analysisWarnings, ScmConfiguration scmConfiguration, InputModuleHierarchy inputModuleHierarchy, @@ -93,9 +95,8 @@ public ProjectFilePreprocessor(AnalysisWarnings analysisWarnings, ScmConfigurati } public void execute() { - ProgressReport progressReport = new ProgressReport("Report about progress of file preprocessing", - TimeUnit.SECONDS.toMillis(10)); progressReport.start("Preprocessing files..."); + progressReport.message(() -> String.format("Preprocessed %s files", totalFilesPreprocessed)); ExclusionCounter exclusionCounter = new ExclusionCounter(); if (useScmExclusion) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/util/ProgressReport.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/util/ProgressReport.java index 13c8f68afaf3..bb2e6481fe9a 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/util/ProgressReport.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/util/ProgressReport.java @@ -19,6 +19,7 @@ */ package org.sonar.scanner.util; +import java.util.function.Supplier; import javax.annotation.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,7 +29,7 @@ public class ProgressReport implements Runnable { private static final Logger LOG = LoggerFactory.getLogger(ProgressReport.class); private final long period; private long startTime; - private String message = ""; + private Supplier messageSupplier = () -> ""; private final Thread thread; private String stopMessage = null; @@ -44,7 +45,7 @@ public void run() { while (!Thread.interrupted()) { try { Thread.sleep(period); - log(message); + log(messageSupplier.get()); } catch (InterruptedException e) { break; } @@ -61,7 +62,11 @@ public void start(String startMessage) { } public void message(String message) { - this.message = message; + this.message(() -> message); + } + + public void message(Supplier messageSupplier) { + this.messageSupplier = messageSupplier; } public void stop(@Nullable String stopMessage) { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/util/ProgressReportTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/util/ProgressReportTest.java index 71a3fd644a12..cf44ce6cf82f 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/util/ProgressReportTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/util/ProgressReportTest.java @@ -42,7 +42,7 @@ public class ProgressReportTest { @Rule public LogTester logTester = new LogTester(); - private ProgressReport underTest = new ProgressReport(THREAD_NAME, 1); + private final ProgressReport underTest = new ProgressReport(THREAD_NAME, 1); @Test public void stop_thread_on_stop() { @@ -66,15 +66,28 @@ public void do_log() { underTest.message("Some message"); boolean logged = false; while (!logged) { - logged = containsWithRetry("Some message"); + logged = waitForMessageStartingWith("Some message"); } underTest.stop("stop"); - assertThat(containsWithRetry("stop")).isTrue(); + assertThat(waitForMessageStartingWith("stop")).isTrue(); } - private boolean containsWithRetry(String message) { + @Test + public void do_log_message_supplier() { + logTester.setLevel(Level.DEBUG); + underTest.start("start"); + underTest.message(() -> "Some message " + System.currentTimeMillis()); + boolean logged = false; + while (!logged) { + logged = waitForMessageStartingWith("Some message "); + } + underTest.stop("stop"); + assertThat(waitForMessageStartingWith("stop")).isTrue(); + } + + private boolean waitForMessageStartingWith(String message) { try { - Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> logTester.logs().contains(message)); + Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> logTester.logs().stream().anyMatch(s -> s.startsWith(message))); } catch (ConditionTimeoutException e) { return false; }