mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/TestSocketMessageListener.java
@Test
    public void testRunWithEmptyMessage() throws InterruptedException, IOException
    {
        // setup the message to be received
        BaseNamespace baseMessage = BaseNamespace.newBuilder().
                setType(BaseMessageType.RequestControllerInfo).
                build();
        
        final TerraHarvestMessage message = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(0, 1, Namespace.Base, 100, baseMessage);
     
        Map<String, Object> props = new HashMap<String, Object>();
        Socket socket = mock(Socket.class);
        
        // setup data coming in, first size 0 message then actual message
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        output.write(0);  // all messages start with an octet count, this one is zero
        message.writeDelimitedTo(output);
        
        InputStream inStream = new ByteArrayInputStream(output.toByteArray());
        when(socket.getInputStream()).thenReturn(inStream);
        props.put(SocketMessageListener.SOCKET_PROP_KEY, socket);
        // mock the channel that will be sent in map
        AbstractSocketChannel channel = mock(AbstractSocketChannel.class);
        props.put(SocketMessageListener.CHANNEL_PROP_KEY, channel);
        m_SUT.activate(props);
        
        Thread thread = new Thread(m_SUT);
        thread.start();
        
        // wait to give thread time to start and try to read message
        Thread.sleep(1000);
        
        // the socket should read in the message and parse as the same message
        verify(m_MessageRouter).handleMessage(message, channel);
        
        // thread should complete on its own since it runs out of messages after 2
        thread.join(100);
        assertThat(thread.isAlive(), is(false));
    }
/**
     * Verify that if the input stream reads the message size as 0 for a message it will read a message after that.
     */
Verify that if the input stream reads the message size as 0 for a message it will read a message after that.
Verify that if the input stream reads the message size as 0 for a message it will read a message after that .
@Test
    public void testRunWithEmptyMessage() throws InterruptedException, IOException
    {
        BaseNamespace baseMessage = BaseNamespace.newBuilder().
                setType(BaseMessageType.RequestControllerInfo).
                build();
        final TerraHarvestMessage message = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(0, 1, Namespace.Base, 100, baseMessage);
        Map<String, Object> props = new HashMap<String, Object>();
        Socket socket = mock(Socket.class);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        output.write(0);  
        message.writeDelimitedTo(output);
        InputStream inStream = new ByteArrayInputStream(output.toByteArray());
        when(socket.getInputStream()).thenReturn(inStream);
        props.put(SocketMessageListener.SOCKET_PROP_KEY, socket);
        AbstractSocketChannel channel = mock(AbstractSocketChannel.class);
        props.put(SocketMessageListener.CHANNEL_PROP_KEY, channel);
        m_SUT.activate(props);
        Thread thread = new Thread(m_SUT);
        thread.start();
        Thread.sleep(1000);
        verify(m_MessageRouter).handleMessage(message, channel);
        thread.join(100);
        assertThat(thread.isAlive(), is(false));
    }
@ Test public void testRunWithEmptyMessage ( ) throws InterruptedException , IOException { BaseNamespace baseMessage = BaseNamespace . newBuilder ( ) . setType ( BaseMessageType . RequestControllerInfo ) . build ( ) ; final TerraHarvestMessage message = TerraHarvestMessageHelper . createTerraHarvestMessage ( 0 , 1 , Namespace . Base , 100 , baseMessage ) ; Map < String , Object > props = new HashMap < String , Object > ( ) ; Socket socket = mock ( Socket . class ) ; ByteArrayOutputStream output = new ByteArrayOutputStream ( ) ; output . write ( 0 ) ; message . writeDelimitedTo ( output ) ; InputStream inStream = new ByteArrayInputStream ( output . toByteArray ( ) ) ; when ( socket . getInputStream ( ) ) . thenReturn ( inStream ) ; props . put ( SocketMessageListener . SOCKET_PROP_KEY , socket ) ; AbstractSocketChannel channel = mock ( AbstractSocketChannel . class ) ; props . put ( SocketMessageListener . CHANNEL_PROP_KEY , channel ) ; m_SUT . activate ( props ) ; Thread thread = new Thread ( m_SUT ) ; thread . start ( ) ; Thread . sleep ( 1000 ) ; verify ( m_MessageRouter ) . handleMessage ( message , channel ) ; thread . join ( 100 ) ; assertThat ( thread . isAlive ( ) , is ( false ) ) ; }
Verify that if the input stream reads the message size as 0 for a message it will read a message after that.
Verify that if the input stream reads the message size as 0 for a message it will read a message after that .
// setup the message to be received // setup data coming in, first size 0 message then actual message // all messages start with an octet count, this one is zero // mock the channel that will be sent in map // wait to give thread time to start and try to read message // the socket should read in the message and parse as the same message // thread should complete on its own since it runs out of messages after 2