Some more testing of the FailoverProvider implementation.

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

Branch: refs/heads/master
Commit: c6813f7a9d38e1e2b7d803a4eb4f2f4730a4a5f4
Parents: 8b5a0f8
Author: Timothy Bish <tabish...@gmail.com>
Authored: Mon Feb 9 18:26:49 2015 -0500
Committer: Timothy Bish <tabish...@gmail.com>
Committed: Mon Feb 9 18:28:07 2015 -0500

----------------------------------------------------------------------
 .../failover/FailoverProviderClosedTest.java    | 141 +++++++++++++++++++
 .../provider/failover/FailoverProviderTest.java |  74 ++++++++--
 .../failover/FailoverProviderTestSupport.java   |  66 +++++++++
 3 files changed, 271 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/c6813f7a/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverProviderClosedTest.java
----------------------------------------------------------------------
diff --git 
a/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverProviderClosedTest.java
 
b/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverProviderClosedTest.java
new file mode 100644
index 0000000..2b47761
--- /dev/null
+++ 
b/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverProviderClosedTest.java
@@ -0,0 +1,141 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.qpid.jms.provider.failover;
+
+import java.io.IOException;
+import java.net.URI;
+
+import org.apache.qpid.jms.message.JmsInboundMessageDispatch;
+import org.apache.qpid.jms.message.JmsOutboundMessageDispatch;
+import org.apache.qpid.jms.meta.JmsConnectionInfo;
+import org.apache.qpid.jms.meta.JmsConsumerInfo;
+import org.apache.qpid.jms.meta.JmsSessionInfo;
+import org.apache.qpid.jms.provider.ProviderConstants.ACK_TYPE;
+import org.apache.qpid.jms.provider.ProviderFuture;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test that methods of FailoverProvider all fail immediately when it is 
closed.
+ */
+public class FailoverProviderClosedTest extends FailoverProviderTestSupport {
+
+    private FailoverProvider provider;
+    private JmsConnectionInfo connection;
+    private JmsSessionInfo session;
+    private JmsConsumerInfo consumer;
+
+    @Override
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+
+        provider = (FailoverProvider) FailoverProviderFactory.create(new 
URI("failover:(mock://localhost)"));
+        provider.close();
+
+        connection = createConnectionInfo();
+        session = createSessionInfo(connection);
+        consumer = createConsumerInfo(session);
+    }
+
+    @Test(timeout=30000)
+    public void testMultipleCloseCalls() {
+        provider.close();
+    }
+
+    @Test(timeout=30000, expected=IOException.class)
+    public void testConnect() throws Exception {
+        provider.connect();
+    }
+
+    @Test(timeout=30000, expected=IOException.class)
+    public void testStart() throws Exception {
+        provider.start();
+    }
+
+    @Test(timeout=30000, expected=IOException.class)
+    public void testCreateResource() throws Exception {
+        ProviderFuture request = new ProviderFuture();
+        provider.create(connection, request);
+    }
+
+    @Test(timeout=30000, expected=IOException.class)
+    public void testStartResource() throws Exception {
+        ProviderFuture request = new ProviderFuture();
+        provider.start(session, request);
+    }
+
+    @Test(timeout=30000, expected=IOException.class)
+    public void testStopResource() throws Exception {
+        ProviderFuture request = new ProviderFuture();
+        provider.stop(session, request);
+    }
+
+    @Test(timeout=30000, expected=IOException.class)
+    public void testDestroyResource() throws Exception {
+        ProviderFuture request = new ProviderFuture();
+        provider.destroy(session, request);
+    }
+
+    @Test(timeout=30000, expected=IOException.class)
+    public void testSend() throws Exception {
+        ProviderFuture request = new ProviderFuture();
+        provider.send(new JmsOutboundMessageDispatch(), request);
+    }
+
+    @Test(timeout=30000, expected=IOException.class)
+    public void testSessionAcknowledge() throws Exception {
+        ProviderFuture request = new ProviderFuture();
+        provider.acknowledge(session.getSessionId(), request);
+    }
+
+    @Test(timeout=30000, expected=IOException.class)
+    public void testAcknowledgeMessage() throws Exception {
+        ProviderFuture request = new ProviderFuture();
+        provider.acknowledge(new JmsInboundMessageDispatch(1), 
ACK_TYPE.CONSUMED, request);
+    }
+
+    @Test(timeout=30000, expected=IOException.class)
+    public void testCommit() throws Exception {
+        ProviderFuture request = new ProviderFuture();
+        provider.commit(session.getSessionId(), request);
+    }
+
+    @Test(timeout=30000, expected=IOException.class)
+    public void testRollback() throws Exception {
+        ProviderFuture request = new ProviderFuture();
+        provider.rollback(session.getSessionId(), request);
+    }
+
+    @Test(timeout=30000, expected=IOException.class)
+    public void testRecover() throws Exception {
+        ProviderFuture request = new ProviderFuture();
+        provider.recover(session.getSessionId(), request);
+    }
+
+    @Test(timeout=30000, expected=IOException.class)
+    public void testUnsubscribe() throws Exception {
+        ProviderFuture request = new ProviderFuture();
+        provider.unsubscribe("subscription-name", request);
+    }
+
+    @Test(timeout=30000, expected=IOException.class)
+    public void testMessagePull() throws Exception {
+        ProviderFuture request = new ProviderFuture();
+        provider.pull(consumer.getConsumerId(), 1, request);
+    }
+}

