NacosCoordinator implements ClusterRegister, ClusterNodesQuery { @Override public List<RemoteInstance> queryRemoteNodes() { List<RemoteInstance> result = new ArrayList<>(); try { List<Instance> instances = namingService.selectInstances(config.getServiceName(), true); if (CollectionUtils.isNotEmpty(instances)) { instances.forEach(instance -> { Address address = new Address(instance.getIp(), instance.getPort(), false); if (address.equals(selfAddress)) { address.setSelf(true); } result.add(new RemoteInstance(address)); }); } } catch (NacosException e) { throw new ServiceQueryException(e.getErrMsg()); } return result; } NacosCoordinator(NamingService namingService, ClusterModuleNacosConfig config); @Override List<RemoteInstance> queryRemoteNodes(); @Override void registerRemote(RemoteInstance remoteInstance);  }
@Test @SuppressWarnings("unchecked") public void queryRemoteNodesWithNonOrEmpty() throws NacosException { when(namingService.selectInstances(anyString(), anyBoolean())).thenReturn(null, Collections.emptyList()); assertEquals(0, coordinator.queryRemoteNodes().size()); }
@Test public void queryRemoteNodes() throws NacosException { registerSelfRemote(); List<Instance> instances = mockInstance(); when(namingService.selectInstances(anyString(), anyBoolean())).thenReturn(instances); List<RemoteInstance> remoteInstances = coordinator.queryRemoteNodes(); assertEquals(2, remoteInstances.size()); RemoteInstance selfInstance = remoteInstances.get(0); validate(selfRemoteAddress, selfInstance); RemoteInstance notSelfInstance = remoteInstances.get(1); validate(remoteAddress, notSelfInstance); }
@Test public void queryRemoteNodesWithNullSelf() throws NacosException { List<Instance> instances = mockInstance(); when(namingService.selectInstances(anyString(), anyBoolean())).thenReturn(instances); List<RemoteInstance> remoteInstances = coordinator.queryRemoteNodes(); assertEquals(remoteInstances.size(), instances.size()); }