1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
/*
* Bitronix Transaction Manager
*
* Copyright (c) 2010, Bitronix Software.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package bitronix.tm.mock;
import bitronix.tm.TransactionManagerServices;
import bitronix.tm.mock.resource.jms.MockXAConnectionFactory;
import bitronix.tm.recovery.RecoveryException;
import bitronix.tm.resource.common.XAPool;
import bitronix.tm.resource.jms.PoolingConnectionFactory;
import junit.framework.TestCase;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.Queue;
import javax.jms.MessageProducer;
import java.lang.reflect.Field;
/**
*
* @author lorban
*/
public class JmsPoolTest extends TestCase {
private PoolingConnectionFactory pcf;
protected void setUp() throws Exception {
TransactionManagerServices.getConfiguration().setJournal("null").setGracefulShutdownInterval(0);
TransactionManagerServices.getTransactionManager();
MockXAConnectionFactory.setStaticCloseXAConnectionException(null);
MockXAConnectionFactory.setStaticCreateXAConnectionException(null);
pcf = new PoolingConnectionFactory();
pcf.setMinPoolSize(1);
pcf.setMaxPoolSize(2);
pcf.setMaxIdleTime(1);
pcf.setClassName(MockXAConnectionFactory.class.getName());
pcf.setUniqueName("pcf");
pcf.setAllowLocalTransactions(true);
pcf.setAcquisitionTimeout(1);
pcf.init();
}
protected void tearDown() throws Exception {
pcf.close();
TransactionManagerServices.getTransactionManager().shutdown();
}
public void testInitFailure() throws Exception {
pcf.close();
pcf = new PoolingConnectionFactory();
pcf.setMinPoolSize(0);
pcf.setMaxPoolSize(2);
pcf.setMaxIdleTime(1);
pcf.setClassName(MockXAConnectionFactory.class.getName());
pcf.setUniqueName("pcf");
pcf.setAllowLocalTransactions(true);
pcf.setAcquisitionTimeout(1);
TransactionManagerServices.getTransactionManager().begin();
MockXAConnectionFactory.setStaticCreateXAConnectionException(new JMSException("not yet started"));
try {
pcf.init();
} catch (Exception e) {
}
MockXAConnectionFactory.setStaticCreateXAConnectionException(null);
pcf.init();
pcf.createConnection().createSession(true, 0).createProducer(null).send(null);
TransactionManagerServices.getTransactionManager().commit();
}
public void testReEnteringRecovery() throws Exception {
pcf.startRecovery();
try {
pcf.startRecovery();
fail("excpected RecoveryException");
} catch (RecoveryException ex) {
assertEquals("recovery already in progress on a PoolingConnectionFactory with an XAPool of resource pcf with 1 connection(s) (0 still available)", ex.getMessage());
}
// make sure startRecovery() can be called again once endRecovery() has been called
pcf.endRecovery();
pcf.startRecovery();
pcf.endRecovery();
}
public void testPoolNotStartingTransactionManager() throws Exception {
// make sure TM is not running
TransactionManagerServices.getTransactionManager().shutdown();
PoolingConnectionFactory pcf = new PoolingConnectionFactory();
pcf.setMinPoolSize(1);
pcf.setMaxPoolSize(2);
pcf.setMaxIdleTime(1);
pcf.setClassName(MockXAConnectionFactory.class.getName());
pcf.setUniqueName("pcf2");
pcf.setAllowLocalTransactions(true);
pcf.setAcquisitionTimeout(1);
pcf.init();
assertFalse(TransactionManagerServices.isTransactionManagerRunning());
Connection c = pcf.createConnection();
Session s = c.createSession(false, 0);
Queue q = s.createQueue("q");
MessageProducer mp = s.createProducer(q);
mp.send(s.createTextMessage("test123"));
mp.close();
s.close();
c.close();
assertFalse(TransactionManagerServices.isTransactionManagerRunning());
pcf.close();
assertFalse(TransactionManagerServices.isTransactionManagerRunning());
}
public void testPoolShrink() throws Exception {
Field poolField = pcf.getClass().getDeclaredField("pool");
poolField.setAccessible(true);
XAPool pool = (XAPool) poolField.get(pcf);
assertEquals(1, pool.inPoolSize());
assertEquals(1, pool.totalPoolSize());
Connection c1 = pcf.createConnection();
assertEquals(0, pool.inPoolSize());
assertEquals(1, pool.totalPoolSize());
Connection c2 = pcf.createConnection();
assertEquals(0, pool.inPoolSize());
assertEquals(2, pool.totalPoolSize());
c1.close();
c2.close();
Thread.sleep(1100); // leave enough time for the ide connections to expire
TransactionManagerServices.getTaskScheduler().interrupt(); // wake up the task scheduler
Thread.sleep(1100); // leave enough time for the scheduled shrinking task to do its work
assertEquals(1, pool.inPoolSize());
assertEquals(1, pool.totalPoolSize());
}
public void testPoolShrinkErrorHandling() throws Exception {
Field poolField = pcf.getClass().getDeclaredField("pool");
poolField.setAccessible(true);
XAPool pool = (XAPool) poolField.get(pcf);
pcf.setMinPoolSize(0);
pcf.reset();
pcf.setMinPoolSize(1);
MockXAConnectionFactory.setStaticCloseXAConnectionException(new JMSException("close fails because connection factory broken"));
pcf.reset();
// the pool is now loaded with one connection which will throw an exception when closed
Thread.sleep(1100); // leave enough time for the ide connections to expire
TransactionManagerServices.getTaskScheduler().interrupt(); // wake up the task scheduler
Thread.sleep(100); // leave enough time for the scheduled shrinking task to do its work
assertEquals(1, pool.inPoolSize());
MockXAConnectionFactory.setStaticCreateXAConnectionException(new JMSException("createXAConnection fails because connection factory broken"));
Thread.sleep(1100); // leave enough time for the ide connections to expire
TransactionManagerServices.getTaskScheduler().interrupt(); // wake up the task scheduler
Thread.sleep(100); // leave enough time for the scheduled shrinking task to do its work
assertEquals(0, pool.inPoolSize());
MockXAConnectionFactory.setStaticCreateXAConnectionException(null);
Thread.sleep(1100); // leave enough time for the ide connections to expire
TransactionManagerServices.getTaskScheduler().interrupt(); // wake up the task scheduler
Thread.sleep(100); // leave enough time for the scheduled shrinking task to do its work
assertEquals(1, pool.inPoolSize());
}
public void testPoolReset() throws Exception {
Field poolField = pcf.getClass().getDeclaredField("pool");
poolField.setAccessible(true);
XAPool pool = (XAPool) poolField.get(pcf);
assertEquals(1, pool.inPoolSize());
assertEquals(1, pool.totalPoolSize());
Connection c1 = pcf.createConnection();
assertEquals(0, pool.inPoolSize());
assertEquals(1, pool.totalPoolSize());
Connection c2 = pcf.createConnection();
assertEquals(0, pool.inPoolSize());
assertEquals(2, pool.totalPoolSize());
c1.close();
c2.close();
pcf.reset();
assertEquals(1, pool.inPoolSize());
assertEquals(1, pool.totalPoolSize());
}
public void testPoolResetErrorHandling() throws Exception {
Field poolField = pcf.getClass().getDeclaredField("pool");
poolField.setAccessible(true);
XAPool pool = (XAPool) poolField.get(pcf);
pcf.setMinPoolSize(0);
pcf.reset();
pcf.setMinPoolSize(1);
MockXAConnectionFactory.setStaticCloseXAConnectionException(new JMSException("close fails because connection factory broken"));
pcf.reset();
// the pool is now loaded with one connection which will throw an exception when closed
pcf.reset();
try {
MockXAConnectionFactory.setStaticCreateXAConnectionException(new JMSException("createXAConnection fails because connection factory broken"));
pcf.reset();
fail("expected JMSException");
} catch (JMSException ex) {
assertEquals("createXAConnection fails because connection factory broken", ex.getMessage());
assertEquals(0, pool.inPoolSize());
}
MockXAConnectionFactory.setStaticCreateXAConnectionException(null);
pcf.reset();
assertEquals(1, pool.inPoolSize());
}
}
|