http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/c6813f7a/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverProviderTest.java
----------------------------------------------------------------------
diff --git 
a/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverProviderTest.java
 
b/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverProviderTest.java
index fced8e0..7cbddc7 100644
--- 
a/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverProviderTest.java
+++ 
b/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverProviderTest.java
@@ -19,6 +19,7 @@ package org.apache.qpid.jms.provider.failover;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.net.URI;
 import java.util.ArrayList;
@@ -27,24 +28,24 @@ import java.util.List;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
-import org.apache.qpid.jms.meta.JmsConnectionId;
-import org.apache.qpid.jms.meta.JmsConnectionInfo;
+import javax.jms.Connection;
+import javax.jms.JMSException;
+
+import org.apache.qpid.jms.JmsConnectionFactory;
 import org.apache.qpid.jms.provider.DefaultProviderListener;
 import org.apache.qpid.jms.provider.ProviderFuture;
 import org.apache.qpid.jms.provider.mock.MockProviderFactory;
-import org.apache.qpid.jms.util.IdGenerator;
 import org.junit.Before;
 import org.junit.Test;
 
 /**
  * Test behavior of the FailoverProvider
  */
-public class FailoverProviderTest {
-
-    private final IdGenerator connectionIdGenerator = new IdGenerator();
+public class FailoverProviderTest extends FailoverProviderTestSupport {
 
     private List<URI> uris;
 
+    @Override
     @Before
     public void setUp() throws Exception {
         uris = new ArrayList<URI>();
@@ -55,6 +56,8 @@ public class FailoverProviderTest {
         uris.add(new URI("mock://192.168.2.4:5672"));
 
         MockProviderFactory.resetStatistics();
+
+        super.setUp();
     }
 
     @Test
@@ -95,10 +98,61 @@ public class FailoverProviderTest {
         assertEquals(1, 
MockProviderFactory.AGGRAGATED_PROVIDER_STATS.getConnectionAttempts());
     }
 
-    protected JmsConnectionInfo createConnectionInfo() {
-        JmsConnectionId id = new 
JmsConnectionId(connectionIdGenerator.generateId());
-        JmsConnectionInfo info = new JmsConnectionInfo(id);
+    @Test(timeout = 30000)
+    public void testCannotStartWithoutListener() throws Exception {
+        FailoverProvider provider = new FailoverProvider(uris, 
Collections.<String, String>emptyMap());
+        assertEquals(FailoverUriPool.DEFAULT_RANDOMIZE_ENABLED, 
provider.isRandomize());
+        assertNull(provider.getRemoteURI());
+        provider.connect();
+
+        try {
+            provider.start();
+            fail("Should not allow a start if no listener added yet.");
+        } catch (IllegalStateException ex) {
+        }
+
+        provider.close();
+    }
+
+    @Test(timeout = 30000)
+    public void testStartupMaxReconnectAttempts() throws Exception {
+        JmsConnectionFactory factory = new JmsConnectionFactory(
+            "failover:(mock://localhost?mock.failOnConnect=true)" +
+            "?failover.startupMaxReconnectAttempts=5" +
+            "&failover.useReconnectBackOff=false");
+
+        Connection connection = factory.createConnection();
+
+        try {
+            connection.start();
+            fail("Should have stopped after five retries.");
+        } catch (JMSException ex) {
+        } finally {
+            connection.close();
+        }
+
+        assertEquals(5, 
MockProviderFactory.AGGRAGATED_PROVIDER_STATS.getProvidersCreated());
+        assertEquals(5, 
MockProviderFactory.AGGRAGATED_PROVIDER_STATS.getConnectionAttempts());
+    }
 
-        return info;
+    @Test(timeout = 30000)
+    public void testMaxReconnectAttempts() throws Exception {
+        JmsConnectionFactory factory = new JmsConnectionFactory(
+            "failover:(mock://localhost?mock.failOnConnect=true)" +
+            "?failover.maxReconnectAttempts=5" +
+            "&failover.useReconnectBackOff=false");
+
+        Connection connection = factory.createConnection();
+
+        try {
+            connection.start();
+            fail("Should have stopped after five retries.");
+        } catch (JMSException ex) {
+        } finally {
+            connection.close();
+        }
+
+        assertEquals(5, 
MockProviderFactory.AGGRAGATED_PROVIDER_STATS.getProvidersCreated());
+        assertEquals(5, 
MockProviderFactory.AGGRAGATED_PROVIDER_STATS.getConnectionAttempts());
     }
 }

http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/c6813f7a/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverProviderTestSupport.java
----------------------------------------------------------------------
diff --git 
a/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverProviderTestSupport.java
 
b/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverProviderTestSupport.java
new file mode 100644
index 0000000..3c5c690
--- /dev/null
+++ 
b/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/failover/FailoverProviderTestSupport.java
@@ -0,0 +1,66 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.qpid.jms.provider.failover;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.qpid.jms.meta.JmsConnectionId;
+import org.apache.qpid.jms.meta.JmsConnectionInfo;
+import org.apache.qpid.jms.meta.JmsConsumerId;
+import org.apache.qpid.jms.meta.JmsConsumerInfo;
+import org.apache.qpid.jms.meta.JmsSessionId;
+import org.apache.qpid.jms.meta.JmsSessionInfo;
+import org.apache.qpid.jms.test.QpidJmsTestCase;
+import org.apache.qpid.jms.util.IdGenerator;
+import org.junit.Before;
+
+/**
+ * Utility methods useful in testing the FailoverProvider
+ */
+public class FailoverProviderTestSupport extends QpidJmsTestCase {
+
+    private final IdGenerator connectionIdGenerator = new IdGenerator();
+    private final AtomicLong nextSessionId = new AtomicLong();
+    private final AtomicLong nextConsumerId = new AtomicLong();
+
+    @Override
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+
+        nextSessionId.set(0);
+        nextConsumerId.set(0);
+    }
+
+    protected JmsConnectionInfo createConnectionInfo() {
+        JmsConnectionId id = new 
JmsConnectionId(connectionIdGenerator.generateId());
+        JmsConnectionInfo info = new JmsConnectionInfo(id);
+        return info;
+    }
+
+    protected JmsSessionInfo createSessionInfo(JmsConnectionInfo connection) {
+        JmsSessionId id = new JmsSessionId(connection.getConnectionId(), 
nextSessionId.incrementAndGet());
+        JmsSessionInfo info = new JmsSessionInfo(id);
+        return info;
+    }
+
+    protected JmsConsumerInfo createConsumerInfo(JmsSessionInfo session) {
+        JmsConsumerId id = new JmsConsumerId(session.getSessionId(), 
nextConsumerId.incrementAndGet());
+        JmsConsumerInfo consumer = new JmsConsumerInfo(id);
+        return consumer;
+    }
+}


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

Reply via email to