LandscapeWatcher { @EventListener({ HeartbeatEvent.class }) public void discoverLandscape() { logger.debug("Discovering landscape"); List<String> applications = this.discoveryClient.getServices() .stream() .filter(this.applicationFilters) .collect(Collectors.toList()); Catalog catalog = this.catalogService.getCatalog(); dispatchEventForDiscoveredApplications(applications, catalog); dispatchEventForDisappearedApplications(applications, catalog); applications.forEach(this::processApplication); } LandscapeWatcher(DiscoveryClient discoveryClient, CatalogService catalogService,
			List<ApplicationFilter> applicationFilters,
			List<ApplicationInstanceFilter> applicationInstanceFilters,
			ApplicationEventPublisher publisher); @EventListener({ HeartbeatEvent.class }) void discoverLandscape();  }
@Test public void shouldDispatchDiscoveredEventsOnFirstHeartbeatEvent() { when(this.discoveryClient.getServices()).thenReturn(Collections.singletonList("a")); ServiceInstance serviceInstance = new DefaultServiceInstance("a-1", "a", "host", 8080, false); when(this.discoveryClient.getInstances("a")).thenReturn(Collections.singletonList(serviceInstance)); this.landscapeWatcher.discoverLandscape(); verify(this.catalogService, times(2)).getCatalog(); verifyNoMoreInteractions(this.catalogService); verify(this.discoveryClient).getServices(); verify(this.discoveryClient).getInstances("a"); verifyNoMoreInteractions(this.discoveryClient); verify(this.publisher, times(2)).publishEvent(this.applicationEventArgumentCaptor.capture()); verifyNoMoreInteractions(this.publisher); List<ApplicationEvent> applicationEvents = this.applicationEventArgumentCaptor.getAllValues(); ApplicationEvent event = applicationEvents.get(0); assertThat(event).isInstanceOfSatisfying(ServiceDiscovered.class, e -> assertThat(e.getSource()).isEqualTo("a")); event = applicationEvents.get(1); assertThat(event).isInstanceOfSatisfying(ServiceInstanceDiscovered.class, e -> assertThat(e.getSource()).isEqualTo(serviceInstance)); }
@Test public void shouldSkipFilteredApplications() { this.applicationFilters.add("a"::equalsIgnoreCase); when(this.discoveryClient.getServices()).thenReturn(Arrays.asList("a", "b")); when(this.catalog.getApplications()).thenReturn(Arrays.asList("a")); this.landscapeWatcher.discoverLandscape(); verify(this.discoveryClient).getServices(); verify(this.discoveryClient).getInstances("a"); verifyNoMoreInteractions(this.discoveryClient); verifyZeroInteractions(this.publisher); }
@Test public void shouldSkipFilteredApplicationInstances() { this.applicationInstanceFilters.add(serviceInstance -> "a-1".equalsIgnoreCase(serviceInstance.getInstanceId())); when(this.discoveryClient.getServices()).thenReturn(Collections.singletonList("a")); when(this.discoveryClient.getInstances("a")).thenReturn( Arrays.asList(new DefaultServiceInstance("a-1", "a", "host", 8080, false), new DefaultServiceInstance("a-2", "a", "host", 8080, false))); when(this.catalog.getApplicationInstancesForApplication("a")).thenReturn(Collections.singletonList("a-1")); this.landscapeWatcher.discoverLandscape(); verify(this.discoveryClient).getServices(); verify(this.discoveryClient).getInstances("a"); verify(this.publisher).publishEvent(this.applicationEventArgumentCaptor.capture()); verifyZeroInteractions(this.publisher); }
@Test public void shouldDispatchEventWhenServiceDisappeared() { when(this.discoveryClient.getServices()).thenReturn(Collections.singletonList("a")); when(this.catalog.getApplications()).thenReturn(Arrays.asList("a", "b")); when(this.discoveryClient.getInstances("a")).thenReturn(Collections.emptyList()); this.landscapeWatcher.discoverLandscape(); verify(this.catalogService, times(2)).getCatalog(); verifyNoMoreInteractions(this.catalogService); verify(this.discoveryClient).getServices(); verify(this.discoveryClient).getInstances("a"); verifyNoMoreInteractions(this.discoveryClient); verify(this.publisher).publishEvent(this.applicationEventArgumentCaptor.capture()); verifyNoMoreInteractions(this.publisher); ApplicationEvent event = this.applicationEventArgumentCaptor.getValue(); assertThat(event).isInstanceOfSatisfying(ServiceDisappeared.class, e -> assertThat(e.getSource()).isEqualTo("b")); }
@Test public void shouldDispatchEventWhenServiceInstanceDisappeared() { when(this.discoveryClient.getServices()).thenReturn(Collections.singletonList("a")); when(this.catalog.getApplications()).thenReturn(Collections.singletonList("a")); ServiceInstance serviceInstance = new DefaultServiceInstance("a-1", "a", "host", 8080, false); when(this.discoveryClient.getInstances("a")).thenReturn(Collections.singletonList(serviceInstance)); when(this.catalog.getApplicationInstancesForApplication("a")).thenReturn(Arrays.asList("a-1", "a-2")); this.landscapeWatcher.discoverLandscape(); verify(this.catalogService, times(2)).getCatalog(); verifyNoMoreInteractions(this.catalogService); verify(this.discoveryClient).getServices(); verify(this.discoveryClient).getInstances("a"); verifyNoMoreInteractions(this.discoveryClient); verify(this.publisher).publishEvent(this.applicationEventArgumentCaptor.capture()); verifyNoMoreInteractions(this.publisher); ApplicationEvent event = this.applicationEventArgumentCaptor.getValue(); assertThat(event).isInstanceOfSatisfying(ServiceInstanceDisappeared.class, e -> assertThat(e.getSource()).isEqualTo("a-2")); }