mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/TestSocketMessageListener.java
@Test
    public void testRunMultipleReadsWithEof() 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 byte for size, will only return 3 bytes before reaching EOF
        when(inStream.read()).thenReturn(6);
        // mock rest of message returned in two different segments
        when(inStream.read(Mockito.any(byte[].class), anyInt(), anyInt())).thenAnswer(new Answer<Integer>()
        {
            private int step = 0;

            @Override
            public Integer answer(InvocationOnMock invocation) throws Throwable
            {
                byte[] buffer = (byte[])invocation.getArguments()[0];
                if (step == 0)  // first read will get the first half of the message
                {
                    buffer[0] = 0x01;
                    buffer[1] = 0x02;
                    buffer[2] = 0x03;
                    step++;
                    return 3;
                }
                
                return -1;  // don't reset step count so EOF will be returned forever
            }
        });
        
        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 EOF is reached
        thread.join(1000);
        
        assertThat(thread.isAlive(), is(false));
        
        // ensure input stream and socket gets closed
        verify(inStream).close();
        verify(socket).close();
    }
/**
     * Verify that if the input stream reaches EOF in the middle of a message the thread will stop.
     */
Verify that if the input stream reaches EOF in the middle of a message the thread will stop.
Verify that if the input stream reaches EOF in the middle of a message the thread will stop .
@Test
    public void testRunMultipleReadsWithEof() throws InterruptedException, IOException
    {
        Map<String, Object> props = new HashMap<String, Object>();
        Socket socket = mock(Socket.class);
        InputStream inStream = mock(InputStream.class);
        when(inStream.read()).thenReturn(6);
        when(inStream.read(Mockito.any(byte[].class), anyInt(), anyInt())).thenAnswer(new Answer<Integer>()
        {
            private int step = 0;
            @Override
            public Integer answer(InvocationOnMock invocation) throws Throwable
            {
                byte[] buffer = (byte[])invocation.getArguments()[0];
                if (step == 0)  
                {
                    buffer[0] = 0x01;
                    buffer[1] = 0x02;
                    buffer[2] = 0x03;
                    step++;
                    return 3;
                }
                return -1;  
            }
        });
        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();
    }
@ Test public void testRunMultipleReadsWithEof ( ) throws InterruptedException , IOException { Map < String , Object > props = new HashMap < String , Object > ( ) ; Socket socket = mock ( Socket . class ) ; InputStream inStream = mock ( InputStream . class ) ; when ( inStream . read ( ) ) . thenReturn ( 6 ) ; when ( inStream . read ( Mockito . any ( byte [ ] . class ) , anyInt ( ) , anyInt ( ) ) ) . thenAnswer ( new Answer < Integer > ( ) { private int step = 0 ; @ Override public Integer answer ( InvocationOnMock invocation ) throws Throwable { byte [ ] buffer = ( byte [ ] ) invocation . getArguments ( ) [ 0 ] ; if ( step == 0 ) { buffer [ 0 ] = 0x01 ; buffer [ 1 ] = 0x02 ; buffer [ 2 ] = 0x03 ; step ++ ; return 3 ; } return - 1 ; } } ) ; 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 that if the input stream reaches EOF in the middle of a message the thread will stop.
Verify that if the input stream reaches EOF in the middle of a message the thread will stop .
// simulate received message from socket // mock initial byte for size, will only return 3 bytes before reaching EOF // mock rest of message returned in two different segments // first read will get the first half of the message // don't reset step count so EOF will be returned forever // should exit out immediately without a call to deactivate since EOF is reached // ensure input stream and socket gets closed