Fix race condition in LiveReloadServerTests.clientClose

pull/4119/head
Andy Wilkinson 9 years ago
parent 500eeb6a70
commit b7ecccf0a3

@ -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<ConnectionClosedException> closedExceptions = new ArrayList<ConnectionClosedException>();
private final List<ConnectionClosedException> closedExceptions = new ArrayList<ConnectionClosedException>();
private final Object monitor = new Object();
MonitoredLiveReloadServer(int port) {
super(port);
@ -234,7 +246,9 @@ public class LiveReloadServerTests {
}
public List<ConnectionClosedException> getClosedExceptions() {
return this.closedExceptions;
synchronized (this.monitor) {
return new ArrayList<ConnectionClosedException>(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;
}
}

Loading…
Cancel
Save