This is an automated email from the ASF dual-hosted git repository.

nnag pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new 3ae2218cfa GEODE-9987: Refactored/Removal of deprecated API (#7303)
3ae2218cfa is described below

commit 3ae2218cfa781f671ab4af3c00bbcc0f58a53baf
Author: Nabarun Nag <[email protected]>
AuthorDate: Fri May 20 14:48:51 2022 -0700

    GEODE-9987: Refactored/Removal of deprecated API (#7303)
    
    * The tests were refactored to remove deprecated functions.
    * This was done so that the starting and stopping of members are
    handled correctly.
---
 .../geode/distributed/HostedLocatorsDUnitTest.java | 339 +++++++++++----------
 1 file changed, 176 insertions(+), 163 deletions(-)

diff --git 
a/geode-core/src/distributedTest/java/org/apache/geode/distributed/HostedLocatorsDUnitTest.java
 
b/geode-core/src/distributedTest/java/org/apache/geode/distributed/HostedLocatorsDUnitTest.java
index c4abb4f83b..230ca98103 100644
--- 
a/geode-core/src/distributedTest/java/org/apache/geode/distributed/HostedLocatorsDUnitTest.java
+++ 
b/geode-core/src/distributedTest/java/org/apache/geode/distributed/HostedLocatorsDUnitTest.java
@@ -14,35 +14,29 @@
  */
 package org.apache.geode.distributed;
 
-import static java.util.concurrent.TimeUnit.MINUTES;
-import static org.apache.geode.distributed.ConfigurationProperties.LOCATORS;
-import static 
org.apache.geode.internal.AvailablePortHelper.getRandomAvailableTCPPorts;
-import static org.apache.geode.test.dunit.Host.getHost;
 import static org.assertj.core.api.Assertions.assertThat;
 
-import java.io.File;
+import java.io.Serializable;
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
 import java.net.UnknownHostException;
 import java.util.Collection;
+import java.util.Enumeration;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.Callable;
 
+import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
-import org.springframework.shell.support.util.FileUtils;
 
-import org.apache.geode.distributed.AbstractLauncher.Status;
-import org.apache.geode.distributed.LocatorLauncher.Builder;
-import org.apache.geode.distributed.LocatorLauncher.LocatorState;
 import org.apache.geode.distributed.internal.ClusterDistributionManager;
-import org.apache.geode.distributed.internal.InternalDistributedSystem;
 import 
org.apache.geode.distributed.internal.membership.InternalDistributedMember;
-import org.apache.geode.internal.inet.LocalHostUtil;
-import org.apache.geode.internal.util.StopWatch;
-import org.apache.geode.test.dunit.SerializableCallable;
-import org.apache.geode.test.dunit.SerializableRunnable;
-import org.apache.geode.test.dunit.internal.JUnit4DistributedTestCase;
+import org.apache.geode.test.dunit.rules.ClusterStartupRule;
+import org.apache.geode.test.dunit.rules.MemberVM;
 import org.apache.geode.test.junit.categories.ClientServerTest;
 
 /**
@@ -51,182 +45,201 @@ import 
org.apache.geode.test.junit.categories.ClientServerTest;
  * @since GemFire 8.0
  */
 @Category({ClientServerTest.class})
-public class HostedLocatorsDUnitTest extends JUnit4DistributedTestCase {
+@SuppressWarnings("serial")
+public class HostedLocatorsDUnitTest implements Serializable {
 
-  private static final int TIMEOUT_MILLISECONDS = (int) MINUTES.toMillis(5);
-
-  private transient volatile LocatorLauncher launcher;
-
-  @Override
-  public final void postSetUp() throws Exception {
-    disconnectAllFromDS();
-  }
-
-  @Override
-  public final void preTearDown() throws Exception {
-    disconnectAllFromDS();
-  }
-
-  private String getUniqueLocatorName() {
-    return getHost(0).getHostName() + "_" + getUniqueName();
-  }
+  @Rule
+  public ClusterStartupRule clusterStartupRule = new ClusterStartupRule();
 
   @Test
-  public void testGetAllHostedLocators() throws Exception {
-    final InternalDistributedSystem system = getSystem();
-    final String dunitLocator = system.getConfig().getLocators();
-    assertThat(dunitLocator).isNotEmpty();
-
-    final int[] ports = getRandomAvailableTCPPorts(4);
+  public void testGetAllHostedLocators() {
+    // Get the localhost IP address
+    String ipAddress = getIPAddress().getHostAddress();
 
-    final String uniqueName = getUniqueLocatorName();
-    for (int whichVm = 0; whichVm < 4; whichVm++) {
-      getHost(0).getVM(whichVm)
-          .invoke(new LocatorStarter(uniqueName, whichVm, ports, 
dunitLocator));
-    }
+    // Collection of expected locator addresses.
+    final Set<String> locators = new HashSet<>();
+    // Starting 4 locators
+    MemberVM locator1 = clusterStartupRule.startLocatorVM(0);
+    int locator1Port = locator1.getPort();
+    locators.add(ipAddress + "[" + locator1Port + "]");
+
+    MemberVM locator2 =
+        clusterStartupRule.startLocatorVM(1, l -> 
l.withConnectionToLocator(locator1Port));
+    int locator2Port = locator2.getPort();
+    locators.add(ipAddress + "[" + locator2Port + "]");
+
+    MemberVM locator3 =
+        clusterStartupRule.startLocatorVM(2,
+            l -> l.withConnectionToLocator(locator1Port, locator2Port));
+    int locator3Port = locator3.getPort();
+    locators.add(ipAddress + "[" + locator3Port + "]");
+
+    MemberVM locator4 =
+        clusterStartupRule.startLocatorVM(3,
+            l -> l.withConnectionToLocator(locator1Port, locator2Port, 
locator3Port));
+    int locator4Port = locator4.getPort();
+    locators.add(ipAddress + "[" + locator4Port + "]");
+
+    MemberVM server1 =
+        clusterStartupRule.startServerVM(4,
+            s -> s.withConnectionToLocator(locator1Port, locator2Port, 
locator3Port, locator4Port));
+    // validation within non-locator
+    server1.invoke(() -> validateLocators(locators));
 
-    validateLocators(dunitLocator, ports, system);
+    // validations within the locators
+    locator1.invoke(() -> validateHostedLocators(locators));
+    locator2.invoke(() -> validateHostedLocators(locators));
+    locator3.invoke(() -> validateHostedLocators(locators));
+    locator4.invoke(() -> validateHostedLocators(locators));
   }
 
   @Test
-  public void testGetAllHostedLocatorsUsingPortZero() throws Exception {
-    final InternalDistributedSystem system = getSystem();
-    final String dunitLocator = system.getConfig().getLocators();
-    assertThat(dunitLocator).isNotEmpty();
-
-    // This will eventually contain the ports used by locators
-    final int[] ports = new int[] {0, 0, 0, 0};
-
-    final String uniqueName = getUniqueLocatorName();
-    for (int whichVm = 0; whichVm < 4; whichVm++) {
-      ports[whichVm] = (Integer) getHost(0).getVM(whichVm).invoke(
-          new LocatorStarter(uniqueName, whichVm, ports, dunitLocator));
-    }
-
-    validateLocators(dunitLocator, ports, system);
-  }
-
-  private void validateLocators(final String dunitLocator, final int[] ports,
-      final InternalDistributedSystem system) throws UnknownHostException {
-    final String host = LocalHostUtil.getLocalHost().getHostAddress();
+  public void testGetAllHostedLocatorsWhenLocatorIsStartedWithPortZero() {
+    // Get the localhost IP address
+    String ipAddress = getIPAddress().getHostAddress();
 
+    // Collection of expected locator addresses.
     final Set<String> locators = new HashSet<>();
-    locators.add(host + "["
-        + dunitLocator.substring(dunitLocator.indexOf("[") + 1, 
dunitLocator.indexOf("]")) + "]");
-    for (int port : ports) {
-      locators.add(host + "[" + port + "]");
-    }
-
+    // Starting 4 locators
+    MemberVM locator1 = clusterStartupRule.startLocatorVM(0, l -> 
l.withPort(0));
+    int locator1Port = locator1.getPort();
+    locators.add(ipAddress + "[" + locator1Port + "]");
+
+    MemberVM locator2 =
+        clusterStartupRule.startLocatorVM(1,
+            l -> l.withConnectionToLocator(locator1Port).withPort(0));
+    int locator2Port = locator2.getPort();
+    locators.add(ipAddress + "[" + locator2Port + "]");
+
+    MemberVM locator3 =
+        clusterStartupRule.startLocatorVM(2,
+            l -> l.withConnectionToLocator(locator1Port, 
locator2Port).withPort(0));
+    int locator3Port = locator3.getPort();
+    locators.add(ipAddress + "[" + locator3Port + "]");
+
+    MemberVM locator4 =
+        clusterStartupRule.startLocatorVM(3,
+            l -> l.withConnectionToLocator(locator1Port, locator2Port, 
locator3Port).withPort(0));
+    int locator4Port = locator4.getPort();
+    locators.add(ipAddress + "[" + locator4Port + "]");
+
+    MemberVM server1 =
+        clusterStartupRule.startServerVM(4,
+            s -> s.withConnectionToLocator(locator1Port, locator2Port, 
locator3Port, locator4Port));
     // validation within non-locator
-    final ClusterDistributionManager dm =
-        (ClusterDistributionManager) system.getDistributionManager();
-
-    validateWithLocators(dm, locators);
-
-    for (int whichVm = 0; whichVm < 4; whichVm++) {
-      getHost(0).getVM(whichVm).invoke(new SerializableRunnable() {
-        @Override
-        public void run() {
-          final ClusterDistributionManager dm =
-              (ClusterDistributionManager) 
InternalDistributedSystem.getAnyInstance()
-                  .getDistributionManager();
-          final InternalDistributedMember self = dm.getDistributionManagerId();
-
-          final Set<InternalDistributedMember> locatorIds = 
dm.getLocatorDistributionManagerIds();
-          assertThat(locatorIds).contains(self);
-
-          final Map<InternalDistributedMember, Collection<String>> 
hostedLocators =
-              dm.getAllHostedLocators();
-          assertThat(hostedLocators).containsKey(self);
-        }
-      });
-    }
+    server1.invoke(() -> validateLocators(locators));
 
-    for (int whichVm = 0; whichVm < 4; whichVm++) {
-      getHost(0).getVM(whichVm).invoke(new SerializableRunnable() {
-        @Override
-        public void run() {
-          final ClusterDistributionManager dm1 =
-              (ClusterDistributionManager) 
InternalDistributedSystem.getAnyInstance()
-                  .getDistributionManager();
-
-          validateWithLocators(dm1, locators);
-        }
-      });
-    }
+    // validations within the locators
+    locator1.invoke(() -> validateHostedLocators(locators));
+    locator2.invoke(() -> validateHostedLocators(locators));
+    locator3.invoke(() -> validateHostedLocators(locators));
+    locator4.invoke(() -> validateHostedLocators(locators));
   }
 
-  private void validateWithLocators(final ClusterDistributionManager dm,
-      final Set<String> locators) {
+  private void validateLocators(Set<String> locators) {
+    ClusterDistributionManager dm =
+        (ClusterDistributionManager) 
ClusterStartupRule.getCache().getDistributionManager();
     final Set<InternalDistributedMember> locatorIds = 
dm.getLocatorDistributionManagerIds();
-    assertThat(locatorIds).hasSize(5);
 
+    assertThat(locatorIds.size()).isEqualTo(4);
     final Map<InternalDistributedMember, Collection<String>> hostedLocators =
         dm.getAllHostedLocators();
-    assertThat(hostedLocators).hasSize(5);
-
+    assertThat(hostedLocators).isNotEmpty();
+    assertThat(hostedLocators.size()).isEqualTo(4);
     for (InternalDistributedMember member : hostedLocators.keySet()) {
-      final Collection<String> hostedLocator = hostedLocators.get(member);
-      assertThat(hostedLocator).hasSize(1);
-      assertThat(locators).containsAll(hostedLocator);
+      assertThat(hostedLocators.get(member).size()).isEqualTo(1);
+      final String hostedLocator = 
hostedLocators.get(member).iterator().next();
+      assertThat(locators).contains(hostedLocator);
     }
   }
 
-  private void waitForLocatorToStart(final LocatorLauncher launcher, int 
timeout, int interval)
-      throws Exception {
-    assertEventuallyTrue("waiting for process to start: " + launcher.status(),
-        () -> {
-          try {
-            final LocatorState LocatorState = launcher.status();
-            return (LocatorState != null && 
Status.ONLINE.equals(LocatorState.getStatus()));
-          } catch (RuntimeException e) {
-            return false;
-          }
-        }, timeout, interval);
+  private void validateHostedLocators(Set<String> locators) {
+    ClusterDistributionManager dm =
+        (ClusterDistributionManager) 
ClusterStartupRule.getCache().getDistributionManager();
+    final InternalDistributedMember self = dm.getDistributionManagerId();
+    final Set<InternalDistributedMember> locatorIDs = 
dm.getLocatorDistributionManagerIds();
+    assertThat(locatorIDs.size()).isEqualTo(4);
+    assertThat(locatorIDs).contains(self);
+    final Map<InternalDistributedMember, Collection<String>> hostedLocators =
+        dm.getAllHostedLocators();
+    assertThat(hostedLocators).isNotEmpty();
+    assertThat(hostedLocators.size()).isEqualTo(4);
+    assertThat(hostedLocators).containsKey(self);
+    for (InternalDistributedMember member : hostedLocators.keySet()) {
+      assertThat(hostedLocators.get(member).size()).isEqualTo(1);
+      final String hostedLocator = 
hostedLocators.get(member).iterator().next();
+      assertThat(locators).contains(hostedLocator);
+    }
   }
 
-  private static void assertEventuallyTrue(final String message, final 
Callable<Boolean> callable,
-      final int timeout, final int interval) throws Exception {
-    boolean done = false;
-    for (StopWatch time = new StopWatch(true); !done && 
time.elapsedTimeMillis() < timeout; done =
-        (callable.call())) {
-      Thread.sleep(interval);
-    }
-    assertThat(done).as(message).isTrue();
+  private static InetAddress getIPAddress() {
+    return Boolean.getBoolean("java.net.preferIPv6Addresses") ? 
getIPv6Address() : getIPv4Address();
   }
 
-  private class LocatorStarter extends SerializableCallable<Object> {
-    private final String uniqueName;
-    private final int whichVm;
-    private final int[] ports;
-    private final String dunitLocator;
-
-    public LocatorStarter(final String uniqueName, final int whichVm, final 
int[] ports,
-        final String dunitLocator) {
-      this.uniqueName = uniqueName;
-      this.whichVm = whichVm;
-      this.ports = ports;
-      this.dunitLocator = dunitLocator;
+  private static InetAddress getIPv4Address() {
+    InetAddress host;
+    try {
+      host = InetAddress.getLocalHost();
+      if (host instanceof Inet4Address && !host.isLinkLocalAddress() && 
!host.isLoopbackAddress()) {
+        return host;
+      }
+    } catch (UnknownHostException e) {
+      String s = "Local host not found";
+      throw new RuntimeException(s, e);
     }
-
-    @Override
-    public Object call() throws Exception {
-      final String name = uniqueName + "-" + whichVm;
-      final File directory = new File(name);
-      if (directory.exists()) {
-        FileUtils.deleteRecursively(directory);
+    try {
+      Enumeration<NetworkInterface> i = 
NetworkInterface.getNetworkInterfaces();
+      while (i.hasMoreElements()) {
+        NetworkInterface ni = i.nextElement();
+        Enumeration<InetAddress> j = ni.getInetAddresses();
+        while (j.hasMoreElements()) {
+          InetAddress inetAddress = j.nextElement();
+          // gemfire won't form connections using link-local addresses
+          if (!inetAddress.isLinkLocalAddress() && 
!inetAddress.isLoopbackAddress()
+              && (inetAddress instanceof Inet4Address)) {
+            return inetAddress;
+          }
+        }
       }
-      directory.mkdir();
-      assertThat(directory).exists().isDirectory();
-
-      final Builder builder = new 
Builder().setMemberName(name).setPort(ports[whichVm])
-          .set(LOCATORS, dunitLocator)
-          .setRedirectOutput(true).setWorkingDirectory(name);
+      String s = "IPv4 address not found";
+      throw new RuntimeException(s);
+    } catch (SocketException e) {
+      String s = "Problem reading IPv4 address";
+      throw new RuntimeException(s, e);
+    }
+  }
 
-      launcher = builder.build();
-      assertThat(launcher.start().getStatus()).isSameAs(Status.ONLINE);
-      waitForLocatorToStart(launcher, TIMEOUT_MILLISECONDS, 10);
-      return launcher.getPort();
+  private static InetAddress getIPv6Address() {
+    try {
+      Enumeration<NetworkInterface> i = 
NetworkInterface.getNetworkInterfaces();
+      while (i.hasMoreElements()) {
+        NetworkInterface ni = i.nextElement();
+        Enumeration<InetAddress> j = ni.getInetAddresses();
+        while (j.hasMoreElements()) {
+          InetAddress inetAddress = j.nextElement();
+          // gemfire won't form connections using link-local addresses
+          if (!inetAddress.isLinkLocalAddress() && 
!inetAddress.isLoopbackAddress()
+              && (inetAddress instanceof Inet6Address)
+              && !isIPv6LinkLocalAddress((Inet6Address) inetAddress)) {
+            return inetAddress;
+          }
+        }
+      }
+      String s = "IPv6 address not found";
+      throw new RuntimeException(s);
+    } catch (SocketException e) {
+      String s = "Problem reading IPv6 address";
+      throw new RuntimeException(s, e);
     }
   }
+
+  /**
+   * Detect LinkLocal IPv6 address where the interface is missing, ie %[0-9].
+   *
+   * @see InetAddress#isLinkLocalAddress()
+   */
+  private static boolean isIPv6LinkLocalAddress(Inet6Address inet6Address) {
+    byte[] addressBytes = inet6Address.getAddress();
+    return ((addressBytes[0] == (byte) 0xfe) && (addressBytes[1] == (byte) 
0x80));
+  }
 }

Reply via email to