1   // 
2   // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   // 
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //  
8   // * Redistributions of source code must retain the above copyright notice,  
9   //	 this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //	 this list of conditions and the following disclaimer in the documentation  
12  //	 and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //	 nor the names of its contributors may be used to endorse or promote products  
15  //	 derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
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     // tasks & cleanup valves ///////////////////////////////////////////////////////////////////
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 }