From: Sébastien Lesaint Date: Tue, 7 Feb 2017 11:04:53 +0000 (+0100) Subject: WebServerBarrierTest may not shutdown all threads it spawns X-Git-Tag: 6.3-RC1~187 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=097b3884b38efd6e91e0b51ecc0fbed15d11cce2;p=sonarqube.git WebServerBarrierTest may not shutdown all threads it spawns --- diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/app/WebServerBarrierTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/app/WebServerBarrierTest.java index 8db3a8e8da8..b90c0bd588f 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/app/WebServerBarrierTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/app/WebServerBarrierTest.java @@ -22,6 +22,7 @@ package org.sonar.ce.app; import java.io.File; import java.util.Random; import java.util.concurrent.CountDownLatch; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -44,6 +45,7 @@ public class WebServerBarrierTest { @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); + private Thread waitingThread; private File sharedDir; private WebServerBarrier underTest; @@ -53,6 +55,13 @@ public class WebServerBarrierTest { underTest = new WebServerBarrier(sharedDir); } + @After + public void tearDown() throws Exception { + if (this.waitingThread != null) { + this.waitingThread.interrupt(); + } + } + @Test public void waitForOperational_does_not_log_anything_if_WebServer_already_operational() { setWebServerOperational(); @@ -66,14 +75,11 @@ public class WebServerBarrierTest { public void waitForOperational_blocks_until_WebServer_is_operational() throws InterruptedException { final CountDownLatch startedLatch = new CountDownLatch(1); final CountDownLatch doneLatch = new CountDownLatch(1); - Thread waitingThread = new Thread() { - @Override - public void run() { - startedLatch.countDown(); - underTest.waitForOperational(); - doneLatch.countDown(); - } - }; + waitingThread = new Thread(() -> { + startedLatch.countDown(); + underTest.waitForOperational(); + doneLatch.countDown(); + }); waitingThread.start(); // wait for waitingThread to be running @@ -92,13 +98,14 @@ public class WebServerBarrierTest { @Test public void waitForOperational_returns_false_if_thread_is_interrupted() throws InterruptedException { - WaitingThread waitingThread = new WaitingThread(new CountDownLatch(1)); - waitingThread.start(); + WaitingThread localThread = new WaitingThread(new CountDownLatch(1)); + waitingThread = localThread; + localThread.start(); - assertThat(waitingThread.latch.await(50, MILLISECONDS)).isTrue(); - waitingThread.interrupt(); + assertThat(localThread.latch.await(50, MILLISECONDS)).isTrue(); + localThread.interrupt(); - assertThat(waitingThread.result).isFalse(); + assertThat(localThread.result).isFalse(); } private void setWebServerOperational() {