HeartbeatTask extends TimerTask { @Override public void run() { if( Utils.isEmptyOrWhitespaces( this.agent.getApplicationName()) || Utils.isEmptyOrWhitespaces( this.agent.getScopedInstancePath()) || this.agent.resetInProgress.get()) return; try { MsgNotifHeartbeat heartBeat = new MsgNotifHeartbeat( this.agent.getApplicationName(), this.agent.getScopedInstancePath(), this.agent.getIpAddress()); heartBeat.setModelRequired( this.agent.needsModel()); this.logger.finer( "Model is required by the agent: " + heartBeat.isModelRequired()); IAgentClient messagingClient = this.agent.getMessagingClient(); if( messagingClient != null && messagingClient.isConnected()) messagingClient.sendMessageToTheDm( heartBeat ); } catch( Exception e ) { this.logger.severe( e.getMessage()); Utils.logException( this.logger, e ); } } HeartbeatTask( Agent agent ); @Override void run();  }
@Test public void testHeartbeat_connected() throws Exception { this.internalClient.openConnection(); Assert.assertTrue( this.agent.getMessagingClient().isConnected()); HeartbeatTask task = new HeartbeatTask( this.agent ); Assert.assertEquals( 0, this.internalClient.messagesForTheDm.size()); task.run(); Assert.assertEquals( 1, this.internalClient.messagesForTheDm.size()); Assert.assertEquals( MsgNotifHeartbeat.class, this.internalClient.messagesForTheDm.get( 0 ).getClass()); }
@Test public void testHeartbeat_notConnected() throws Exception { this.internalClient.closeConnection(); Assert.assertFalse( this.agent.getMessagingClient().isConnected()); HeartbeatTask task = new HeartbeatTask( this.agent ); Assert.assertEquals( 0, this.internalClient.messagesForTheDm.size()); task.run(); Assert.assertEquals( 0, this.internalClient.messagesForTheDm.size()); }
@Test public void testHeartbeat_agentWithoutIdentity_noApp() throws Exception { this.agent.setApplicationName( "" ); this.internalClient.openConnection(); Assert.assertTrue( this.agent.getMessagingClient().isConnected()); HeartbeatTask task = new HeartbeatTask( this.agent ); Assert.assertEquals( 0, this.internalClient.messagesForTheDm.size()); task.run(); Assert.assertEquals( 0, this.internalClient.messagesForTheDm.size()); }
@Test public void testHeartbeat_agentWithoutIdentity_noScopedInstancePath() throws Exception { this.agent.setScopedInstancePath( null ); this.internalClient.openConnection(); Assert.assertTrue( this.agent.getMessagingClient().isConnected()); HeartbeatTask task = new HeartbeatTask( this.agent ); Assert.assertEquals( 0, this.internalClient.messagesForTheDm.size()); task.run(); Assert.assertEquals( 0, this.internalClient.messagesForTheDm.size()); }
@Test public void testHeartbeat_resetInProgress() throws Exception { this.agent.resetInProgress.set( true ); Assert.assertFalse( Utils.isEmptyOrWhitespaces( this.agent.getApplicationName())); Assert.assertFalse( Utils.isEmptyOrWhitespaces( this.agent.getScopedInstancePath())); this.internalClient.openConnection(); Assert.assertTrue( this.agent.getMessagingClient().isConnected()); HeartbeatTask task = new HeartbeatTask( this.agent ); Assert.assertEquals( 0, this.internalClient.messagesForTheDm.size()); task.run(); Assert.assertEquals( 0, this.internalClient.messagesForTheDm.size()); }
@Test public void testHeartbeat_nullClient() { Agent agent = new Agent(); Assert.assertNull( agent.getMessagingClient()); HeartbeatTask task = new HeartbeatTask( agent ); task.run(); }
@Test public void testHeartbeat_exception() throws Exception { this.internalClient.openConnection(); this.internalClient.failMessageSending.set( true ); Assert.assertTrue( this.agent.getMessagingClient().isConnected()); HeartbeatTask task = new HeartbeatTask( this.agent ); Assert.assertEquals( 0, this.internalClient.messagesForTheDm.size()); task.run(); Assert.assertEquals( 0, this.internalClient.messagesForTheDm.size()); }