qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject qpid-jms git commit: Tests for the FailoverUriPool class.
Date Fri, 06 Feb 2015 21:07:30 GMT
Repository: qpid-jms
Updated Branches:
  refs/heads/master 1aadd71ac -> 47348747d


Tests for the FailoverUriPool class.

Project: http://git-wip-us.apache.org/repos/asf/qpid-jms/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-jms/commit/47348747
Tree: http://git-wip-us.apache.org/repos/asf/qpid-jms/tree/47348747
Diff: http://git-wip-us.apache.org/repos/asf/qpid-jms/diff/47348747

Branch: refs/heads/master
Commit: 47348747d7bb91cb6aafa94b5f7cb37b66b238d2
Parents: 1aadd71
Author: Timothy Bish <tabish121@gmail.com>
Authored: Fri Feb 6 16:07:09 2015 -0500
Committer: Timothy Bish <tabish121@gmail.com>
Committed: Fri Feb 6 16:07:26 2015 -0500

----------------------------------------------------------------------
 .../jms/provider/failover/FailoverUriPool.java  |  16 +-
 .../provider/failover/FailoverUriPoolTest.java  | 240 +++++++++++++++++++
 2 files changed, 255 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/47348747/qpid-jms-client/src/main/java/org/apache/qpid/jms/provider/failover/FailoverUriPool.java
