@Test public void readAll_ignores_NodeHealth_of_more_than_30_seconds_before_cluster_time() { NodeHealth nodeHealth = randomNodeHealth(); Map<String, TimestampedNodeHealth> map = new HashMap<>(); String memberUuid = randomAlphanumeric(5); TimestampedNodeHealth timestampedNodeHealth = new TimestampedNodeHealth(nodeHealth, clusterTime - 30 * 1000 - random.nextInt(99)); map.put(memberUuid, timestampedNodeHealth); doReturn(map).when(hazelcastMember).getReplicatedMap(MAP_SQ_HEALTH_STATE); when(hazelcastMember.getMemberUuids()).thenReturn(map.keySet()); when(hazelcastMember.getClusterTime()).thenReturn(clusterTime); assertThat(underTest.readAll()).isEmpty(); }
@Test public void readAll_ignores_NodeHealth_of_30_seconds_before_cluster_time() { NodeHealth nodeHealth = randomNodeHealth(); Map<String, TimestampedNodeHealth> map = new HashMap<>(); String memberUuid = randomAlphanumeric(5); TimestampedNodeHealth timestampedNodeHealth = new TimestampedNodeHealth(nodeHealth, clusterTime - 30 * 1000); map.put(memberUuid, timestampedNodeHealth); doReturn(map).when(hazelcastMember).getReplicatedMap(MAP_SQ_HEALTH_STATE); when(hazelcastMember.getMemberUuids()).thenReturn(map.keySet()); when(hazelcastMember.getClusterTime()).thenReturn(clusterTime); assertThat(underTest.readAll()).isEmpty(); }
@Test public void readAll_logs_map_sq_health_state_content_and_the_content_effectively_returned_if_TRACE() { logging.setLevel(Level.TRACE); Map<String, TimestampedNodeHealth> map = new HashMap<>(); String uuid = randomAlphanumeric(44); NodeHealth nodeHealth = randomNodeHealth(); map.put(uuid, new TimestampedNodeHealth(nodeHealth, clusterTime - 1)); when(hazelcastMember.getClusterTime()).thenReturn(clusterTime); when(hazelcastMember.getMemberUuids()).thenReturn(singleton(uuid)); doReturn(map).when(hazelcastMember).getReplicatedMap(MAP_SQ_HEALTH_STATE); underTest.readAll(); assertThat(logging.getLogs()).hasSize(1); assertThat(logging.hasLog(Level.TRACE, "Reading " + new HashMap<>(map) + " and keeping " + singleton(nodeHealth))).isTrue(); }
@Test public void readAll_returns_all_NodeHealth_in_map_sq_health_state_for_existing_client_uuids_aged_less_than_30_seconds() { NodeHealth[] nodeHealths = IntStream.range(0, 1 + random.nextInt(6)).mapToObj(i -> randomNodeHealth()).toArray(NodeHealth[]::new); Map<String, TimestampedNodeHealth> allNodeHealths = new HashMap<>(); Map<String, NodeHealth> expected = new HashMap<>(); String randomUuidBase = randomAlphanumeric(5); for (int i = 0; i < nodeHealths.length; i++) { String memberUuid = randomUuidBase + i; TimestampedNodeHealth timestampedNodeHealth = new TimestampedNodeHealth(nodeHealths[i], clusterTime - random.nextInt(30 * 1000)); allNodeHealths.put(memberUuid, timestampedNodeHealth); if (random.nextBoolean()) { expected.put(memberUuid, nodeHealths[i]); } } doReturn(allNodeHealths).when(hazelcastMember).getReplicatedMap(MAP_SQ_HEALTH_STATE); when(hazelcastMember.getMemberUuids()).thenReturn(expected.keySet()); when(hazelcastMember.getClusterTime()).thenReturn(clusterTime); assertThat(underTest.readAll()) .containsOnly(expected.values().stream().toArray(NodeHealth[]::new)); assertThat(logging.getLogs()).isEmpty(); }
@Test public void readAll_logs_message_for_each_timed_out_NodeHealth_ignored_if_TRACE() { logging.setLevel(Level.TRACE); Map<String, TimestampedNodeHealth> map = new HashMap<>(); String memberUuid1 = randomAlphanumeric(44); String memberUuid2 = randomAlphanumeric(44); map.put(memberUuid1, new TimestampedNodeHealth(randomNodeHealth(), clusterTime - 30 * 1000)); map.put(memberUuid2, new TimestampedNodeHealth(randomNodeHealth(), clusterTime - 30 * 1000)); doReturn(map).when(hazelcastMember).getReplicatedMap(MAP_SQ_HEALTH_STATE); when(hazelcastMember.getMemberUuids()).thenReturn(ImmutableSet.of(memberUuid1, memberUuid2)); when(hazelcastMember.getClusterTime()).thenReturn(clusterTime); underTest.readAll(); assertThat(logging.getLogs()).hasSize(3); assertThat(logging.getLogs(Level.TRACE)) .containsOnly( "Reading " + new HashMap<>(map) + " and keeping []", "Ignoring NodeHealth of member " + memberUuid1 + " because it is too old", "Ignoring NodeHealth of member " + memberUuid2 + " because it is too old"); }
@Test public void readAll_logs_message_for_each_non_existing_member_ignored_if_TRACE() { logging.setLevel(Level.TRACE); Map<String, TimestampedNodeHealth> map = new HashMap<>(); String memberUuid1 = randomAlphanumeric(44); String memberUuid2 = randomAlphanumeric(44); map.put(memberUuid1, new TimestampedNodeHealth(randomNodeHealth(), clusterTime - 1)); map.put(memberUuid2, new TimestampedNodeHealth(randomNodeHealth(), clusterTime - 1)); when(hazelcastMember.getClusterTime()).thenReturn(clusterTime); doReturn(map).when(hazelcastMember).getReplicatedMap(MAP_SQ_HEALTH_STATE); underTest.readAll(); assertThat(logging.getLogs()).hasSize(3); assertThat(logging.getLogs(Level.TRACE)) .containsOnly( "Reading " + new HashMap<>(map) + " and keeping []", "Ignoring NodeHealth of member " + memberUuid1 + " because it is not part of the cluster at the moment", "Ignoring NodeHealth of member " + memberUuid2 + " because it is not part of the cluster at the moment"); }