1 17 18 package org.apache.geronimo.connector.work; 19 20 import java.lang.reflect.Constructor ; 21 import javax.resource.spi.work.ExecutionContext ; 22 import javax.resource.spi.work.Work ; 23 import javax.resource.spi.work.WorkEvent ; 24 import javax.resource.spi.work.WorkException ; 25 import javax.resource.spi.work.WorkListener ; 26 27 import org.apache.geronimo.pool.ThreadPool; 28 import org.apache.geronimo.transaction.manager.GeronimoTransactionManager; 29 import org.apache.geronimo.transaction.manager.XAWork; 30 import org.apache.geronimo.testsupport.TestSupport; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 40 public class PooledWorkManagerTest extends TestSupport { 41 42 private GeronimoWorkManager workManager; 43 44 protected void setUp() throws Exception { 45 super.setUp(); 46 47 XAWork xaWork = new GeronimoTransactionManager(); 48 ThreadPool pool = new ThreadPool(1, "Connector Test", 30000, ThreadPool.class.getClassLoader(), "foo:test=bar"); 49 pool.setWaitWhenBlocked(true); 50 51 workManager = new GeronimoWorkManager(pool, pool, pool, xaWork); 52 workManager.doStart(); 53 } 54 55 public void testDoWork() throws Exception { 56 int nbThreads = 2; 57 AbstractDummyWork threads[] = helperTest(DummyDoWork.class, nbThreads, 500, 600); 58 int nbStopped = 0; 59 int nbTimeout = 0; 60 for (int i = 0; i < threads.length; i++) { 61 if ( null != threads[i].listener.completedEvent ) { 62 nbStopped++; 63 } else if ( null != threads[i].listener.rejectedEvent ) { 64 assertTrue("Should be a time out exception.", 65 threads[i].listener.rejectedEvent.getException(). 66 getErrorCode() == WorkException.START_TIMED_OUT); 67 nbTimeout++; 68 } else { 69 fail("WORK_COMPLETED or WORK_REJECTED expected"); 70 } 71 } 72 assertEquals("Wrong number of works in the WORK_COMPLETED state", 1, nbStopped); 73 assertEquals("Wrong number of works in the START_TIMED_OUT state", 1, nbTimeout); 74 } 75 76 public void testStartWork() throws Exception { 77 AbstractDummyWork threads[] = helperTest(DummyStartWork.class, 2, 10000, 100); 78 int nbStopped = 0; 79 int nbStarted = 0; 80 for (int i = 0; i < threads.length; i++) { 81 if ( null != threads[i].listener.completedEvent ) { 82 nbStopped++; 83 } else if ( null != threads[i].listener.startedEvent ) { 84 nbStarted++; 85 } else { 86 fail("WORK_COMPLETED or WORK_STARTED expected"); 87 } 88 } 89 assertEquals("At least one work should be in the WORK_COMPLETED state.", 1, nbStopped); 90 assertEquals("At least one work should be in the WORK_STARTED state.", 1, nbStarted); 91 } 92 93 public void testScheduleWork() throws Exception { 94 AbstractDummyWork threads[] = 95 helperTest(DummyScheduleWork.class, 3, 10000, 100); 96 int nbAccepted = 0; 97 int nbStarted = 0; 98 99 for (int i = 0; i < threads.length; i++) { 100 if ( null != threads[i].listener.acceptedEvent ) { 101 nbAccepted++; 102 } else if ( null != threads[i].listener.startedEvent ) { 103 nbStarted++; 104 } else { 105 fail("WORK_ACCEPTED or WORK_STARTED expected"); 106 } 107 } 108 109 assertTrue("At least one work should be in the WORK_ACCEPTED state.", nbAccepted > 0); 110 } 111 112 public void testLifecycle() throws Exception { 113 testDoWork(); 114 workManager.doStop(); 115 workManager.doStart(); 116 testDoWork(); 117 } 118 119 private AbstractDummyWork[] helperTest(Class aWork, int nbThreads, 120 int aTimeOut, int aTempo) 121 throws Exception { 122 Constructor constructor = aWork.getConstructor( 123 new Class []{PooledWorkManagerTest.class, String .class, 124 int.class, int.class}); 125 AbstractDummyWork rarThreads[] = new AbstractDummyWork[nbThreads]; 126 for (int i = 0; i < nbThreads; i++) { 127 rarThreads[i] = (AbstractDummyWork) 128 constructor.newInstance( 129 new Object []{this, "Work" + i, 130 new Integer (aTimeOut), new Integer (aTempo)}); 131 } 132 for (int i = 0; i < nbThreads; i++) { 133 rarThreads[i].start(); 134 } 135 for (int i = 0; i < nbThreads; i++) { 136 rarThreads[i].join(); 137 } 138 return rarThreads; 139 } 140 141 public abstract class AbstractDummyWork extends Thread { 142 public final DummyWorkListener listener; 143 protected final String name; 144 private final int timeout; 145 private final int tempo; 146 public AbstractDummyWork(String aName, int aTimeOut, int aTempo) { 147 listener = new DummyWorkListener(); 148 timeout = aTimeOut; 149 tempo = aTempo; 150 name = aName; 151 } 152 public void run() { 153 try { 154 perform(new DummyWork(name, tempo), timeout, null, listener); 155 } catch (Exception e) { 156 log.debug(e.getMessage(), e); 157 } 158 } 159 160 protected abstract void perform(Work work, 161 long startTimeout, 162 ExecutionContext execContext, 163 WorkListener workListener) throws Exception ; 164 } 165 166 public class DummyDoWork extends AbstractDummyWork { 167 public DummyDoWork(String aName, int aTimeOut, int aTempo) { 168 super(aName, aTimeOut, aTempo); 169 } 170 171 protected void perform(Work work, 172 long startTimeout, 173 ExecutionContext execContext, 174 WorkListener workListener) throws Exception { 175 workManager.doWork(work, startTimeout, execContext, workListener); 176 } 177 } 178 179 public class DummyStartWork extends AbstractDummyWork { 180 public DummyStartWork(String aName, int aTimeOut, int aTempo) { 181 super(aName, aTimeOut, aTempo); 182 } 183 184 protected void perform(Work work, 185 long startTimeout, 186 ExecutionContext execContext, 187 WorkListener workListener) throws Exception { 188 workManager.startWork(work, startTimeout, execContext, workListener); 189 } 190 } 191 192 public class DummyScheduleWork extends AbstractDummyWork { 193 public DummyScheduleWork(String aName, int aTimeOut, int aTempo) { 194 super(aName, aTimeOut, aTempo); 195 } 196 197 protected void perform(Work work, 198 long startTimeout, 199 ExecutionContext execContext, 200 WorkListener workListener) throws Exception { 201 workManager.scheduleWork(work, startTimeout, execContext, workListener); 202 } 203 } 204 205 public static class DummyWork implements Work { 206 private Log log = LogFactory.getLog(getClass()); 207 208 private final String name; 209 private final int tempo; 210 211 public DummyWork(String aName, int aTempo) { 212 name = aName; 213 tempo = aTempo; 214 } 215 216 public void release() { 217 } 218 219 public void run() { 220 try { 221 Thread.sleep(tempo); 222 } catch (InterruptedException e) { 223 log.debug(e.getMessage(), e); 224 } 225 } 226 227 public String toString() { 228 return name; 229 } 230 } 231 232 public static class DummyWorkListener implements WorkListener { 233 private Log log = LogFactory.getLog(getClass()); 234 235 public WorkEvent acceptedEvent; 236 public WorkEvent rejectedEvent; 237 public WorkEvent startedEvent; 238 public WorkEvent completedEvent; 239 240 public void workAccepted(WorkEvent e) { 241 acceptedEvent = e; 242 log.debug("accepted: " + e); 243 } 244 245 public void workRejected(WorkEvent e) { 246 rejectedEvent = e; 247 log.debug("rejected: " + e); 248 } 249 250 public void workStarted(WorkEvent e) { 251 startedEvent = e; 252 log.debug("started: " + e); 253 } 254 255 public void workCompleted(WorkEvent e) { 256 completedEvent = e; 257 log.debug("completed: " + e); 258 } 259 } 260 } 261 | Popular Tags |