KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > work > PooledWorkManagerTest


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.connector.work;
19
20 import java.lang.reflect.Constructor JavaDoc;
21 import javax.resource.spi.work.ExecutionContext JavaDoc;
22 import javax.resource.spi.work.Work JavaDoc;
23 import javax.resource.spi.work.WorkEvent JavaDoc;
24 import javax.resource.spi.work.WorkException JavaDoc;
25 import javax.resource.spi.work.WorkListener JavaDoc;
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 /**
35  * Timing is crucial for this test case, which focuses on the synchronization
36  * specificities of the doWork, startWork and scheduleWork.
37  *
38  * @version $Rev: 470597 $ $Date: 2006-11-02 18:30:55 -0500 (Thu, 02 Nov 2006) $
39  */

40 public class PooledWorkManagerTest extends TestSupport {
41
42     private GeronimoWorkManager workManager;
43
44     protected void setUp() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
113         testDoWork();
114         workManager.doStop();
115         workManager.doStart();
116         testDoWork();
117     }
118
119     private AbstractDummyWork[] helperTest(Class JavaDoc aWork, int nbThreads,
120                                            int aTimeOut, int aTempo)
121         throws Exception JavaDoc {
122         Constructor JavaDoc constructor = aWork.getConstructor(
123             new Class JavaDoc[]{PooledWorkManagerTest.class, String JavaDoc.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 JavaDoc[]{this, "Work" + i,
130                         new Integer JavaDoc(aTimeOut), new Integer JavaDoc(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 JavaDoc {
142         public final DummyWorkListener listener;
143         protected final String JavaDoc name;
144         private final int timeout;
145         private final int tempo;
146         public AbstractDummyWork(String JavaDoc 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 JavaDoc e) {
156                 log.debug(e.getMessage(), e);
157             }
158         }
159
160         protected abstract void perform(Work JavaDoc work,
161                                         long startTimeout,
162                                         ExecutionContext JavaDoc execContext,
163                                         WorkListener JavaDoc workListener) throws Exception JavaDoc;
164     }
165
166     public class DummyDoWork extends AbstractDummyWork {
167         public DummyDoWork(String JavaDoc aName, int aTimeOut, int aTempo) {
168             super(aName, aTimeOut, aTempo);
169         }
170
171         protected void perform(Work JavaDoc work,
172                                long startTimeout,
173                                ExecutionContext JavaDoc execContext,
174                                WorkListener JavaDoc workListener) throws Exception JavaDoc {
175             workManager.doWork(work, startTimeout, execContext, workListener);
176         }
177     }
178
179     public class DummyStartWork extends AbstractDummyWork {
180         public DummyStartWork(String JavaDoc aName, int aTimeOut, int aTempo) {
181             super(aName, aTimeOut, aTempo);
182         }
183
184         protected void perform(Work JavaDoc work,
185                                long startTimeout,
186                                ExecutionContext JavaDoc execContext,
187                                WorkListener JavaDoc workListener) throws Exception JavaDoc {
188             workManager.startWork(work, startTimeout, execContext, workListener);
189         }
190     }
191
192     public class DummyScheduleWork extends AbstractDummyWork {
193         public DummyScheduleWork(String JavaDoc aName, int aTimeOut, int aTempo) {
194             super(aName, aTimeOut, aTempo);
195         }
196
197         protected void perform(Work JavaDoc work,
198                                long startTimeout,
199                                ExecutionContext JavaDoc execContext,
200                                WorkListener JavaDoc workListener) throws Exception JavaDoc {
201             workManager.scheduleWork(work, startTimeout, execContext, workListener);
202         }
203     }
204
205     public static class DummyWork implements Work JavaDoc {
206         private Log log = LogFactory.getLog(getClass());
207
208         private final String JavaDoc name;
209         private final int tempo;
210
211         public DummyWork(String JavaDoc 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 JavaDoc e) {
223                 log.debug(e.getMessage(), e);
224             }
225         }
226
227         public String JavaDoc toString() {
228             return name;
229         }
230     }
231
232     public static class DummyWorkListener implements WorkListener JavaDoc {
233         private Log log = LogFactory.getLog(getClass());
234
235         public WorkEvent JavaDoc acceptedEvent;
236         public WorkEvent JavaDoc rejectedEvent;
237         public WorkEvent JavaDoc startedEvent;
238         public WorkEvent JavaDoc completedEvent;
239
240         public void workAccepted(WorkEvent JavaDoc e) {
241             acceptedEvent = e;
242             log.debug("accepted: " + e);
243         }
244
245         public void workRejected(WorkEvent JavaDoc e) {
246             rejectedEvent = e;
247             log.debug("rejected: " + e);
248         }
249
250         public void workStarted(WorkEvent JavaDoc e) {
251             startedEvent = e;
252             log.debug("started: " + e);
253         }
254
255         public void workCompleted(WorkEvent JavaDoc e) {
256             completedEvent = e;
257             log.debug("completed: " + e);
258         }
259     }
260 }
261
Popular Tags