@Override public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { this.delegate.afterConnectionClosed(session, closeStatus); }
@Override public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) { try { getDelegate().afterConnectionClosed(session, closeStatus); } catch (Throwable ex) { if (logger.isWarnEnabled()) { logger.warn("Unhandled exception after connection closed for " + this, ex); } } }
@OnWebSocketClose public void onWebSocketClose(int statusCode, String reason) { CloseStatus closeStatus = new CloseStatus(statusCode, reason); try { this.webSocketHandler.afterConnectionClosed(this.wsSession, closeStatus); } catch (Throwable ex) { if (logger.isWarnEnabled()) { logger.warn("Unhandled exception after connection closed for " + this, ex); } } }
@Override public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { try { getHandler(session).afterConnectionClosed(session, closeStatus); } finally { destroyHandler(session); } }
@Override public void onClose(javax.websocket.Session session, CloseReason reason) { CloseStatus closeStatus = new CloseStatus(reason.getCloseCode().getCode(), reason.getReasonPhrase()); try { this.handler.afterConnectionClosed(this.wsSession, closeStatus); } catch (Throwable ex) { if (logger.isWarnEnabled()) { logger.warn("Unhandled on-close exception for " + this.wsSession, ex); } } }
@Test @SuppressWarnings({"rawtypes", "unchecked"}) public void cancelInactivityTasks() throws Exception { TcpConnection<byte[]> tcpConnection = getTcpConnection(); ScheduledFuture future = mock(ScheduledFuture.class); when(this.taskScheduler.scheduleWithFixedDelay(any(), eq(1L))).thenReturn(future); tcpConnection.onReadInactivity(mock(Runnable.class), 2L); tcpConnection.onWriteInactivity(mock(Runnable.class), 2L); this.webSocketHandlerCaptor.getValue().afterConnectionClosed(this.webSocketSession, CloseStatus.NORMAL); verify(future, times(2)).cancel(true); verifyNoMoreInteractions(future); }
@Test public void webSocketConnectionClosed() throws Exception { connect().afterConnectionClosed(this.webSocketSession, CloseStatus.NORMAL); verify(this.stompSession).afterConnectionClosed(); }
@Test public void onClose() throws Throwable { this.adapter.onWebSocketClose(1000, "reason"); verify(this.webSocketHandler).afterConnectionClosed(this.webSocketSession, CloseStatus.NORMAL.withReason("reason")); }
@Test public void afterConnectionClosed() throws Exception { CloseStatus closeStatus = CloseStatus.NORMAL; willThrow(new IllegalStateException("error")) .given(this.delegate).afterConnectionClosed(this.session, closeStatus); this.decorator.afterConnectionClosed(this.session, closeStatus); assertNull(this.session.getCloseStatus()); }
@Test public void onClose() throws Throwable { this.adapter.onClose(this.session, new CloseReason(CloseCodes.NORMAL_CLOSURE, "reason")); verify(this.webSocketHandler).afterConnectionClosed(this.webSocketSession, CloseStatus.NORMAL.withReason("reason")); }
@Test public void closeWithWebSocketHandlerExceptions() throws Exception { willThrow(new Exception()).given(this.webSocketHandler).afterConnectionClosed(this.session, CloseStatus.NORMAL); this.session.delegateConnectionEstablished(); this.session.setActive(true); this.session.close(CloseStatus.NORMAL); assertEquals(CloseStatus.NORMAL, this.session.getCloseStatus()); assertClosed(); }
@Test public void writeFrameIoException() throws Exception { this.session.setExceptionOnWrite(new IOException()); this.session.delegateConnectionEstablished(); try { this.session.writeFrame(SockJsFrame.openFrame()); fail("expected exception"); } catch (SockJsTransportFailureException ex) { assertEquals(CloseStatus.SERVER_ERROR, this.session.getCloseStatus()); verify(this.webSocketHandler).afterConnectionClosed(this.session, CloseStatus.SERVER_ERROR); } }
@Test public void delegateConnectionClosed() throws Exception { this.session.delegateConnectionEstablished(); this.session.delegateConnectionClosed(CloseStatus.GOING_AWAY); assertClosed(); assertEquals(1, this.session.getNumberOfLastActiveTimeUpdates()); verify(this.webSocketHandler).afterConnectionClosed(this.session, CloseStatus.GOING_AWAY); }
@Test public void connectReceiveAndCloseWithPrelude() throws Exception { StringBuilder sb = new StringBuilder(2048); for (int i = 0; i < 2048; i++) { sb.append('h'); } String body = sb.toString() + "\n" + "o\n" + "a[\"foo\"]\n" + "c[3000,\"Go away!\"]"; ClientHttpResponse response = response(HttpStatus.OK, body); connect(response); verify(this.webSocketHandler).afterConnectionEstablished(any()); verify(this.webSocketHandler).handleMessage(any(), eq(new TextMessage("foo"))); verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!"))); verifyNoMoreInteractions(this.webSocketHandler); }
@Test public void afterTransportClosed() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.afterTransportClosed(CloseStatus.SERVER_ERROR); assertThat(this.session.isOpen(), equalTo(false)); verify(this.handler).afterConnectionEstablished(this.session); verify(this.handler).afterConnectionClosed(this.session, CloseStatus.SERVER_ERROR); verifyNoMoreInteractions(this.handler); }
@Test public void close() throws Exception { this.session.delegateConnectionEstablished(); assertOpen(); this.session.setActive(true); this.session.close(); assertEquals(1, this.session.getSockJsFramesWritten().size()); assertEquals(SockJsFrame.closeFrameGoAway(), this.session.getSockJsFramesWritten().get(0)); assertEquals(1, this.session.getNumberOfLastActiveTimeUpdates()); assertTrue(this.session.didCancelHeartbeat()); assertEquals(new CloseStatus(3000, "Go away!"), this.session.getCloseStatus()); assertClosed(); verify(this.webSocketHandler).afterConnectionClosed(this.session, new CloseStatus(3000, "Go away!")); }
@Test public void responseClosedAfterDisconnected() throws Exception { String body = "o\n" + "c[3000,\"Go away!\"]\n" + "a[\"foo\"]\n"; ClientHttpResponse response = response(HttpStatus.OK, body); connect(response); verify(this.webSocketHandler).afterConnectionEstablished(any()); verify(this.webSocketHandler).afterConnectionClosed(any(), any()); verifyNoMoreInteractions(this.webSocketHandler); verify(response).close(); }
@Test public void errorResponseStatus() throws Exception { connect(response(HttpStatus.OK, "o\n"), response(HttpStatus.INTERNAL_SERVER_ERROR, "Oops")); verify(this.webSocketHandler).afterConnectionEstablished(any()); verify(this.webSocketHandler).handleTransportError(any(), any()); verify(this.webSocketHandler).afterConnectionClosed(any(), any()); verifyNoMoreInteractions(this.webSocketHandler); }
@Test public void connectReceiveAndClose() throws Exception { String body = "o\n" + "a[\"foo\"]\n" + "c[3000,\"Go away!\"]"; ClientHttpResponse response = response(HttpStatus.OK, body); connect(response); verify(this.webSocketHandler).afterConnectionEstablished(any()); verify(this.webSocketHandler).handleMessage(any(), eq(new TextMessage("foo"))); verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!"))); verifyNoMoreInteractions(this.webSocketHandler); }
@Test public void connectReceiveAndCloseWithStompFrame() throws Exception { StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND); accessor.setDestination("/destination"); MessageHeaders headers = accessor.getMessageHeaders(); Message<byte[]> message = MessageBuilder.createMessage("body".getBytes(StandardCharsets.UTF_8), headers); byte[] bytes = new StompEncoder().encode(message); TextMessage textMessage = new TextMessage(bytes); SockJsFrame frame = SockJsFrame.messageFrame(new Jackson2SockJsMessageCodec(), textMessage.getPayload()); String body = "o\n" + frame.getContent() + "\n" + "c[3000,\"Go away!\"]"; ClientHttpResponse response = response(HttpStatus.OK, body); connect(response); verify(this.webSocketHandler).afterConnectionEstablished(any()); verify(this.webSocketHandler).handleMessage(any(), eq(textMessage)); verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!"))); verifyNoMoreInteractions(this.webSocketHandler); }