mil.dod.th.remote.client/test/mil/dod/th/remote/client/impl/TestMessageListenerServiceImpl.java
@Test
    public void testAddRemoveRemoteChannel() throws InterruptedException, IOException
    {
        final Semaphore readWait = new Semaphore(0);
        InputStream input = mock(InputStream.class);
        doAnswer(new Answer<Object>()
        {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable
            {
                readWait.acquire();
                throw new IOException("close");
            }
        }).when(input).read();
        doAnswer(new Answer<Object>()
        {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable
            {
                readWait.release();
                return null;
            }
        }).when(input).close();

        m_SUT.addRemoteChannel(SRC_ID, input, m_ChannelCallback);

        // Wait to make sure the background thread is running
        Thread.sleep(100);

        m_SUT.removeRemoteChannel(SRC_ID);

        // Wait for onChannelRemoved
        assertThat(m_CallbackWaitRem.tryAcquire(1, TimeUnit.SECONDS), is(true));

        // Should not be able to remove channel twice
        try
        {
            m_SUT.removeRemoteChannel(SRC_ID);
            fail("removeRemoteChannel should throw IllegalArgumentException");
        }
        catch (IllegalArgumentException e)
        {
            // Expected
        }
    }
/**
     * Verify that a remote channel can be added and removed with appropriate callbacks made.
     */
Verify that a remote channel can be added and removed with appropriate callbacks made.
Verify that a remote channel can be added and removed with appropriate callbacks made .
@Test
    public void testAddRemoveRemoteChannel() throws InterruptedException, IOException
    {
        final Semaphore readWait = new Semaphore(0);
        InputStream input = mock(InputStream.class);
        doAnswer(new Answer<Object>()
        {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable
            {
                readWait.acquire();
                throw new IOException("close");
            }
        }).when(input).read();
        doAnswer(new Answer<Object>()
        {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable
            {
                readWait.release();
                return null;
            }
        }).when(input).close();
        m_SUT.addRemoteChannel(SRC_ID, input, m_ChannelCallback);
        Thread.sleep(100);
        m_SUT.removeRemoteChannel(SRC_ID);
        assertThat(m_CallbackWaitRem.tryAcquire(1, TimeUnit.SECONDS), is(true));
        try
        {
            m_SUT.removeRemoteChannel(SRC_ID);
            fail("removeRemoteChannel should throw IllegalArgumentException");
        }
        catch (IllegalArgumentException e)
        {
        }
    }
@ Test public void testAddRemoveRemoteChannel ( ) throws InterruptedException , IOException { final Semaphore readWait = new Semaphore ( 0 ) ; InputStream input = mock ( InputStream . class ) ; doAnswer ( new Answer < Object > ( ) { @ Override public Object answer ( InvocationOnMock invocation ) throws Throwable { readWait . acquire ( ) ; throw new IOException ( "close" ) ; } } ) . when ( input ) . read ( ) ; doAnswer ( new Answer < Object > ( ) { @ Override public Object answer ( InvocationOnMock invocation ) throws Throwable { readWait . release ( ) ; return null ; } } ) . when ( input ) . close ( ) ; m_SUT . addRemoteChannel ( SRC_ID , input , m_ChannelCallback ) ; Thread . sleep ( 100 ) ; m_SUT . removeRemoteChannel ( SRC_ID ) ; assertThat ( m_CallbackWaitRem . tryAcquire ( 1 , TimeUnit . SECONDS ) , is ( true ) ) ; try { m_SUT . removeRemoteChannel ( SRC_ID ) ; fail ( "removeRemoteChannel should throw IllegalArgumentException" ) ; } catch ( IllegalArgumentException e ) { } }
Verify that a remote channel can be added and removed with appropriate callbacks made.
Verify that a remote channel can be added and removed with appropriate callbacks made .
// Wait to make sure the background thread is running // Wait for onChannelRemoved // Should not be able to remove channel twice // Expected