mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/TestSocketMessageListener.java
@Test(timeout = 4000)
    public void testDeactivate() throws InterruptedException, IOException
    {
        final TerraHarvestMessage message = TerraHarvestMessageHelper.createBaseMessage();
     
        // simulate received message from socket
        Socket socket = mock(Socket.class);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        message.writeDelimitedTo(output);
        
        // Circular array is used to keep the SocketMessageListener from terminating before deactivate is called.  If 
        // we do not use this class and pass in a regular InputStream object, the SocketMessageListener will read from 
        // the InputStream once. Then it will try to read again, but data is no longer there so the 
        // SocketMessageListener will set running to false and will deactivate itself.
        InputStream inStream = new CircularArrayInputStream(output.toByteArray());
      
        when(socket.getInputStream()).thenReturn(inStream);
        
        Map<String, Object> props = new HashMap<String, Object>();
        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();
       
        // give thread time to start
        Thread.sleep(1000);
        
        //make sure that the thread is alive
        assertThat(thread.isAlive(), is(true));
        
        // should cause thread to stop
        m_SUT.deactivate();
        
        // wait for thread to stop or just timeout
        thread.join(1000);
        
        // verify thread has stopped in case of timeout
        assertThat(thread.isAlive(), is(false));
    }
/**
     * Verify run method exits when deactivate is called.
     */
Verify run method exits when deactivate is called.
Verify run method exits when deactivate is called .
@Test(timeout = 4000)
    public void testDeactivate() throws InterruptedException, IOException
    {
        final TerraHarvestMessage message = TerraHarvestMessageHelper.createBaseMessage();
        Socket socket = mock(Socket.class);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        message.writeDelimitedTo(output);
        InputStream inStream = new CircularArrayInputStream(output.toByteArray());
        when(socket.getInputStream()).thenReturn(inStream);
        Map<String, Object> props = new HashMap<String, Object>();
        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);
        assertThat(thread.isAlive(), is(true));
        m_SUT.deactivate();
        thread.join(1000);
        assertThat(thread.isAlive(), is(false));
    }
@ Test ( timeout = 4000 ) public void testDeactivate ( ) throws InterruptedException , IOException { final TerraHarvestMessage message = TerraHarvestMessageHelper . createBaseMessage ( ) ; Socket socket = mock ( Socket . class ) ; ByteArrayOutputStream output = new ByteArrayOutputStream ( ) ; message . writeDelimitedTo ( output ) ; InputStream inStream = new CircularArrayInputStream ( output . toByteArray ( ) ) ; when ( socket . getInputStream ( ) ) . thenReturn ( inStream ) ; Map < String , Object > props = new HashMap < String , Object > ( ) ; 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 ) ; assertThat ( thread . isAlive ( ) , is ( true ) ) ; m_SUT . deactivate ( ) ; thread . join ( 1000 ) ; assertThat ( thread . isAlive ( ) , is ( false ) ) ; }
Verify run method exits when deactivate is called.
Verify run method exits when deactivate is called .
// simulate received message from socket // Circular array is used to keep the SocketMessageListener from terminating before deactivate is called.  If  // we do not use this class and pass in a regular InputStream object, the SocketMessageListener will read from  // the InputStream once. Then it will try to read again, but data is no longer there so the  // SocketMessageListener will set running to false and will deactivate itself. // mock the channel that will be sent in map // give thread time to start //make sure that the thread is alive // should cause thread to stop // wait for thread to stop or just timeout // verify thread has stopped in case of timeout