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;
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
+ private Thread waitingThread;
private File sharedDir;
private WebServerBarrier underTest;
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();
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
@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() {