ConsulCoordinator implements ClusterRegister, ClusterNodesQuery { @Override public List<RemoteInstance> queryRemoteNodes() { HealthClient healthClient = client.healthClient(); List<ServiceHealth> nodes = healthClient.getHealthyServiceInstances(serviceName).getResponse(); List<RemoteInstance> remoteInstances = new ArrayList<>(); if (CollectionUtils.isNotEmpty(nodes)) { nodes.forEach(node -> { if (!Strings.isNullOrEmpty(node.getService().getAddress())) { Address address = new Address(node.getService().getAddress(), node.getService().getPort(), false); if (address.equals(selfAddress)) { address.setSelf(true); } remoteInstances.add(new RemoteInstance(address)); } }); } return remoteInstances; } ConsulCoordinator(ClusterModuleConsulConfig config, Consul client); @Override List<RemoteInstance> queryRemoteNodes(); @Override void registerRemote(RemoteInstance remoteInstance);  }
@Test @SuppressWarnings("unchecked") public void queryRemoteNodesWithNonOrEmpty() { when(consulResponse.getResponse()).thenReturn(null, Collections.emptyList()); assertEquals(0, coordinator.queryRemoteNodes().size()); assertEquals(0, coordinator.queryRemoteNodes().size()); }
@Test public void queryRemoteNodes() { registerSelfRemote(); List<ServiceHealth> serviceHealths = mockHealth(); when(consulResponse.getResponse()).thenReturn(serviceHealths); List<RemoteInstance> remoteInstances = coordinator.queryRemoteNodes(); assertEquals(2, remoteInstances.size()); RemoteInstance selfInstance = remoteInstances.get(0); velidate(selfRemoteAddress, selfInstance); RemoteInstance notSelfInstance = remoteInstances.get(1); velidate(remoteAddress, notSelfInstance); }
@Test public void queryRemoteNodesWithNullSelf() { List<ServiceHealth> serviceHealths = mockHealth(); when(consulResponse.getResponse()).thenReturn(serviceHealths); List<RemoteInstance> remoteInstances = coordinator.queryRemoteNodes(); assertEquals(2, remoteInstances.size()); }