1 22 package org.jboss.ejb3.test.concurrent.unit; 23 24 import java.lang.Thread.UncaughtExceptionHandler; 25 26 import javax.ejb.ConcurrentAccessException ; 27 28 import org.jboss.ejb3.test.concurrent.MyStateful; 29 import org.jboss.test.JBossTestCase; 30 import junit.framework.Test; 31 32 38 public class ConcurrentUnitTestCase 39 extends JBossTestCase 40 { 41 public ConcurrentUnitTestCase(String name) 42 { 43 super(name); 44 } 45 46 private static class MyUncaughtExceptionHandler implements UncaughtExceptionHandler 47 { 48 private Throwable uncaught; 49 50 Throwable getUncaughtException() 51 { 52 return uncaught; 53 } 54 55 public void uncaughtException(Thread t, Throwable e) 56 { 57 this.uncaught = e; 58 } 59 } 60 61 public void testConcurrentAccess() throws Exception 62 { 63 final MyStateful session = (MyStateful) getInitialContext().lookup("MyStatefulBean/remote"); 64 65 Runnable r = new Runnable () 66 { 67 public void run() 68 { 69 session.waitAndSee(); 70 } 71 }; 72 73 session.doNothing(); 75 76 MyUncaughtExceptionHandler eh = new MyUncaughtExceptionHandler(); 78 79 Thread t1 = new Thread (r); 80 t1.setUncaughtExceptionHandler(eh); 81 Thread t2 = new Thread (r); 82 t2.setUncaughtExceptionHandler(eh); 83 84 t1.start(); 85 t2.start(); 86 87 t1.join(); 88 t2.join(); 89 90 Throwable t = eh.getUncaughtException(); 91 assertNotNull(t); 92 assertTrue("Expected a javax.ejb.ConcurrentAccessException", t instanceof ConcurrentAccessException ); 93 } 94 95 98 public void testConcurrentProxyAccess() throws Exception 99 { 100 final MyStateful session = (MyStateful) getInitialContext().lookup("MyStatefulBean/remote"); 101 102 Runnable r = new Runnable () 103 { 104 public void run() 105 { 106 session.waitAndSee(); 107 } 108 }; 109 110 113 MyUncaughtExceptionHandler eh = new MyUncaughtExceptionHandler(); 115 116 Thread t1 = new Thread (r); 117 t1.setUncaughtExceptionHandler(eh); 118 Thread t2 = new Thread (r); 119 t2.setUncaughtExceptionHandler(eh); 120 121 t1.start(); 122 t2.start(); 123 124 t1.join(); 125 t2.join(); 126 127 Throwable t = eh.getUncaughtException(); 128 assertNotNull("No exception occured during a concurrent call", t); 129 fail("never comes here"); 130 } 131 132 public static Test suite() throws Exception 133 { 134 return getDeploySetup(ConcurrentUnitTestCase.class, "concurrent.jar"); 135 } 136 137 } 138 | Popular Tags |