RequestNotifier implements Runnable { @Override public void run() { MessageResponse messageResponse = null; try { Object response = listener.onRequest( messageRequest.getPayload() ); if ( response != null ) { messageResponse = new MessageResponse( messageRequest.getId(), new Payload( response, localPeer.getId() ), null ); } } catch ( Exception e ) { LOG.error( "Error thrown by RequestListener", e ); messageResponse = new MessageResponse( messageRequest.getId(), null, e.getMessage() ); } if ( messageResponse != null ) { Message responseMessage = messenger.createMessage( messageResponse ); try { Peer sourcePeer = getPeerManager().getPeer( message.getSourcePeerId() ); messenger.sendMessage( sourcePeer, responseMessage, RecipientType.PEER_RESPONSE_LISTENER.name(), Timeouts.PEER_MESSAGE_TIMEOUT, messageRequest.getHeaders() ); } catch ( PeerException | MessageException e ) { LOG.error( "Error sending response to peer message", e ); } } } RequestNotifier( final Messenger messenger, final RequestListener listener, final Message message,
                            final MessageRequest messageRequest, final LocalPeer localPeer ); @Override void run();  }
@Test public void testRun() throws Exception { requestNotifier.run(); verify( messenger ).sendMessage( eq( peer ), any( Message.class ), anyString(), anyInt(), anyMap() ); doThrow( messageException ).when( messenger ) .sendMessage( eq( peer ), any( Message.class ), anyString(), anyInt(), anyMap() ); requestNotifier.run(); verify( messageException ).printStackTrace( any( PrintStream.class ) ); doThrow( exception ).when( requestListener ).onRequest( payload ); requestNotifier.run(); verify( exception ).printStackTrace( any( PrintStream.class ) ); }