]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4898 improve (again) stability of WatcherThreadTest
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 12 Sep 2014 20:35:06 +0000 (22:35 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 12 Sep 2014 20:35:06 +0000 (22:35 +0200)
server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/WatcherThread.java
server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/WatcherThreadTest.java

index a0b92f5ddc8a11c08327fef17994750893db7aef..55880a989a7f4b2adfb90cbf4cc827bf60e4b2dd 100644 (file)
@@ -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
       }
     }
   }
index 5c953c1fef93f3ca4fa5475f45f69f1be4517c1b..45febd53d732f55c699ffff0e59d718b31fefb27 100644 (file)
@@ -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();
   }
 }