----------------------------------------------------------------------
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 c986286..eaf6a2d 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
@@ -63,6 +63,20 @@ public class FailoverUriPool {
     }
 
     /**
+     * @returns the current size of the URI pool.
+     */
+    public int size() {
+        return uris.size();
+    }
+
+    /**
+     * @returns true if the URI pool is empty.
+     */
+    public boolean isEmpty() {
+        return uris.isEmpty();
+    }
+
+    /**
      * Returns the next URI in the pool of URIs.  The URI will be shifted to the
      * end of the list and not be attempted again until the full list has been
      * returned once.
@@ -119,7 +133,7 @@ public class FailoverUriPool {
      *        The new URI to add to the pool.
      */
     public void add(URI uri) {
-        if (!contains(uri)) {
+        if (uri != null && !contains(uri)) {
             if (!nestedOptions.isEmpty()) {
                 try {
                     URISupport.applyParameters(uri, nestedOptions);

http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/47348747/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverUriPoolTest.java
----------------------------------------------------------------------
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 de4fe6e..de395eb 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
@@ -17,8 +17,12 @@
 package org.apache.qpid.jms.provider.failover;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
 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 java.net.URI;
 import java.net.URISyntaxException;
@@ -49,6 +53,20 @@ public class FailoverUriPoolTest {
     public void testCreateEmptyPool() {
         FailoverUriPool pool = new FailoverUriPool();
         assertEquals(FailoverUriPool.DEFAULT_RANDOMIZE_ENABLED, pool.isRandomize());
+
+        assertTrue(pool.isEmpty());
+        assertEquals(0, pool.size());
+        assertNotNull(pool.getNestedOptions());
+        assertTrue(pool.getNestedOptions().isEmpty());
+    }
+
+    @Test
+    public void testCreateEmptyPoolFromNullUris() {
+        FailoverUriPool pool = new FailoverUriPool(null, null);
+        assertEquals(FailoverUriPool.DEFAULT_RANDOMIZE_ENABLED, pool.isRandomize());
+
+        assertNotNull(pool.getNestedOptions());
+        assertTrue(pool.getNestedOptions().isEmpty());
     }
 
     @Test
@@ -59,4 +77,226 @@ public class FailoverUriPoolTest {
         assertNotNull(pool.getNestedOptions());
         assertTrue(pool.getNestedOptions().isEmpty());
     }
+
+    @Test
+    public void testGetNextFromEmptyPool() {
+        FailoverUriPool pool = new FailoverUriPool();
+        assertEquals(FailoverUriPool.DEFAULT_RANDOMIZE_ENABLED, pool.isRandomize());
+
+        assertNull(pool.getNext());
+    }
+
+    @Test
+    public void testGetNextFromSingleValuePool() {
+        FailoverUriPool pool = new FailoverUriPool(new URI[] {uris.get(0) }, null);
+
+        assertEquals(uris.get(0), pool.getNext());
+        assertEquals(uris.get(0), pool.getNext());
+        assertEquals(uris.get(0), pool.getNext());
+    }
+
+    @Test
+    public void testAddUriToEmptyPool() {
+        FailoverUriPool pool = new FailoverUriPool();
+        assertTrue(pool.isEmpty());
+        pool.add(uris.get(0));
+        assertFalse(pool.isEmpty());
+        assertEquals(uris.get(0), pool.getNext());
+    }
+
+    @Test
+    public void testDuplicatesNotAdded() {
+        FailoverUriPool pool = new FailoverUriPool(uris.toArray(new URI[0]), null);
+
+        assertEquals(uris.size(), pool.size());
+        pool.add(uris.get(0));
+        assertEquals(uris.size(), pool.size());
+        pool.add(uris.get(1));
+        assertEquals(uris.size(), pool.size());
+    }
+
+    @Test
+    public void testDuplicatesNotAddedWhenQueryPresent() throws URISyntaxException {
+        FailoverUriPool pool = new FailoverUriPool();
+
+        assertTrue(pool.isEmpty());
+        pool.add(new URI("tcp://127.0.0.1:5672?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(1, pool.size());
+        pool.add(new URI("tcp://localhost:5672?transport.tcpNoDelay=false"));
+        assertEquals(1, pool.size());
+    }
+
+    @Test
+    public void testDuplicatesNotAddedWithHostResolution() 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());
+
+        assertEquals(1, pool.size());
+        pool.add(new URI("tcp://localhost:5673"));
+        assertEquals(2, pool.size());
+    }
+
+    @Test
+    public void testDuplicatesNotAddedUnresolvable() throws URISyntaxException {
+        FailoverUriPool pool = new FailoverUriPool();
+
+        assertTrue(pool.isEmpty());
+        pool.add(new URI("tcp://shouldbeunresolvable:5672"));
+        assertFalse(pool.isEmpty());
+
+        assertEquals(1, pool.size());
+        pool.add(new URI("tcp://shouldbeunresolvable:5672"));
+        assertEquals(1, pool.size());
+
+        assertEquals(1, pool.size());
+        pool.add(new URI("tcp://SHOULDBEUNRESOLVABLE:5672"));
+        assertEquals(1, pool.size());
+
+        assertEquals(1, pool.size());
+        pool.add(new URI("tcp://SHOULDBEUNRESOLVABLE2:5672"));
+        assertEquals(2, pool.size());
+    }
+
+    @Test
+    public void testDuplicatesNotAddedWhenQueryPresentAndUnresolveable() throws URISyntaxException
{
+        FailoverUriPool pool = new FailoverUriPool();
+
+        assertTrue(pool.isEmpty());
+        pool.add(new URI("tcp://shouldbeunresolvable:5672?transport.tcpNoDelay=true"));
+        assertFalse(pool.isEmpty());
+
+        assertEquals(1, pool.size());
+        pool.add(new URI("tcp://shouldbeunresolvable:5672?transport.tcpNoDelay=false"));
+        assertEquals(1, pool.size());
+
+        assertEquals(1, pool.size());
+        pool.add(new URI("tcp://SHOULDBEUNRESOLVABLE:5672?transport.tcpNoDelay=true"));
+        assertEquals(1, pool.size());
+
+        assertEquals(1, pool.size());
+        pool.add(new URI("tcp://SHOULDBEUNRESOLVABLE2:5672?transport.tcpNoDelay=true"));
+        assertEquals(2, pool.size());
+    }
+
+    @Test
+    public void testAddUriToPoolRandomized() throws URISyntaxException {
+        URI newUri = new URI("tcp://192.168.2." + (uris.size() + 1) + ":5672");
+
+        FailoverUriPool pool = new FailoverUriPool(uris.toArray(new URI[0]), null);
+        pool.setRandomize(true);
+        pool.add(newUri);
+
+        URI found = null;
+
+        for (int i = 0; i < uris.size() + 1; ++i) {
+            URI next = pool.getNext();
+            if (newUri.equals(next)) {
+                found = next;
+            }
+        }
+
+        if (found == null) {
+            fail("URI added was not retrieved from the pool");
+        }
+    }
+
+    @Test
+    public void testAddUriToPoolNotRandomized() throws URISyntaxException {
+        URI newUri = new URI("tcp://192.168.2." + (uris.size() + 1) + ":5672");
+
+        FailoverUriPool pool = new FailoverUriPool(uris.toArray(new URI[0]), null);
+        pool.setRandomize(false);
+        pool.add(newUri);
+
+        for (int i = 0; i < uris.size(); ++i) {
+            assertNotEquals(newUri, pool.getNext());
+        }
+
+        assertEquals(newUri, pool.getNext());
+    }
+
+    @Test
+    public void testRemoveURIFromPool() throws URISyntaxException {
+        FailoverUriPool pool = new FailoverUriPool(uris.toArray(new URI[0]), null);
+        pool.setRandomize(false);
+
+        URI removed = uris.get(0);
+
+        pool.remove(removed);
+
+        for (int i = 0; i < uris.size() + 1; ++i) {
+            if (removed.equals(pool.getNext())) {
+                fail("URI was not removed from the pool");
+            }
+        }
+    }
+
+    @Test
+    public void testConnectedShufflesWhenRandomizing() {
+        assertConnectedEffectOnPool(true, true);
+    }
+
+    @Test
+    public void testConnectedDoesNotShufflesWhenNoRandomizing() {
+        assertConnectedEffectOnPool(false, false);
+    }
+
+    private void assertConnectedEffectOnPool(boolean randomize, boolean shouldShuffle) {
+
+        FailoverUriPool pool = new FailoverUriPool(uris.toArray(new URI[0]), null);
+        pool.setRandomize(randomize);
+
+        List<URI> current = new ArrayList<URI>();
+        List<URI> previous = new ArrayList<URI>();
+
+        boolean shuffled = false;
+
+        for (int i = 0; i < 10; ++i) {
+
+            for (int j = 0; j < uris.size(); ++j) {
+                current.add(pool.getNext());
+            }
+
+            pool.connected();
+
+            if (!previous.isEmpty() && !previous.equals(current)) {
+                shuffled = true;
+                break;
+            }
+
+            previous.clear();
+            previous.addAll(current);
+            current.clear();
+        }
+
+        if (shouldShuffle) {
+            assertTrue("URIs did not get randomized", shuffled);
+        } else {
+            assertFalse("URIs should not get randomized", shuffled);
+        }
+    }
+
+    @Test
+    public void testAddOrRemoveNullHasNoAffect() throws URISyntaxException {
+        FailoverUriPool pool = new FailoverUriPool(uris.toArray(new URI[0]), null);
+        assertEquals(uris.size(), pool.size());
+
+        pool.add(null);
+        assertEquals(uris.size(), pool.size());
+        pool.remove(null);
+        assertEquals(uris.size(), pool.size());
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message