1 17 18 package org.apache.geronimo.timer; 19 20 import javax.transaction.TransactionManager ; 21 22 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger; 23 24 import junit.framework.TestCase; 25 26 import org.apache.geronimo.pool.ThreadPool; 27 import org.apache.geronimo.timer.vm.VMWorkerPersistence; 28 29 35 public abstract class AbstractThreadPooledTimerTest extends TestCase { 36 37 private static final int COUNT = 20; private static final long DELAY = 1000; 39 private static final long SLOP = 200; 40 41 private static final String key = "testThreadPooledTimer"; 42 private Object userId = null; 43 private ThreadPool threadPool; 44 private ThreadPooledTimer timer; 45 46 private AtomicInteger counter = new AtomicInteger(0); 47 protected TransactionManager transactionManager; 48 protected ExecutorTaskFactory executableWorkFactory; 49 protected UserTaskFactory userTaskFactory; 50 51 private Object userKey = "test user info"; 52 53 protected void setUp() throws Exception { 54 userTaskFactory = new MockUserTaskFactory(); 55 threadPool = new ThreadPool(30, "TestPool", 10000, this.getClass().getClassLoader(), "foo:bar=baz"); 56 WorkerPersistence workerPersistence = new VMWorkerPersistence(); 57 timer = new ThreadPooledTimer(executableWorkFactory, workerPersistence, threadPool, transactionManager); 58 timer.doStart(); 59 60 counter.set(0); 61 } 62 63 protected void tearDown() throws Exception { 64 timer.doStop(); 65 threadPool.doStop(); 66 } 67 68 public void testTasks() throws Exception { 69 for (long i = 0; i < COUNT; i++) { 70 timer.schedule(userTaskFactory, key, userId, userKey, i); 71 } 72 Thread.sleep(COUNT + SLOP); 73 assertEquals(COUNT, counter.get()); 74 } 75 76 public void testCancel() throws Exception { 77 WorkInfo[] workInfos = new WorkInfo[COUNT]; 78 for (long i = 0; i < COUNT; i++) { 79 workInfos[(int) i] = timer.schedule(userTaskFactory, key, userId, userKey, DELAY); 80 } 81 for (int i = 0; i < workInfos.length; i++) { 82 workInfos[i].getExecutorFeedingTimerTask().cancel(); 83 } 84 Thread.sleep(SLOP + DELAY); 85 assertEquals(0, counter.get()); 86 } 87 88 public void testPersistence() throws Exception { 89 for (long i = 0; i < COUNT; i++) { 90 timer.schedule(userTaskFactory, key, userId, userKey, DELAY); 91 } 92 timer.doStop(); 93 assertEquals(0, counter.get()); 94 95 timer.doStart(); 96 timer.playback(key, userTaskFactory); 97 Thread.sleep(2 * SLOP + DELAY); 98 assertEquals(COUNT, counter.get()); 99 } 100 101 public void testTasksInUnspecifiedTxContext() throws Exception { 102 testTasks(); 103 } 104 105 public void testCancelInUnspecifiedTxContext() throws Exception { 106 testCancel(); 107 } 108 109 public void testPersistenceInUnspecifiedTxContext() throws Exception { 110 testPersistence(); 111 } 112 113 public void testTasksInTransaction() throws Exception { 114 transactionManager.begin(); 115 for (long i = 0; i < COUNT; i++) { 116 timer.schedule(userTaskFactory, key, userId, userKey, i); 117 } 118 Thread.sleep(COUNT + SLOP); 119 assertEquals(0, counter.get()); 120 transactionManager.commit(); 121 Thread.sleep(COUNT + SLOP); 122 assertEquals(COUNT, counter.get()); 123 } 124 125 public void testCancelInCommittedTransaction() throws Exception { 126 Thread.sleep(SLOP + DELAY); 127 WorkInfo[] workInfos = new WorkInfo[COUNT]; 128 for (long i = 0; i < COUNT; i++) { 129 workInfos[(int) i] = timer.scheduleAtFixedRate(key, userTaskFactory, userId, userKey, DELAY, DELAY); 130 } 131 Thread.sleep(SLOP + DELAY); 132 assertEquals(COUNT, counter.get()); 133 transactionManager.begin(); 134 for (int i = 0; i < workInfos.length; i++) { 135 workInfos[i].getExecutorFeedingTimerTask().cancel(); 136 } 137 Thread.sleep(SLOP + DELAY); 138 assertEquals(COUNT, counter.get()); 139 transactionManager.commit(); 140 Thread.sleep(SLOP + DELAY); 141 assertEquals(COUNT, counter.get()); 142 } 143 144 public void testCancelInRolledBackTransaction() throws Exception { 145 Thread.sleep(SLOP + DELAY); 146 WorkInfo[] workInfos = new WorkInfo[COUNT]; 147 for (long i = 0; i < COUNT; i++) { 148 workInfos[(int) i] = timer.scheduleAtFixedRate(key, userTaskFactory, userId, userKey, DELAY, DELAY); 149 } 150 Thread.sleep(SLOP + DELAY); 151 assertEquals(COUNT, counter.get()); 152 transactionManager.begin(); 153 for (int i = 0; i < workInfos.length; i++) { 154 workInfos[i].getExecutorFeedingTimerTask().cancel(); 155 } 156 Thread.sleep(SLOP + DELAY); 157 assertEquals(COUNT, counter.get()); 158 transactionManager.rollback(); 159 Thread.sleep(SLOP + DELAY); 160 assertEquals(3 * COUNT, counter.get()); 162 } 163 164 public void testRepeatCountFromPersisted() throws Exception { 165 assert DELAY > 2 * SLOP; 166 timer.scheduleAtFixedRate(key, userTaskFactory, userId, userKey, 0L, DELAY); 167 Thread.sleep(4 * DELAY + SLOP); 168 timer.doStop(); 169 assertEquals(5, counter.get()); 170 171 timer.doStart(); 172 timer.playback(key, userTaskFactory); 173 Thread.sleep(5 * DELAY + SLOP); 174 assertEquals(2 * 5, counter.get()); 175 176 } 177 178 private class MockUserTaskFactory implements UserTaskFactory { 179 public Runnable newTask(long id) { 180 return new Runnable () { 181 public void run() { 182 counter.incrementAndGet(); 183 } 184 185 }; 186 } 187 188 } 189 190 } 191 | Popular Tags |