protected void watchForUpdates() { List<ConsulHealthEntry> instancesHealth = loadCurrentHealthList(); LoadBalancerUpdate lbUpdate = diffServiceStatus(instancesHealth); if (! lbUpdate.isEmpty()) { loadbalancer.updateServiceEndpoints(lbUpdate); } }
protected void reportInitialServicesList(List<ConsulHealthEntry> instances) { LoadBalancerUpdate update = new LoadBalancerUpdate(); for (ConsulHealthEntry service : instances) { update.addNewService(newServiceEndpoint(service)); discoveredServices.put(service.getId(), service); } loadbalancer.updateServiceEndpoints(update); }
@Test public void verifyMainLoop() { LoadBalancer lb = mock(LoadBalancer.class); worker.setLoadbalancer(lb); worker.shutdownSemaphore.release(); worker.sleeper = mock(Sleeper.class); when(response.getContentAsString()).thenReturn(null).thenReturn(healthInfo).thenReturn(healthInfo); worker.run(); verify(lb, times(1)).updateServiceEndpoints(any(LoadBalancerUpdate.class)); } }
@Test public void initialServiceReported() { List<ConsulHealthEntry> list = new ArrayList<>(); ConsulHealthEntry entry = new ConsulHealthEntry("1", ConsulHealthEntry.Status.Passing, "1.1.1.1", 1); list.add(entry); LoadBalancer lb = mock(LoadBalancer.class); worker.setLoadbalancer(lb); worker.reportInitialServicesList(list); assertThat(worker.discoveredServices).hasSize(1); verify(lb, times(1)).updateServiceEndpoints(any(LoadBalancerUpdate.class)); }
@Test public void serviceFlappingAfterStartup() throws Exception { //there was a bug where if a consumed service went down and came back, there //were extra and incorrect updates posted to the lb //1. is available 2. becomes unavail 3. becomes avail again when(response.getContentAsString()).thenReturn(healthInfo).thenReturn(emptyHealthInfo). then((Answer<String>) invocation -> { worker.shutdownSemaphore.release(); return healthInfo; }); LoadBalancer lb = mock(LoadBalancer.class); worker.setLoadbalancer(lb); worker.sleeper = mock(Sleeper.class); worker.run(); ArgumentCaptor<LoadBalancerUpdate> captor = ArgumentCaptor.forClass(LoadBalancerUpdate.class); verify(lb, times(3)).updateServiceEndpoints(captor.capture()); assertThat(captor.getAllValues()).hasSize(3); }
@Test public void serviceAppearsAfterStartup() throws Exception { //there was a bug where if a consumed service wasn't available at startup, //even when it came online later, it wouldn't be tracked. String emptyHealthInfo = "[]"; when(response.getContentAsString()).thenReturn(emptyHealthInfo). thenReturn(healthInfo); LoadBalancer lb = mock(LoadBalancer.class); worker.setLoadbalancer(lb); worker.shutdownSemaphore.release(); worker.sleeper = mock(Sleeper.class); worker.run(); ArgumentCaptor<LoadBalancerUpdate> captor = ArgumentCaptor.forClass(LoadBalancerUpdate.class); verify(lb, times(1)).updateServiceEndpoints(captor.capture()); assertThat(captor.getAllValues()).hasSize(1); LoadBalancerUpdate secondUpdate = captor.getAllValues().get(0); assertThat(secondUpdate.getNewServices()).isNotEmpty(); }