KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > test > MultiThreadedTxDsUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jca.test;
23
24 import javax.management.ObjectName JavaDoc;
25
26 import junit.framework.Test;
27
28 import org.jboss.test.JBossTestCase;
29 import org.jboss.test.jca.mbean.MTOperation;
30
31 /**
32  * Multithreaded Tx JCA tests
33  * over DefaultDS
34  *
35  * @author <a HREF="dimitris@jboss.org">Dimitris Andreadis</a>
36  * @version $Revision: 37406 $
37  */

38 public class MultiThreadedTxDsUnitTestCase extends JBossTestCase
39 {
40    static String JavaDoc[] SIG = new String JavaDoc[] { String JavaDoc.class.getName(), new MTOperation[0][0].getClass().getName() };
41
42    ObjectName JavaDoc mtMBean;
43
44    public MultiThreadedTxDsUnitTestCase(String JavaDoc name)
45    {
46       super(name);
47
48       try
49       {
50          // we can share the same target mbean with the
51
// sister MultiThreadedTxUnitTestCase test
52
mtMBean = new ObjectName JavaDoc("jboss.test:test=MultiThreadedTxUnitTestCase");
53       }
54       catch (Exception JavaDoc e)
55       {
56          throw new RuntimeException JavaDoc(e.toString());
57       }
58    }
59
60    public void runTest(MTOperation[][] ops) throws Exception JavaDoc
61    {
62       getServer().invoke(mtMBean, "testMTOperations", new Object JavaDoc[] { getName(), ops }, SIG);
63    }
64
65    public static Test suite() throws Exception JavaDoc
66    {
67       Test t1 = getDeploySetup(MultiThreadedTxDsUnitTestCase.class, "mtjcatest.sar");
68       Test t2 = getDeploySetup(t1, "testdriver-ds.xml");
69       return getDeploySetup(t2, "jbosstestdriver.jar");
70    }
71
72    /**
73     * Have two threads getting/closing connections within the same tx.
74     * First thread begins/commits the tx. Use DefaultDS.
75     */

76    public void testTwoThreadsEnlistConnsInSameTx() throws Exception JavaDoc
77    {
78       runTest(new MTOperation[][]
79       {
80          {
81             // thread 0
82
new MTOperation(MTOperation.DS_DEFAULT_LOOKUP),
83             new MTOperation(MTOperation.TM_BEGIN, 10),
84             new MTOperation(MTOperation.DS_GET_CONN, 1),
85             new MTOperation(MTOperation.DS_GET_CONN, 2),
86             new MTOperation(MTOperation.DS_GET_CONN, 3),
87             new MTOperation(MTOperation.XX_SLEEP_RANDOM),
88             new MTOperation(MTOperation.DS_CLOSE_CONN, 3),
89             new MTOperation(MTOperation.DS_CLOSE_CONN, 2),
90             new MTOperation(MTOperation.DS_CLOSE_CONN, 1),
91             new MTOperation(MTOperation.XX_WAIT_FOR_SIGNAL, 999),
92             new MTOperation(MTOperation.TM_COMMIT)
93          }
94          ,
95          {
96             // thread 1
97
new MTOperation(MTOperation.DS_DEFAULT_LOOKUP),
98             new MTOperation(MTOperation.XX_WAIT_FOR_TX, 10),
99             new MTOperation(MTOperation.TM_RESUME, 10),
100             new MTOperation(MTOperation.DS_GET_CONN, 4),
101             new MTOperation(MTOperation.DS_GET_CONN, 5),
102             new MTOperation(MTOperation.DS_GET_CONN, 6),
103             new MTOperation(MTOperation.XX_SLEEP_RANDOM),
104             new MTOperation(MTOperation.DS_CLOSE_CONN, 6),
105             new MTOperation(MTOperation.DS_CLOSE_CONN, 5),
106             new MTOperation(MTOperation.DS_CLOSE_CONN, 4),
107             new MTOperation(MTOperation.TM_SUSPEND, 10),
108             new MTOperation(MTOperation.XX_POST_SIGNAL, 999)
109          }
110       });
111    }
112    
113    /**
114     * Try to close a connection enlisted in one transaction
115     * inside a different tx, and in the original tx, too.
116     */

117    public void testCloseConnTwiceInDifferentTx() throws Exception JavaDoc
118    {
119       runTest(new MTOperation[][]
120       {
121          {
122             // thread 0
123
new MTOperation(MTOperation.DS_DEFAULT_LOOKUP),
124             new MTOperation(MTOperation.TM_BEGIN, 10),
125             new MTOperation(MTOperation.DS_GET_CONN, 1),
126             new MTOperation(MTOperation.XX_WAIT_FOR_SIGNAL, 999),
127             new MTOperation(MTOperation.DS_CLOSE_CONN, 1),
128             new MTOperation(MTOperation.TM_COMMIT)
129          }
130          ,
131          {
132             // thread 1
133
new MTOperation(MTOperation.DS_DEFAULT_LOOKUP),
134             new MTOperation(MTOperation.TM_BEGIN, 20),
135             new MTOperation(MTOperation.XX_WAIT_FOR_CONN, 1),
136             new MTOperation(MTOperation.DS_CLOSE_CONN, 1),
137             new MTOperation(MTOperation.TM_COMMIT),
138             new MTOperation(MTOperation.XX_POST_SIGNAL, 999)
139          }
140       });
141    }
142    
143    /**
144     * Thread0 begins a tx, creates a connection and waits.
145     * N Threads resume thead0 tx and create and destroy 3
146     * connections each. Thread0 waits for them and commits the tx.
147     * The DefaultDS is used.
148     */

149    public void testStressConnsMultipleThreadsInSameTx() throws Exception JavaDoc
150    {
151       final int numThreads = 60;
152       
153       MTOperation[][] stressTest = new MTOperation[numThreads + 1][];
154
155       // thread 0
156
MTOperation[] thread0 = new MTOperation[5 + numThreads];
157       thread0[0] = new MTOperation(MTOperation.DS_DEFAULT_LOOKUP);
158       thread0[1] = new MTOperation(MTOperation.TM_BEGIN, 10);
159       thread0[2] = new MTOperation(MTOperation.DS_GET_CONN, 0);
160       for (int i = 0; i < numThreads; i++)
161       {
162          thread0[3+i] = new MTOperation(MTOperation.XX_WAIT_FOR_SIGNAL, i+1);
163       }
164       thread0[3 + numThreads] = new MTOperation(MTOperation.DS_CLOSE_CONN, 0);
165       thread0[4 + numThreads] = new MTOperation(MTOperation.TM_COMMIT);
166
167       stressTest[0] = thread0;
168       
169       // threads 1 -> numThreads
170
for (int i = 1; i <= numThreads; i++)
171       {
172          stressTest[i] = new MTOperation[] {
173                new MTOperation(MTOperation.DS_DEFAULT_LOOKUP),
174                new MTOperation(MTOperation.XX_WAIT_FOR_TX, 10),
175                new MTOperation(MTOperation.TM_RESUME, 10),
176                new MTOperation(MTOperation.DS_GET_CONN, 1000+i),
177                new MTOperation(MTOperation.XX_SLEEP_RANDOM),
178                new MTOperation(MTOperation.DS_GET_CONN, 2000+i),
179                new MTOperation(MTOperation.XX_SLEEP_RANDOM),
180                new MTOperation(MTOperation.DS_GET_CONN, 3000+i),
181                new MTOperation(MTOperation.XX_SLEEP_RANDOM),
182                new MTOperation(MTOperation.DS_CLOSE_CONN, 3000+i),
183                new MTOperation(MTOperation.XX_SLEEP_RANDOM),
184                new MTOperation(MTOperation.DS_CLOSE_CONN, 2000+i),
185                new MTOperation(MTOperation.XX_SLEEP_RANDOM),
186                new MTOperation(MTOperation.DS_CLOSE_CONN, 1000+i),
187                new MTOperation(MTOperation.TM_SUSPEND, 10),
188                new MTOperation(MTOperation.XX_POST_SIGNAL, i)
189          };
190       }
191       runTest(stressTest);
192    }
193    
194    /**
195     * Create multiple threads that get and close connections
196     * within different transactions. The DefaultDS is used.
197     */

198    public void testStressMultipleThreadsDifferentTx() throws Exception JavaDoc
199    {
200       final int numThreads = 60;
201       
202       MTOperation[][] stressTest = new MTOperation[numThreads][];
203
204       // threads 0 -> numThreads
205
for (int i = 0; i < numThreads; i++)
206       {
207          stressTest[i] = new MTOperation[] {
208                new MTOperation(MTOperation.DS_DEFAULT_LOOKUP),
209                new MTOperation(MTOperation.TM_BEGIN, 500+i),
210                new MTOperation(MTOperation.DS_GET_CONN, 1000+i),
211                new MTOperation(MTOperation.XX_SLEEP_RANDOM),
212                new MTOperation(MTOperation.DS_GET_CONN, 2000+i),
213                new MTOperation(MTOperation.XX_SLEEP_RANDOM),
214                new MTOperation(MTOperation.DS_GET_CONN, 3000+i),
215                new MTOperation(MTOperation.XX_SLEEP_RANDOM),
216                new MTOperation(MTOperation.DS_GET_CONN, 4000+i),
217                new MTOperation(MTOperation.XX_SLEEP_RANDOM),
218                new MTOperation(MTOperation.DS_GET_CONN, 5000+i),
219                new MTOperation(MTOperation.XX_SLEEP_RANDOM),
220                new MTOperation(MTOperation.DS_CLOSE_CONN, 5000+i),
221                new MTOperation(MTOperation.XX_SLEEP_RANDOM),
222                new MTOperation(MTOperation.DS_CLOSE_CONN, 4000+i),
223                new MTOperation(MTOperation.XX_SLEEP_RANDOM),
224                new MTOperation(MTOperation.DS_CLOSE_CONN, 3000+i),
225                new MTOperation(MTOperation.XX_SLEEP_RANDOM),
226                new MTOperation(MTOperation.DS_CLOSE_CONN, 2000+i),
227                new MTOperation(MTOperation.XX_SLEEP_RANDOM),
228                new MTOperation(MTOperation.DS_CLOSE_CONN, 1000+i),
229                new MTOperation(MTOperation.TM_COMMIT)
230          };
231       }
232       runTest(stressTest);
233    }
234 }
235
Popular Tags