From b7ecccf0a3a691b79f8df7c14d34a5d11f4edebd Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 7 Oct 2015 13:39:59 +0100 Subject: [PATCH] Fix race condition in LiveReloadServerTests.clientClose --- .../livereload/LiveReloadServerTests.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/LiveReloadServerTests.java b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/LiveReloadServerTests.java index 624e051ca0..103e92aa44 100644 --- a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/LiveReloadServerTests.java +++ b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/LiveReloadServerTests.java @@ -48,6 +48,7 @@ import static org.junit.Assert.assertThat; * Tests for {@link LiveReloadServer}. * * @author Phillip Webb + * @author Andy Wilkinson */ public class LiveReloadServerTests { @@ -122,9 +123,18 @@ public class LiveReloadServerTests { finally { client.stop(); } + awaitClosedException(); assertThat(this.server.getClosedExceptions().size(), greaterThan(0)); } + private void awaitClosedException() throws InterruptedException { + long startTime = System.currentTimeMillis(); + while (this.server.getClosedExceptions().isEmpty() + && System.currentTimeMillis() - startTime > 10000) { + Thread.sleep(100); + } + } + @Test public void serverClose() throws Exception { WebSocketClient client = new WebSocketClient(); @@ -221,7 +231,9 @@ public class LiveReloadServerTests { */ private static class MonitoredLiveReloadServer extends LiveReloadServer { - private List closedExceptions = new ArrayList(); + private final List closedExceptions = new ArrayList(); + + private final Object monitor = new Object(); MonitoredLiveReloadServer(int port) { super(port); @@ -234,7 +246,9 @@ public class LiveReloadServerTests { } public List getClosedExceptions() { - return this.closedExceptions; + synchronized (this.monitor) { + return new ArrayList(this.closedExceptions); + } } private class MonitoredConnection extends Connection { @@ -250,7 +264,9 @@ public class LiveReloadServerTests { super.run(); } catch (ConnectionClosedException ex) { - MonitoredLiveReloadServer.this.closedExceptions.add(ex); + synchronized (MonitoredLiveReloadServer.this.monitor) { + MonitoredLiveReloadServer.this.closedExceptions.add(ex); + } throw ex; } }