KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > threadpool > ThreadQueueTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.util.threadpool;
38
39 import junit.framework.TestCase;
40
41 /**
42  * @author Jared Richardson
43  * <p/>
44  * JUnit test class to work on net.sourceforge.cruisecontrol.util.ThreadQueue
45  */

46 public class ThreadQueueTest extends TestCase {
47     private static final String JavaDoc TASK_NAME = "TASK:";
48     private static final int TASK_COUNT = 5;
49     private static final int TENTH_OF_SECOND = 100;
50     
51     protected void setUp() throws Exception JavaDoc {
52         for (int i = 1; i < TASK_COUNT + 1; i++) {
53             final String JavaDoc taskName = TASK_NAME + i;
54
55             IdleThreadQueueClient task = new IdleThreadQueueClient(taskName);
56             ThreadQueue.addTask(task);
57             assertEquals(i, ThreadQueue.numTotalTasks());
58         }
59
60         sleep(TENTH_OF_SECOND);
61     }
62
63     protected void tearDown() {
64         ThreadQueue.terminate();
65     }
66     
67     public void testIsIdle() throws Exception JavaDoc {
68         assertFalse(ThreadQueue.isIdle(TASK_NAME + 1));
69         assertTrue(ThreadQueue.isIdle(TASK_NAME + 2));
70         assertTrue(ThreadQueue.isIdle(TASK_NAME + 3));
71
72         tasksThatCompleteShouldNotBeIdle();
73
74         caseOfNameShouldNotMatter();
75
76         tasksThatDontExistShouldNotBeIdle();
77     }
78
79     private void tasksThatCompleteShouldNotBeIdle() {
80         ThreadQueue.waitFor(TASK_NAME + 2);
81         assertFalse(ThreadQueue.isIdle(TASK_NAME + 2));
82     }
83
84     private void caseOfNameShouldNotMatter() {
85         String JavaDoc taskName = TASK_NAME + TASK_COUNT;
86         assertTrue(ThreadQueue.isIdle(taskName.toLowerCase()));
87     }
88
89     private void tasksThatDontExistShouldNotBeIdle() {
90         assertFalse(ThreadQueue.isIdle(TASK_NAME + 42));
91     }
92     
93     public void testInterrupt() throws Exception JavaDoc {
94         assertFalse(ThreadQueue.isIdle(TASK_NAME + 1));
95         ThreadQueue.interrupt(TASK_NAME + 1);
96         assertInterrupted(TASK_NAME + 1);
97         
98         assertTrue(ThreadQueue.isIdle(TASK_NAME + TASK_COUNT));
99         ThreadQueue.interrupt(TASK_NAME + TASK_COUNT);
100         assertInterrupted(TASK_NAME + TASK_COUNT);
101     }
102     
103     public void testExecution() {
104         verifyCountOfRunningAndIdleTasksCorrect();
105
106         for (int i = 1; i < TASK_COUNT + 1; i++) {
107             String JavaDoc taskName = TASK_NAME + i;
108             assertTrue(ThreadQueue.taskExists(taskName));
109             assertTrue(ThreadQueue.isActive(taskName));
110         }
111
112         // now let them all finish
113
ThreadQueue.waitForAll();
114
115         assertEquals(0, ThreadQueue.numRunningTasks());
116         assertEquals(0, ThreadQueue.numWaitingTasks());
117
118         for (int i = 1; i < TASK_COUNT + 1; i++) {
119             String JavaDoc taskName = TASK_NAME + i;
120
121             assertTrue(ThreadQueue.taskExists(taskName));
122             assertFalse(ThreadQueue.isActive(taskName));
123             
124             // check the return values of all the worker threads
125
Object JavaDoc rawResult = ThreadQueue.getResult(taskName);
126             assertTrue(rawResult instanceof String JavaDoc);
127             assertEquals("DONE WITH " + taskName, (String JavaDoc) rawResult);
128         }
129     }
130
131     private void verifyCountOfRunningAndIdleTasksCorrect() {
132         // check that the number of running tasks is the same number as we have
133
// worker threads available. Be sure that every available worker thread
134
// is in use
135
int numRunningTasks = ThreadQueue.numRunningTasks();
136         int numWorkerThreads = ThreadQueue.getMaxNumWorkerThreads();
137         assertEquals(numWorkerThreads, numRunningTasks);
138
139         // make sure the correct number of idle tasks are idle
140
// the waiting number should be the total number of worker tasks less the
141
// number of threads
142
int numThatShouldBeWaiting = TASK_COUNT - ThreadQueue.getMaxNumWorkerThreads();
143         // unless the overall number of worker tasks is less than the number of threads
144
if (ThreadQueue.getMaxNumWorkerThreads() > TASK_COUNT) {
145             numThatShouldBeWaiting = 0;
146         }
147         assertEquals(numThatShouldBeWaiting, ThreadQueue.numWaitingTasks());
148     }
149
150     private static void sleep(int ms) {
151         try {
152             Thread.sleep(ms);
153         } catch (Exception JavaDoc e) {
154         }
155     }
156     
157     private static void assertInterrupted(String JavaDoc taskName) {
158         assertFalse(ThreadQueue.isActive(taskName));
159         assertFalse(ThreadQueue.isDone(taskName));
160         assertFalse(ThreadQueue.isIdle(taskName));
161     }
162
163 }
164
Popular Tags