From 824a5d0ba8caeea4b8287da4066609a8bfa5268d Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 12 Sep 2014 22:35:06 +0200 Subject: [PATCH] SONAR-4898 improve (again) stability of WatcherThreadTest --- .../sonar/process/monitor/WatcherThread.java | 22 +++++++------------ .../process/monitor/WatcherThreadTest.java | 17 +++++--------- 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() { - @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(); } } -- 2.39.5