@Test
public void testDeliverMessages() throws Exception
{

    when( messengerDataService.getEnvelopes() ).thenReturn( Sets.newHashSet( envelope ) );
    when( envelope.getTimeToLive() ).thenReturn( TIME_TO_LIVE );
    when( envelope.getTargetPeerId() ).thenReturn( TARGET_PEER_ID );
    when( envelope.getCreateDate() ).thenReturn( CREATE_DATE );
    when( peerManager.getPeer( TARGET_PEER_ID ) ).thenReturn( peer );
    when( peer.getId() ).thenReturn( TARGET_PEER_ID );
    when( peerManager.getLocalPeer() ).thenReturn( localPeer );
      when( peerManager.getLocalPeerInfo() ).thenReturn( peerInfo );
    when( peerInfo.getId() ).thenReturn( UUID.randomUUID().toString() );
    when( localPeer.isLocal() ).thenReturn( false );
    when( localPeer.getId() ).thenReturn( UUID.randomUUID().toString() );
    when( completer.take() ).thenReturn( future );
    when( messenger.getPeerManager() ).thenReturn( peerManager );
    messageSender.deliverMessages();
    verify( completer ).submit( isA( RemotePeerMessageSender.class ) );
    when( peer.isLocal() ).thenReturn( true );
    messageSender.deliverMessages();
    verify( completer ).submit( isA( LocalPeerMessageSender.class ) );
    doThrow( new InterruptedException() ).when( completer ).take();
    messageSender.deliverMessages();
    verify( logger ).warn( anyString(), isA( InterruptedException.class ) );
}