mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/TestSocketMessageListener.java
@Test
    public void testRunSocketClosedDuringRead() throws InterruptedException, IOException
    {
        // simulate received message from socket
        Map<String, Object> props = new HashMap<String, Object>();
        Socket socket = mock(Socket.class);
        
        InputStream inStream = mock(InputStream.class);
        // mock initial size read to throw exception
        when(inStream.read()).thenThrow(new SocketException());
        when(socket.getInputStream()).thenReturn(inStream);
        props.put(SocketMessageListener.SOCKET_PROP_KEY, socket);
        m_SUT.activate(props);
        
        Thread thread = new Thread(m_SUT);
        thread.start();
        
        // should exit out immediately without a call to deactivate since input stream is closed
        thread.join(1000);
        
        assertThat(thread.isAlive(), is(false));
        
        // ensure input stream and socket gets closed
        verify(inStream).close();
        verify(socket).close();
        
        // no other way to check, but we don't want a stack trace to be logged as code should handle this as a normal
        // condition
        verify(m_Logging, never()).error(Mockito.any(Throwable.class), anyString());
    }
/**
     * Verify that if the input stream is closed in the middle of reading, the thread will stop.
     */
Verify that if the input stream is closed in the middle of reading, the thread will stop.
Verify that if the input stream is closed in the middle of reading the thread will stop .
@Test
    public void testRunSocketClosedDuringRead() throws InterruptedException, IOException
    {
        Map<String, Object> props = new HashMap<String, Object>();
        Socket socket = mock(Socket.class);
        InputStream inStream = mock(InputStream.class);
        when(inStream.read()).thenThrow(new SocketException());
        when(socket.getInputStream()).thenReturn(inStream);
        props.put(SocketMessageListener.SOCKET_PROP_KEY, socket);
        m_SUT.activate(props);
        Thread thread = new Thread(m_SUT);
        thread.start();
        thread.join(1000);
        assertThat(thread.isAlive(), is(false));
        verify(inStream).close();
        verify(socket).close();
        verify(m_Logging, never()).error(Mockito.any(Throwable.class), anyString());
    }
@ Test public void testRunSocketClosedDuringRead ( ) throws InterruptedException , IOException { Map < String , Object > props = new HashMap < String , Object > ( ) ; Socket socket = mock ( Socket . class ) ; InputStream inStream = mock ( InputStream . class ) ; when ( inStream . read ( ) ) . thenThrow ( new SocketException ( ) ) ; when ( socket . getInputStream ( ) ) . thenReturn ( inStream ) ; props . put ( SocketMessageListener . SOCKET_PROP_KEY , socket ) ; m_SUT . activate ( props ) ; Thread thread = new Thread ( m_SUT ) ; thread . start ( ) ; thread . join ( 1000 ) ; assertThat ( thread . isAlive ( ) , is ( false ) ) ; verify ( inStream ) . close ( ) ; verify ( socket ) . close ( ) ; verify ( m_Logging , never ( ) ) . error ( Mockito . any ( Throwable . class ) , anyString ( ) ) ; }
Verify that if the input stream is closed in the middle of reading, the thread will stop.
Verify that if the input stream is closed in the middle of reading the thread will stop .
// simulate received message from socket // mock initial size read to throw exception // should exit out immediately without a call to deactivate since input stream is closed // ensure input stream and socket gets closed // no other way to check, but we don't want a stack trace to be logged as code should handle this as a normal // condition