]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4898 improve stability of WatcherThreadTest on slow machines
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 12 Sep 2014 16:09:22 +0000 (18:09 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 12 Sep 2014 16:09:22 +0000 (18:09 +0200)
server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/WatcherThreadTest.java

index 86f3a47e6ff3821f8ae658e12b20a32d14adfff4..251dcdb21ea223a744905870542cd67efd859db5 100644 (file)
@@ -24,6 +24,8 @@ import org.mockito.Mockito;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 
+import java.util.concurrent.atomic.AtomicBoolean;
+
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -33,9 +35,11 @@ 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);
+    final AtomicBoolean waiting = new AtomicBoolean(false);
     when(ref.getProcess().waitFor()).thenAnswer(new Answer<Object>() {
       @Override
       public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
+        waiting.set(true);
         Thread.sleep(Long.MAX_VALUE);
         return 0;
       }
@@ -44,9 +48,11 @@ public class WatcherThreadTest {
 
     WatcherThread watcher = new WatcherThread(ref, monitor);
     watcher.start();
-    Thread.sleep(50L);
-    watcher.interrupt();
 
+    while (!waiting.get()) {
+      Thread.sleep(50L);
+    }
+    watcher.interrupt();
     verify(ref).hardKill();
   }
 }