Polishing and minor refactoring

See gh-23828
This commit is contained in:
Rossen Stoyanchev
2020-03-13 12:10:46 +00:00
parent 7b7dfcaaed
commit 1a8caf9e2b
2 changed files with 46 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,7 +25,6 @@ import org.junit.jupiter.api.Test;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator;
import org.springframework.web.socket.sockjs.SockJsMessageDeliveryException;
import org.springframework.web.socket.sockjs.SockJsTransportFailureException;
@@ -48,9 +47,10 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
*/
public class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSession> {
@Override
protected TestSockJsSession initSockJsSession() {
return new TestSockJsSession("1", this.sockJsConfig, this.webSocketHandler, Collections.<String, Object>emptyMap());
return new TestSockJsSession("1", this.sockJsConfig, this.webSocketHandler, Collections.emptyMap());
}
@@ -94,8 +94,10 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSes
@Test
public void delegateMessages() throws Exception {
String msg1 = "message 1";
String msg2 = "message 2";
this.session.delegateMessages(msg1, msg2);
verify(this.webSocketHandler).handleMessage(this.session, new TextMessage(msg1));
@@ -105,24 +107,26 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSes
@Test
public void delegateMessagesWithErrorAndConnectionClosing() throws Exception {
WebSocketHandler wsHandler = new ExceptionWebSocketHandlerDecorator(this.webSocketHandler);
TestSockJsSession sockJsSession = new TestSockJsSession(
"1", this.sockJsConfig, wsHandler, Collections.<String, Object>emptyMap());
TestSockJsSession session = new TestSockJsSession("1", this.sockJsConfig,
new ExceptionWebSocketHandlerDecorator(this.webSocketHandler), Collections.emptyMap());
String msg1 = "message 1";
String msg2 = "message 2";
String msg3 = "message 3";
willThrow(new IOException()).given(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2));
willThrow(new IOException()).given(this.webSocketHandler).handleMessage(session, new TextMessage(msg2));
sockJsSession.delegateConnectionEstablished();
assertThatExceptionOfType(SockJsMessageDeliveryException.class).isThrownBy(() ->
sockJsSession.delegateMessages(msg1, msg2, msg3))
.satisfies(ex -> assertThat(ex.getUndeliveredMessages()).containsExactly(msg3));
verify(this.webSocketHandler).afterConnectionEstablished(sockJsSession);
verify(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg1));
verify(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2));
verify(this.webSocketHandler).afterConnectionClosed(sockJsSession, CloseStatus.SERVER_ERROR);
session.delegateConnectionEstablished();
assertThatExceptionOfType(SockJsMessageDeliveryException.class)
.isThrownBy(() -> session.delegateMessages(msg1, msg2, msg3))
.satisfies(ex -> assertThat(ex.getUndeliveredMessages()).containsExactly(msg3));
verify(this.webSocketHandler).afterConnectionEstablished(session);
verify(this.webSocketHandler).handleMessage(session, new TextMessage(msg1));
verify(this.webSocketHandler).handleMessage(session, new TextMessage(msg2));
verify(this.webSocketHandler).afterConnectionClosed(session, CloseStatus.SERVER_ERROR);
verifyNoMoreInteractions(this.webSocketHandler);
}
@@ -151,7 +155,7 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSes
assertThat(this.session.getCloseStatus().getCode()).isEqualTo(3000);
this.session.close(CloseStatus.SERVER_ERROR);
assertThat(this.session.getCloseStatus().getCode()).as("Close should be ignored if already closed").isEqualTo(3000);
assertThat(this.session.getCloseStatus().getCode()).as("Should ignore close if already closed").isEqualTo(3000);
}
@Test
@@ -219,7 +223,7 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSes
}
@Test
public void writeFrame() throws Exception {
public void writeFrame() {
this.session.writeFrame(SockJsFrame.openFrame());
assertThat(this.session.getSockJsFramesWritten().size()).isEqualTo(1);
@@ -238,7 +242,7 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSes
}
@Test
public void sendHeartbeat() throws Exception {
public void sendHeartbeat() {
this.session.setActive(true);
this.session.sendHeartbeat();
@@ -250,7 +254,7 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSes
}
@Test
public void scheduleHeartbeatNotActive() throws Exception {
public void scheduleHeartbeatNotActive() {
this.session.setActive(false);
this.session.scheduleHeartbeat();
@@ -258,7 +262,7 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSes
}
@Test
public void sendHeartbeatWhenDisabled() throws Exception {
public void sendHeartbeatWhenDisabled() {
this.session.disableHeartbeat();
this.session.setActive(true);
this.session.sendHeartbeat();
@@ -267,7 +271,7 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSes
}
@Test
public void scheduleAndCancelHeartbeat() throws Exception {
public void scheduleAndCancelHeartbeat() {
ScheduledFuture<?> task = mock(ScheduledFuture.class);
willReturn(task).given(this.taskScheduler).schedule(any(Runnable.class), any(Date.class));