User: d_jencks
Date: 02/03/24 14:00:45
Added: src/main/org/jboss/test/jca/test
BaseConnectionManagerUnitTestCase.java
CachedConnectionBankStressTestCase.java
CachedConnectionSessionUnitTestCase.java
ManagedConnectionPoolUnitTestCase.java
XATxConnectionManagerUnitTestCase.java
Log:
Tests for new ConnectionManager implementation. Many of these fail with the current
less-than-spec-compliant jca-jdbc wrappers, but work ok with e.g. the firebird
connector. Also, nojars combined targets removed, any test target can be run with
-Dnojars=t. Also, log4j logging fixed and moved to xml config.
Revision Changes Path
1.1
jbosstest/src/main/org/jboss/test/jca/test/BaseConnectionManagerUnitTestCase.java
Index: BaseConnectionManagerUnitTestCase.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*
*/
package org.jboss.test.jca.test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.resource.ResourceException;
import javax.resource.spi.ConnectionRequestInfo;
import javax.resource.spi.ManagedConnection;
import javax.resource.spi.ManagedConnectionFactory;
import javax.security.auth.Subject;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.logging.Logger;
import org.jboss.resource.connectionmanager.BaseConnectionManager2;
import org.jboss.resource.connectionmanager.JBossManagedConnectionPool;
import org.jboss.resource.connectionmanager.CachedConnectionManager;
import org.jboss.resource.connectionmanager.NoTxConnectionManager;
import org.jboss.resource.connectionmanager.InternalManagedConnectionPool;
import org.jboss.resource.connectionmanager.ManagedConnectionPool;
import org.jboss.test.jca.adapter.TestConnectionRequestInfo;
import org.jboss.test.jca.adapter.TestManagedConnectionFactory;
/**
* Unit Test for class ManagedConnectionPool
*
*
* Created: Wed Jan 2 00:06:35 2002
*
* @author <a href="mailto:[EMAIL PROTECTED]">David Jencks</a>
* @version
*/
public class BaseConnectionManagerUnitTestCase extends TestCase
{
Logger log = Logger.getLogger(getClass());
boolean failed;
Subject subject = new Subject();
ConnectionRequestInfo cri = new TestConnectionRequestInfo();
CachedConnectionManager ccm = new CachedConnectionManager();
/**
* Creates a new <code>BaseConnectionManagerUnitTestCase</code> instance.
*
* @param name test name
*/
public BaseConnectionManagerUnitTestCase (String name)
{
super(name);
}
private BaseConnectionManager2 getCM(
InternalManagedConnectionPool.PoolParams pp,
String poolingStrategyName)
throws Exception
{
ManagedConnectionFactory mcf = new TestManagedConnectionFactory();
JBossManagedConnectionPool mcp = new JBossManagedConnectionPool();
mcp.setCriteria(poolingStrategyName);
mcp.setMinSize(pp.minSize);
mcp.setMaxSize(pp.maxSize);
mcp.setBlockingTimeoutMillis(pp.blockingTimeout);
mcp.setIdleTimeout(pp.idleTimeout);
mcp.start();
mcp.getManagedConnectionPool().setManagedConnectionFactory(mcf);
BaseConnectionManager2 cm = new NoTxConnectionManager(mcf, ccm,
mcp.getManagedConnectionPool());
return cm;
}
public void testGetManagedConnections() throws Exception
{
InternalManagedConnectionPool.PoolParams pp = new
InternalManagedConnectionPool.PoolParams();
pp.minSize = 0;
pp.maxSize = 5;
pp.blockingTimeout = 100;
pp.idleTimeout = 500;
BaseConnectionManager2 cm = getCM(pp, "ByNothing");
ArrayList cs = new ArrayList();
for (int i = 0; i < pp.maxSize; i++)
{
ManagedConnection mc = cm.getManagedConnection(null, null);
assertTrue("Got a null connection!", mc != null);
cs.add(mc);
} // end of for ()
assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(),
cm.getConnectionCount() == pp.maxSize);
try
{
cm.getManagedConnection(null, null);
fail("Got a connection more than maxSize!");
}
catch (ResourceException re)
{
//expected
} // end of try-catch
for (Iterator i = cs.iterator(); i.hasNext();)
{
cm.returnManagedConnection((ManagedConnection)i.next(), false);
} // end of for ()
cm.shutdown();
assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(),
cm.getConnectionCount() == 0);
}
public void testShortBlocking() throws Exception
{
final int reps = 100;
final int threadsPerConnection = 4;
final long sleepTime = 2;
failed = false;
InternalManagedConnectionPool.PoolParams pp = new
InternalManagedConnectionPool.PoolParams();
pp.minSize = 0;
pp.maxSize = 5;
pp.blockingTimeout = 10;
pp.idleTimeout = 500;
final BaseConnectionManager2 cm = getCM(pp, "ByNothing");
for (int i = 0; i < pp.maxSize * threadsPerConnection; i++)
{
Runnable t = new Runnable() {
public void run()
{
for (int j = 0; j < reps; j++)
{
try
{
ManagedConnection mc = cm.getManagedConnection(null, null);
Thread.sleep(sleepTime);
cm.returnManagedConnection(mc, false);
}
catch (ResourceException re)
{
BaseConnectionManagerUnitTestCase.this.failed = true;
} // end of try-catch
catch (InterruptedException ie)
{
return;
} // end of catch
} // end of for ()
}
};
new Thread(t).start();
} // end of for ()
assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(),
cm.getConnectionCount() == pp.maxSize);
assertTrue("Blocking Timeout occurred in ShortBlocking test", !failed);
cm.shutdown();
}
public void testIdleTimeout() throws Exception
{
InternalManagedConnectionPool.PoolParams pp = new
InternalManagedConnectionPool.PoolParams();
pp.minSize = 0;
pp.maxSize = 5;
pp.blockingTimeout = 10;
pp.idleTimeout = 10;
BaseConnectionManager2 cm = getCM(pp, "ByNothing");
Collection mcs = new ArrayList(pp.maxSize);
for (int i = 0 ; i < pp.maxSize; i++)
{
mcs.add(cm.getManagedConnection(subject, cri));
} // end of for ()
for (Iterator i = mcs.iterator(); i.hasNext(); )
{
cm.returnManagedConnection((ManagedConnection)i.next(), false);
} // end of for ()
assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(),
cm.getConnectionCount() == pp.maxSize);
Thread.sleep((long)pp.idleTimeout * 10);
// cm.removeTimedOut();
assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(),
cm.getConnectionCount() == 0);
}
public void testFillToMin() throws Exception
{
InternalManagedConnectionPool.PoolParams pp = new
InternalManagedConnectionPool.PoolParams();
pp.minSize = 3;
pp.maxSize = 5;
pp.blockingTimeout = 10;
pp.idleTimeout = 20;
BaseConnectionManager2 cm = getCM(pp, "ByNothing");
ManagedConnection mc = cm.getManagedConnection(subject, cri);
cm.returnManagedConnection(mc, false);
Thread.sleep(10);//allow filltoMin to work
assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(),
cm.getConnectionCount() == pp.minSize);
Thread.sleep((long)(pp.idleTimeout * 7)/2);//try to get in the middle of
cleanups
// cm.removeTimedOut();
assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(),
cm.getConnectionCount() == pp.minSize);
cm.shutdown();
}
}//
1.1
jbosstest/src/main/org/jboss/test/jca/test/CachedConnectionBankStressTestCase.java
Index: CachedConnectionBankStressTestCase.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*
*/
package org.jboss.test.jca.test;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.test.JBossTestCase;
import org.jboss.test.jca.bank.interfaces.TellerHome;
import org.jboss.test.jca.bank.interfaces.Teller;
import org.jboss.test.jca.bank.interfaces.Account;
import org.apache.log4j.Category;
/**
* CachedConnectionBankStressTestCase.java
* Tests connection disconnect-reconnect mechanism.
*
* Created: Mon Mar 18 07:57:41 2002
*
* @author <a href="mailto:[EMAIL PROTECTED]">David Jencks</a>
* @version
*/
public class CachedConnectionBankStressTestCase extends JBossTestCase
{
private TellerHome th;
private Teller t;
private Exception exc;
private int iter;
public CachedConnectionBankStressTestCase (String name)
{
super(name);
}
protected void setUp() throws Exception
{
th = (TellerHome)getInitialContext().lookup("Teller");
t = th.create();
t.setUp();
}
protected void tearDown() throws Exception
{
if (t != null)
{
t.tearDown();
} // end of if ()
}
public static Test suite() throws Exception
{
return getDeploySetup(CachedConnectionBankStressTestCase.class,
"jcabanktest.jar");
}
public void testCachedConnectionBank() throws Exception
{
Account[] accounts = new Account[getThreadCount()];
for (int i = 0; i < getThreadCount(); i++)
{
accounts[i] = t.createAccount(new Integer(i));
} // end of for ()
final Object lock = new Object();
iter = 0;
getLog().info("Start test. "+getThreadCount()+ " threads,
"+getIterationCount()+" iterations");
long start = System.currentTimeMillis();
for (int i = 0; i < getThreadCount() - 1; i++)
{
//Thread.sleep(500); // Wait between each client
new Thread(new TransferThread(accounts[i],
accounts[(i + 1) % getThreadCount()],
getIterationCount(),
lock)).start();
synchronized (lock)
{
iter++;
}
}
synchronized(lock)
{
while(iter > 0)
{
lock.wait();
}
}
if (exc != null) throw exc;
for (int i = 1; i < getThreadCount() - 1; i++)
{
assertTrue("nonzero final balance for" + i, accounts[i].getBalance() == 0);
} // end of for ()
long end = System.currentTimeMillis();
getLog().info("Time:"+(end-start));
getLog().info("Avg.
time/call(ms):"+((end-start)/(getThreadCount()*getIterationCount())));
}
public class TransferThread implements Runnable
{
Category log = Category.getInstance(getClass().getName());
Account to;
Account from;
int iterationCount;
Object lock;
public TransferThread(final Account to,
final Account from,
final int iterationCount,
final Object lock) throws Exception
{
this.to = to;
this.from = from;
this.iterationCount = iterationCount;
this.lock = lock;
}
public void run()
{
try
{
for (int j = 0; j < iterationCount; j++)
{
if (exc != null) break;
t.transfer(from,to, 1);
}
} catch (Exception e)
{
exc = e;
}
synchronized(lock)
{
iter--;
log.info("Only "+iter+" left");
lock.notifyAll();
}
}
}
}// CachedConnectionSessionUnitTestCase
1.1
jbosstest/src/main/org/jboss/test/jca/test/CachedConnectionSessionUnitTestCase.java
Index: CachedConnectionSessionUnitTestCase.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*
*/
package org.jboss.test.jca.test;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.test.JBossTestCase;
import org.jboss.test.jca.interfaces.CachedConnectionSessionHome;
import org.jboss.test.jca.interfaces.CachedConnectionSession;
/**
* CachedConnectionSessionUnitTestCase.java
* Tests connection disconnect-reconnect mechanism.
*
* Created: Fri Mar 15 22:48:41 2002
*
* @author <a href="mailto:[EMAIL PROTECTED]">David Jencks</a>
* @version
*/
public class CachedConnectionSessionUnitTestCase extends JBossTestCase
{
private CachedConnectionSessionHome sh;
private CachedConnectionSession s;
public CachedConnectionSessionUnitTestCase (String name)
{
super(name);
}
protected void setUp() throws Exception
{
sh =
(CachedConnectionSessionHome)getInitialContext().lookup("CachedConnectionSession");
s = sh.create();
s.createTable();
}
protected void tearDown() throws Exception
{
if (s != null)
{
s.dropTable();
} // end of if ()
}
public static Test suite() throws Exception
{
return getDeploySetup(CachedConnectionSessionUnitTestCase.class,
"jcatest.jar");
}
public void testCachedConnectionSession() throws Exception
{
s.insert(1L, "testing");
assertTrue("did not get expected value back", "testing".equals(s.fetch(1L)));
}
}// CachedConnectionSessionUnitTestCase
1.1
jbosstest/src/main/org/jboss/test/jca/test/ManagedConnectionPoolUnitTestCase.java
Index: ManagedConnectionPoolUnitTestCase.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*
*/
package org.jboss.test.jca.test;
import java.util.ArrayList;
import java.util.Iterator;
import javax.resource.ResourceException;
import javax.resource.spi.ConnectionRequestInfo;
import javax.resource.spi.ManagedConnection;
import javax.resource.spi.ManagedConnectionFactory;
import javax.security.auth.Subject;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.logging.Logger;
import org.jboss.resource.connectionmanager.ManagedConnectionPool;
import org.jboss.resource.connectionmanager.InternalManagedConnectionPool;
import org.jboss.test.jca.adapter.TestConnectionRequestInfo;
import org.jboss.test.jca.adapter.TestManagedConnectionFactory;
/**
* Unit Test for class ManagedConnectionPool
*
*
* Created: Wed Jan 2 00:06:35 2002
*
* @author <a href="mailto:[EMAIL PROTECTED]">David Jencks</a>
* @version
*/
public class ManagedConnectionPoolUnitTestCase extends TestCase
{
Logger log = Logger.getLogger(getClass());
boolean failed;
/**
* Creates a new <code>ManagedConnectionPoolUnitTestCase</code> instance.
*
* @param name test name
*/
public ManagedConnectionPoolUnitTestCase (String name)
{
super(name);
}
public void testGetManagedConnections() throws Exception
{
ManagedConnectionFactory mcf = new TestManagedConnectionFactory();
InternalManagedConnectionPool.PoolParams pp = new
InternalManagedConnectionPool.PoolParams();
pp.minSize = 0;
pp.maxSize = 5;
pp.blockingTimeout = 100;
pp.idleTimeout = 500;
Subject subject = new Subject();
ConnectionRequestInfo cri = new TestConnectionRequestInfo();
InternalManagedConnectionPool mcp = new InternalManagedConnectionPool(mcf,
subject, cri, pp, log);
ArrayList cs = new ArrayList();
for (int i = 0; i < pp.maxSize; i++)
{
ManagedConnection mc = mcp.getConnection(null, null);
assertTrue("Got a null connection!", mc != null);
cs.add(mc);
} // end of for ()
assertTrue("Wrong number of connections counted: " + mcp.getConnectionCount(),
mcp.getConnectionCount() == pp.maxSize);
try
{
mcp.getConnection(null, null);
fail("Got a connection more than maxSize!");
}
catch (ResourceException re)
{
//expected
} // end of try-catch
for (Iterator i = cs.iterator(); i.hasNext();)
{
mcp.returnConnection((ManagedConnection)i.next(), false);
} // end of for ()
mcp.shutdown();
assertTrue("Wrong number of connections counted: " + mcp.getConnectionCount(),
mcp.getConnectionCount() == 0);
}
public void testShortBlocking() throws Exception
{
final int reps = 100;
final int threadsPerConnection = 4;
final long sleepTime = 2;
failed = false;
ManagedConnectionFactory mcf = new TestManagedConnectionFactory();
InternalManagedConnectionPool.PoolParams pp = new
InternalManagedConnectionPool.PoolParams();
pp.minSize = 0;
pp.maxSize = 5;
pp.blockingTimeout = 10;
pp.idleTimeout = 500;
Subject subject = new Subject();
ConnectionRequestInfo cri = new TestConnectionRequestInfo();
final InternalManagedConnectionPool mcp = new
InternalManagedConnectionPool(mcf, subject, cri, pp, log);
for (int i = 0; i < pp.maxSize * threadsPerConnection; i++)
{
Runnable t = new Runnable() {
public void run()
{
for (int j = 0; j < reps; j++)
{
try
{
ManagedConnection mc = mcp.getConnection(null, null);
Thread.sleep(sleepTime);
mcp.returnConnection(mc, false);
}
catch (ResourceException re)
{
ManagedConnectionPoolUnitTestCase.this.failed = true;
} // end of try-catch
catch (InterruptedException ie)
{
return;
} // end of catch
} // end of for ()
}
};
new Thread(t).start();
} // end of for ()
assertTrue("Wrong number of connections counted: " + mcp.getConnectionCount(),
mcp.getConnectionCount() == pp.maxSize);
assertTrue("Blocking Timeout occurred in ShortBlocking test", !failed);
mcp.shutdown();
}
public void testFillToMinAndIdleTimeout() throws Exception
{
ManagedConnectionFactory mcf = new TestManagedConnectionFactory();
InternalManagedConnectionPool.PoolParams pp = new
InternalManagedConnectionPool.PoolParams();
pp.minSize = 5;
pp.maxSize = 5;
pp.blockingTimeout = 10;
pp.idleTimeout = 10;
Subject subject = new Subject();
ConnectionRequestInfo cri = new TestConnectionRequestInfo();
InternalManagedConnectionPool mcp = new InternalManagedConnectionPool(mcf,
subject, cri, pp, log);
mcp.fillToMin();
assertTrue("Wrong number of connections counted: " + mcp.getConnectionCount(),
mcp.getConnectionCount() == pp.minSize);
Thread.sleep((long)pp.idleTimeout + 1);
mcp.removeTimedOut();
assertTrue("Wrong number of connections counted: " + mcp.getConnectionCount(),
mcp.getConnectionCount() == pp.minSize);
}
}//
1.1
jbosstest/src/main/org/jboss/test/jca/test/XATxConnectionManagerUnitTestCase.java
Index: XATxConnectionManagerUnitTestCase.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*
*/
package org.jboss.test.jca.test; // Generated package name
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.resource.ResourceException;
import javax.resource.spi.ConnectionRequestInfo;
import javax.resource.spi.ManagedConnection;
import javax.resource.spi.ManagedConnectionFactory;
import javax.security.auth.Subject;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.logging.Logger;
import org.jboss.resource.connectionmanager.BaseConnectionManager2;
import org.jboss.resource.connectionmanager.CachedConnectionManager;
import org.jboss.resource.connectionmanager.XATxConnectionManager;
import org.jboss.resource.connectionmanager.JBossManagedConnectionPool;
import org.jboss.resource.connectionmanager.InternalManagedConnectionPool;
import org.jboss.resource.connectionmanager.ManagedConnectionPool;
import org.jboss.test.jca.adapter.TestConnection;
import org.jboss.test.jca.adapter.TestConnectionRequestInfo;
import org.jboss.test.jca.adapter.TestManagedConnectionFactory;
import org.jboss.test.JBossTestCase;
import org.jboss.tm.TxManager;
import org.jboss.tm.usertx.client.ServerVMClientUserTransaction;
/**
* XATxConnectionManagerUnitTestCase.java
*
*
* Created: Mon Jan 14 00:43:40 2002
*
* @author <a href="mailto:[EMAIL PROTECTED]">David Jencks</a>
* @version
*/
public class XATxConnectionManagerUnitTestCase extends JBossTestCase
{
Logger log = Logger.getLogger(getClass());
private TransactionManager tm;
private ServerVMClientUserTransaction ut;
private CachedConnectionManager ccm;
private TestManagedConnectionFactory mcf;
private XATxConnectionManager cm;
private ConnectionRequestInfo cri;
public XATxConnectionManagerUnitTestCase (String name)
{
super(name);
}
protected void setUp() throws Exception
{
tm = TxManager.getInstance();
ut = new ServerVMClientUserTransaction(tm);
ccm = new CachedConnectionManager();
ut.registerTxStartedListener(ccm);
mcf = new TestManagedConnectionFactory();
JBossManagedConnectionPool mcp = new JBossManagedConnectionPool();
mcp.setCriteria("ByNothing");
mcp.setMinSize(0);
mcp.setMaxSize(5);
mcp.setBlockingTimeoutMillis(100);
mcp.setIdleTimeout(500);
mcp.start();
Subject subject = new Subject();
cri = new TestConnectionRequestInfo();
mcp.getManagedConnectionPool().setManagedConnectionFactory(mcf);
cm = new XATxConnectionManager(mcf, ccm, mcp.getManagedConnectionPool(), tm);
}
protected void tearDown() throws Exception
{
}
public void testGetConnection() throws Exception
{
getLog().info("testGetConnection");
TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri);
assertTrue("Connection is null", c != null);
c.close();
}
public void testEnlistInExistingTx() throws Exception
{
getLog().info("testEnlistInExistingTx");
ut.begin();
TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri);
assertTrue("Connection not enlisted in tx!", c.isInTx());
c.close();
assertTrue("Connection still enlisted in tx!", !c.isInTx());
ut.commit();
assertTrue("Connection still enlisted in tx!", !c.isInTx());
}
public void testEnlistCheckedOutConnectionInNewTx() throws Exception
{
getLog().info("testEnlistCheckedOutConnectionInNewTx");
Object key = this;
ccm.pushMetaAwareObject(key);
TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri);
assertTrue("Connection already enlisted in tx!", !c.isInTx());
ut.begin();
assertTrue("Connection not enlisted in tx!", c.isInTx());
ut.commit();
assertTrue("Connection still enlisted in tx!", !c.isInTx());
c.close();
ccm.popMetaAwareObject();
}
public void testReconnectConnectionHandlesOnNotification() throws Exception
{
getLog().info("testReconnectConnectionHandlesOnNotification");
Object key1 = new Object();
Object key2 = new Object();
ccm.pushMetaAwareObject(key1);
ut.begin();
ccm.pushMetaAwareObject(key2);
TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri);
assertTrue("Connection not enlisted in tx!", c.isInTx());
ccm.popMetaAwareObject();//key2
ut.commit();
ut.begin();
ccm.pushMetaAwareObject(key2);
assertTrue("Connection not enlisted in tx!", c.isInTx());
ccm.popMetaAwareObject();//key2
ut.commit();
assertTrue("Connection still enlisted in tx!", !c.isInTx());
ccm.pushMetaAwareObject(key2);
c.close();
ccm.popMetaAwareObject();//key2
ccm.popMetaAwareObject();//key1
}
}// XATxConnectionManagerUnitTestCase
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development