mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/TestQueuedMessageSender.java
@Test
    public void testQueuingMultipleMessages() throws InterruptedException
    {
        // create some messages that will be queued
        final TerraHarvestMessage[] messages = new TerraHarvestMessage[10]; 
        for (int i=0; i < 10; i++)
        {
            messages[i] = constructMessage(i);
        }
        
        // mock channel to send messages successfully
        // answer will verify that messages are sent in the order they are queued
        when(m_Channel.trySendMessage(Mockito.any(TerraHarvestMessage.class))).thenAnswer(new Answer<Boolean>()
        {
            @Override
            public Boolean answer(InvocationOnMock invocation) throws Throwable
            {
                TerraHarvestMessage message = (TerraHarvestMessage)invocation.getArguments()[0];
                
                if (message.getMessageId() != m_NextMessageIndex)
                {
                    System.err.format("Expecting %d message id and got %d%n", m_NextMessageIndex, 
                            message.getMessageId());
                    m_OutOfOrderDetected = true;
                }
                m_NextMessageIndex++;
                
                // always report message as sent
                return true;
            }
        });

        for (int i=0; i < 10; i++)
        {
            boolean result = m_SUT.queue(messages[i]);
            assertThat("message is reported as queued", result, is(true));
        }
        
        // wait for the sender thread to attempt sending message
        Thread.sleep(1000);
        
        assertThat("answer method didn't find failure", m_OutOfOrderDetected, is(false));
        
        // verify each message is sent exactly once
        for (int i=0; i < 10; i++)
        {
            verify(m_Channel, times(1)).trySendMessage(messages[i]);
        }
    }
/**
     * Verify that a queued message will be sent to the channel for an attempt to send repeatedly as long as successful.
     * 
     * Verify messages are sent in order.
     */
Verify that a queued message will be sent to the channel for an attempt to send repeatedly as long as successful.

Verify messages are sent in order.
Verify that a queued message will be sent to the channel for an attempt to send repeatedly as long as successful . Verify messages are sent in order .
@Test
    public void testQueuingMultipleMessages() throws InterruptedException
    {
        final TerraHarvestMessage[] messages = new TerraHarvestMessage[10]; 
        for (int i=0; i < 10; i++)
        {
            messages[i] = constructMessage(i);
        }
        when(m_Channel.trySendMessage(Mockito.any(TerraHarvestMessage.class))).thenAnswer(new Answer<Boolean>()
        {
            @Override
            public Boolean answer(InvocationOnMock invocation) throws Throwable
            {
                TerraHarvestMessage message = (TerraHarvestMessage)invocation.getArguments()[0];
                if (message.getMessageId() != m_NextMessageIndex)
                {
                    System.err.format("Expecting %d message id and got %d%n", m_NextMessageIndex, 
                            message.getMessageId());
                    m_OutOfOrderDetected = true;
                }
                m_NextMessageIndex++;
                return true;
            }
        });
        for (int i=0; i < 10; i++)
        {
            boolean result = m_SUT.queue(messages[i]);
            assertThat("message is reported as queued", result, is(true));
        }
        Thread.sleep(1000);
        assertThat("answer method didn't find failure", m_OutOfOrderDetected, is(false));
        for (int i=0; i < 10; i++)
        {
            verify(m_Channel, times(1)).trySendMessage(messages[i]);
        }
    }
@ Test public void testQueuingMultipleMessages ( ) throws InterruptedException { final TerraHarvestMessage [ ] messages = new TerraHarvestMessage [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) { messages [ i ] = constructMessage ( i ) ; } when ( m_Channel . trySendMessage ( Mockito . any ( TerraHarvestMessage . class ) ) ) . thenAnswer ( new Answer < Boolean > ( ) { @ Override public Boolean answer ( InvocationOnMock invocation ) throws Throwable { TerraHarvestMessage message = ( TerraHarvestMessage ) invocation . getArguments ( ) [ 0 ] ; if ( message . getMessageId ( ) != m_NextMessageIndex ) { System . err . format ( "Expecting %d message id and got %d%n" , m_NextMessageIndex , message . getMessageId ( ) ) ; m_OutOfOrderDetected = true ; } m_NextMessageIndex ++ ; return true ; } } ) ; for ( int i = 0 ; i < 10 ; i ++ ) { boolean result = m_SUT . queue ( messages [ i ] ) ; assertThat ( "message is reported as queued" , result , is ( true ) ) ; } Thread . sleep ( 1000 ) ; assertThat ( "answer method didn't find failure" , m_OutOfOrderDetected , is ( false ) ) ; for ( int i = 0 ; i < 10 ; i ++ ) { verify ( m_Channel , times ( 1 ) ) . trySendMessage ( messages [ i ] ) ; } }
Verify that a queued message will be sent to the channel for an attempt to send repeatedly as long as successful.
Verify that a queued message will be sent to the channel for an attempt to send repeatedly as long as successful .
// create some messages that will be queued // mock channel to send messages successfully // answer will verify that messages are sent in the order they are queued // always report message as sent // wait for the sender thread to attempt sending message // verify each message is sent exactly once