private void refresh() { try { NodeHealth nodeHealth = nodeHealthProvider.get(); sharedHealthState.writeMine(nodeHealth); } catch (HazelcastInstanceNotActiveException | RetryableHazelcastException e) { LOG.debug("Hazelcast is no more active", e); } catch (Throwable t) { LOG.error("An error occurred while attempting to refresh HealthState of the current node in the shared state:", t); } }
@Test public void do_not_log_errors_when_hazelcast_is_not_active() { logging.setLevel(DEBUG); doThrow(new HazelcastInstanceNotActiveException()).when(sharedHealthState).writeMine(any()); ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class); underTest.start(); verify(executorService).scheduleWithFixedDelay(runnableCaptor.capture(), eq(1L), eq(10L), eq(TimeUnit.SECONDS)); Runnable runnable = runnableCaptor.getValue(); runnable.run(); assertThat(logging.getLogs(ERROR)).isEmpty(); assertThat(logging.hasLog(DEBUG, "Hazelcast is no more active")).isTrue(); } }
@Test public void start_adds_runnable_with_10_second_delay_and_initial_delay_putting_NodeHealth_from_provider_into_SharedHealthState() { ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class); NodeHealth[] nodeHealths = { testSupport.randomNodeHealth(), testSupport.randomNodeHealth(), testSupport.randomNodeHealth() }; Error expected = new Error("Simulating exception raised by NodeHealthProvider"); when(nodeHealthProvider.get()) .thenReturn(nodeHealths[0]) .thenReturn(nodeHealths[1]) .thenReturn(nodeHealths[2]) .thenThrow(expected); underTest.start(); verify(executorService).scheduleWithFixedDelay(runnableCaptor.capture(), eq(1L), eq(10L), eq(TimeUnit.SECONDS)); Runnable runnable = runnableCaptor.getValue(); runnable.run(); runnable.run(); runnable.run(); verify(sharedHealthState).writeMine(nodeHealths[0]); verify(sharedHealthState).writeMine(nodeHealths[1]); verify(sharedHealthState).writeMine(nodeHealths[2]); try { runnable.run(); } catch (IllegalStateException e) { fail("Runnable should catch any Throwable"); } }