KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > deadlock > test > BeanStressTestCase


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.deadlock.test;
23
24 import javax.naming.Context JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.ejb.DuplicateKeyException JavaDoc;
27 import javax.ejb.ObjectNotFoundException JavaDoc;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.Random JavaDoc;
34
35 import junit.framework.Test;
36
37 import org.jboss.test.deadlock.interfaces.BeanOrder;
38 import org.jboss.test.deadlock.interfaces.EnterpriseEntityHome;
39 import org.jboss.test.deadlock.interfaces.EnterpriseEntity;
40 import org.jboss.test.deadlock.interfaces.StatelessSessionHome;
41 import org.jboss.test.deadlock.interfaces.StatelessSession;
42 import org.jboss.test.JBossTestCase;
43 import org.jboss.ejb.plugins.TxInterceptorCMT;
44
45 /**
46  * Sample client for the jboss container.
47  *
48  * @author <a HREF="mailto:bill@burkecentral.com">Bill Burke</a>
49  * @version $Id: BeanStressTestCase.java 58115 2006-11-04 08:42:14Z scott.stark@jboss.org $
50  */

51 public class BeanStressTestCase
52    extends JBossTestCase
53 {
54    org.jboss.logging.Logger log = getLog();
55
56    static boolean deployed = false;
57    static int test = 0;
58    static Date JavaDoc startDate = new Date JavaDoc();
59
60    protected final String JavaDoc namingFactory =
61       System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
62
63    protected final String JavaDoc providerURL =
64       System.getProperty(Context.PROVIDER_URL);
65
66    public BeanStressTestCase(String JavaDoc name)
67    {
68       super(name);
69    }
70
71    boolean failed = false;
72
73    private StatelessSession getSession() throws Exception JavaDoc
74    {
75
76       StatelessSessionHome home = (StatelessSessionHome) new InitialContext JavaDoc().lookup("nextgen.StatelessSession");
77       return home.create();
78    }
79
80    public class RunTest implements Runnable JavaDoc
81    {
82       public String JavaDoc test;
83
84       public RunTest(String JavaDoc test)
85       {
86          this.test = test;
87       }
88
89       public void run()
90       {
91          if (test.equals("AB"))
92             runAB();
93          else
94             runBA();
95       }
96
97       private void runAB()
98       {
99          log.debug("running AB");
100          try
101          {
102             getSession().callAB();
103          }
104          catch (Exception JavaDoc ex)
105          {
106             failed = true;
107          }
108       }
109
110       private void runBA()
111       {
112          log.debug("running BA");
113          try
114          {
115             getSession().callBA();
116          }
117          catch (Exception JavaDoc ex)
118          {
119             failed = true;
120          }
121       }
122    }
123
124    public void testDeadLock()
125       throws Exception JavaDoc
126    {
127       EnterpriseEntityHome home = (EnterpriseEntityHome) new InitialContext JavaDoc().lookup("nextgenEnterpriseEntity");
128       try
129       {
130          EnterpriseEntity A = home.findByPrimaryKey("A");
131       }
132       catch (ObjectNotFoundException JavaDoc ex)
133       {
134          home.create("A");
135       }
136       try
137       {
138          EnterpriseEntity B = home.findByPrimaryKey("B");
139       }
140       catch (ObjectNotFoundException JavaDoc ex)
141       {
142          home.create("B");
143       }
144       Thread JavaDoc one = new Thread JavaDoc(new RunTest("AB"));
145       Thread JavaDoc two = new Thread JavaDoc(new RunTest("BA"));
146       one.start();
147       two.start();
148       one.join();
149       two.join();
150       if (failed)
151       {
152          fail("testing of deadlock AB BA scenario failed");
153       }
154    }
155
156    Random JavaDoc random = new Random JavaDoc();
157
158    int target;
159    int iterations;
160
161    Object JavaDoc lock = new Object JavaDoc();
162    int completed = 0;
163
164    Exception JavaDoc unexpected;
165
166    public class OrderTest
167       implements Runnable JavaDoc
168    {
169       BeanOrder beanOrder;
170       EnterpriseEntityHome home;
171
172       String JavaDoc toStringCached;
173
174       public OrderTest(EnterpriseEntityHome home, int beanCount, int depth)
175       {
176          // Create the list of beans
177
ArrayList JavaDoc list = new ArrayList JavaDoc();
178          for (int i = 0; i < depth; i++)
179             list.add(new Integer JavaDoc(i % beanCount).toString());
180
181          // Shuffle them
182
Collections.shuffle(list, random);
183
184          beanOrder = new BeanOrder((String JavaDoc[]) list.toArray(new String JavaDoc[beanCount]));
185          this.home = home;
186       }
187
188       public void run()
189       {
190          try
191          {
192             EnterpriseEntity bean = home.findByPrimaryKey(beanOrder.order[0]);
193             home = null;
194             for (int i = 0; i < iterations; i++)
195             {
196                log.debug("Before: iter=" + i + " " + this);
197                bean.callAnotherBean(beanOrder);
198                log.debug("After : iter=" + i + " " + this);
199             }
200          }
201          catch (Exception JavaDoc e)
202          {
203             if (TxInterceptorCMT.isADE(e) == null)
204             {
205                log.debug("Saw exception for " + this, e);
206                unexpected = e;
207             }
208          }
209       }
210
211       public String JavaDoc toString()
212       {
213          if (toStringCached != null)
214             return toStringCached;
215
216          StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
217          buffer.append(" hash=").append(hashCode());
218          buffer.append(" order=").append(Arrays.asList(beanOrder.order));
219
220          toStringCached = buffer.toString();
221          return toStringCached;
222       }
223    }
224
225    public class TestThread
226       extends Thread JavaDoc
227    {
228       OrderTest test;
229
230       public TestThread(OrderTest test)
231       {
232          super(test);
233          this.test = test;
234       }
235
236       public void run()
237       {
238          super.run();
239          synchronized (lock)
240          {
241             completed++;
242             log.debug("Completed " + completed + " of " + target);
243             lock.notifyAll();
244          }
245       }
246    }
247
248    public void waitForCompletion()
249       throws Exception JavaDoc
250    {
251       log.debug("Waiting for completion");
252       synchronized (lock)
253       {
254          while (completed < target)
255          {
256             lock.wait();
257          }
258       }
259       if (unexpected != null)
260       {
261          log.error("Unexpected exception", unexpected);
262          fail("Unexpected exception");
263       }
264    }
265
266    /**
267     * Creates a number of threads to invoke on the
268     * session beans at random to produce deadlocks.
269     * The test will timeout if a deadlock detection is missed.
270     */

271    public void testAllCompleteOrFail()
272       throws Exception JavaDoc
273    {
274       doAllCompleteOrFail("nextgenEnterpriseEntity" ,2);
275    }
276
277    /**
278     * Creates a number of threads to invoke on the
279     * session beans at random to produce deadlocks.
280     * The test will timeout if a deadlock detection is missed.
281     */

282    public void testAllCompleteOrFailReentrant()
283       throws Exception JavaDoc
284    {
285       doAllCompleteOrFail("nextgenEnterpriseEntityReentrant", 4);
286    }
287
288    /**
289     * Creates a number of threads to invoke on the
290     * session beans at random to produce deadlocks.
291     * The test will timeout if a deadlock detection is missed.
292     */

293    public void testAllCompleteOrFailNotSupported()
294       throws Exception JavaDoc
295    {
296       doAllCompleteOrFail("nextgenEnterpriseEntityNotSupported", 2);
297    }
298
299    /**
300     * Creates a number of threads to invoke on the
301     * session beans at random to produce deadlocks.
302     * The test will timeout if a deadlock detection is missed.
303     */

304    public void testAllCompleteOrFailNotSupportedReentrant()
305       throws Exception JavaDoc
306    {
307       doAllCompleteOrFail("nextgenEnterpriseEntityNotSupportedReentrant", 4);
308    }
309
310    /**
311     * Creates a number of threads to invoke on the
312     * session beans at random to produce deadlocks.
313     * The test will timeout if a deadlock detection is missed.
314     */

315    public void doAllCompleteOrFail(String JavaDoc jndiName, int depth)
316       throws Exception JavaDoc
317    {
318       log.debug("========= Starting " + getName());
319
320       // Non-standard: We want a lot of threads and a small number of beans
321
// for maximum contention
322
// target = getThreadCount();
323
// int beanCount = getBeanCount();
324
target = 40;
325       int beanCount = 2;
326       completed = 0;
327       unexpected = null;
328
329       // Create some beans
330
EnterpriseEntityHome home = (EnterpriseEntityHome) new InitialContext JavaDoc().lookup(jndiName);
331       for (int i = 0; i < beanCount; i++)
332       {
333          try
334          {
335             home.create(new Integer JavaDoc(i).toString());
336          }
337          catch (DuplicateKeyException JavaDoc weDontCare)
338          {
339          }
340       }
341
342       // Create some threads
343
TestThread[] threads = new TestThread[target];
344       for (int i = 0; i < target; i++)
345          threads[i] = new TestThread(new OrderTest(home, beanCount, depth));
346
347       // Start the threads
348
for (int i = 0; i < target; i++)
349       {
350          log.debug("Starting " + threads[i].test);
351          threads[i].start();
352       }
353
354       waitForCompletion();
355
356       log.debug("========= Completed " + getName());
357    }
358
359    public class CMRTest
360       implements Runnable JavaDoc
361    {
362       StatelessSession session;
363       String JavaDoc jndiName;
364       String JavaDoc start;
365
366       public CMRTest(StatelessSession session, String JavaDoc jndiName, String JavaDoc start)
367       {
368          this.session = session;
369          this.jndiName = jndiName;
370          this.start = start;
371       }
372
373       public void run()
374       {
375          try
376          {
377             session.cmrTest(jndiName, start);
378          }
379          catch (Exception JavaDoc e)
380          {
381             if (TxInterceptorCMT.isADE(e) == null)
382             {
383                log.debug("Saw exception for " + this, e);
384                unexpected = e;
385             }
386          }
387       }
388
389       public String JavaDoc toString()
390       {
391          return hashCode() + " " + start;
392       }
393    }
394
395    public class CMRTestThread
396       extends Thread JavaDoc
397    {
398       CMRTest test;
399
400       public CMRTestThread(CMRTest test)
401       {
402          super(test);
403          this.test = test;
404       }
405
406       public void run()
407       {
408          super.run();
409          synchronized (lock)
410          {
411             completed++;
412             log.debug("Completed " + completed + " of " + target);
413             lock.notifyAll();
414          }
415       }
416    }
417
418    /**
419     * Creates a number of threads to CMR relationships.
420     * The test will timeout if a deadlock detection is missed.
421     */

422    public void testAllCompleteOrFailCMR()
423       throws Exception JavaDoc
424    {
425       doAllCompleteOrFailCMR("local/nextgenEnterpriseEntity");
426    }
427
428    /**
429     * Creates a number of threads to CMR relationships.
430     * The test will timeout if a deadlock detection is missed.
431     */

432    public void doAllCompleteOrFailCMR(String JavaDoc jndiName)
433       throws Exception JavaDoc
434    {
435       log.debug("========= Starting " + getName());
436
437       // Non-standard: We want a lot of threads and a small number of beans
438
// for maximum contention
439
// target = getThreadCount();
440
// int beanCount = getBeanCount();
441
target = 40;
442       completed = 0;
443       unexpected = null;
444
445       // Create some beans
446
StatelessSessionHome home = (StatelessSessionHome) new InitialContext JavaDoc().lookup("nextgen.StatelessSession");
447       StatelessSession session = home.create();
448       session.createCMRTestData(jndiName);
449
450       // Create some threads
451
CMRTestThread[] threads = new CMRTestThread[target];
452       for (int i = 0; i < target; i++)
453          threads[i] = new CMRTestThread(new CMRTest(session, jndiName, i % 2 == 0 ? "First" : "Second"));
454
455       // Start the threads
456
for (int i = 0; i < target; i++)
457       {
458          log.debug("Starting " + threads[i].test);
459          threads[i].start();
460       }
461
462       waitForCompletion();
463
464       log.debug("========= Completed " + getName());
465    }
466
467    /*
468    public void testRequiresNewDeadlock()
469       throws Exception
470    {
471       
472       EnterpriseEntityHome home = (EnterpriseEntityHome)new InitialContext().lookup("nextgenEnterpriseEntity");
473       try
474       {
475      EnterpriseEntity C = home.findByPrimaryKey("C");
476       }
477       catch (ObjectNotFoundException ex)
478       {
479      home.create("C");
480       }
481
482       boolean deadlockExceptionThrown = false;
483       try
484       {
485          getSession().requiresNewTest(true);
486       }
487       catch (RemoteException ex)
488       {
489          if (ex.detail instanceof ApplicationDeadlockException)
490          {
491             deadlockExceptionThrown = true;
492          }
493       }
494       assertTrue("ApplicationDeadlockException was not thrown", deadlockExceptionThrown);
495    }
496    */

497
498    public void testCleanup() throws Exception JavaDoc
499    {
500       // Restart the db pool
501
super.restartDBPool();
502    }
503
504    public static Test suite() throws Exception JavaDoc
505    {
506       return getDeploySetup(BeanStressTestCase.class, "deadlock.jar");
507    }
508 }
509
Popular Tags