This is an automated email from the ASF dual-hosted git repository. bschuchardt pushed a commit to branch feature/GEODE-3523 in repository https://gitbox.apache.org/repos/asf/geode.git
commit c54e6189a46e4544e096cc817b920ad13b85efe9 Author: Bruce Schuchardt <[email protected]> AuthorDate: Fri Apr 20 15:47:06 2018 -0700 GEODE-3523 AutoConnectionSourceDUnitTest: testDynamicallyFindLocators failed Refactored the test into two smaller tests, one that tests that a client discovers another locator and another that tests that a client discovers that a locator has gone away. I did some general updating of the code in this class to - replace SerializableCallable/Runnables with lambdas - replace wait-loops with Awaitility - replace use of AvailablePortHelper with wildcard binds - replace ignoring of Exceptions with checks for the correct exception class - replaced heavy use of deprecated methods with their non-deprecated versions --- .../controllers/RestAPIsAndInterOpsDUnitTest.java | 10 +- .../web/controllers/RestAPIsWithSSLDUnitTest.java | 29 +- .../internal/AutoConnectionSourceDUnitTest.java | 585 +++++++++------------ .../internal/LocatorLoadBalancingDUnitTest.java | 81 ++- .../cache/client/internal/LocatorTestBase.java | 140 ++--- .../ClientTxCommitShouldNotHangRegressionTest.java | 8 +- ...FireAndForgetFunctionOnAllServersDUnitTest.java | 8 +- ...tewayReceiverAutoConnectionSourceDUnitTest.java | 7 +- 8 files changed, 343 insertions(+), 525 deletions(-) diff --git a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPIsAndInterOpsDUnitTest.java b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPIsAndInterOpsDUnitTest.java index 2daddc8..5bebd41 100644 --- a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPIsAndInterOpsDUnitTest.java +++ b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPIsAndInterOpsDUnitTest.java @@ -67,7 +67,6 @@ import org.apache.geode.cache.client.internal.LocatorTestBase; import org.apache.geode.cache.server.CacheServer; import org.apache.geode.cache.server.ServerLoadProbe; import org.apache.geode.distributed.DistributedSystem; -import org.apache.geode.internal.AvailablePort; import org.apache.geode.internal.AvailablePortHelper; import org.apache.geode.internal.cache.GemFireCacheImpl; import org.apache.geode.internal.cache.InternalCache; @@ -619,12 +618,11 @@ public class RestAPIsAndInterOpsDUnitTest extends LocatorTestBase { VM client = host.getVM(3); // start locator - int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - final String locatorHostName = NetworkUtils.getServerHostName(locator.getHost()); - locator.invoke(() -> startLocator(locatorHostName, locatorPort, "")); + final String hostName = NetworkUtils.getServerHostName(); + int locatorPort = locator.invoke(() -> startLocator(hostName, "")); // find locators - String locators = locatorHostName + "[" + locatorPort + "]"; + String locators = hostName + "[" + locatorPort + "]"; // start manager (peer cache) manager.invoke(() -> startManager(/* groups */null, locators, new String[] {REGION_NAME}, @@ -637,7 +635,7 @@ public class RestAPIsAndInterOpsDUnitTest extends LocatorTestBase { locators, new String[] {REGION_NAME}, CacheServer.DEFAULT_LOAD_PROBE)); // create a client cache - client.invoke(() -> createClientCache(locatorHostName, locatorPort)); + client.invoke(() -> createClientCache(hostName, locatorPort)); // create region in Manager, peer cache and Client cache nodes manager.invoke(() -> createRegion()); diff --git a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPIsWithSSLDUnitTest.java b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPIsWithSSLDUnitTest.java index 61ff06a..d5d0efd 100644 --- a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPIsWithSSLDUnitTest.java +++ b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPIsWithSSLDUnitTest.java @@ -64,7 +64,6 @@ import java.io.InputStreamReader; import java.net.BindException; import java.security.KeyStore; import java.util.Arrays; -import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -85,8 +84,6 @@ import org.apache.http.ssl.SSLContexts; import org.json.JSONObject; import org.junit.Test; import org.junit.experimental.categories.Category; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; import org.apache.geode.cache.AttributesFactory; import org.apache.geode.cache.Cache; @@ -103,7 +100,6 @@ import org.apache.geode.cache.client.internal.LocatorTestBase; import org.apache.geode.cache.server.CacheServer; import org.apache.geode.distributed.DistributedSystem; import org.apache.geode.distributed.internal.InternalDistributedSystem; -import org.apache.geode.internal.AvailablePort; import org.apache.geode.internal.AvailablePortHelper; import org.apache.geode.internal.cache.GemFireCacheImpl; import org.apache.geode.internal.cache.InternalCache; @@ -115,15 +111,12 @@ import org.apache.geode.test.dunit.NetworkUtils; import org.apache.geode.test.dunit.VM; import org.apache.geode.test.junit.categories.DistributedTest; import org.apache.geode.test.junit.categories.RestAPITest; -import org.apache.geode.test.junit.runners.CategoryWithParameterizedRunnerFactory; import org.apache.geode.util.test.TestUtil; /** * @since GemFire 8.0 */ @Category({DistributedTest.class, RestAPITest.class}) -@RunWith(Parameterized.class) [email protected](CategoryWithParameterizedRunnerFactory.class) public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { private static final long serialVersionUID = -254776154266339226L; @@ -131,13 +124,7 @@ public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { private final String PEOPLE_REGION_NAME = "People"; private final String INVALID_CLIENT_ALIAS = "INVALID_CLIENT_ALIAS"; - @Parameterized.Parameter - public String urlContext; - - @Parameterized.Parameters - public static Collection<String> data() { - return Arrays.asList("/geode", "/gemfire-api"); - } + public String urlContext = "/geode"; public RestAPIsWithSSLDUnitTest() { super(); @@ -258,15 +245,12 @@ public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { VM client = host.getVM(3); // start locator - final int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - final String locatorHostName = NetworkUtils.getServerHostName(locator.getHost()); + final String hostName = NetworkUtils.getServerHostName(locator.getHost()); - locator.invoke("Start Locator", () -> { - startLocator(locatorHostName, locatorPort, ""); - }); + int locatorPort = startLocatorInVM(locator, hostName, ""); // find locators - String locators = locatorHostName + "[" + locatorPort + "]"; + String locators = hostName + "[" + locatorPort + "]"; // start manager (peer cache) manager.invoke("StartManager", @@ -274,7 +258,6 @@ public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { // start startBridgeServer With RestService enabled String restEndpoint = server.invoke("startBridgeServerWithRestServiceOnInVM", () -> { - final String hostName = server.getHost().getHostName(); final int restServicePort = AvailablePortHelper.getRandomAvailableTCPPort(); startBridgeServer(hostName, restServicePort, locators, new String[] {REGION_NAME}, sslProperties, clusterLevel); @@ -283,8 +266,8 @@ public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { // create a client cache client.invoke("Create ClientCache", () -> { - new ClientCacheFactory().setPdxReadSerialized(true) - .addPoolLocator(locatorHostName, locatorPort).create(); + new ClientCacheFactory().setPdxReadSerialized(true).addPoolLocator(hostName, locatorPort) + .create(); return null; }); diff --git a/geode-core/src/test/java/org/apache/geode/cache/client/internal/AutoConnectionSourceDUnitTest.java b/geode-core/src/test/java/org/apache/geode/cache/client/internal/AutoConnectionSourceDUnitTest.java index dc7ad3a..e9945b2 100644 --- a/geode-core/src/test/java/org/apache/geode/cache/client/internal/AutoConnectionSourceDUnitTest.java +++ b/geode-core/src/test/java/org/apache/geode/cache/client/internal/AutoConnectionSourceDUnitTest.java @@ -14,25 +14,41 @@ */ package org.apache.geode.cache.client.internal; -import static org.junit.Assert.*; - -import java.io.*; -import java.net.*; -import java.util.*; - -import org.junit.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.apache.geode.test.dunit.NetworkUtils.getServerHostName; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.Serializable; +import java.net.InetSocketAddress; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; + +import org.awaitility.Awaitility; import org.junit.Assert; -import org.junit.experimental.categories.*; - -import org.apache.geode.cache.*; -import org.apache.geode.cache.client.*; -import org.apache.geode.cache.server.*; -import org.apache.geode.distributed.internal.*; -import org.apache.geode.internal.*; -import org.apache.geode.internal.cache.*; -import org.apache.geode.management.membership.*; -import org.apache.geode.test.dunit.*; -import org.apache.geode.test.junit.categories.*; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import org.apache.geode.cache.Cache; +import org.apache.geode.cache.Region; +import org.apache.geode.cache.RegionDestroyedException; +import org.apache.geode.cache.client.Pool; +import org.apache.geode.cache.client.PoolManager; +import org.apache.geode.cache.server.CacheServer; +import org.apache.geode.distributed.internal.ServerLocation; +import org.apache.geode.internal.AvailablePort; +import org.apache.geode.management.membership.ClientMembership; +import org.apache.geode.management.membership.ClientMembershipEvent; +import org.apache.geode.management.membership.ClientMembershipListenerAdapter; +import org.apache.geode.test.dunit.IgnoredException; +import org.apache.geode.test.dunit.RMIException; +import org.apache.geode.test.dunit.VM; +import org.apache.geode.test.junit.categories.ClientServerTest; +import org.apache.geode.test.junit.categories.DistributedTest; /** * Tests cases that are particular for the auto connection source - dynamically discovering servers, @@ -55,21 +71,19 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { @Test public void testDiscoverBridgeServers() throws Exception { - final Host host = Host.getHost(0); - VM vm0 = host.getVM(0); - VM vm1 = host.getVM(1); - VM vm2 = host.getVM(2); + VM vm0 = VM.getVM(0); + VM vm1 = VM.getVM(1); + VM vm2 = VM.getVM(2); - int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - String hostName = NetworkUtils.getServerHostName(vm0.getHost()); - vm0.invoke("Start Locator", () -> startLocator(hostName, locatorPort, "")); + String hostName = getServerHostName(); + int locatorPort = vm0.invoke("Start Locator", () -> startLocator(hostName, "")); - String locators = NetworkUtils.getServerHostName(vm0.getHost()) + "[" + locatorPort + "]"; + String locators = getServerHostName(vm0.getHost()) + "[" + locatorPort + "]"; vm1.invoke("Start BridgeServer", () -> startBridgeServer(null, locators)); vm2.invoke("StartBridgeClient", - () -> startBridgeClient(null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort)); + () -> startBridgeClient(null, getServerHostName(vm0.getHost()), locatorPort)); putAndWaitForSuccess(vm2, REGION_NAME, "key", "value"); @@ -79,12 +93,11 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { @Test public void testNoLocators() { - final Host host = Host.getHost(0); - VM vm0 = host.getVM(0); + VM vm0 = VM.getVM(0); try { vm0.invoke("StartBridgeClient", - () -> startBridgeClient(null, NetworkUtils.getServerHostName(vm0.getHost()), + () -> startBridgeClient(null, getServerHostName(vm0.getHost()), AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET))); checkLocators(vm0, new InetSocketAddress[] {}, new InetSocketAddress[] {}); putInVM(vm0, "key", "value"); @@ -96,16 +109,13 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { @Test public void testNoBridgeServer() { - final Host host = Host.getHost(0); - VM vm0 = host.getVM(0); - VM vm1 = host.getVM(1); + VM vm0 = VM.getVM(0); + VM vm1 = VM.getVM(1); - int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - String hostName = NetworkUtils.getServerHostName(vm0.getHost()); - vm0.invoke("Start Locator", () -> startLocator(hostName, locatorPort, "")); + String hostName = getServerHostName(); + int locatorPort = vm0.invoke("Start Locator", () -> startLocator(hostName, "")); try { - vm1.invoke("StartBridgeClient", () -> startBridgeClient(null, - NetworkUtils.getServerHostName(vm0.getHost()), locatorPort)); + vm1.invoke("StartBridgeClient", () -> startBridgeClient(null, hostName, locatorPort)); putInVM(vm0, "key", "value"); fail("Client cache should not have been able to start"); } catch (Exception e) { @@ -115,22 +125,20 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { @Test public void testDynamicallyFindBridgeServer() throws Exception { - final Host host = Host.getHost(0); - VM vm0 = host.getVM(0); - VM vm1 = host.getVM(1); - VM vm2 = host.getVM(2); - VM vm3 = host.getVM(3); + VM vm0 = VM.getVM(0); + VM vm1 = VM.getVM(1); + VM vm2 = VM.getVM(2); + VM vm3 = VM.getVM(3); - int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - String hostName = NetworkUtils.getServerHostName(vm0.getHost()); - vm0.invoke("Start Locator", () -> startLocator(hostName, locatorPort, "")); + String hostName = getServerHostName(); + int locatorPort = vm0.invoke("Start Locator", () -> startLocator(hostName, "")); - String locators = NetworkUtils.getServerHostName(vm0.getHost()) + "[" + locatorPort + "]"; + String locators = getLocatorString(hostName, locatorPort); vm1.invoke("Start BridgeServer", () -> startBridgeServer(null, locators)); vm2.invoke("StartBridgeClient", - () -> startBridgeClient(null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort)); + () -> startBridgeClient(null, getServerHostName(vm0.getHost()), locatorPort)); putAndWaitForSuccess(vm2, REGION_NAME, "key", "value"); @@ -144,152 +152,152 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { } @Test - public void testDynamicallyFindLocators() throws Exception { - try { - final Host host = Host.getHost(0); - final String hostName = NetworkUtils.getServerHostName(host); - VM vm0 = host.getVM(0); - VM vm1 = host.getVM(1); - VM vm2 = host.getVM(2); - VM vm3 = host.getVM(3); - - int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(3); - - final int locatorPort0 = ports[0]; - final int locatorPort1 = ports[1]; - final int locatorPort3 = ports[2]; - String locators = - getLocatorString(host, new int[] {locatorPort0, locatorPort1, locatorPort3}); - vm0.invoke("Start Locator1 ", - () -> startLocator(NetworkUtils.getServerHostName(vm0.getHost()), locatorPort0, - locators)); - vm1.invoke("Start Locator2 ", - () -> startLocator(NetworkUtils.getServerHostName(vm1.getHost()), locatorPort1, - locators)); - - vm2.invoke("StartBridgeClient", () -> startBridgeClient(null, - NetworkUtils.getServerHostName(vm0.getHost()), locatorPort0)); - - InetSocketAddress locatorToWaitFor = new InetSocketAddress(hostName, locatorPort1); - waitForLocatorDiscovery(vm2, locatorToWaitFor); - InetSocketAddress[] initialLocators = - new InetSocketAddress[] {new InetSocketAddress(hostName, locatorPort0)}; - checkLocators(vm2, initialLocators, - new InetSocketAddress[] {new InetSocketAddress(hostName, locatorPort0), - new InetSocketAddress(hostName, locatorPort1)}); - - vm0.invoke("Stop Locator", () -> stopLocator()); - vm0.invoke("Start BridgeServer", () -> startBridgeServer(null, locators)); - - putAndWaitForSuccess(vm2, REGION_NAME, "key", "value"); - Assert.assertEquals("value", getInVM(vm0, "key")); - checkLocators(vm2, initialLocators, - new InetSocketAddress[] {new InetSocketAddress(hostName, locatorPort1)}); - - vm3.invoke("Start Locator", () -> startLocator(NetworkUtils.getServerHostName(vm3.getHost()), - locatorPort3, locators)); - stopBridgeMemberVM(vm0); - locatorToWaitFor = new InetSocketAddress(hostName, locatorPort3); - waitForLocatorDiscovery(vm2, locatorToWaitFor); - checkLocators(vm2, initialLocators, - new InetSocketAddress[] {new InetSocketAddress(hostName, locatorPort1), - new InetSocketAddress(hostName, locatorPort3)}); - vm1.invoke("Stop Locator", () -> stopLocator()); - vm1.invoke("Start BridgeServer", () -> startBridgeServer(null, locators)); - putAndWaitForSuccess(vm2, REGION_NAME, "key2", "value2"); - Assert.assertEquals("value2", getInVM(vm1, "key2")); - checkLocators(vm2, initialLocators, - new InetSocketAddress[] {new InetSocketAddress(hostName, locatorPort3)}); - } catch (Exception ec) { - if (ec.getCause() != null && (ec.getCause().getCause() instanceof BindException)) - return;// BindException let it pass - throw ec; - } + public void testClientDynamicallyFindsNewLocator() throws Exception { + final String hostName = getServerHostName(); + VM locator0VM = VM.getVM(0); + VM locator1VM = VM.getVM(1); + VM clientVM = VM.getVM(2); + VM serverVM = VM.getVM(3); + + final int locator0Port = locator0VM.invoke("Start Locator1 ", () -> startLocator(hostName, "")); + + clientVM.invoke("StartBridgeClient", () -> startBridgeClient(null, hostName, locator0Port)); + + final int locator1Port = locator1VM.invoke("Start Locator2 ", + () -> startLocator(hostName, getLocatorString(hostName, locator0Port))); + + serverVM.invoke("Start BridgeServer", + () -> startBridgeServer(null, getLocatorString(hostName, locator0Port, locator1Port))); + + putAndWaitForSuccess(clientVM, REGION_NAME, "key", "value"); + Assert.assertEquals("value", getInVM(serverVM, "key")); + + InetSocketAddress locatorToWaitFor = new InetSocketAddress(hostName, locator1Port); + waitForLocatorDiscovery(clientVM, locatorToWaitFor); + } + + @Test + public void testClientDynamicallyDropsStoppedLocator() throws Exception { + final String hostName = getServerHostName(); + VM locator0VM = VM.getVM(0); + VM locator1VM = VM.getVM(1); + VM clientVM = VM.getVM(2); + VM serverVM = VM.getVM(3); + + final int locator0Port = locator0VM.invoke("Start Locator1 ", () -> startLocator(hostName, "")); + final int locator1Port = locator1VM.invoke("Start Locator2 ", + () -> startLocator(hostName, getLocatorString(hostName, locator0Port))); + + clientVM.invoke("StartBridgeClient", () -> startBridgeClient(null, hostName, locator0Port)); + + waitForLocatorDiscovery(clientVM, new InetSocketAddress(hostName, locator1Port)); + + InetSocketAddress[] initialLocators = + new InetSocketAddress[] {new InetSocketAddress(hostName, locator0Port)}; + + checkLocators(clientVM, initialLocators, new InetSocketAddress(hostName, locator0Port), + new InetSocketAddress(hostName, locator1Port)); + + // stop one of the locators and ensure that the client can find and use a server + locator0VM.invoke("Stop Locator", this::stopLocator); + + serverVM.invoke("Start BridgeServer", + () -> startBridgeServer(null, getLocatorString(hostName, locator1Port))); + + putAndWaitForSuccess(clientVM, REGION_NAME, "key", "value"); + Assert.assertEquals("value", getInVM(serverVM, "key")); + + checkLocators(clientVM, initialLocators, new InetSocketAddress(hostName, locator1Port)); + } @Test - public void testEmbeddedLocator() throws Exception { - final Host host = Host.getHost(0); - VM vm0 = host.getVM(0); - VM vm1 = host.getVM(1); - VM vm2 = host.getVM(2); - VM vm3 = host.getVM(3); + public void testClientCanUseAnEmbeddedLocator() throws Exception { + VM vm0 = VM.getVM(0); + VM vm1 = VM.getVM(1); int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - String locators = NetworkUtils.getServerHostName(vm0.getHost()) + "[" + locatorPort + "]"; + String locators = getLocatorString(getServerHostName(), locatorPort); vm0.invoke("Start BridgeServer", () -> startBridgeServerWithEmbeddedLocator(null, locators, new String[] {REGION_NAME}, CacheServer.DEFAULT_LOAD_PROBE)); - vm2.invoke("StartBridgeClient", - () -> startBridgeClient(null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort)); + vm1.invoke("StartBridgeClient", + () -> startBridgeClient(null, getServerHostName(), locatorPort)); - putAndWaitForSuccess(vm2, REGION_NAME, "key", "value"); + putAndWaitForSuccess(vm1, REGION_NAME, "key", "value"); - Assert.assertEquals("value", getInVM(vm2, "key")); + Assert.assertEquals("value", getInVM(vm1, "key")); } private void waitForLocatorDiscovery(VM vm, final InetSocketAddress locatorToWaitFor) { - vm.invoke(new SerializableCallable() { - public Object call() throws InterruptedException { - MyLocatorCallback callback = (MyLocatorCallback) remoteObjects.get(CALLBACK_KEY); - - boolean discovered = callback.waitForDiscovery(locatorToWaitFor, MAX_WAIT); - Assert.assertTrue( - "Waited " + MAX_WAIT + " for " + locatorToWaitFor - + " to be discovered on client. List is now: " + callback.getDiscovered(), - discovered); - return null; - } + vm.invoke(() -> { + MyLocatorCallback callback = (MyLocatorCallback) remoteObjects.get(CALLBACK_KEY); + + boolean discovered = callback.waitForDiscovery(locatorToWaitFor, MAX_WAIT); + Assert.assertTrue( + "Waited " + MAX_WAIT + " for " + locatorToWaitFor + + " to be discovered on client. List is now: " + callback.getDiscovered(), + discovered); + return null; }); } @Test - public void testServerGroups() throws Exception { - final Host host = Host.getHost(0); - VM vm0 = host.getVM(0); - VM vm1 = host.getVM(1); - VM vm2 = host.getVM(2); - VM vm3 = host.getVM(3); + public void testClientFindsServerGroups() { + VM vm0 = VM.getVM(0); + VM vm1 = VM.getVM(1); + VM vm2 = VM.getVM(2); + VM vm3 = VM.getVM(3); - int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - vm0.invoke("Start Locator", - () -> startLocator(NetworkUtils.getServerHostName(vm0.getHost()), locatorPort, "")); + int locatorPort = vm0.invoke("Start Locator", () -> startLocator(getServerHostName(), "")); - String locators = NetworkUtils.getServerHostName(vm0.getHost()) + "[" + locatorPort + "]"; + String locators = getLocatorString(getServerHostName(), locatorPort); vm1.invoke("Start BridgeServer", () -> startBridgeServer(new String[] {"group1", "group2"}, locators, new String[] {"A", "B"})); vm2.invoke("Start BridgeServer", () -> startBridgeServer(new String[] {"group2", "group3"}, locators, new String[] {"B", "C"})); - vm3.invoke("StartBridgeClient", () -> startBridgeClient("group1", - NetworkUtils.getServerHostName(vm0.getHost()), locatorPort, new String[] {"A", "B", "C"})); + vm3.invoke("StartBridgeClient", () -> startBridgeClient("group1", getServerHostName(), + locatorPort, new String[] {"A", "B", "C"})); putAndWaitForSuccess(vm3, "A", "key", "value"); Assert.assertEquals("value", getInVM(vm1, "A", "key")); try { putInVM(vm3, "C", "key2", "value2"); fail("Should not have been able to find Region C on the server"); - } catch (Exception expected) { + } catch (RMIException expected) { + Throwable realCause = expected; + while (realCause.getCause() != null) { + realCause = realCause.getCause(); + } + assertEquals("Found wrong exception: " + realCause, RegionDestroyedException.class, + realCause.getClass()); } stopBridgeMemberVM(vm3); - vm3.invoke("StartBridgeClient", () -> startBridgeClient("group3", - NetworkUtils.getServerHostName(vm0.getHost()), locatorPort, new String[] {"A", "B", "C"})); + vm3.invoke("StartBridgeClient", () -> startBridgeClient("group3", getServerHostName(), + locatorPort, new String[] {"A", "B", "C"})); try { putInVM(vm3, "A", "key3", "value"); fail("Should not have been able to find Region A on the server"); - } catch (Exception expected) { + } catch (RMIException expected) { + Throwable realCause = expected; + while (realCause.getCause() != null) { + realCause = realCause.getCause(); + } + assertEquals("Found wrong exception: " + realCause, RegionDestroyedException.class, + realCause.getClass()); } putInVM(vm3, "C", "key4", "value"); Assert.assertEquals("value", getInVM(vm2, "C", "key4")); stopBridgeMemberVM(vm3); - vm3.invoke("StartBridgeClient", () -> startBridgeClient("group2", - NetworkUtils.getServerHostName(vm0.getHost()), locatorPort, new String[] {"A", "B", "C"})); + vm3.invoke("StartBridgeClient", () -> startBridgeClient("group2", getServerHostName(), + locatorPort, new String[] {"A", "B", "C"})); putInVM(vm3, "B", "key5", "value"); Assert.assertEquals("value", getInVM(vm1, "B", "key5")); Assert.assertEquals("value", getInVM(vm2, "B", "key5")); @@ -306,50 +314,41 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { } @Test - public void testTwoServersInSameVM() throws Exception { - final Host host = Host.getHost(0); - VM vm0 = host.getVM(0); - VM vm1 = host.getVM(1); - VM vm2 = host.getVM(2); - // VM vm3 = host.getVM(3); - - int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); + public void testTwoServersInSameVM() { + VM vm0 = VM.getVM(0); + VM vm1 = VM.getVM(1); + VM vm2 = VM.getVM(2); - vm0.invoke("Start Locator", - () -> startLocator(NetworkUtils.getServerHostName(vm0.getHost()), locatorPort, "")); + int locatorPort = vm0.invoke("Start Locator", () -> startLocator(getServerHostName(), "")); - final String locators = NetworkUtils.getServerHostName(vm0.getHost()) + "[" + locatorPort + "]"; + final String locators = getLocatorString(getServerHostName(), locatorPort); - final int serverPort1 = vm1.invoke("Start BridgeServer", - () -> startBridgeServer(new String[] {"group1"}, locators)); + final int serverPort1 = + vm1.invoke("Start Server", () -> startBridgeServer(new String[] {"group1"}, locators)); final int serverPort2 = - vm1.invoke("Start CacheServer", () -> addCacheServer(new String[] {"group2"})); + vm1.invoke("Start Server", () -> addCacheServer(new String[] {"group2"})); - vm2.invoke("StartBridgeClient", () -> startBridgeClient("group2", - NetworkUtils.getServerHostName(vm0.getHost()), locatorPort)); + vm2.invoke("Start Client", () -> startBridgeClient("group2", getServerHostName(), locatorPort)); - checkEndpoints(vm2, new int[] {serverPort2}); + checkEndpoints(vm2, serverPort2); stopBridgeMemberVM(vm2); - vm2.invoke("StartBridgeClient", () -> startBridgeClient("group1", - NetworkUtils.getServerHostName(vm0.getHost()), locatorPort)); + vm2.invoke("Start Client", () -> startBridgeClient("group1", getServerHostName(), locatorPort)); - checkEndpoints(vm2, new int[] {serverPort1}); + checkEndpoints(vm2, serverPort1); } @Test - public void testClientMembershipListener() throws Exception { - final Host host = Host.getHost(0); - VM locatorVM = host.getVM(0); - VM bridge1VM = host.getVM(1); - VM bridge2VM = host.getVM(2); - VM clientVM = host.getVM(3); - int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - locatorVM.invoke("Start Locator", - () -> startLocator(NetworkUtils.getServerHostName(locatorVM.getHost()), locatorPort, "")); + public void testClientMembershipListener() { + VM locatorVM = VM.getVM(0); + VM bridge1VM = VM.getVM(1); + VM bridge2VM = VM.getVM(2); + VM clientVM = VM.getVM(3); + int locatorPort = + locatorVM.invoke("Start Locator", () -> startLocator(getServerHostName(), "")); - String locators = NetworkUtils.getServerHostName(locatorVM.getHost()) + "[" + locatorPort + "]"; + String locators = getLocatorString(getServerHostName(), locatorPort); // start a bridge server with a listener addBridgeListener(bridge1VM); @@ -359,11 +358,10 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { // start a bridge client with a listener addBridgeListener(clientVM); clientVM.invoke("StartBridgeClient", () -> { - String locatorHostName = NetworkUtils.getServerHostName(locatorVM.getHost()); - startBridgeClient(null, locatorHostName, locatorPort); + startBridgeClient(null, getServerHostName(), locatorPort); }); // wait for client to connect - checkEndpoints(clientVM, new int[] {serverPort1}); + checkEndpoints(clientVM, serverPort1); // make sure the client and bridge server both noticed each other waitForJoin(bridge1VM); @@ -380,13 +378,13 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { Assert.assertEquals(1, clientListener.getJoins()); resetBridgeListener(clientVM); - checkEndpoints(clientVM, new int[] {serverPort1}); + checkEndpoints(clientVM, serverPort1); // start another bridge server and make sure it is detected by the client int serverPort2 = bridge2VM.invoke("Start BridgeServer", () -> startBridgeServer(null, locators)); - checkEndpoints(clientVM, new int[] {serverPort1, serverPort2}); + checkEndpoints(clientVM, serverPort1, serverPort2); serverListener = getBridgeListener(bridge1VM); Assert.assertEquals(0, serverListener.getCrashes()); Assert.assertEquals(0, serverListener.getDepartures()); @@ -402,7 +400,7 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { // stop the second bridge server and make sure it is detected by the client stopBridgeMemberVM(bridge2VM); - checkEndpoints(clientVM, new int[] {serverPort1}); + checkEndpoints(clientVM, serverPort1); serverListener = getBridgeListener(bridge1VM); Assert.assertEquals(0, serverListener.getCrashes()); Assert.assertEquals(0, serverListener.getDepartures()); @@ -423,42 +421,23 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { Assert.assertEquals(0, serverListener.getJoins()); } - protected Object getInVM(VM vm, final Serializable key) { + private Object getInVM(VM vm, final Serializable key) { return getInVM(vm, REGION_NAME, key); } - protected Object getInVM(VM vm, final String regionName, final Serializable key) { - return vm.invoke(new SerializableCallable("Get in VM") { - public Object call() throws Exception { - Cache cache = (Cache) remoteObjects.get(CACHE_KEY); - Region region = cache.getRegion(regionName); - return region.get(key); - } + private Object getInVM(VM vm, final String regionName, final Serializable key) { + return vm.invoke("Get in VM", () -> { + Cache cache = (Cache) remoteObjects.get(CACHE_KEY); + Region region = cache.getRegion(regionName); + return region.get(key); }); } - protected void putAndWaitForSuccess(VM vm, final String regionName, final Serializable key, - final Serializable value) throws InterruptedException { - long endTime = System.currentTimeMillis() + MAX_WAIT; - long remaining = MAX_WAIT; - int i = 0; - while (true) { - try { - System.err.println("Attempt: " + (i++)); - putInVM(vm, regionName, key, value); - break; - } catch (NoAvailableLocatorsException | org.apache.geode.test.dunit.RMIException e) { - if (!(e instanceof NoAvailableLocatorsException) - && !(e.getCause() instanceof NoAvailableServersException)) { - throw e; - } - if (remaining <= 0) { - throw e; - } - Wait.pause(100); - remaining = endTime - System.currentTimeMillis(); - } - } + private void putAndWaitForSuccess(VM vm, final String regionName, final Serializable key, + final Serializable value) { + Awaitility.await().atMost(MAX_WAIT, MILLISECONDS).until(() -> { + putInVM(vm, regionName, key, value); + }); } protected void putInVM(VM vm, final Serializable key, final Serializable value) { @@ -467,13 +446,8 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { protected void putInVM(VM vm, final String regionName, final Serializable key, final Serializable value) { - vm.invoke(new SerializableCallable("Put in VM") { - public Object call() throws Exception { - Cache cache = (Cache) remoteObjects.get(CACHE_KEY); - Region region = cache.getRegion(regionName); - return region.put(key, value); - } - }); + vm.invoke("Put in VM", + () -> ((Cache) remoteObjects.get(CACHE_KEY)).getRegion(regionName).put(key, value)); } /** @@ -482,152 +456,91 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { * @param vm - the vm the client is running in * @param expectedPorts - The server ports we expect the client to be connected to. */ - protected void checkEndpoints(VM vm, final int[] expectedPorts) { - vm.invoke(new SerializableRunnable("Check endpoint") { - public void run() { - PoolImpl pool = (PoolImpl) PoolManager.find(POOL_NAME); - int retryCount = 50; - List/* <ServerLocation> */ endpoints; + protected void checkEndpoints(VM vm, final int... expectedPorts) { + vm.invoke("Check endpoint", () -> { + PoolImpl pool = (PoolImpl) PoolManager.find(POOL_NAME); + HashSet expectedEndpointPorts = new HashSet(); + for (int i = 0; i < expectedPorts.length; i++) { + expectedEndpointPorts.add(new Integer(expectedPorts[i])); + } + Awaitility.await().atMost(5, SECONDS).until(() -> { + List<ServerLocation> endpoints; HashSet actualEndpointPorts; - HashSet expectedEndpointPorts = new HashSet(); - for (int i = 0; i < expectedPorts.length; i++) { - expectedEndpointPorts.add(new Integer(expectedPorts[i])); + endpoints = pool.getCurrentServers(); + actualEndpointPorts = new HashSet(); + for (Iterator itr = endpoints.iterator(); itr.hasNext();) { + ServerLocation sl = (ServerLocation) itr.next(); + actualEndpointPorts.add(new Integer(sl.getPort())); } - do { - endpoints = pool.getCurrentServers(); - actualEndpointPorts = new HashSet(); - for (Iterator itr = endpoints.iterator(); itr.hasNext();) { - ServerLocation sl = (ServerLocation) itr.next(); - actualEndpointPorts.add(new Integer(sl.getPort())); - } - if (expectedEndpointPorts.size() == actualEndpointPorts.size()) { - break; - } - Wait.pause(100); - } while (retryCount-- > 0); - Assert.assertEquals(expectedEndpointPorts, actualEndpointPorts); - } + assertEquals(expectedEndpointPorts, actualEndpointPorts); + }); }); } protected void checkLocators(VM vm, final InetSocketAddress[] expectedInitial, - final InetSocketAddress[] expected) { - vm.invoke(new SerializableRunnable("Check locators") { - public void run() { - Pool pool = PoolManager.find(POOL_NAME); - - List<InetSocketAddress> initialLocators = pool.getLocators(); - Assert.assertEquals(expectedInitial.length, initialLocators.size()); - Arrays.sort(expectedInitial, Comparator.comparing(InetSocketAddress::getPort)); - for (int i = 0; i < initialLocators.size(); i++) { - InetSocketAddress locator = initialLocators.get(i); - InetSocketAddress expectedOne = expectedInitial[i]; - Assert.assertEquals(expectedOne, locator); - } + final InetSocketAddress... expected) { + vm.invoke("Check locators", () -> { + Pool pool = PoolManager.find(POOL_NAME); + + List<InetSocketAddress> initialLocators = pool.getLocators(); + Assert.assertEquals(expectedInitial.length, initialLocators.size()); + Arrays.sort(expectedInitial, Comparator.comparing(InetSocketAddress::getPort)); + for (int i = 0; i < initialLocators.size(); i++) { + InetSocketAddress locatorAddress = initialLocators.get(i); + InetSocketAddress expectedAddress = expectedInitial[i]; + Assert.assertEquals(expectedAddress, locatorAddress); + } - List<InetSocketAddress> locators = pool.getOnlineLocators(); - Assert.assertEquals("found " + locators, expected.length, locators.size()); - Arrays.sort(expected, Comparator.comparing(InetSocketAddress::getPort)); - for (int i = 0; i < locators.size(); i++) { - InetSocketAddress locator = locators.get(i); - InetSocketAddress expectedOne = expected[i]; - Assert.assertEquals(expectedOne, locator); - } + List<InetSocketAddress> locators = pool.getOnlineLocators(); + Assert.assertEquals("found " + locators, expected.length, locators.size()); + Arrays.sort(expected, Comparator.comparing(InetSocketAddress::getPort)); + for (int i = 0; i < locators.size(); i++) { + InetSocketAddress locatorAddress = locators.get(i); + InetSocketAddress expectedAddress = expected[i]; + Assert.assertEquals(expectedAddress, locatorAddress); } }); } protected void addBridgeListener(VM vm) { - vm.invoke(new SerializableRunnable("Add membership listener") { - public void run() { - MyListener listener = new MyListener(); - ClientMembership.registerClientMembershipListener(listener); - remoteObjects.put(BRIDGE_LISTENER, listener); - } + vm.invoke("Add membership listener", () -> { + MyListener listener = new MyListener(); + ClientMembership.registerClientMembershipListener(listener); + remoteObjects.put(BRIDGE_LISTENER, listener); }); } protected void resetBridgeListener(VM vm) { - vm.invoke(new SerializableRunnable("Reset membership listener") { - public void run() { - MyListener listener = (MyListener) remoteObjects.get(BRIDGE_LISTENER); - listener.reset(); - } + vm.invoke("Reset membership listener", () -> { + MyListener listener = (MyListener) remoteObjects.get(BRIDGE_LISTENER); + listener.reset(); }); } private MyListener getBridgeListener(VM vm) { - return (MyListener) vm.invoke(new SerializableCallable("Get membership listener") { - public Object call() { - return remoteObjects.get(BRIDGE_LISTENER); - } + return (MyListener) vm.invoke("Get membership listener", () -> { + return remoteObjects.get(BRIDGE_LISTENER); }); } private void waitForJoin(VM vm) { - vm.invoke(new SerializableRunnable("wait for join") { - public void run() { - MyListener listener = (MyListener) remoteObjects.get(BRIDGE_LISTENER); - synchronized (listener) { - long end = System.currentTimeMillis() + 10000; - while (listener.joins == 0) { - try { - long remaining = end - System.currentTimeMillis(); - if (remaining <= 0) { - break; - } - listener.wait(remaining); - } catch (InterruptedException e) { - fail("interrupted"); - } - } - GemFireCacheImpl cache = GemFireCacheImpl.getInstance(); - } - } + vm.invoke("wait for join", () -> { + MyListener listener = (MyListener) remoteObjects.get(BRIDGE_LISTENER); + Awaitility.await().atMost(10, SECONDS).until(() -> listener.getJoins() > 0); }); } private void waitForCrash(VM vm) { - vm.invoke(new SerializableRunnable("wait for crash") { - public void run() { - MyListener listener = (MyListener) remoteObjects.get(BRIDGE_LISTENER); - synchronized (listener) { - long end = System.currentTimeMillis() + 10000; - while (listener.crashes == 0) { - try { - long remaining = end - System.currentTimeMillis(); - if (remaining <= 0) { - return; - } - listener.wait(remaining); - } catch (InterruptedException e) { - fail("interrupted"); - } - } - } - } + vm.invoke("wait for crash", () -> { + MyListener listener = (MyListener) remoteObjects.get(BRIDGE_LISTENER); + Awaitility.await().atMost(10, SECONDS).until(() -> listener.getCrashes() > 0); }); } private void waitForDeparture(VM vm) { - vm.invoke(new SerializableRunnable("wait for departure") { - public void run() { - MyListener listener = (MyListener) remoteObjects.get(BRIDGE_LISTENER); - synchronized (listener) { - long end = System.currentTimeMillis() + 10000; - while (listener.departures == 0) { - try { - long remaining = end - System.currentTimeMillis(); - if (remaining < 0) { - break; - } - listener.wait(remaining); - } catch (InterruptedException e) { - fail("interrupted"); - } - } - } - } + vm.invoke("wait for departure", () -> { + MyListener listener = (MyListener) remoteObjects.get(BRIDGE_LISTENER); + Awaitility.await().atMost(10, SECONDS).until(() -> listener.getDepartures() > 0); }); } diff --git a/geode-core/src/test/java/org/apache/geode/cache/client/internal/LocatorLoadBalancingDUnitTest.java b/geode-core/src/test/java/org/apache/geode/cache/client/internal/LocatorLoadBalancingDUnitTest.java index bdc4d6c..423a5b4 100644 --- a/geode-core/src/test/java/org/apache/geode/cache/client/internal/LocatorLoadBalancingDUnitTest.java +++ b/geode-core/src/test/java/org/apache/geode/cache/client/internal/LocatorLoadBalancingDUnitTest.java @@ -47,7 +47,6 @@ import org.apache.geode.distributed.internal.InternalLocator; import org.apache.geode.distributed.internal.ServerLocation; import org.apache.geode.distributed.internal.ServerLocator; import org.apache.geode.distributed.internal.tcpserver.TcpClient; -import org.apache.geode.internal.AvailablePort; import org.apache.geode.internal.cache.CacheServerImpl; import org.apache.geode.internal.cache.PoolFactoryImpl; import org.apache.geode.internal.logging.InternalLogWriter; @@ -89,18 +88,16 @@ public class LocatorLoadBalancingDUnitTest extends LocatorTestBase { VM vm1 = host.getVM(1); VM vm2 = host.getVM(2); - int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - String hostName = NetworkUtils.getServerHostName(vm0.getHost()); - vm0.invoke("Start Locator", () -> startLocator(hostName, locatorPort, "")); + String hostName = NetworkUtils.getServerHostName(); + int locatorPort = vm0.invoke("Start Locator", () -> startLocator(hostName, "")); - String locators = getLocatorString(host, locatorPort); + String locators = getLocatorString(hostName, locatorPort); int serverPort = vm1.invoke("Start BridgeServer", () -> startBridgeServer(new String[] {"a", "b"}, locators)); ServerLoad expectedLoad = new ServerLoad(0f, 1 / 800.0f, 0f, 1f); - ServerLocation expectedLocation = - new ServerLocation(NetworkUtils.getServerHostName(vm0.getHost()), serverPort); + ServerLocation expectedLocation = new ServerLocation(hostName, serverPort); Map expected = new HashMap(); expected.put(expectedLocation, expectedLoad); @@ -109,8 +106,7 @@ public class LocatorLoadBalancingDUnitTest extends LocatorTestBase { int serverPort2 = vm2.invoke("Start BridgeServer", () -> startBridgeServer(new String[] {"a", "b"}, locators)); - ServerLocation expectedLocation2 = - new ServerLocation(NetworkUtils.getServerHostName(vm0.getHost()), serverPort2); + ServerLocation expectedLocation2 = new ServerLocation(hostName, serverPort2); expected.put(expectedLocation2, expectedLoad); vm0.invoke("check Locator Load", () -> checkLocatorLoad(expected)); @@ -126,44 +122,44 @@ public class LocatorLoadBalancingDUnitTest extends LocatorTestBase { VM vm0 = host.getVM(0); VM vm1 = host.getVM(1); - int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - String hostName = NetworkUtils.getServerHostName(vm0.getHost()); - vm0.invoke("Start Locator", () -> startLocator(hostName, locatorPort, "")); - String locators = getLocatorString(host, locatorPort); + String hostName = NetworkUtils.getServerHostName(); + int locatorPort = vm0.invoke("Start Locator", () -> startLocator(hostName, "")); + String locators = getLocatorString(hostName, locatorPort); int serverPort = vm1.invoke("Start BridgeServer", () -> startBridgeServer(new String[] {"a", "b"}, locators)); ServerLoad expectedLoad = new ServerLoad(2 / 800f, 1 / 800.0f, 0f, 1f); - ServerLocation expectedLocation = - new ServerLocation(NetworkUtils.getServerHostName(host), serverPort); + ServerLocation expectedLocation = new ServerLocation(hostName, serverPort); Map expected = new HashMap(); expected.put(expectedLocation, expectedLoad); SocketCreatorFactory.setDistributionConfig(new DistributionConfigImpl(new Properties())); ClientConnectionResponse response; - response = (ClientConnectionResponse) new TcpClient().requestToServer( - InetAddress.getByName(NetworkUtils.getServerHostName(host)), locatorPort, - new ClientConnectionRequest(Collections.EMPTY_SET, null), 10000); + response = + (ClientConnectionResponse) new TcpClient().requestToServer(InetAddress.getByName(hostName), + locatorPort, new ClientConnectionRequest(Collections.EMPTY_SET, null), 10000); Assert.assertEquals(expectedLocation, response.getServer()); - response = (ClientConnectionResponse) new TcpClient().requestToServer( - InetAddress.getByName(NetworkUtils.getServerHostName(host)), locatorPort, - new ClientConnectionRequest(Collections.EMPTY_SET, null), 10000, true); + response = + (ClientConnectionResponse) new TcpClient().requestToServer(InetAddress.getByName(hostName), + locatorPort, new ClientConnectionRequest(Collections.EMPTY_SET, null), 10000, true); Assert.assertEquals(expectedLocation, response.getServer()); // we expect that the connection load load will be 2 * the loadPerConnection vm0.invoke("check Locator Load", () -> checkLocatorLoad(expected)); QueueConnectionResponse response2; - response2 = (QueueConnectionResponse) new TcpClient().requestToServer( - InetAddress.getByName(NetworkUtils.getServerHostName(host)), locatorPort, - new QueueConnectionRequest(null, 2, Collections.EMPTY_SET, null, false), 10000, true); + response2 = + (QueueConnectionResponse) new TcpClient().requestToServer(InetAddress.getByName(hostName), + locatorPort, new QueueConnectionRequest(null, 2, Collections.EMPTY_SET, null, false), + 10000, true); Assert.assertEquals(Collections.singletonList(expectedLocation), response2.getServers()); - response2 = (QueueConnectionResponse) new TcpClient().requestToServer( - InetAddress.getByName(NetworkUtils.getServerHostName(host)), locatorPort, - new QueueConnectionRequest(null, 5, Collections.EMPTY_SET, null, false), 10000, true); + response2 = + (QueueConnectionResponse) new TcpClient().requestToServer(InetAddress.getByName(hostName), + locatorPort, new QueueConnectionRequest(null, 5, Collections.EMPTY_SET, null, false), + 10000, true); Assert.assertEquals(Collections.singletonList(expectedLocation), response2.getServers()); @@ -184,18 +180,16 @@ public class LocatorLoadBalancingDUnitTest extends LocatorTestBase { VM vm1 = host.getVM(1); VM vm2 = host.getVM(2); - int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - String hostName = NetworkUtils.getServerHostName(vm0.getHost()); - vm0.invoke("Start Locator", () -> startLocator(hostName, locatorPort, "")); - String locators = getLocatorString(host, locatorPort); + String hostName = NetworkUtils.getServerHostName(); + int locatorPort = vm0.invoke("Start Locator", () -> startLocator(hostName, "")); + String locators = getLocatorString(hostName, locatorPort); final int serverPort = vm1.invoke("Start BridgeServer", () -> startBridgeServer(new String[] {"a", "b"}, locators)); // We expect 0 load Map expected = new HashMap(); - ServerLocation expectedLocation = - new ServerLocation(NetworkUtils.getServerHostName(host), serverPort); + ServerLocation expectedLocation = new ServerLocation(hostName, serverPort); ServerLoad expectedLoad = new ServerLoad(0f, 1 / 800.0f, 0f, 1f); expected.put(expectedLocation, expectedLoad); vm0.invoke("check Locator Load", () -> checkLocatorLoad(expected)); @@ -236,17 +230,16 @@ public class LocatorLoadBalancingDUnitTest extends LocatorTestBase { VM vm2 = host.getVM(2); VM vm3 = host.getVM(3); - int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - String hostName = NetworkUtils.getServerHostName(vm0.getHost()); - vm0.invoke("Start Locator", () -> startLocator(hostName, locatorPort, "")); - String locators = getLocatorString(host, locatorPort); + String hostName = NetworkUtils.getServerHostName(); + int locatorPort = vm0.invoke("Start Locator", () -> startLocator(hostName, "")); + String locators = getLocatorString(hostName, locatorPort); vm1.invoke("Start BridgeServer", () -> startBridgeServer(new String[] {"a", "b"}, locators)); vm2.invoke("Start BridgeServer", () -> startBridgeServer(new String[] {"a", "b"}, locators)); vm3.invoke("StartBridgeClient", () -> { PoolFactoryImpl pf = new PoolFactoryImpl(null); - pf.addLocator(NetworkUtils.getServerHostName(host), locatorPort); + pf.addLocator(hostName, locatorPort); pf.setMinConnections(80); pf.setMaxConnections(80); pf.setSubscriptionEnabled(false); @@ -300,10 +293,9 @@ public class LocatorLoadBalancingDUnitTest extends LocatorTestBase { VM vm2 = host.getVM(2); VM vm3 = host.getVM(3); - int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - String hostName = NetworkUtils.getServerHostName(vm0.getHost()); - vm0.invoke("Start Locator", () -> startLocator(hostName, locatorPort, "")); - String locators = getLocatorString(host, locatorPort); + String hostName = NetworkUtils.getServerHostName(); + int locatorPort = vm0.invoke("Start Locator", () -> startLocator(hostName, "")); + String locators = getLocatorString(hostName, locatorPort); int serverPort1 = vm1.invoke("Start BridgeServer", () -> startBridgeServer(new String[] {"a"}, locators)); @@ -387,10 +379,9 @@ public class LocatorLoadBalancingDUnitTest extends LocatorTestBase { VM vm2 = host.getVM(2); // VM vm3 = host.getVM(3); - int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); String hostName = NetworkUtils.getServerHostName(vm0.getHost()); - vm0.invoke("Start Locator", () -> startLocator(hostName, locatorPort, "")); - String locators = getLocatorString(host, locatorPort); + int locatorPort = vm0.invoke("Start Locator", () -> startLocator(hostName, "")); + String locators = getLocatorString(hostName, locatorPort); final ServerLoad load1 = new ServerLoad(.3f, .01f, .44f, 4564f); final ServerLoad load2 = new ServerLoad(23.2f, 1.1f, 22.3f, .3f); diff --git a/geode-core/src/test/java/org/apache/geode/cache/client/internal/LocatorTestBase.java b/geode-core/src/test/java/org/apache/geode/cache/client/internal/LocatorTestBase.java index 4b8296b..f8ef9f1 100644 --- a/geode-core/src/test/java/org/apache/geode/cache/client/internal/LocatorTestBase.java +++ b/geode-core/src/test/java/org/apache/geode/cache/client/internal/LocatorTestBase.java @@ -37,8 +37,8 @@ import org.apache.commons.lang.StringUtils; import org.apache.geode.cache.AttributesFactory; import org.apache.geode.cache.Cache; import org.apache.geode.cache.CacheFactory; -import org.apache.geode.cache.DataPolicy; import org.apache.geode.cache.RegionAttributes; +import org.apache.geode.cache.RegionShortcut; import org.apache.geode.cache.Scope; import org.apache.geode.cache.client.Pool; import org.apache.geode.cache.client.PoolManager; @@ -48,11 +48,8 @@ import org.apache.geode.distributed.DistributedSystem; import org.apache.geode.distributed.Locator; import org.apache.geode.internal.cache.PoolFactoryImpl; import org.apache.geode.test.dunit.Assert; -import org.apache.geode.test.dunit.Host; import org.apache.geode.test.dunit.Invoke; import org.apache.geode.test.dunit.LogWriterUtils; -import org.apache.geode.test.dunit.NetworkUtils; -import org.apache.geode.test.dunit.SerializableCallable; import org.apache.geode.test.dunit.SerializableRunnable; import org.apache.geode.test.dunit.VM; import org.apache.geode.test.dunit.internal.JUnit4DistributedTestCase; @@ -109,45 +106,28 @@ public abstract class LocatorTestBase extends JUnit4DistributedTestCase { protected void postTearDownLocatorTestBase() throws Exception {} - protected void startLocator(final String hostName, final int locatorPort, - final String otherLocators) { + protected int startLocator(final String hostName, final String otherLocators) throws Exception { final String testName = getUniqueName(); disconnectFromDS(); Properties props = new Properties(); - props.setProperty(MCAST_PORT, String.valueOf(0)); - props.setProperty(LOCATORS, otherLocators); - props.setProperty(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel()); - props.setProperty(ENABLE_CLUSTER_CONFIGURATION, "false"); + props.put(MCAST_PORT, String.valueOf(0)); + props.put(LOCATORS, otherLocators); + props.put(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel()); + props.put(ENABLE_CLUSTER_CONFIGURATION, "false"); + File logFile = new File(""); + InetAddress bindAddr = null; try { - File logFile = new File(testName + "-locator" + locatorPort + ".log"); - InetAddress bindAddr = null; - try { - bindAddr = InetAddress.getByName(hostName); - } catch (UnknownHostException uhe) { - Assert.fail("While resolving bind address ", uhe); - } - Locator locator = Locator.startLocatorAndDS(locatorPort, logFile, bindAddr, props); - remoteObjects.put(LOCATOR_KEY, locator); - } catch (IOException ex) { - Assert.fail("While starting locator on port " + locatorPort, ex); + bindAddr = InetAddress.getByName(hostName); + } catch (UnknownHostException uhe) { + Assert.fail("While resolving bind address ", uhe); } + Locator locator = Locator.startLocatorAndDS(0, logFile, bindAddr, props); + remoteObjects.put(LOCATOR_KEY, locator); + return locator.getPort(); } - protected void startLocatorInVM(final VM vm, final int locatorPort, final String otherLocators) { - vm.invoke(new SerializableRunnable("Create Locator") { - public void run() { - String hostName = NetworkUtils.getServerHostName(vm.getHost()); - startLocator(hostName, locatorPort, otherLocators); - } - }); - } - - protected void stopLocatorInVM(VM vm) { - vm.invoke(new SerializableRunnable("Stop Locator") { - public void run() { - stopLocator(); - } - }); + protected int startLocatorInVM(final VM vm, final String hostName, final String otherLocators) { + return vm.invoke("create locator", () -> startLocator(hostName, otherLocators)); } protected void stopLocator() { @@ -165,17 +145,7 @@ public abstract class LocatorTestBase extends JUnit4DistributedTestCase { server.setPort(0); server.setGroups(groups); server.start(); - return new Integer(server.getPort()); - } - - protected int addCacheServerInVM(VM vm, final String[] groups) { - SerializableCallable connect = new SerializableCallable("Add Bridge server") { - public Object call() throws Exception { - return addCacheServer(groups); - } - }; - Integer port = (Integer) vm.invoke(connect); - return port.intValue(); + return server.getPort(); } protected int startBridgeServerInVM(VM vm, final String[] groups, final String locators, @@ -192,13 +162,8 @@ public abstract class LocatorTestBase extends JUnit4DistributedTestCase { protected int startBridgeServerInVM(VM vm, final String[] groups, final String locators, final String[] regions, final ServerLoadProbe probe, final boolean useGroupsProperty) { - SerializableCallable connect = new SerializableCallable("Start bridge server") { - public Object call() throws IOException { - return startBridgeServer(groups, locators, regions, probe, useGroupsProperty); - } - }; - Integer port = (Integer) vm.invoke(connect); - return port.intValue(); + return vm.invoke("start bridge server", + () -> startBridgeServer(groups, locators, regions, probe, useGroupsProperty)); } protected int startBridgeServer(String[] groups, String locators) throws IOException { @@ -213,21 +178,14 @@ public abstract class LocatorTestBase extends JUnit4DistributedTestCase { protected int startBridgeServer(final String[] groups, final String locators, final String[] regions, final ServerLoadProbe probe, final boolean useGroupsProperty) throws IOException { - Properties props = new Properties(); - props.setProperty(MCAST_PORT, "0"); - props.setProperty(LOCATORS, locators); + CacheFactory cacheFactory = new CacheFactory().set(MCAST_PORT, "0").set(LOCATORS, locators); if (useGroupsProperty && groups != null) { - props.setProperty(GROUPS, StringUtils.join(groups, ",")); + cacheFactory.set(GROUPS, StringUtils.join(groups, ",")); } - DistributedSystem ds = getSystem(props); - Cache cache = CacheFactory.create(ds); - AttributesFactory factory = new AttributesFactory(); - factory.setScope(Scope.DISTRIBUTED_ACK); - factory.setEnableBridgeConflation(true); - factory.setDataPolicy(DataPolicy.REPLICATE); - RegionAttributes attrs = factory.create(); - for (int i = 0; i < regions.length; i++) { - cache.createRegion(regions[i], attrs); + Cache cache = cacheFactory.create(); + for (String regionName : regions) { + cache.createRegionFactory(RegionShortcut.REPLICATE).setEnableSubscriptionConflation(true) + .create(regionName); } CacheServer server = cache.addCacheServer(); server.setPort(0); @@ -239,24 +197,16 @@ public abstract class LocatorTestBase extends JUnit4DistributedTestCase { remoteObjects.put(CACHE_KEY, cache); - return new Integer(server.getPort()); + return server.getPort(); } protected int startBridgeServerWithEmbeddedLocator(final String[] groups, final String locators, final String[] regions, final ServerLoadProbe probe) throws IOException { - Properties props = new Properties(); - props.setProperty(MCAST_PORT, String.valueOf(0)); - props.setProperty(START_LOCATOR, locators); - props.setProperty(LOCATORS, locators); - DistributedSystem ds = getSystem(props); - Cache cache = CacheFactory.create(ds); - AttributesFactory factory = new AttributesFactory(); - factory.setScope(Scope.DISTRIBUTED_ACK); - factory.setEnableBridgeConflation(true); - factory.setDataPolicy(DataPolicy.REPLICATE); - RegionAttributes attrs = factory.create(); - for (int i = 0; i < regions.length; i++) { - cache.createRegion(regions[i], attrs); + Cache cache = new CacheFactory().set(MCAST_PORT, "0").set(LOCATORS, locators) + .set(START_LOCATOR, locators).create(); + for (String regionName : regions) { + cache.createRegionFactory(RegionShortcut.REPLICATE).setEnableSubscriptionConflation(true) + .create(regionName); } CacheServer server = cache.addCacheServer(); server.setGroups(groups); @@ -266,34 +216,23 @@ public abstract class LocatorTestBase extends JUnit4DistributedTestCase { remoteObjects.put(CACHE_KEY, cache); - return new Integer(server.getPort()); - } - - protected int startBridgeServerWithEmbeddedLocatorInVM(VM vm, final String[] groups, - final String locators, final String[] regions, final ServerLoadProbe probe) { - SerializableCallable connect = new SerializableCallable("Start bridge server") { - public Object call() throws IOException { - return startBridgeServerWithEmbeddedLocator(groups, locators, regions, probe); - } - }; - Integer port = (Integer) vm.invoke(connect); - return port.intValue(); + return server.getPort(); } protected void startBridgeClientInVM(VM vm, final String group, final String host, final int port) throws Exception { - startBridgeClientInVM(vm, group, host, port, new String[] {REGION_NAME}); + startBridgeClientInVM(vm, group, host, port, REGION_NAME); } protected void startBridgeClientInVM(VM vm, final String group, final String host, final int port, - final String[] regions) throws Exception { + final String... regions) throws Exception { PoolFactoryImpl pf = new PoolFactoryImpl(null); pf.addLocator(host, port).setServerGroup(group).setPingInterval(200) .setSubscriptionEnabled(true).setSubscriptionRedundancy(-1); startBridgeClientInVM(vm, pf.getPoolAttributes(), regions); } - protected void startBridgeClientInVM(VM vm, final Pool pool, final String[] regions) + protected void startBridgeClientInVM(VM vm, final Pool pool, final String... regions) throws Exception { SerializableRunnable connect = new SerializableRunnable("Start bridge client") { public void run() throws Exception { @@ -354,15 +293,14 @@ public abstract class LocatorTestBase extends JUnit4DistributedTestCase { }); } - public String getLocatorString(Host host, int locatorPort) { - return getLocatorString(host, new int[] {locatorPort}); + public String getLocatorString(String hostName, int locatorPort) { + return getLocatorString(hostName, new int[] {locatorPort}); } - public String getLocatorString(Host host, int[] locatorPorts) { + public String getLocatorString(String hostName, int... locatorPorts) { StringBuffer str = new StringBuffer(); for (int i = 0; i < locatorPorts.length; i++) { - str.append(NetworkUtils.getServerHostName(host)).append("[").append(locatorPorts[i]) - .append("]"); + str.append(hostName).append("[").append(locatorPorts[i]).append("]"); if (i < locatorPorts.length - 1) { str.append(","); } diff --git a/geode-core/src/test/java/org/apache/geode/internal/cache/ClientTxCommitShouldNotHangRegressionTest.java b/geode-core/src/test/java/org/apache/geode/internal/cache/ClientTxCommitShouldNotHangRegressionTest.java index 42a8f15..97010ca 100644 --- a/geode-core/src/test/java/org/apache/geode/internal/cache/ClientTxCommitShouldNotHangRegressionTest.java +++ b/geode-core/src/test/java/org/apache/geode/internal/cache/ClientTxCommitShouldNotHangRegressionTest.java @@ -30,7 +30,6 @@ import org.apache.geode.cache.client.ClientCacheFactory; import org.apache.geode.cache.client.ClientRegionShortcut; import org.apache.geode.cache.client.PoolManager; import org.apache.geode.cache.client.internal.LocatorTestBase; -import org.apache.geode.internal.AvailablePort; import org.apache.geode.test.dunit.NetworkUtils; import org.apache.geode.test.dunit.VM; import org.apache.geode.test.junit.categories.DistributedTest; @@ -116,12 +115,11 @@ public class ClientTxCommitShouldNotHangRegressionTest extends LocatorTestBase { region1Name = uniqueName + "_R1"; region2Name = uniqueName + "_R2"; - hostName = NetworkUtils.getServerHostName(getHost(0)); - locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); + hostName = NetworkUtils.getServerHostName(); - locator.invoke("Start locator", () -> startLocator(hostName, locatorPort, "")); + locatorPort = locator.invoke("Start locator", () -> startLocator(hostName, "")); - String locators = getLocatorString(getHost(0), locatorPort); + String locators = getLocatorString(hostName, locatorPort); server1.invoke("Start server", () -> startBridgeServer(new String[] {region1Name}, locators, new String[] {region1Name})); diff --git a/geode-core/src/test/java/org/apache/geode/internal/cache/FireAndForgetFunctionOnAllServersDUnitTest.java b/geode-core/src/test/java/org/apache/geode/internal/cache/FireAndForgetFunctionOnAllServersDUnitTest.java index 2dca67a..accd9ca 100644 --- a/geode-core/src/test/java/org/apache/geode/internal/cache/FireAndForgetFunctionOnAllServersDUnitTest.java +++ b/geode-core/src/test/java/org/apache/geode/internal/cache/FireAndForgetFunctionOnAllServersDUnitTest.java @@ -28,7 +28,6 @@ import org.apache.geode.cache.client.internal.PoolImpl; import org.apache.geode.cache.execute.Execution; import org.apache.geode.cache.execute.Function; import org.apache.geode.cache.execute.FunctionService; -import org.apache.geode.internal.AvailablePort; import org.apache.geode.internal.cache.functions.FireAndForgetFunctionOnAllServers; import org.apache.geode.test.dunit.Assert; import org.apache.geode.test.dunit.Host; @@ -66,13 +65,12 @@ public class FireAndForgetFunctionOnAllServersDUnitTest extends LocatorTestBase VM server2 = host.getVM(2); VM client = host.getVM(3); - final int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - final String locatorHost = NetworkUtils.getServerHostName(host); + final String locatorHost = NetworkUtils.getServerHostName(); // Step 1. Start a locator and one cache server. - locator.invoke("Start Locator", () -> startLocator(locatorHost, locatorPort, "")); + final int locatorPort = locator.invoke("Start Locator", () -> startLocator(locatorHost, "")); - String locString = getLocatorString(host, locatorPort); + String locString = getLocatorString(locatorHost, locatorPort); // Step 2. Start a server and create a replicated region "R1". server1.invoke("Start BridgeServer", diff --git a/geode-wan/src/test/java/org/apache/geode/cache/wan/GatewayReceiverAutoConnectionSourceDUnitTest.java b/geode-wan/src/test/java/org/apache/geode/cache/wan/GatewayReceiverAutoConnectionSourceDUnitTest.java index 5683bc3..73af8af 100644 --- a/geode-wan/src/test/java/org/apache/geode/cache/wan/GatewayReceiverAutoConnectionSourceDUnitTest.java +++ b/geode-wan/src/test/java/org/apache/geode/cache/wan/GatewayReceiverAutoConnectionSourceDUnitTest.java @@ -28,7 +28,6 @@ import org.apache.geode.cache.client.internal.AutoConnectionSourceImpl; import org.apache.geode.cache.client.internal.LocatorTestBase; import org.apache.geode.cache.client.internal.PoolImpl; import org.apache.geode.distributed.internal.ServerLocation; -import org.apache.geode.internal.AvailablePort; import org.apache.geode.test.dunit.Host; import org.apache.geode.test.dunit.NetworkUtils; import org.apache.geode.test.dunit.SerializableRunnable; @@ -75,10 +74,10 @@ public class GatewayReceiverAutoConnectionSourceDUnitTest extends LocatorTestBas VM vm1 = host.getVM(1); VM vm2 = host.getVM(2); - int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - startLocatorInVM(vm0, locatorPort, ""); + String hostName = NetworkUtils.getServerHostName(); + int locatorPort = startLocatorInVM(vm0, hostName, ""); - String locators = NetworkUtils.getServerHostName(vm0.getHost()) + "[" + locatorPort + "]"; + String locators = getLocatorString(hostName, locatorPort); int serverPort = startBridgeServerInVM(vm1, serverGroups, locators, true); -- To stop receiving notification emails like this one, please contact [email protected].
