1 22 package org.jboss.test.lock.test; 23 24 import java.rmi.*; 25 26 import java.util.HashMap ; 27 import java.util.StringTokenizer ; 28 29 import javax.ejb.FinderException ; 30 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import junit.framework.Assert; 34 35 import junit.framework.Test; 36 import junit.framework.TestCase; 37 import junit.framework.TestSuite; 38 39 import org.jboss.logging.Logger; 40 41 import org.jboss.test.JBossTestCase; 42 import org.jboss.test.lock.interfaces.EnterpriseEntity; 43 44 import org.jboss.test.lock.interfaces.EnterpriseEntityHome; 45 46 49 public abstract class EnterpriseEntityTest 50 extends JBossTestCase 51 { 52 55 public final static int DEFAULT_THREAD_COUNT = 20; 56 59 public final static int DEFAULT_ITERATIONS = 10; 60 61 private String jndiname; 62 63 66 private int nbThreads; 67 private int completedThreads; 68 69 72 private int iterations; 73 74 private Worker[] threads; 75 76 private HashMap param = new HashMap (); 77 78 private EnterpriseEntity entity; 79 80 private boolean failed; 81 82 88 public EnterpriseEntityTest(final String name, 89 final String jndiname) 90 { 91 super(name); 92 this.jndiname = jndiname; 93 } 94 95 100 public void testSingleBean() throws Exception 101 { 102 getLog().debug("Spawning " + nbThreads + " threads for " + 103 iterations + " iterations with single bean call"); 104 105 Task prototype = 106 new Task() 107 { 108 115 public void run(String name, int i) throws Exception 116 { 117 entity.setField(name + " i=" + i); 118 } 119 }; 120 121 run(prototype); 122 } 123 124 129 public void testB2B() throws Exception 130 { 131 getLog().debug("Spawning " + nbThreads + " threads for " + 132 iterations + " iterations with bean to bean call"); 133 134 entity.setNextEntity("daniel"); 135 136 Task prototype = 137 new Task() 138 { 139 146 public void run(String name, int i) throws Exception 147 { 148 entity.setAndCopyField(name + " i=" + i); 149 } 150 }; 151 152 run(prototype); 153 } 154 155 160 protected void setUp() throws Exception 161 { 162 nbThreads = getThreadCount(); iterations = getIterationCount(); getLog().debug("+++ Setting up: " + getClass().getName() + " test: " + getName()); 165 EnterpriseEntityHome home = 166 (EnterpriseEntityHome)getInitialContext().lookup(jndiname); 167 168 try 169 { 170 entity = home.findByPrimaryKey("seb"); 171 } 172 catch (FinderException e) 173 { 174 entity = home.create("seb"); 175 } 176 177 threads = new Worker[nbThreads]; 179 } 180 181 184 protected synchronized void setFailed() 185 { 186 failed = true; 187 } 188 189 190 196 protected void startAll(Task prototype) throws Exception 197 { 198 completedThreads = 0; 199 for (int i = 0; i < nbThreads; i++) 200 { 201 Task task = (Task)prototype.clone(); 202 threads[i] = new Worker("Thread #" + (i + 1), task, getLog()); 203 threads[i].start(); 204 } 205 } 206 207 212 protected void joinAll() throws Exception 213 { 214 for (int i = 0; i < nbThreads; i++) 216 { 217 threads[i].join(); 218 } 219 } 220 221 227 protected void run(Task prototype) throws Exception 228 { 229 startAll(prototype); 230 joinAll(); 231 assertTrue(!failed); 232 } 233 234 239 protected boolean hasFailed() 240 { 241 return failed; 242 } 243 244 245 249 252 public abstract class Task 253 implements Cloneable 254 { 255 262 public abstract void run(String name, int i) throws Exception ; 263 264 269 public Object clone() 270 { 271 try 272 { 273 return super.clone(); 274 } 275 catch (CloneNotSupportedException e) 276 { 277 throw new InternalError (); 278 } 279 } 280 } 281 282 285 public class Worker 286 extends Thread 287 { 288 291 public String name; 292 295 public boolean running; 296 299 public Task task; 300 301 private Logger log; 302 303 310 public Worker(final String name, final Task task, Logger log) 311 { 312 this.name = name; 313 this.task = task; 314 this.log = log; 315 running = true; 316 } 317 318 321 public void run() 322 { 323 long start = System.currentTimeMillis(); 324 int i; 325 326 for (i = 0; i < iterations; i++) 327 { 328 if (!running || hasFailed()) 329 { 330 break; 331 } 332 333 try 334 { 335 task.run(name, i); 336 } 338 catch (Throwable t) 339 { 340 log.error(name + " caught an exception, dying", t); 341 t.printStackTrace(); 342 running = false; 343 setFailed(); 344 } 345 } 346 347 synchronized (this) 348 { 349 completedThreads++; 350 } 351 long end = System.currentTimeMillis(); 352 log.debug(name + ": did " + i + 353 " iterations in " + (end - start) + "ms, complete=" + completedThreads); 354 } 355 } 356 } 357 358 | Popular Tags |