@Test public void shouldNotThrowAnyExceptionWhenDataReceivedBeforeClose() throws Throwable { // Given WebSocketClient client = mock( WebSocketClient.class ); WebSocketConnection conn = new WebSocketConnection( client ); when( client.isStopped() ).thenReturn( true ); byte[] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // When conn.onWebSocketBinary( data, 0, 10 ); conn.recv( 10 ); // Then // no exception }
@Test public void shouldThrowIOExceptionWhenNotEnoughDataReceivedBeforeClose() throws Throwable { // Given WebSocketClient client = mock( WebSocketClient.class ); WebSocketConnection conn = new WebSocketConnection( client ); when( client.isStopped() ).thenReturn( true, true ); byte[] data = {0, 1, 2, 3}; // When && Then conn.onWebSocketBinary( data, 0, 4 ); expectedException.expect( IOException.class ); expectedException.expectMessage( "Connection closed while waiting for data from the server." ); conn.recv( 10 ); } }
/** * {@inheritDoc} */ @Override public boolean isConnected() { return (client != null) && !client.isStopped() && (session != null); }
/** * {@inheritDoc} */ @Override public synchronized void connect() throws SerialException { if (!isConnected()) { try { if (client.isStopping() || client.isStopped()) { client.start(); } client.connect(this, new URI(wsUrl)); this.wait(DEFAULT_CONNECT_TIME); if (this.session == null) { client.stop(); throw new SerialException("Failed to connect to " + alias + " in " + (DEFAULT_CONNECT_TIME / 1000) + " seconds"); } // Setting the idle timeout for the client connection. client.setMaxIdleTimeout(CLIENT_MAX_IDLE_TIMEOUT); } catch (Throwable t) { throw new SerialException("Failed to establish the web socket connection.", t); } } else { logger.warn(alias + " is already connected"); } }
@Override protected void connectNativeClient() throws TimeoutException, Exception { if (jettyClient == null || jettyClient.isStopped() || jettyClient.isStopping()) {