@Test public void scheduleHeartbeatNotActive() throws Exception { this.session.setActive(false); this.session.scheduleHeartbeat(); verifyNoMoreInteractions(this.taskScheduler); }
@Test public void sendHeartbeatWhenDisabled() throws Exception { this.session.disableHeartbeat(); this.session.setActive(true); this.session.sendHeartbeat(); assertEquals(Collections.emptyList(), this.session.getSockJsFramesWritten()); }
@Test public void getTimeSinceLastActive() throws Exception { Thread.sleep(1); long time1 = this.session.getTimeSinceLastActive(); assertTrue(time1 > 0); Thread.sleep(1); long time2 = this.session.getTimeSinceLastActive(); assertTrue(time2 > time1); this.session.delegateConnectionEstablished(); Thread.sleep(1); this.session.setActive(false); assertTrue(this.session.getTimeSinceLastActive() > 0); this.session.setActive(true); assertEquals(0, this.session.getTimeSinceLastActive()); }
@Test public void tryCloseWithWebSocketHandlerExceptions() throws Exception { this.session.delegateConnectionEstablished(); this.session.setActive(true); this.session.tryCloseWithSockJsTransportError(new Exception(), CloseStatus.BAD_DATA); assertEquals(CloseStatus.BAD_DATA, this.session.getCloseStatus()); assertClosed(); }
@Test public void closeWhenNotActive() throws Exception { this.session.delegateConnectionEstablished(); assertOpen(); this.session.setActive(false); this.session.close(); assertEquals(Collections.emptyList(), this.session.getSockJsFramesWritten()); }
@Test public void closeWithWriteFrameExceptions() throws Exception { this.session.setExceptionOnWrite(new IOException()); this.session.delegateConnectionEstablished(); this.session.setActive(true); this.session.close(); assertEquals(new CloseStatus(3000, "Go away!"), this.session.getCloseStatus()); assertClosed(); }
@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 sendHeartbeat() throws Exception { this.session.setActive(true); this.session.sendHeartbeat(); assertEquals(1, this.session.getSockJsFramesWritten().size()); assertEquals(SockJsFrame.heartbeatFrame(), this.session.getSockJsFramesWritten().get(0)); verify(this.taskScheduler).schedule(any(Runnable.class), any(Date.class)); verifyNoMoreInteractions(this.taskScheduler); }
@Test public void scheduleAndCancelHeartbeat() throws Exception { ScheduledFuture<?> task = mock(ScheduledFuture.class); willReturn(task).given(this.taskScheduler).schedule(any(Runnable.class), any(Date.class)); this.session.setActive(true); this.session.scheduleHeartbeat(); verify(this.taskScheduler).schedule(any(Runnable.class), any(Date.class)); verifyNoMoreInteractions(this.taskScheduler); given(task.isCancelled()).willReturn(false); given(task.cancel(false)).willReturn(true); this.session.cancelHeartbeat(); verify(task).cancel(false); verifyNoMoreInteractions(task); }
@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!")); }