001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jca.test;
023:
024: import javax.management.ObjectName;
025:
026: import junit.framework.Test;
027:
028: import org.jboss.test.JBossTestCase;
029: import org.jboss.test.jca.mbean.MTOperation;
030:
031: /**
032: * Multithreaded Tx JCA tests
033: * over DefaultDS
034: *
035: * @author <a href="dimitris@jboss.org">Dimitris Andreadis</a>
036: * @version $Revision: 57211 $
037: */
038: public class MultiThreadedTxDsUnitTestCase extends JBossTestCase {
039: static String[] SIG = new String[] { String.class.getName(),
040: new MTOperation[0][0].getClass().getName() };
041:
042: ObjectName mtMBean;
043:
044: public MultiThreadedTxDsUnitTestCase(String name) {
045: super (name);
046:
047: try {
048: // we can share the same target mbean with the
049: // sister MultiThreadedTxUnitTestCase test
050: mtMBean = new ObjectName(
051: "jboss.test:test=MultiThreadedTxUnitTestCase");
052: } catch (Exception e) {
053: throw new RuntimeException(e.toString());
054: }
055: }
056:
057: public void runTest(MTOperation[][] ops) throws Exception {
058: getServer().invoke(mtMBean, "testMTOperations",
059: new Object[] { getName(), ops }, SIG);
060: }
061:
062: public static Test suite() throws Exception {
063: Test t1 = getDeploySetup(MultiThreadedTxDsUnitTestCase.class,
064: "mtjcatest.sar");
065: Test t2 = getDeploySetup(t1, "testdriver-ds.xml");
066: return getDeploySetup(t2, "jbosstestdriver.jar");
067: }
068:
069: /**
070: * Have two threads getting/closing connections within the same tx.
071: * First thread begins/commits the tx. Use DefaultDS.
072: */
073: public void testTwoThreadsEnlistConnsInSameTx() throws Exception {
074: runTest(new MTOperation[][] {
075: {
076: // thread 0
077: new MTOperation(MTOperation.DS_DEFAULT_LOOKUP),
078: new MTOperation(MTOperation.TM_BEGIN, 10),
079: new MTOperation(MTOperation.DS_GET_CONN, 1),
080: new MTOperation(MTOperation.DS_GET_CONN, 2),
081: new MTOperation(MTOperation.DS_GET_CONN, 3),
082: new MTOperation(MTOperation.XX_SLEEP_RANDOM),
083: new MTOperation(MTOperation.DS_CLOSE_CONN, 3),
084: new MTOperation(MTOperation.DS_CLOSE_CONN, 2),
085: new MTOperation(MTOperation.DS_CLOSE_CONN, 1),
086: new MTOperation(MTOperation.XX_WAIT_FOR_SIGNAL,
087: 999),
088: new MTOperation(MTOperation.TM_COMMIT) },
089: {
090: // thread 1
091: new MTOperation(MTOperation.DS_DEFAULT_LOOKUP),
092: new MTOperation(MTOperation.XX_WAIT_FOR_TX, 10),
093: new MTOperation(MTOperation.TM_RESUME, 10),
094: new MTOperation(MTOperation.DS_GET_CONN, 4),
095: new MTOperation(MTOperation.DS_GET_CONN, 5),
096: new MTOperation(MTOperation.DS_GET_CONN, 6),
097: new MTOperation(MTOperation.XX_SLEEP_RANDOM),
098: new MTOperation(MTOperation.DS_CLOSE_CONN, 6),
099: new MTOperation(MTOperation.DS_CLOSE_CONN, 5),
100: new MTOperation(MTOperation.DS_CLOSE_CONN, 4),
101: new MTOperation(MTOperation.TM_SUSPEND, 10),
102: new MTOperation(MTOperation.XX_POST_SIGNAL, 999) } });
103: }
104:
105: /**
106: * Try to close a connection enlisted in one transaction
107: * inside a different tx, and in the original tx, too.
108: */
109: public void testCloseConnTwiceInDifferentTx() throws Exception {
110: runTest(new MTOperation[][] {
111: {
112: // thread 0
113: new MTOperation(MTOperation.DS_DEFAULT_LOOKUP),
114: new MTOperation(MTOperation.TM_BEGIN, 10),
115: new MTOperation(MTOperation.DS_GET_CONN, 1),
116: new MTOperation(MTOperation.XX_WAIT_FOR_SIGNAL,
117: 999),
118: new MTOperation(MTOperation.DS_CLOSE_CONN, 1),
119: new MTOperation(MTOperation.TM_COMMIT) },
120: {
121: // thread 1
122: new MTOperation(MTOperation.DS_DEFAULT_LOOKUP),
123: new MTOperation(MTOperation.TM_BEGIN, 20),
124: new MTOperation(MTOperation.XX_WAIT_FOR_CONN, 1),
125: new MTOperation(MTOperation.DS_CLOSE_CONN, 1),
126: new MTOperation(MTOperation.TM_COMMIT),
127: new MTOperation(MTOperation.XX_POST_SIGNAL, 999) } });
128: }
129:
130: /**
131: * Thread0 begins a tx, creates a connection and waits.
132: * N Threads resume thead0 tx and create and destroy 3
133: * connections each. Thread0 waits for them and commits the tx.
134: * The DefaultDS is used.
135: */
136: public void testStressConnsMultipleThreadsInSameTx()
137: throws Exception {
138: final int numThreads = 60;
139:
140: MTOperation[][] stressTest = new MTOperation[numThreads + 1][];
141:
142: // thread 0
143: MTOperation[] thread0 = new MTOperation[5 + numThreads];
144: thread0[0] = new MTOperation(MTOperation.DS_DEFAULT_LOOKUP);
145: thread0[1] = new MTOperation(MTOperation.TM_BEGIN, 10);
146: thread0[2] = new MTOperation(MTOperation.DS_GET_CONN, 0);
147: for (int i = 0; i < numThreads; i++) {
148: thread0[3 + i] = new MTOperation(
149: MTOperation.XX_WAIT_FOR_SIGNAL, i + 1);
150: }
151: thread0[3 + numThreads] = new MTOperation(
152: MTOperation.DS_CLOSE_CONN, 0);
153: thread0[4 + numThreads] = new MTOperation(MTOperation.TM_COMMIT);
154:
155: stressTest[0] = thread0;
156:
157: // threads 1 -> numThreads
158: for (int i = 1; i <= numThreads; i++) {
159: stressTest[i] = new MTOperation[] {
160: new MTOperation(MTOperation.DS_DEFAULT_LOOKUP),
161: new MTOperation(MTOperation.XX_WAIT_FOR_TX, 10),
162: new MTOperation(MTOperation.TM_RESUME, 10),
163: new MTOperation(MTOperation.DS_GET_CONN, 1000 + i),
164: new MTOperation(MTOperation.XX_SLEEP_RANDOM),
165: new MTOperation(MTOperation.DS_GET_CONN, 2000 + i),
166: new MTOperation(MTOperation.XX_SLEEP_RANDOM),
167: new MTOperation(MTOperation.DS_GET_CONN, 3000 + i),
168: new MTOperation(MTOperation.XX_SLEEP_RANDOM),
169: new MTOperation(MTOperation.DS_CLOSE_CONN, 3000 + i),
170: new MTOperation(MTOperation.XX_SLEEP_RANDOM),
171: new MTOperation(MTOperation.DS_CLOSE_CONN, 2000 + i),
172: new MTOperation(MTOperation.XX_SLEEP_RANDOM),
173: new MTOperation(MTOperation.DS_CLOSE_CONN, 1000 + i),
174: new MTOperation(MTOperation.TM_SUSPEND, 10),
175: new MTOperation(MTOperation.XX_POST_SIGNAL, i) };
176: }
177: runTest(stressTest);
178: }
179:
180: /**
181: * Create multiple threads that get and close connections
182: * within different transactions. The DefaultDS is used.
183: */
184: public void testStressMultipleThreadsDifferentTx() throws Exception {
185: final int numThreads = 60;
186:
187: MTOperation[][] stressTest = new MTOperation[numThreads][];
188:
189: // threads 0 -> numThreads
190: for (int i = 0; i < numThreads; i++) {
191: stressTest[i] = new MTOperation[] {
192: new MTOperation(MTOperation.DS_DEFAULT_LOOKUP),
193: new MTOperation(MTOperation.TM_BEGIN, 500 + i),
194: new MTOperation(MTOperation.DS_GET_CONN, 1000 + i),
195: new MTOperation(MTOperation.XX_SLEEP_RANDOM),
196: new MTOperation(MTOperation.DS_GET_CONN, 2000 + i),
197: new MTOperation(MTOperation.XX_SLEEP_RANDOM),
198: new MTOperation(MTOperation.DS_GET_CONN, 3000 + i),
199: new MTOperation(MTOperation.XX_SLEEP_RANDOM),
200: new MTOperation(MTOperation.DS_GET_CONN, 4000 + i),
201: new MTOperation(MTOperation.XX_SLEEP_RANDOM),
202: new MTOperation(MTOperation.DS_GET_CONN, 5000 + i),
203: new MTOperation(MTOperation.XX_SLEEP_RANDOM),
204: new MTOperation(MTOperation.DS_CLOSE_CONN, 5000 + i),
205: new MTOperation(MTOperation.XX_SLEEP_RANDOM),
206: new MTOperation(MTOperation.DS_CLOSE_CONN, 4000 + i),
207: new MTOperation(MTOperation.XX_SLEEP_RANDOM),
208: new MTOperation(MTOperation.DS_CLOSE_CONN, 3000 + i),
209: new MTOperation(MTOperation.XX_SLEEP_RANDOM),
210: new MTOperation(MTOperation.DS_CLOSE_CONN, 2000 + i),
211: new MTOperation(MTOperation.XX_SLEEP_RANDOM),
212: new MTOperation(MTOperation.DS_CLOSE_CONN, 1000 + i),
213: new MTOperation(MTOperation.TM_COMMIT) };
214: }
215: runTest(stressTest);
216: }
217: }
|