This is an automated email from the ASF dual-hosted git repository. tabish pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/qpid-jms.git
The following commit(s) were added to refs/heads/main by this push: new f0788de QPIDJMS-536 Remove InetAddress.getByName lookup for compare new 50e4f8f This closes #40 f0788de is described below commit f0788dee4c8129a1f7414ed7fd6884b7631e536a Author: Stephen Nimmo <stephenni...@gmail.com> AuthorDate: Wed May 5 16:31:48 2021 -0400 QPIDJMS-536 Remove InetAddress.getByName lookup for compare URIs should be able to resolve to the same IP address, as the brokers may be running behind a proxy and the remote will select the appropraite ultimate destination based on service name lookups etc. Updated tests to remove constraints around resolving the URI back to an IP Address. --- .../jms/provider/failover/FailoverUriPool.java | 29 +---- .../jms/provider/failover/FailoverUriPoolTest.java | 125 +++++++++++---------- 2 files changed, 73 insertions(+), 81 deletions(-) diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/provider/failover/FailoverUriPool.java b/qpid-jms-client/src/main/java/org/apache/qpid/jms/provider/failover/FailoverUriPool.java index f690da2..68b0b5a 100644 --- a/qpid-jms-client/src/main/java/org/apache/qpid/jms/provider/failover/FailoverUriPool.java +++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/provider/failover/FailoverUriPool.java @@ -16,8 +16,6 @@ */ package org.apache.qpid.jms.provider.failover; -import java.io.IOException; -import java.net.InetAddress; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; @@ -307,30 +305,15 @@ public class FailoverUriPool { private boolean compareURIs(final URI first, final URI second) { boolean result = false; + if (first == null || second == null) { return result; - } - - if (first.getPort() == second.getPort()) { - InetAddress firstAddr = null; - InetAddress secondAddr = null; - try { - firstAddr = InetAddress.getByName(first.getHost()); - secondAddr = InetAddress.getByName(second.getHost()); - - if (firstAddr.equals(secondAddr)) { - result = true; - } - } catch (IOException e) { - if (firstAddr == null) { - LOG.error("Failed to Lookup INetAddress for URI[ " + first + " ] : " + e); - } else { - LOG.error("Failed to Lookup INetAddress for URI[ " + second + " ] : " + e); - } + } else if (first.getPort() == second.getPort()) { + final String firstHost = first.getHost(); + final String secondHost = second.getHost(); - if (first.getHost().equalsIgnoreCase(second.getHost())) { - result = true; - } + if (firstHost.equalsIgnoreCase(secondHost)) { + result = true; } } diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverUriPoolTest.java b/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverUriPoolTest.java index 0ff0942..173a0b0 100644 --- a/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverUriPoolTest.java +++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverUriPoolTest.java @@ -23,7 +23,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static org.junit.Assume.assumeFalse; +import static org.junit.Assume.assumeTrue; import java.net.InetAddress; import java.net.URI; @@ -152,78 +152,81 @@ public class FailoverUriPoolTest extends QpidJmsTestCase { FailoverUriPool pool = new FailoverUriPool(); assertTrue(pool.isEmpty()); - pool.add(new URI("tcp://127.0.0.1:5672?transport.tcpNoDelay=true")); + pool.add(new URI("tcp://localhost:5671?transport.tcpNoDelay=true")); assertFalse(pool.isEmpty()); assertEquals(1, pool.size()); pool.add(new URI("tcp://localhost:5672?transport.tcpNoDelay=true")); - assertEquals(1, pool.size()); + assertEquals(2, pool.size()); - assertEquals(1, pool.size()); + assertEquals(2, pool.size()); pool.add(new URI("tcp://localhost:5672?transport.tcpNoDelay=false")); - assertEquals(1, pool.size()); + assertEquals(2, pool.size()); } @Test - public void testDuplicatesNotAddedWithHostResolution() throws URISyntaxException { + public void testNoHostResolutionPerformedFilterDuplicatesOnly() throws URISyntaxException { FailoverUriPool pool = new FailoverUriPool(); assertTrue(pool.isEmpty()); pool.add(new URI("tcp://127.0.0.1:5672")); assertFalse(pool.isEmpty()); - - assertEquals(1, pool.size()); - pool.add(new URI("tcp://localhost:5672")); assertEquals(1, pool.size()); + assertFalse(pool.isEmpty()); + pool.add(new URI("tcp://127.0.0.1:5672")); // Not added as it duplicates previous + assertFalse(pool.isEmpty()); assertEquals(1, pool.size()); - pool.add(new URI("tcp://localhost:5673")); + + pool.add(new URI("tcp://localhost:5672")); // Added since there is no host resolution assertEquals(2, pool.size()); + + pool.add(new URI("tcp://localhost:5673")); // Added since the port is different + assertEquals(3, pool.size()); + + pool.add(new URI("tcp://localhost:5672")); // Not added as it duplicates previous + assertEquals(3, pool.size()); } @Test - public void testDuplicatesNotAddedUnresolvable() throws Exception { - assumeFalse("Host resolution works when not expected", checkIfResolutionWorks()); - + public void testDuplicatesNotAddedWhenCaseIsDifferent() throws Exception { FailoverUriPool pool = new FailoverUriPool(); assertTrue(pool.isEmpty()); - pool.add(new URI("tcp://shouldbeunresolvable:5672")); + pool.add(new URI("tcp://somerandomhostname:5672")); assertFalse(pool.isEmpty()); assertEquals(1, pool.size()); - pool.add(new URI("tcp://shouldbeunresolvable:5672")); + pool.add(new URI("tcp://SomerandomHostname:5672")); assertEquals(1, pool.size()); assertEquals(1, pool.size()); - pool.add(new URI("tcp://SHOULDBEUNRESOLVABLE:5672")); + pool.add(new URI("tcp://SOMERANDOMHOSTNAME:5672")); assertEquals(1, pool.size()); assertEquals(1, pool.size()); - pool.add(new URI("tcp://SHOULDBEUNRESOLVABLE2:5672")); + pool.add(new URI("tcp://SOMERANDOMHOSTNAME2:5672")); assertEquals(2, pool.size()); } @Test - public void testDuplicatesNotAddedWhenQueryPresentAndUnresolveable() throws URISyntaxException { - assumeFalse("Host resolution works when not expected", checkIfResolutionWorks()); - + public void testDuplicatesNotAddedWhenCaseIsDifferentAndQueryStringPresent() throws URISyntaxException { FailoverUriPool pool = new FailoverUriPool(); assertTrue(pool.isEmpty()); - pool.add(new URI("tcp://shouldbeunresolvable:5672?transport.tcpNoDelay=true")); + pool.add(new URI("tcp://somerandomhostname:5672?transport.tcpNoDelay=true")); assertFalse(pool.isEmpty()); assertEquals(1, pool.size()); - pool.add(new URI("tcp://shouldbeunresolvable:5672?transport.tcpNoDelay=false")); + pool.add(new URI("tcp://SomerandomHostname:5672?transport.tcpNoDelay=false")); assertEquals(1, pool.size()); assertEquals(1, pool.size()); - pool.add(new URI("tcp://SHOULDBEUNRESOLVABLE:5672?transport.tcpNoDelay=true")); + pool.add(new URI("tcp://SOMERANDOMHOSTNAME:5672?transport.tcpNoDelay=true")); assertEquals(1, pool.size()); assertEquals(1, pool.size()); - pool.add(new URI("tcp://SHOULDBEUNRESOLVABLE2:5672?transport.tcpNoDelay=true")); + pool.add(new URI("tcp://SOMERANDOMHOSTNAME2:5672?transport.tcpNoDelay=true")); assertEquals(2, pool.size()); } @@ -353,60 +356,54 @@ public class FailoverUriPoolTest extends QpidJmsTestCase { assertTrue(pool.isEmpty()); pool.add(new URI("tcp://127.0.0.1:5672?transport.tcpNoDelay=true")); assertFalse(pool.isEmpty()); - pool.remove(new URI("tcp://localhost:5672?transport.tcpNoDelay=true")); - assertTrue(pool.isEmpty()); - pool.add(new URI("tcp://127.0.0.1:5672?transport.tcpNoDelay=true")); - assertFalse(pool.isEmpty()); - pool.remove(new URI("tcp://localhost:5672?transport.tcpNoDelay=false")); + pool.remove(new URI("tcp://127.0.0.1:5672?transport.tcpNoDelay=true")); assertTrue(pool.isEmpty()); } @Test - public void testRemoveWithHostResolution() throws URISyntaxException { + public void testRemove() throws URISyntaxException { FailoverUriPool pool = new FailoverUriPool(); assertTrue(pool.isEmpty()); pool.add(new URI("tcp://127.0.0.1:5672")); assertFalse(pool.isEmpty()); - pool.remove(new URI("tcp://localhost:5672")); + pool.remove(new URI("tcp://127.0.0.1:5672")); assertTrue(pool.isEmpty()); pool.add(new URI("tcp://127.0.0.1:5672")); assertFalse(pool.isEmpty()); + pool.add(new URI("tcp://127.0.0.1:5673")); + assertFalse(pool.isEmpty()); pool.remove(new URI("tcp://localhost:5673")); assertFalse(pool.isEmpty()); } @Test - public void testRemoveWhenUnresolvable() throws URISyntaxException { - assumeFalse("Host resolution works when not expected", checkIfResolutionWorks()); - + public void testRemoveHostIgnoresCaseInHostname() throws URISyntaxException { FailoverUriPool pool = new FailoverUriPool(); assertTrue(pool.isEmpty()); - pool.add(new URI("tcp://shouldbeunresolvable:5672")); + pool.add(new URI("tcp://somerandomhostname:5672")); assertFalse(pool.isEmpty()); - pool.remove(new URI("tcp://SHOULDBEUNRESOLVABLE:5672")); + pool.remove(new URI("tcp://SOMERANDOMHOSTNAME:5672")); assertTrue(pool.isEmpty()); - pool.add(new URI("tcp://shouldbeunresolvable:5672")); + pool.add(new URI("tcp://somerandomhostname:5672")); assertFalse(pool.isEmpty()); - pool.remove(new URI("tcp://shouldbeunresolvable:5673")); + pool.remove(new URI("tcp://somerandomhostname:5673")); assertFalse(pool.isEmpty()); } @Test - public void testRemoveWhenQueryPresentAndUnresolveable() throws URISyntaxException { - assumeFalse("Host resolution works when not expected", checkIfResolutionWorks()); - + public void testRemoveWhenQueryPresentIgnoresCaseInHostname() throws URISyntaxException { FailoverUriPool pool = new FailoverUriPool(); assertTrue(pool.isEmpty()); - pool.add(new URI("tcp://shouldbeunresolvable:5672?transport.tcpNoDelay=true")); + pool.add(new URI("tcp://somerandomhostname:5672?transport.tcpNoDelay=true")); assertFalse(pool.isEmpty()); - pool.remove(new URI("tcp://SHOULDBEUNRESOLVABLE:5672?transport.tcpNoDelay=true")); + pool.remove(new URI("tcp://SOMERANDOMHOSTNAME:5672?transport.tcpNoDelay=true")); assertTrue(pool.isEmpty()); - pool.add(new URI("tcp://shouldbeunresolvable:5672?transport.tcpNoDelay=true")); + pool.add(new URI("tcp://somerandomhostname:5672?transport.tcpNoDelay=true")); assertFalse(pool.isEmpty()); - pool.remove(new URI("tcp://shouldbeunresolvable:5673?transport.tcpNoDelay=true")); + pool.remove(new URI("tcp://somerandomhostname:5673?transport.tcpNoDelay=true")); assertFalse(pool.isEmpty()); } @@ -421,7 +418,6 @@ public class FailoverUriPoolTest extends QpidJmsTestCase { } private void assertConnectedEffectOnPool(boolean randomize, boolean shouldShuffle) { - FailoverUriPool pool = new FailoverUriPool(uris, null); pool.setRandomize(randomize); @@ -547,18 +543,6 @@ public class FailoverUriPoolTest extends QpidJmsTestCase { assertEquals(uris.get(0).getHost(), pool.getNext().getHost()); } - private boolean checkIfResolutionWorks() { - boolean resolutionWorks = false; - try { - resolutionWorks = InetAddress.getByName("shouldbeunresolvable") != null; - resolutionWorks = InetAddress.getByName("SHOULDBEUNRESOLVABLE") != null; - resolutionWorks = InetAddress.getByName("SHOULDBEUNRESOLVABLE2") != null; - } catch (Exception e) { - } - - return resolutionWorks; - } - @Test public void testRemoveAll() throws URISyntaxException { FailoverUriPool pool = new FailoverUriPool(uris, null); @@ -570,4 +554,29 @@ public class FailoverUriPoolTest extends QpidJmsTestCase { pool.removeAll(); } + + @Test + public void testLoopBackAndLocalHostAllowedInSamePool() throws URISyntaxException { + assumeTrue(checkLocalHostResolvesToLoopbackIP()); + + FailoverUriPool pool = new FailoverUriPool(); + + assertTrue(pool.isEmpty()); + pool.add(new URI("tcp://127.0.0.1:5672")); + assertFalse(pool.isEmpty()); + assertEquals(1, pool.size()); + pool.add(new URI("tcp://localhost:5672")); + assertEquals(2, pool.size()); + } + + private boolean checkLocalHostResolvesToLoopbackIP() { + try { + InetAddress localhost = InetAddress.getByName("localhost"); + InetAddress loopback = InetAddress.getLoopbackAddress(); + + return localhost.equals(loopback); + } catch (Exception failsOnError) { + return false; + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org