1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.objectledge.threads.impl;
29
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import junit.framework.TestCase;
34
35 import org.jcontainer.dna.Logger;
36 import org.jcontainer.dna.impl.Log4JLogger;
37 import org.objectledge.context.Context;
38 import org.objectledge.pipeline.ProcessingException;
39 import org.objectledge.pipeline.Valve;
40 import org.objectledge.threads.Task;
41
42 /***
43 *
44 * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
45 * @version $Id: WorkerTest.java,v 1.3 2005/01/28 02:52:07 rafal Exp $
46 */
47 public class WorkerTest extends TestCase
48 {
49 private static final int DELAY = 200;
50
51 private static Map whiteboard = new HashMap();
52
53 private static Logger log =
54 new Log4JLogger(org.apache.log4j.Logger.getLogger(WorkerTest.class));
55
56 /***
57 * Constructor for WorkerTest.
58 * @param arg0
59 */
60 public WorkerTest(String arg0)
61 {
62 super(arg0);
63 }
64
65 public void testShortRunning()
66 throws Exception
67 {
68 synchronized(whiteboard)
69 {
70 Task task = new ShortRunningTask();
71 int priority = Thread.NORM_PRIORITY;
72 ThreadGroup threadGroup = new ThreadGroup("Test group");
73 Context context = new Context();
74 whiteboard.clear();
75 Valve cleanup = new CleanupValve();
76 Worker worker = new Worker("testShortRunning", priority,
77 null, threadGroup, log, context, cleanup);
78 worker.dispatch(task);
79 Thread.sleep(DELAY);
80 worker.stop();
81 Thread.sleep(DELAY);
82 assertNotNull(whiteboard.get("started"));
83 assertNull(whiteboard.get("interrupted"));
84 assertNotNull(whiteboard.get("cleaned up"));
85 }
86 }
87
88 public void testLongRunning()
89 throws Exception
90 {
91 synchronized(whiteboard)
92 {
93 Task task = new LongRunningTask();
94 int priority = Thread.NORM_PRIORITY;
95 ThreadGroup threadGroup = new ThreadGroup("Test group");
96 Context context = new Context();
97 whiteboard.clear();
98 Valve cleanup = new CleanupValve();
99 Worker worker = new Worker("testLongRunning", priority,
100 null, threadGroup, log, context, cleanup);
101 worker.dispatch(task);
102 worker.getCurrentTask();
103 Thread.sleep(DELAY);
104 worker.stop();
105 Thread.sleep(DELAY);
106 assertNotNull(whiteboard.get("started"));
107 assertNotNull(whiteboard.get("interrupted"));
108 assertNotNull(whiteboard.get("cleaned up"));
109 }
110 }
111
112 public void testFailing()
113 throws Exception
114 {
115 synchronized(whiteboard)
116 {
117 Task task = new FailingTask();
118 int priority = Thread.NORM_PRIORITY;
119 ThreadGroup threadGroup = new ThreadGroup("Test group");
120 Context context = new Context();
121 whiteboard.clear();
122 Valve cleanup = new CleanupValve();
123 Worker worker = new Worker("testFailing", priority,
124 null, threadGroup, log, context, cleanup);
125 worker.dispatch(task);
126 Thread.sleep(DELAY);
127 worker.stop();
128 Thread.sleep(DELAY);
129 assertNotNull(whiteboard.get("started"));
130 assertNull(whiteboard.get("interrupted"));
131 assertNotNull(whiteboard.get("cleaned up"));
132 }
133 }
134
135 public void testIdleStop()
136 throws Exception
137 {
138 synchronized(whiteboard)
139 {
140 int priority = Thread.NORM_PRIORITY;
141 ThreadGroup threadGroup = new ThreadGroup("Test group");
142 Context context = new Context();
143 whiteboard.clear();
144 Valve cleanup = new CleanupValve();
145 Worker worker = new Worker("testLongRunning", priority,
146 null, threadGroup, log, context, cleanup);
147 Thread.sleep(DELAY);
148 worker.stop();
149 }
150 }
151
152 public void testFailedCleanup()
153 throws Exception
154 {
155 synchronized(whiteboard)
156 {
157 Task task = new LongRunningTask();
158 int priority = Thread.NORM_PRIORITY;
159 ThreadGroup threadGroup = new ThreadGroup("Test group");
160 Context context = new Context();
161 whiteboard.clear();
162 Valve cleanup = new FailingCleanupValve();
163 Worker worker = new Worker("testFailedCleanup", priority,
164 null, threadGroup, log, context, cleanup);
165 worker.dispatch(task);
166 Thread.sleep(DELAY);
167 worker.stop();
168 Thread.sleep(DELAY);
169 assertNotNull(whiteboard.get("started"));
170 assertNotNull(whiteboard.get("interrupted"));
171 assertNull(whiteboard.get("cleaned up"));
172 }
173 }
174
175
176
177 private class LongRunningTask
178 extends Task
179 {
180 public synchronized void process(Context context)
181 {
182 try
183 {
184 whiteboard.put("started", "yes");
185 this.wait();
186 }
187 catch(InterruptedException e)
188 {
189 whiteboard.put("interrupted", "yes");
190 return;
191 }
192 }
193 }
194
195 private class ShortRunningTask
196 extends Task
197 {
198 public void process(Context context)
199 {
200 whiteboard.put("started", "yes");
201 }
202 }
203
204 private class FailingTask
205 extends Task
206 {
207 public void process(Context context)
208 throws ProcessingException
209 {
210 whiteboard.put("started", "yes");
211 throw new ProcessingException("processing failed");
212 }
213 }
214
215 private class CleanupValve
216 implements Valve
217 {
218 public void process(Context context)
219 {
220 whiteboard.put("cleaned up", "yes");
221 }
222 }
223
224 private class FailingCleanupValve
225 implements Valve
226 {
227 public void process(Context context)
228 throws ProcessingException
229 {
230 throw new ProcessingException("failed cleanup");
231 }
232 }
233 }