KickJava   Java API By Example, From Geeks To Geeks.

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


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.Attribute JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26
27 import junit.framework.Test;
28
29 import org.jboss.logging.Logger;
30 import org.jboss.test.JBossTestCase;
31 import org.jboss.test.jca.bank.interfaces.Account;
32 import org.jboss.test.jca.bank.interfaces.Teller;
33 import org.jboss.test.jca.bank.interfaces.TellerHome;
34
35 /**
36  * CachedConnectionBankStressTestCase.java
37  * Tests connection disconnect-reconnect mechanism.
38  *
39  * Created: Mon Mar 18 07:57:41 2002
40  *
41  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
42  * @version
43  */

44
45 public class CachedConnectionBankStressTestCase extends JBossTestCase
46 {
47
48    private TellerHome th;
49    private Teller t;
50
51    private Exception JavaDoc exc;
52
53    private int iter;
54
55    public CachedConnectionBankStressTestCase (String JavaDoc name)
56    {
57       super(name);
58    }
59
60    protected void setUp() throws Exception JavaDoc
61    {
62       ObjectName JavaDoc CCM = new ObjectName JavaDoc("jboss.jca:service=CachedConnectionManager");
63       getServer().setAttribute(CCM, new Attribute JavaDoc("SpecCompliant", Boolean.TRUE));
64       th = (TellerHome)getInitialContext().lookup("Teller");
65       t = th.create();
66       t.setUp();
67    }
68
69    protected void tearDown() throws Exception JavaDoc
70    {
71       if (t != null)
72       {
73          t.tearDown();
74       } // end of if ()
75
ObjectName JavaDoc CCM = new ObjectName JavaDoc("jboss.jca:service=CachedConnectionManager");
76       getServer().setAttribute(CCM, new Attribute JavaDoc("SpecCompliant", Boolean.FALSE));
77
78    }
79
80    public static Test suite() throws Exception JavaDoc
81    {
82       return getDeploySetup(CachedConnectionBankStressTestCase.class, "jcabanktest.jar");
83    }
84
85    public void testCachedConnectionBank() throws Exception JavaDoc
86    {
87       Account[] accounts = new Account[getThreadCount()];
88       for (int i = 0; i < getThreadCount(); i++)
89       {
90          accounts[i] = t.createAccount(new Integer JavaDoc(i));
91       } // end of for ()
92
final Object JavaDoc lock = new Object JavaDoc();
93
94       iter = 0;
95       getLog().info("Start test. "+getThreadCount()+ " threads, "+getIterationCount()+" iterations");
96       long start = System.currentTimeMillis();
97
98       for (int i = 0; i < getThreadCount() - 1; i++)
99       {
100          //Thread.sleep(500); // Wait between each client
101
new Thread JavaDoc(new TransferThread(accounts[i],
102                             accounts[(i + 1) % getThreadCount()],
103                             getIterationCount(),
104                             lock)).start();
105          synchronized (lock)
106          {
107             iter++;
108          }
109       }
110
111       synchronized(lock)
112       {
113          while(iter > 0)
114          {
115             lock.wait();
116          }
117       }
118
119       if (exc != null) throw exc;
120
121       for (int i = 1; i < getThreadCount() - 1; i++)
122       {
123          assertTrue("nonzero final balance for" + i, accounts[i].getBalance() == 0);
124       } // end of for ()
125

126
127       long end = System.currentTimeMillis();
128
129       getLog().info("Time:"+(end-start));
130       getLog().info("Avg. time/call(ms):"+((end-start)/(getThreadCount()*getIterationCount())));
131 }
132
133
134
135
136    public class TransferThread implements Runnable JavaDoc
137    {
138       Logger log = Logger.getLogger(getClass().getName());
139       Account to;
140       Account from;
141       int iterationCount;
142       Object JavaDoc lock;
143
144       public TransferThread(final Account to,
145                             final Account from,
146                             final int iterationCount,
147                             final Object JavaDoc lock) throws Exception JavaDoc
148       {
149          this.to = to;
150          this.from = from;
151          this.iterationCount = iterationCount;
152          this.lock = lock;
153       }
154
155       public void run()
156       {
157          try
158          {
159
160             for (int j = 0; j < iterationCount; j++)
161             {
162                if (exc != null) break;
163
164                t.transfer(from,to, 1);
165             }
166          } catch (Exception JavaDoc e)
167          {
168             exc = e;
169          }
170
171          synchronized(lock)
172          {
173             iter--;
174             log.info("Only "+iter+" left");
175             lock.notifyAll();
176          }
177       }
178    }
179 }// CachedConnectionSessionUnitTestCase
180
Popular Tags