You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HazelcastMemberBuilderTest.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.process.cluster.hz;
  21. import java.net.InetAddress;
  22. import java.net.UnknownHostException;
  23. import java.util.Arrays;
  24. import java.util.Collections;
  25. import java.util.List;
  26. import org.junit.Before;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.rules.DisableOnDebug;
  30. import org.junit.rules.ExpectedException;
  31. import org.junit.rules.TestRule;
  32. import org.junit.rules.Timeout;
  33. import org.sonar.process.NetworkUtilsImpl;
  34. import org.sonar.process.ProcessId;
  35. import static java.util.Arrays.asList;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. import static org.mockito.Mockito.mock;
  38. import static org.mockito.Mockito.when;
  39. import static org.sonar.process.ProcessProperties.Property.CLUSTER_NODE_HZ_PORT;
  40. public class HazelcastMemberBuilderTest {
  41. @Rule
  42. public ExpectedException expectedException = ExpectedException.none();
  43. @Rule
  44. public TestRule safeguardTimeout = new DisableOnDebug(Timeout.seconds(60));
  45. // use loopback for support of offline builds
  46. private final InetAddress loopback = InetAddress.getLoopbackAddress();
  47. private final InetAdressResolver inetAdressResolver = mock(InetAdressResolver.class);
  48. private final HazelcastMemberBuilder underTest = new HazelcastMemberBuilder(inetAdressResolver);
  49. @Before
  50. public void before() throws UnknownHostException {
  51. when(inetAdressResolver.getAllByName("foo")).thenReturn(Collections.singletonList("foo/5.6.7.8"));
  52. when(inetAdressResolver.getAllByName("bar")).thenReturn(Collections.singletonList("bar/8.7.6.5"));
  53. when(inetAdressResolver.getAllByName("wizz")).thenReturn(Arrays.asList("wizz/1.2.3.4", "wizz/2.3.4.5", "wizz/3.4.5.6"));
  54. when(inetAdressResolver.getAllByName("ninja")).thenReturn(Arrays.asList("ninja/4.5.6.7", "ninja/5.6.7.8"));
  55. }
  56. @Test
  57. public void testMultipleIPsByHostname() {
  58. underTest.setMembers(asList("wizz:9001", "ninja"));
  59. List<String> members = underTest.getMembers();
  60. assertThat(members).containsExactlyInAnyOrder("1.2.3.4:9001", "2.3.4.5:9001", "3.4.5.6:9001", "4.5.6.7:9003", "5.6.7.8:9003");
  61. }
  62. @Test
  63. public void build_member() {
  64. HazelcastMember member = underTest
  65. .setProcessId(ProcessId.COMPUTE_ENGINE)
  66. .setNodeName("bar")
  67. .setPort(NetworkUtilsImpl.INSTANCE.getNextLoopbackAvailablePort())
  68. .setNetworkInterface(loopback.getHostAddress())
  69. .build();
  70. assertThat(member.getUuid()).isNotNull();
  71. assertThat(member.getClusterTime()).isPositive();
  72. assertThat(member.getCluster().getMembers()).hasSize(1);
  73. assertThat(member.getMemberUuids()).containsOnlyOnce(member.getUuid());
  74. assertThat(member.getAtomicReference("baz")).isNotNull();
  75. assertThat(member.getLock("baz")).isNotNull();
  76. assertThat(member.getReplicatedMap("baz")).isNotNull();
  77. member.close();
  78. }
  79. @Test
  80. public void default_port_is_added_when_missing() {
  81. underTest.setMembers(asList("foo", "bar:9100", "1.2.3.4"));
  82. assertThat(underTest.getMembers()).containsExactly(
  83. "5.6.7.8:" + CLUSTER_NODE_HZ_PORT.getDefaultValue(),
  84. "8.7.6.5:9100",
  85. "1.2.3.4:" + CLUSTER_NODE_HZ_PORT.getDefaultValue());
  86. }
  87. @Test
  88. public void fail_if_elasticsearch_process() {
  89. expectedException.expect(IllegalArgumentException.class);
  90. expectedException.expectMessage("Hazelcast must not be enabled on Elasticsearch node");
  91. underTest.setProcessId(ProcessId.ELASTICSEARCH);
  92. }
  93. }