001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.timer;
017:
018: import javax.transaction.TransactionManager;
019:
020: import java.util.concurrent.atomic.AtomicInteger;
021:
022: import junit.framework.TestCase;
023:
024: import org.apache.geronimo.pool.ThreadPool;
025: import org.apache.geronimo.timer.vm.VMWorkerPersistence;
026:
027: /**
028: *
029: *
030: * @version $Rev: 598759 $ $Date: 2007-11-27 12:32:26 -0800 (Tue, 27 Nov 2007) $
031: *
032: * */
033: public abstract class AbstractThreadPooledTimerTest extends TestCase {
034:
035: private static final int COUNT = 20; //should run with much higher counts, but fails sometimes on slow hardware.
036: private static final long DELAY = 1000;
037: private static final long SLOP = 200;
038:
039: private static final String key = "testThreadPooledTimer";
040: private Object userId = null;
041: private ThreadPool threadPool;
042: private ThreadPooledTimer timer;
043:
044: private AtomicInteger counter = new AtomicInteger(0);
045: protected TransactionManager transactionManager;
046: protected ExecutorTaskFactory executableWorkFactory;
047: protected UserTaskFactory userTaskFactory;
048:
049: private Object userKey = "test user info";
050:
051: protected void setUp() throws Exception {
052: userTaskFactory = new MockUserTaskFactory();
053: threadPool = new ThreadPool(30, 30, "TestPool", 10000, this
054: .getClass().getClassLoader(), "foo:bar=baz");
055: WorkerPersistence workerPersistence = new VMWorkerPersistence();
056: timer = new ThreadPooledTimer(executableWorkFactory,
057: workerPersistence, threadPool, transactionManager);
058: timer.doStart();
059:
060: counter.set(0);
061: }
062:
063: protected void tearDown() throws Exception {
064: timer.doStop();
065: threadPool.doStop();
066: }
067:
068: public void testTasks() throws Exception {
069: for (long i = 0; i < COUNT; i++) {
070: timer.schedule(userTaskFactory, key, userId, userKey, i);
071: }
072: Thread.sleep(COUNT + SLOP);
073: assertEquals(COUNT, counter.get());
074: }
075:
076: public void testCancel() throws Exception {
077: WorkInfo[] workInfos = new WorkInfo[COUNT];
078: for (long i = 0; i < COUNT; i++) {
079: workInfos[(int) i] = timer.schedule(userTaskFactory, key,
080: userId, userKey, DELAY);
081: }
082: for (int i = 0; i < workInfos.length; i++) {
083: workInfos[i].getExecutorFeedingTimerTask().cancel();
084: }
085: Thread.sleep(SLOP + DELAY);
086: assertEquals(0, counter.get());
087: }
088:
089: public void testPersistence() throws Exception {
090: for (long i = 0; i < COUNT; i++) {
091: timer
092: .schedule(userTaskFactory, key, userId, userKey,
093: DELAY);
094: }
095: timer.doStop();
096: assertEquals(0, counter.get());
097:
098: timer.doStart();
099: timer.playback(key, userTaskFactory);
100: Thread.sleep(2 * SLOP + DELAY);
101: assertEquals(COUNT, counter.get());
102: }
103:
104: public void testTasksInUnspecifiedTxContext() throws Exception {
105: testTasks();
106: }
107:
108: public void testCancelInUnspecifiedTxContext() throws Exception {
109: testCancel();
110: }
111:
112: public void testPersistenceInUnspecifiedTxContext()
113: throws Exception {
114: testPersistence();
115: }
116:
117: public void testTasksInTransaction() throws Exception {
118: transactionManager.begin();
119: for (long i = 0; i < COUNT; i++) {
120: timer.schedule(userTaskFactory, key, userId, userKey, i);
121: }
122: Thread.sleep(COUNT + SLOP);
123: assertEquals(0, counter.get());
124: transactionManager.commit();
125: Thread.sleep(COUNT + SLOP);
126: assertEquals(COUNT, counter.get());
127: }
128:
129: public void testCancelInCommittedTransaction() throws Exception {
130: Thread.sleep(SLOP + DELAY);
131: WorkInfo[] workInfos = new WorkInfo[COUNT];
132: for (long i = 0; i < COUNT; i++) {
133: workInfos[(int) i] = timer.scheduleAtFixedRate(key,
134: userTaskFactory, userId, userKey, DELAY, DELAY);
135: }
136: Thread.sleep(SLOP + DELAY);
137: assertEquals(COUNT, counter.get());
138: transactionManager.begin();
139: for (int i = 0; i < workInfos.length; i++) {
140: workInfos[i].getExecutorFeedingTimerTask().cancel();
141: }
142: Thread.sleep(SLOP + DELAY);
143: assertEquals(COUNT, counter.get());
144: transactionManager.commit();
145: Thread.sleep(SLOP + DELAY);
146: assertEquals(COUNT, counter.get());
147: }
148:
149: public void testCancelInRolledBackTransaction() throws Exception {
150: Thread.sleep(SLOP + DELAY);
151: WorkInfo[] workInfos = new WorkInfo[COUNT];
152: for (long i = 0; i < COUNT; i++) {
153: workInfos[(int) i] = timer.scheduleAtFixedRate(key,
154: userTaskFactory, userId, userKey, DELAY, DELAY);
155: }
156: Thread.sleep(SLOP + DELAY);
157: assertEquals(COUNT, counter.get());
158: transactionManager.begin();
159: for (int i = 0; i < workInfos.length; i++) {
160: workInfos[i].getExecutorFeedingTimerTask().cancel();
161: }
162: Thread.sleep(SLOP + DELAY);
163: assertEquals(COUNT, counter.get());
164: transactionManager.rollback();
165: Thread.sleep(SLOP + DELAY);
166: // Catches up with two periods.
167: assertEquals(3 * COUNT, counter.get());
168: }
169:
170: public void testRepeatCountFromPersisted() throws Exception {
171: assert DELAY > 2 * SLOP;
172: timer.scheduleAtFixedRate(key, userTaskFactory, userId,
173: userKey, 0L, DELAY);
174: Thread.sleep(4 * DELAY + SLOP);
175: timer.doStop();
176: assertEquals(5, counter.get());
177:
178: timer.doStart();
179: timer.playback(key, userTaskFactory);
180: Thread.sleep(5 * DELAY + SLOP);
181: assertEquals(2 * 5, counter.get());
182:
183: }
184:
185: private class MockUserTaskFactory implements UserTaskFactory {
186: public Runnable newTask(long id) {
187: return new Runnable() {
188: public void run() {
189: counter.incrementAndGet();
190: }
191:
192: };
193: }
194:
195: }
196:
197: }
|