KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > common > threads > ThreadPoolTest


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2002-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: ThreadPoolTest.java,v 1.1 2004/11/26 01:50:36 tanderson Exp $
44  */

45 package org.exolab.jms.common.threads;
46
47 import java.util.HashSet JavaDoc;
48
49 import junit.framework.Test;
50 import junit.framework.TestCase;
51 import junit.framework.TestSuite;
52
53 import org.apache.commons.logging.Log;
54 import org.apache.commons.logging.LogFactory;
55
56 import org.exolab.jms.common.threads.CompletionListener;
57 import org.exolab.jms.common.threads.ThreadPool;
58
59
60 /**
61  * This class tests the behaviour of the ThreadPool
62  *
63  * @author <a HREF="mailto:mourikis@intalio.com>Jim Mourikis</a>
64  * @author <a HREF="mailto:tima@intalio.com">Tim Anderson</a>
65  * @version $Revision: 1.1 $
66  * @see ThreadPool
67  * @see CompletionListener
68  */

69 public class ThreadPoolTest extends TestCase {
70
71     /**
72      * The logger
73      */

74     private static final Log _log = LogFactory.getLog(ThreadPoolTest.class);
75
76
77     /**
78      * Construct an instance of this class for a specific test case.
79      *
80      * @param name the name of test case
81      */

82     public ThreadPoolTest(String JavaDoc name) {
83         super(name);
84     }
85
86     /**
87      * Sets up the test suite
88      *
89      * @return a test suite
90      */

91     public static Test suite() {
92         return new TestSuite(ThreadPoolTest.class);
93     }
94
95     /**
96      * Test ThreadPool#execute, by creating some Runnable objects and getting
97      * a thread from the pool to execute them.
98      *
99      * @throws Exception for any error
100      */

101     public void testExecute() throws Exception JavaDoc {
102
103         ThreadPool pool = new ThreadPool(3);
104
105         Runnable JavaDoc ra = RunnableHelper.makeRunnable("Worker A", 3000);
106         pool.execute(ra);
107         
108         Runnable JavaDoc rb = RunnableHelper.makeRunnable("Worker B", 1000);
109         pool.execute(rb);
110
111         Runnable JavaDoc rc = RunnableHelper.makeRunnable("Worker C", 2000);
112         pool.execute(rc);
113
114         Runnable JavaDoc rd = RunnableHelper.makeRunnable("Worker D", 6000);
115         pool.execute(rd);
116
117         Runnable JavaDoc re = RunnableHelper.makeRunnable("Worker E", 1000);
118         pool.execute(re);
119
120         Thread.currentThread().sleep(15000);
121         pool.stopRequestAllWorkers();
122     }
123
124     /**
125      * Test ThreadPool#queue, by creating some Runnable some objects,
126      * queueing them, and verifying that they execute
127      *
128      * @throws Exception for any error
129      */

130     public void testQueue() throws Exception JavaDoc {
131         ThreadPool pool = new ThreadPool(3);
132
133         TestListener listener = new TestListener();
134         Runnable JavaDoc ra = RunnableHelper.makeRunnable("Worker A", 3000);
135         listener.addTarget(ra);
136         pool.queue(ra, listener);
137         
138         Runnable JavaDoc rb = RunnableHelper.makeRunnable("Worker B", 1000);
139         listener.addTarget(rb);
140         pool.queue(rb, listener);
141         
142         Runnable JavaDoc rc = RunnableHelper.makeRunnable("Worker C", 2000);
143         listener.addTarget(rc);
144         pool.queue(rc, listener);
145         
146         Runnable JavaDoc rd = RunnableHelper.makeRunnable("Worker D", 6000);
147         listener.addTarget(rd);
148         pool.queue(rd, listener);
149         
150         Runnable JavaDoc re = RunnableHelper.makeRunnable("Worker E", 1000);
151         listener.addTarget(re);
152         pool.queue(re, listener);
153
154         Thread.currentThread().sleep(15000);
155         pool.stopRequestAllWorkers();
156
157         if (listener.getCompleted() != 5) {
158             fail("Only " + listener.getCompleted()
159                  + " workers completed in the allocated time");
160         }
161
162         if (listener.getErrors() != 0) {
163             fail("CompletionListener detected " + listener.getErrors() +
164                  " errors");
165         }
166     }
167
168     private class TestListener implements CompletionListener {
169
170         private HashSet JavaDoc _targets = new HashSet JavaDoc();
171         private HashSet JavaDoc _completed = new HashSet JavaDoc();
172         int _errors = 0;
173
174         public void addTarget(Runnable JavaDoc target) {
175             _targets.add(target);
176         }
177
178         public synchronized void completed(Runnable JavaDoc target) {
179             if (_targets.contains(target)) {
180                 if (!_completed.contains(target)) {
181                     _completed.add(target);
182                     _log.debug(target + " has completed");
183                 } else {
184                     _log.error(target + " has already completed");
185                     ++_errors;
186                 }
187             } else {
188                 _log.error("Target=" + target + " not registered "
189                            + "with the completion listener");
190                 ++_errors;
191             }
192         }
193         
194         public synchronized int getCompleted() {
195             return _completed.size();
196         }
197
198         public synchronized int getErrors() {
199             return _errors;
200         }
201
202     } //-- TestListener
203

204     public static class RunnableHelper {
205
206         /**
207          * A simple method to return a runnable object that can be used
208          * to run on a thread from the pool
209          *
210          * @param name the name of the object for tracking purposes.
211          * @param firstDelay when started, sleep for this long
212          * @return a runnable object
213          */

214         public static Runnable JavaDoc makeRunnable(final String JavaDoc name,
215                                             final long firstDelay) {
216             return new Runnable JavaDoc() {
217                 public void run() {
218                     try {
219                         _log.debug(name + ": starting up");
220                         Thread.sleep(firstDelay);
221                         _log.debug(name + ": doing some stuff");
222                         Thread.sleep(2000);
223                         _log.debug(name + ": leaving");
224                     } catch (InterruptedException JavaDoc exception) {
225                         _log.debug(name + ": got interrupted!");
226                     } catch (Exception JavaDoc exception) {
227                         _log.error(exception.getMessage(), exception);
228                     }
229                 }
230
231                 public String JavaDoc toString() {
232                     return name;
233                 }
234             };
235         }
236     } //-- RunnableHelper
237

238 } //-- ThreadPoolTest
239
Popular Tags