aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-process-monitor
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2014-09-12 22:35:06 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2014-09-12 22:35:06 +0200
commit824a5d0ba8caeea4b8287da4066609a8bfa5268d (patch)
treea5ab508d9c15651f2d4a6acdb22db68c4d814613 /server/sonar-process-monitor
parent5de5566591c3fa67edee667e482cbde9312227ee (diff)
downloadsonarqube-824a5d0ba8caeea4b8287da4066609a8bfa5268d.tar.gz
sonarqube-824a5d0ba8caeea4b8287da4066609a8bfa5268d.zip
SONAR-4898 improve (again) stability of WatcherThreadTest
Diffstat (limited to 'server/sonar-process-monitor')
-rw-r--r--server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/WatcherThread.java22
-rw-r--r--server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/WatcherThreadTest.java17
2 files changed, 13 insertions, 26 deletions
diff --git a/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/WatcherThread.java b/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/WatcherThread.java
index a0b92f5ddc8..55880a989a7 100644
--- a/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/WatcherThread.java
+++ b/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/WatcherThread.java
@@ -20,7 +20,6 @@
package org.sonar.process.monitor;
import org.slf4j.LoggerFactory;
-import org.sonar.process.ProcessUtils;
/**
* This thread blocks as long as the monitored process is physically alive.
@@ -33,7 +32,7 @@ import org.sonar.process.ProcessUtils;
*/
class WatcherThread extends Thread {
- private final ProcessRef process;
+ private final ProcessRef processRef;
private final Monitor monitor;
WatcherThread(ProcessRef processRef, Monitor monitor) {
@@ -41,7 +40,7 @@ class WatcherThread extends Thread {
// and thread group
// -> do not override toString()
super(String.format("Watch[%s]", processRef.getKey()));
- this.process = processRef;
+ this.processRef = processRef;
this.monitor = monitor;
}
@@ -50,20 +49,15 @@ class WatcherThread extends Thread {
boolean alive = true;
while (alive) {
try {
- process.getProcess().waitFor();
- process.setTerminated(true);
- LoggerFactory.getLogger(getClass()).info(process + " is down");
+ processRef.getProcess().waitFor();
+ processRef.setTerminated(true);
+ alive = false;
+ LoggerFactory.getLogger(getClass()).info(String.format("%s is down", processRef));
+
// terminate all other processes, but in another thread
monitor.stop();
- alive = false;
} catch (InterruptedException ignored) {
- if (ProcessUtils.isAlive(process.getProcess())) {
- LoggerFactory.getLogger(getClass()).error(String.format(
- "Watcher of [%s] was interrupted but process is still alive. Killing it.", process.getKey()));
- }
- alive = false;
- } finally {
- process.hardKill();
+ // continue to watch process
}
}
}
diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/WatcherThreadTest.java b/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/WatcherThreadTest.java
index 5c953c1fef9..45febd53d73 100644
--- a/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/WatcherThreadTest.java
+++ b/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/WatcherThreadTest.java
@@ -21,26 +21,19 @@ package org.sonar.process.monitor;
import org.junit.Test;
import org.mockito.Mockito;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
import static org.mockito.Mockito.*;
public class WatcherThreadTest {
@Test(timeout = 10000L)
- public void kill_process_if_watcher_is_interrupted() throws Exception {
- ProcessRef ref = mock(ProcessRef.class, Mockito.RETURNS_DEEP_STUBS);
- when(ref.getProcess().waitFor()).thenAnswer(new Answer<Object>() {
- @Override
- public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
- throw new InterruptedException();
- }
- });
+ public void continue_even_if_interrupted() throws Exception {
Monitor monitor = mock(Monitor.class);
-
+ ProcessRef ref = mock(ProcessRef.class, Mockito.RETURNS_DEEP_STUBS);
+ when(ref.getProcess().waitFor()).thenThrow(new InterruptedException()).thenReturn(0);
WatcherThread watcher = new WatcherThread(ref, monitor);
watcher.start();
- verify(ref).hardKill();
+ watcher.join();
+ verify(monitor).stop();
}
}