Stabilize LiveReloadServerTests

Closes gh-27503
This commit is contained in:
Andy Wilkinson 2021-07-27 14:36:31 +01:00
parent 031a036c9f
commit ade922baec
2 changed files with 13 additions and 12 deletions

View File

@ -91,13 +91,12 @@ class Connection {
} }
private void runWebSocket() throws Exception { private void runWebSocket() throws Exception {
this.webSocket = true;
String accept = getWebsocketAcceptResponse(); String accept = getWebsocketAcceptResponse();
this.outputStream.writeHeaders("HTTP/1.1 101 Switching Protocols", "Upgrade: websocket", "Connection: Upgrade", this.outputStream.writeHeaders("HTTP/1.1 101 Switching Protocols", "Upgrade: websocket", "Connection: Upgrade",
"Sec-WebSocket-Accept: " + accept); "Sec-WebSocket-Accept: " + accept);
new Frame("{\"command\":\"hello\",\"protocols\":[\"http://livereload.com/protocols/official-7\"]," new Frame("{\"command\":\"hello\",\"protocols\":[\"http://livereload.com/protocols/official-7\"],"
+ "\"serverName\":\"spring-boot\"}").write(this.outputStream); + "\"serverName\":\"spring-boot\"}").write(this.outputStream);
Thread.sleep(100);
this.webSocket = true;
while (this.running) { while (this.running) {
readWebSocketFrame(); readWebSocketFrame();
} }

View File

@ -30,8 +30,10 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -218,17 +220,17 @@ class LiveReloadServerTests {
} }
static class LiveReloadWebSocketHandler extends TextWebSocketHandler { class LiveReloadWebSocketHandler extends TextWebSocketHandler {
private WebSocketSession session; private volatile WebSocketSession session;
private final CountDownLatch helloLatch = new CountDownLatch(2); private final CountDownLatch helloLatch = new CountDownLatch(2);
private final List<String> messages = new ArrayList<>(); private final List<String> messages = new CopyOnWriteArrayList<>();
private int pongCount; private final AtomicInteger pongCount = new AtomicInteger();
private CloseStatus closeStatus; private volatile CloseStatus closeStatus;
@Override @Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception { public void afterConnectionEstablished(WebSocketSession session) throws Exception {
@ -239,20 +241,20 @@ class LiveReloadServerTests {
void awaitHello() throws InterruptedException { void awaitHello() throws InterruptedException {
this.helloLatch.await(1, TimeUnit.MINUTES); this.helloLatch.await(1, TimeUnit.MINUTES);
Thread.sleep(200);
} }
@Override @Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) { protected void handleTextMessage(WebSocketSession session, TextMessage message) {
if (message.getPayload().contains("hello")) { String payload = message.getPayload();
this.messages.add(payload);
if (payload.contains("hello")) {
this.helloLatch.countDown(); this.helloLatch.countDown();
} }
this.messages.add(message.getPayload());
} }
@Override @Override
protected void handlePongMessage(WebSocketSession session, PongMessage message) { protected void handlePongMessage(WebSocketSession session, PongMessage message) {
this.pongCount++; this.pongCount.incrementAndGet();
} }
@Override @Override
@ -273,7 +275,7 @@ class LiveReloadServerTests {
} }
int getPongCount() { int getPongCount() {
return this.pongCount; return this.pongCount.get();
} }
CloseStatus getCloseStatus() { CloseStatus getCloseStatus() {