KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > timer > AbstractThreadPooledTimerTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.timer;
19
20 import javax.transaction.TransactionManager JavaDoc;
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 /**
30  *
31  *
32  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
33  *
34  * */

35 public abstract class AbstractThreadPooledTimerTest extends TestCase {
36
37     private static final int COUNT = 20; //should run with much higher counts, but fails sometimes on slow hardware.
38
private static final long DELAY = 1000;
39     private static final long SLOP = 200;
40
41     private static final String JavaDoc key = "testThreadPooledTimer";
42     private Object JavaDoc userId = null;
43     private ThreadPool threadPool;
44     private ThreadPooledTimer timer;
45
46     private AtomicInteger counter = new AtomicInteger(0);
47     protected TransactionManager JavaDoc transactionManager;
48     protected ExecutorTaskFactory executableWorkFactory;
49     protected UserTaskFactory userTaskFactory;
50
51     private Object JavaDoc userKey = "test user info";
52
53     protected void setUp() throws Exception JavaDoc {
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 JavaDoc {
64         timer.doStop();
65         threadPool.doStop();
66     }
67
68     public void testTasks() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
102         testTasks();
103     }
104
105     public void testCancelInUnspecifiedTxContext() throws Exception JavaDoc {
106         testCancel();
107     }
108
109     public void testPersistenceInUnspecifiedTxContext() throws Exception JavaDoc {
110         testPersistence();
111     }
112
113     public void testTasksInTransaction() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
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         // Catches up with two periods.
161
assertEquals(3 * COUNT, counter.get());
162     }
163
164     public void testRepeatCountFromPersisted() throws Exception JavaDoc {
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 JavaDoc newTask(long id) {
180             return new Runnable JavaDoc() {
181                 public void run() {
182                     counter.incrementAndGet();
183                 }
184
185             };
186         }
187
188     }
189
190 }
191
Popular Tags