From 5b4fb8aa2bafa691860745d182a831e1e852eeb1 Mon Sep 17 00:00:00 2001 From: gtully Date: Wed, 20 Sep 2017 15:37:43 +0100 Subject: [PATCH] [no jira] test to exercise pool setBlockIfSessionPoolIsFull false --- .../jms/pool/PooledConnectionTest.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/activemq-jms-pool/src/test/java/org/apache/activemq/jms/pool/PooledConnectionTest.java b/activemq-jms-pool/src/test/java/org/apache/activemq/jms/pool/PooledConnectionTest.java index 9c6e059baa..b0f33fc1a9 100644 --- a/activemq-jms-pool/src/test/java/org/apache/activemq/jms/pool/PooledConnectionTest.java +++ b/activemq-jms-pool/src/test/java/org/apache/activemq/jms/pool/PooledConnectionTest.java @@ -16,17 +16,23 @@ */ package org.apache.activemq.jms.pool; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.IllegalStateException; +import javax.jms.JMSException; +import javax.jms.Session; import org.apache.activemq.ActiveMQConnectionFactory; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.HashSet; +import java.util.LinkedList; + /** * A couple of tests against the PooledConnection class. * @@ -106,4 +112,57 @@ public class PooledConnectionTest extends JmsPoolTestSupport { LOG.debug("ConnectionFactory initialized."); return cf; } + + @Test(timeout = 60000) + public void testAllSessionsAvailableOnConstrainedPool() throws Exception { + PooledConnectionFactory cf = new PooledConnectionFactory(); + cf.setConnectionFactory(new ActiveMQConnectionFactory( + "vm://localhost?broker.persistent=false&broker.useJmx=false&broker.schedulerSupport=false")); + cf.setMaxConnections(5); + cf.setMaximumActiveSessionPerConnection(2); + cf.setBlockIfSessionPoolIsFull(false); + + LinkedList connections = new LinkedList<>(); + HashSet sessions = new HashSet(); + + for (int i=0; i<10; i++) { + Connection conn = cf.createConnection(); + LOG.info("connection: " + i + ", " + ((PooledConnection)conn).getConnection()); + + conn.start(); + connections.add(conn); + sessions.add(conn.createSession(false, Session.AUTO_ACKNOWLEDGE)); + } + + assertEquals(sessions.size(), 10); + assertEquals(connections.size(), 10); + + Connection connectionToClose = connections.getLast(); + connectionToClose.close(); + + Connection conn = cf.createConnection(); + LOG.info("connection:" + ((PooledConnection)conn).getConnection()); + + conn.start(); + connections.add(conn); + try { + sessions.add(conn.createSession(false, Session.AUTO_ACKNOWLEDGE)); + } catch (JMSException expected) { + conn.close(); + } + + conn = cf.createConnection(); + LOG.info("connection:" + ((PooledConnection)conn).getConnection()); + + conn.start(); + connections.add(conn); + try { + sessions.add(conn.createSession(false, Session.AUTO_ACKNOWLEDGE)); + } catch (JMSException expected) { + conn.close(); + } + + assertEquals(sessions.size(), 10); + assertEquals(connections.size(), 12); + } }