aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2015-06-22 10:52:06 +0200
committerDuarte Meneses <duarte.meneses@sonarsource.com>2015-06-23 08:42:32 +0200
commite5a7b62cb7ceef25cf656d9423e4a4b46fec8afa (patch)
tree2798dc75f841069e42b4d270194def772214cb72 /sonar-batch
parent791f266fcc7521fc73310135d3d462f7f0aa3363 (diff)
downloadsonarqube-e5a7b62cb7ceef25cf656d9423e4a4b46fec8afa.tar.gz
sonarqube-e5a7b62cb7ceef25cf656d9423e4a4b46fec8afa.zip
SONAR-6659 ProgressMonitor during file indexation is not properly stopped in case of error
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/util/ProgressReport.java3
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/util/ProgressReportTest.java73
2 files changed, 75 insertions, 1 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/util/ProgressReport.java b/sonar-batch/src/main/java/org/sonar/batch/util/ProgressReport.java
index 42905cab04f..f152acca65e 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/util/ProgressReport.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/util/ProgressReport.java
@@ -34,6 +34,7 @@ public class ProgressReport implements Runnable {
this.period = period;
thread = new Thread(this);
thread.setName(threadName);
+ thread.setDaemon(true);
}
@Override
@@ -43,7 +44,7 @@ public class ProgressReport implements Runnable {
Thread.sleep(period);
log(message);
} catch (InterruptedException e) {
- thread.interrupt();
+ break;
}
}
log(stopMessage);
diff --git a/sonar-batch/src/test/java/org/sonar/batch/util/ProgressReportTest.java b/sonar-batch/src/test/java/org/sonar/batch/util/ProgressReportTest.java
new file mode 100644
index 00000000000..dd2d131bf0e
--- /dev/null
+++ b/sonar-batch/src/test/java/org/sonar/batch/util/ProgressReportTest.java
@@ -0,0 +1,73 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.batch.util;
+
+import java.util.Set;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.Test;
+import org.junit.Before;
+
+public class ProgressReportTest {
+ private static final String THREAD_NAME = "progress";
+ private ProgressReport progressReport;
+
+ @Before
+ public void setUp() {
+ progressReport = new ProgressReport(THREAD_NAME, 1000);
+ }
+
+ @Test
+ public void die_on_stop() {
+ progressReport.start("start");
+ assertThat(isThreadAlive(THREAD_NAME)).isTrue();
+ progressReport.stop("stop");
+ assertThat(isThreadAlive(THREAD_NAME)).isFalse();
+ }
+
+ @Test
+ public void do_not_block_app() {
+ progressReport.start("start");
+ assertThat(isDaemon(THREAD_NAME)).isTrue();
+ progressReport.stop("stop");
+ }
+
+ private static boolean isDaemon(String name) {
+ Thread t = getThread(name);
+ return (t != null) && t.isDaemon();
+ }
+
+ private static boolean isThreadAlive(String name) {
+ Thread t = getThread(name);
+ return (t != null) && t.isAlive();
+ }
+
+ private static Thread getThread(String name) {
+ Set<Thread> threads = Thread.getAllStackTraces().keySet();
+
+ for (Thread t : threads) {
+ if (t.getName().equals(name)) {
+ return t;
+ }
+ }
+ return null;
+ }
+}