1 22 package org.jboss.test.jca.test; 23 24 import java.util.ArrayList ; 25 26 import org.jboss.test.JBossTestCase; 27 28 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt; 29 30 36 public class AbstractConcurrentStressTest extends JBossTestCase 37 { 38 private ArrayList done = new ArrayList (); 39 private int total; 40 private Throwable failed = null; 41 private SynchronizedInt nextId = new SynchronizedInt(0); 42 43 public interface ConcurrentTestCallback 44 { 45 void finished() throws Throwable ; 46 } 47 48 public void runConcurrentTest(ConcurrentRunnable[] runnables, ConcurrentTestCallback callback) throws Throwable 49 { 50 total = runnables.length; 51 Thread [] threads = new Thread [total]; 52 for (int i = 0; i < total; ++i) 53 { 54 threads[i] = new Thread (runnables[i], getName() + "-" + runnables[i].id); 55 threads[i].start(); 56 } 57 for (int i = 0; i < total; ++i) 58 threads[i].join(); 59 60 if (callback != null) 61 callback.finished(); 62 63 if (failed != null) 64 throw failed; 65 66 for (int i = 0; i < total; ++i) 67 runnables[i].doCheck(); 68 } 69 70 public abstract class ConcurrentRunnable implements Runnable 71 { 72 private Throwable failure; 73 protected int id; 74 75 public ConcurrentRunnable() 76 { 77 id = nextId.increment(); 78 } 79 80 public abstract void doStart() throws Throwable ; 81 public abstract void doRun() throws Throwable ; 82 public abstract void doEnd() throws Throwable ; 83 public synchronized void setFailure(Throwable failure) 84 { 85 if (failure != null) 86 { 87 this.failure = failure; 88 failed(failure); 89 log.error("Error in " + this, failure); 90 } 91 if (failure instanceof RuntimeException ) 92 throw (RuntimeException ) failure; 93 else if (failure instanceof Error ) 94 throw (Error ) failure; 95 throw new RuntimeException (failure); 96 } 97 public void doCheck() throws Throwable 98 { 99 if (failure != null) 100 throw failure; 101 } 102 103 public void run() 104 { 105 try 106 { 107 doStart(); 108 } 109 catch (Throwable t) 110 { 111 setFailure(t); 112 } 113 waitDone(); 114 for (int i =0; i < getIterationCount(); ++i) 115 { 116 try 117 { 118 doRun(); 119 } 120 catch (Throwable t) 121 { 122 setFailure(t); 123 break; 124 } 125 } 126 waitDone(); 127 try 128 { 129 doEnd(); 130 } 131 catch (Throwable t) 132 { 133 setFailure(t); 134 } 135 waitDone(); 136 } 137 } 138 139 protected synchronized void failed(Throwable failure) 140 { 141 if (failed == null) 142 failed = failure; 143 for (int i = 0; i < done.size(); ++i) 144 done.get(i).notify(); 145 done.clear(); 146 } 147 148 protected synchronized void waitDone() 149 { 150 if (failed != null) 151 return; 152 if (done.size() < total - 1) 153 { 154 done.add(this); 155 doWait(); 156 } 157 else 158 { 159 for (int i = 0; i < done.size(); ++i) 160 done.get(i).notify(); 161 done.clear(); 162 } 163 } 164 165 protected void doWait() 166 { 167 boolean interrupted = false; 168 try 169 { 170 while (failed == null) 171 { 172 try 173 { 174 wait(); 175 return; 176 } 177 catch (InterruptedException e) 178 { 179 interrupted = true; 180 } 181 } 182 } 183 finally 184 { 185 if (interrupted) 186 Thread.currentThread().interrupt(); 187 } 188 189 } 190 191 public AbstractConcurrentStressTest(String name) 192 { 193 super(name); 194 } 195 } 196 | Popular Tags |