ClusterHealthModule extends Module { @Override public void stop() { logger.trace("Stopping status module."); timer.cancel(); } ClusterHealthModule(ModuleConfiguration configuration, List<Reporter> reporters,
            final GlobalConfiguration globalConfiguration); @Override void stop();  }
@Test public void should_load_default_configuration_and_initialize() throws ConfigurationException { InfoProvider infoProvider = mock(InfoProvider.class); when(infoProvider.getUnreachableNodes()).thenReturn(new ArrayList<String>()); PowerMockito.mockStatic(DiagnosticsAgent.class); PowerMockito.when(DiagnosticsAgent.getInfoProvider()).thenReturn(infoProvider); int periodInMinutes = 1; boolean numberOfUnreachableNodesEnabled = true; final ClusterHealthModule module = new ClusterHealthModule( testConfiguration(periodInMinutes, numberOfUnreachableNodesEnabled), testReporters(), GlobalConfiguration.getDefault()); module.stop(); PowerMockito.verifyStatic(); }
@Test public void should_report_number_of_unreachable_nodes_when_started() throws ConfigurationException, InterruptedException { InfoProvider infoProvider = mock(InfoProvider.class); List<String> unreachableNodes = new ArrayList<>(); unreachableNodes.add("127.0.0.1"); unreachableNodes.add("127.0.0.2"); when(infoProvider.getUnreachableNodes()).thenReturn(unreachableNodes); PowerMockito.mockStatic(DiagnosticsAgent.class); PowerMockito.when(DiagnosticsAgent.getInfoProvider()).thenReturn(infoProvider); final CountDownLatch latch = new CountDownLatch(1); final LatchTestReporter testReporter = new LatchTestReporter(null, GlobalConfiguration.getDefault(), latch); final List<Reporter> reporters = new ArrayList<Reporter>() { { add(testReporter); } }; int periodInMinutes = 1; boolean numberOfUnreachableNodesEnabled = true; final ClusterHealthModule module = new ClusterHealthModule( testConfiguration(periodInMinutes, numberOfUnreachableNodesEnabled), reporters, GlobalConfiguration.getDefault()); boolean wait = latch.await(1100, TimeUnit.MILLISECONDS); module.stop(); assertThat(wait).isTrue(); assertThat(testReporter.getReported().size()).isEqualTo(1); }
@Test public void should_report_two_unreachable_nodes_when_started() throws ConfigurationException, InterruptedException { InfoProvider infoProvider = mock(InfoProvider.class); List<String> unreachableNodes = new ArrayList<>(); unreachableNodes.add("127.0.0.1"); unreachableNodes.add("127.0.0.2"); when(infoProvider.getUnreachableNodes()).thenReturn(unreachableNodes); PowerMockito.mockStatic(DiagnosticsAgent.class); PowerMockito.when(DiagnosticsAgent.getInfoProvider()).thenReturn(infoProvider); final CountDownLatch latch = new CountDownLatch(1); final LatchTestReporter testReporter = new LatchTestReporter(null, GlobalConfiguration.getDefault(), latch); final List<Reporter> reporters = new ArrayList<Reporter>() { { add(testReporter); } }; int periodInMinutes = 1; boolean numberOfUnreachableNodesEnabled = true; final ClusterHealthModule module = new ClusterHealthModule( testConfiguration(periodInMinutes, numberOfUnreachableNodesEnabled), reporters, GlobalConfiguration.getDefault()); boolean wait = latch.await(1100, TimeUnit.MILLISECONDS); module.stop(); assertThat(wait).isTrue(); assertThat(testReporter.getReported().get(0).getValue()).isEqualTo(2.0); }
@Test public void should_report_zero_unreachable_nodes_when_started() throws ConfigurationException, InterruptedException { InfoProvider infoProvider = mock(InfoProvider.class); List<String> unreachableNodes = new ArrayList<>(); when(infoProvider.getUnreachableNodes()).thenReturn(unreachableNodes); PowerMockito.mockStatic(DiagnosticsAgent.class); PowerMockito.when(DiagnosticsAgent.getInfoProvider()).thenReturn(infoProvider); final CountDownLatch latch = new CountDownLatch(1); final LatchTestReporter testReporter = new LatchTestReporter(null, GlobalConfiguration.getDefault(), latch); final List<Reporter> reporters = new ArrayList<Reporter>() { { add(testReporter); } }; int periodInMinutes = 1; boolean numberOfUnreachableNodesEnabled = true; final ClusterHealthModule module = new ClusterHealthModule( testConfiguration(periodInMinutes, numberOfUnreachableNodesEnabled), reporters, GlobalConfiguration.getDefault()); boolean wait = latch.await(1100, TimeUnit.MILLISECONDS); module.stop(); assertThat(wait).isTrue(); assertThat(testReporter.getReported().get(0).getValue()).isEqualTo(0.0); }
@Test public void should_not_report_number_of_unreachable_nodes_if_not_enabled() throws ConfigurationException, InterruptedException { InfoProvider infoProvider = mock(InfoProvider.class); List<String> unreachableNodes = new ArrayList<>(); unreachableNodes.add("127.0.0.1"); unreachableNodes.add("127.0.0.2"); when(infoProvider.getUnreachableNodes()).thenReturn(unreachableNodes); PowerMockito.mockStatic(DiagnosticsAgent.class); PowerMockito.when(DiagnosticsAgent.getInfoProvider()).thenReturn(infoProvider); final CountDownLatch latch = new CountDownLatch(1); final LatchTestReporter testReporter = new LatchTestReporter(null, GlobalConfiguration.getDefault(), latch); final List<Reporter> reporters = new ArrayList<Reporter>() { { add(testReporter); } }; int periodInMinutes = 1; boolean numberOfUnreachableNodesEnabled = false; final ClusterHealthModule module = new ClusterHealthModule( testConfiguration(periodInMinutes, numberOfUnreachableNodesEnabled), reporters, GlobalConfiguration.getDefault()); boolean wait = latch.await(1100, TimeUnit.MILLISECONDS); module.stop(); assertThat(wait).isFalse(); assertThat(testReporter.getReported().size()).isEqualTo(0); }