1 22 package org.jboss.test.lock.test; 23 24 import javax.ejb.CreateException ; 25 import javax.ejb.RemoveException ; 26 import javax.naming.InitialContext ; 27 import javax.rmi.PortableRemoteObject ; 28 29 import junit.framework.Test; 30 31 import org.jboss.logging.Logger; 32 import org.jboss.test.JBossTestCase; 33 import org.jboss.test.lock.interfaces.EnterpriseEntity; 34 import org.jboss.test.lock.interfaces.EnterpriseEntityHome; 35 36 42 public class SpinUnitTestCase extends JBossTestCase 43 { 44 49 public SpinUnitTestCase(String name) 50 { 51 super(name); 52 } 53 54 static void create() throws Exception 55 { 56 InitialContext jndiContext = new InitialContext (); 57 Object obj = jndiContext.lookup("EnterpriseEntity_A"); 58 obj = PortableRemoteObject.narrow(obj, EnterpriseEntityHome.class); 59 EnterpriseEntityHome home = (EnterpriseEntityHome)obj; 60 try 61 { 62 home.create("Bean1"); 63 } 64 catch (CreateException e) 65 { 66 } 67 } 68 69 static void remove() throws Exception 70 { 71 InitialContext jndiContext = new InitialContext (); 72 Object obj = jndiContext.lookup("EnterpriseEntity_A"); 73 obj = PortableRemoteObject.narrow(obj, EnterpriseEntityHome.class); 74 EnterpriseEntityHome home = (EnterpriseEntityHome)obj; 75 try 76 { 77 home.remove("Bean1"); 78 } 79 catch (RemoveException e) 80 { 81 } 82 } 83 84 89 public void testContention() throws Exception 90 { 91 getLog().debug("+++ testContention()"); 92 InitialContext jndiContext = new InitialContext (); 93 Object obj = jndiContext.lookup("EnterpriseEntity_A"); 94 obj = PortableRemoteObject.narrow(obj, EnterpriseEntityHome.class); 95 EnterpriseEntityHome home = (EnterpriseEntityHome)obj; 96 getLog().debug("Found EnterpriseEntityHome @ jndiName=EnterpriseEntity"); 97 Run r0 = new Run(home.findByPrimaryKey("Bean1"), getLog()); 98 Run r1 = new Run(home.findByPrimaryKey("Bean1"), getLog()); 99 Run r2 = new Run(home.findByPrimaryKey("Bean1"), getLog()); 100 Thread t0 = new Thread (r0); 101 Thread t1 = new Thread (r1); 102 Thread t2 = new Thread (r2); 103 t0.start(); 104 Thread.sleep(100); 105 t1.start(); 106 Thread.sleep(100); 107 t2.start(); 108 getLog().debug("Waiting for t0..."); 109 try 110 { 111 t0.join(5000); 112 assertTrue(r0.ex == null); 113 } 114 catch (InterruptedException e) 115 { 116 getLog().debug("Timed out waiting for t1"); 117 } 118 getLog().debug("Waiting for t1..."); 119 try 120 { 121 t1.join(5000); 122 assertTrue(r1.ex == null); 123 } 124 catch (InterruptedException e) 125 { 126 getLog().debug("Timed out waiting for t1"); 127 } 128 getLog().debug("Waiting for t2..."); 129 try 130 { 131 t2.join(5000); 132 assertTrue(r2.ex == null); 133 } 134 catch (InterruptedException e) 135 { 136 getLog().debug("Timed out waiting for t2"); 137 } 138 139 getLog().debug("End threads"); 140 } 141 142 147 protected void setUp() throws Exception 148 { 149 try 150 { 151 create(); 152 } 153 catch (Exception e) 154 { 155 getLog().error("setup error in create: ", e); 156 throw e; 157 } 158 } 159 160 165 protected void tearDown() throws Exception 166 { 167 try 168 { 169 remove(); 170 } 171 catch (Exception e) 172 { 173 getLog().error("teardown error in remove: ", e); 174 throw e; 175 } 176 } 177 178 public static Test suite() throws Exception 179 { 180 return getDeploySetup(SpinUnitTestCase.class, "locktest.jar"); 181 } 182 183 184 185 188 static class Run implements Runnable 189 { 190 EnterpriseEntity bean; 191 Exception ex; 192 private Logger log; 193 194 Run(EnterpriseEntity bean, Logger log) 195 { 196 this.bean = bean; 197 this.log = log; 198 } 199 200 203 public synchronized void run() 204 { 205 notifyAll(); 206 try 207 { 208 long start = System.currentTimeMillis(); 209 bean.sleep(5000); 210 long end = System.currentTimeMillis(); 211 long elapsed = end - start; 212 log.debug(" bean.sleep() time = " + elapsed + " ms"); 213 } 214 catch (Exception e) 215 { 216 ex = e; 217 } 218 } 219 } 220 221 } 222 | Popular Tags |