HostRegistryImpl implements HostRegistry { void updateHosts() { try { Set<ResourceHostInfo> cachedResourceHosts = getResourceHostsInfo(); Set<ResourceHost> registeredResourceHosts = Sets.newHashSet(); LocalPeer localPeer = getLocalPeer(); if ( localPeer != null ) { registeredResourceHosts.addAll( localPeer.getResourceHosts() ); } if ( cachedResourceHosts.size() > registeredResourceHosts.size() ) { requestHeartbeats( cachedResourceHosts ); return; } else if ( localPeer != null ) { boolean noManagement = false; try { localPeer.getManagementHost(); } catch ( HostNotFoundException e ) { noManagement = true; } if ( noManagement ) { Set<ResourceHostInfo> allHosts = Sets.newHashSet(); allHosts.addAll( registeredResourceHosts ); allHosts.addAll( cachedResourceHosts ); requestHeartbeats( allHosts ); return; } } if ( !CollectionUtil.isCollectionEmpty( cachedResourceHosts ) ) { checkAndUpdateHosts( cachedResourceHosts ); } if ( !CollectionUtil.isCollectionEmpty( registeredResourceHosts ) ) { checkAndUpdateHosts( Sets.<ResourceHostInfo>newHashSet( registeredResourceHosts ) ); } } catch ( Exception e ) { LOG.error( "Error checking hosts: {}", e.getMessage() ); } }  @Override void updateResourceHostEntryTimestamp( final String resourceHostId ); @Override ContainerHostInfo getContainerHostInfoById( final String id ); @Override ContainerHostInfo getContainerHostInfoByHostname( final String hostname ); @Override ContainerHostInfo getContainerHostInfoByContainerName( final String containerName ); @Override Set<ContainerHostInfo> getContainerHostsInfo(); @Override ResourceHostInfo getResourceHostInfoById( final String id ); @Override ResourceHostInfo getResourceHostInfoByHostname( final String hostname ); @Override Set<ResourceHostInfo> getResourceHostsInfo(); @Override ResourceHostInfo getResourceHostByContainerHost( final ContainerHostInfo containerHostInfo ); @Override HostInfo getHostInfoById( final String hostId ); @Override void removeResourceHost( final String id ); @Override void addHostListener( final HostListener listener ); @Override void removeHostListener( final HostListener listener ); void init(); void dispose(); @Override boolean pingHost( String hostIp );  }
@Test public void testUpdateHosts() throws Exception { LocalPeer localPeer = mock( LocalPeer.class ); ResourceHost resourceHost = mock( ResourceHost.class ); doReturn( localPeer ).when( registry ).getLocalPeer(); doReturn( Sets.newHashSet( resourceHost ) ).when( localPeer ).getResourceHosts(); registry.updateHosts(); verify( registry, times( 2 ) ).checkAndUpdateHosts( anySet() ); }