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: DaemonTest.java,v 1.5 2005/01/28 02:52:07 rafal Exp $
46   */
47  public class DaemonTest 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(DaemonTest.class));
55  
56      /***
57       * Constructor for DaemonTest.
58       * @param arg0
59       */
60      public DaemonTest(String arg0)
61      {
62          super(arg0);
63      }
64  
65      public void testLongRunning()
66          throws Exception
67      {
68          synchronized(whiteboard)
69          {
70              Task task = new LongRunningTask();
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              Daemon daemon = new Daemon(task, priority, threadGroup, log, context, cleanup);
77              Thread.sleep(DELAY);
78              daemon.stop();
79              Thread.sleep(DELAY);
80              assertNotNull(whiteboard.get("started"));
81              assertNotNull(whiteboard.get("interrupted"));
82              assertNotNull(whiteboard.get("cleaned up"));
83          }
84      }
85      
86      public void testShortRunning()
87          throws Exception
88      {
89          synchronized(whiteboard)
90          {
91              Task task = new ShortRunningTask();
92              int priority = Thread.NORM_PRIORITY;
93              ThreadGroup threadGroup = new ThreadGroup("Test group");
94              Context context = new Context();
95              whiteboard.clear();
96              Valve cleanup = new CleanupValve();
97              Daemon daemon = new Daemon(task, priority, threadGroup, log, context, cleanup);
98              Thread.sleep(DELAY);
99              daemon.stop();
100             Thread.sleep(DELAY);
101             assertNotNull(whiteboard.get("started"));
102             assertNull(whiteboard.get("interrupted"));
103             assertNotNull(whiteboard.get("cleaned up"));
104         }
105     }
106     
107     public void testFailing()
108         throws Exception
109     {
110         synchronized(whiteboard)
111         {
112             Task task = new FailingTask();
113             int priority = Thread.NORM_PRIORITY;
114             ThreadGroup threadGroup = new ThreadGroup("Test group");
115             Context context = new Context();
116             whiteboard.clear();
117             Valve cleanup = new CleanupValve();
118             Daemon daemon = new Daemon(task, priority, threadGroup, log, context, cleanup);
119             Thread.sleep(DELAY);
120             daemon.stop();
121             Thread.sleep(DELAY);
122             assertNotNull(whiteboard.get("started"));
123             assertNull(whiteboard.get("interrupted"));
124             assertNotNull(whiteboard.get("cleaned up"));
125         }
126     }
127 
128     public void testDoubleStop()
129         throws Exception
130     {
131         synchronized(whiteboard)
132         {
133             Task task = new LongRunningTask();
134             int priority = Thread.NORM_PRIORITY;
135             ThreadGroup threadGroup = new ThreadGroup("Test group");
136             Context context = new Context();
137             whiteboard.clear();
138             Valve cleanup = new CleanupValve();
139             Daemon daemon = new Daemon(task, priority, threadGroup, log, context, cleanup);
140             Thread.sleep(DELAY);
141             daemon.stop();
142             Thread.sleep(DELAY);
143             daemon.stop();
144             Thread.sleep(DELAY);
145             assertNotNull(whiteboard.get("started"));
146             assertNotNull(whiteboard.get("interrupted"));
147             assertNotNull(whiteboard.get("cleaned up"));
148         }
149     }
150 
151     public void testNoCleanup()
152         throws Exception
153     {
154         synchronized(whiteboard)
155         {
156             Task task = new LongRunningTask();
157             int priority = Thread.NORM_PRIORITY;
158             ThreadGroup threadGroup = new ThreadGroup("Test group");
159             Context context = new Context();
160             whiteboard.clear();
161             Valve cleanup = null;
162             Daemon daemon = new Daemon(task, priority, threadGroup, log, context, cleanup);
163             Thread.sleep(DELAY);
164             daemon.stop();
165             Thread.sleep(DELAY);
166             assertNotNull(whiteboard.get("started"));
167             assertNotNull(whiteboard.get("interrupted"));
168             assertNull(whiteboard.get("cleaned up"));
169         }
170     }
171 
172     public void testFailedCleanup()
173         throws Exception
174     {
175         synchronized(whiteboard)
176         {
177             Task task = new LongRunningTask();
178             int priority = Thread.NORM_PRIORITY;
179             ThreadGroup threadGroup = new ThreadGroup("Test group");
180             Context context = new Context();
181             whiteboard.clear();
182             Valve cleanup = new FailingCleanupValve();
183             Daemon daemon = new Daemon(task, priority, threadGroup, log, context, cleanup);
184             Thread.sleep(DELAY);
185             daemon.stop();
186             Thread.sleep(DELAY);
187             assertNotNull(whiteboard.get("started"));
188             assertNotNull(whiteboard.get("interrupted"));
189             assertNull(whiteboard.get("cleaned up"));
190         }
191     }
192 
193     private class LongRunningTask
194         extends Task
195     {
196         public synchronized void process(Context context)
197         {
198            try
199            {
200                whiteboard.put("started", "yes");
201                this.wait();
202            }
203            catch(InterruptedException e)
204            {
205                whiteboard.put("interrupted", "yes");
206                return;
207            }
208         }
209     }
210 
211     private class ShortRunningTask
212         extends Task
213     {
214         public void process(Context context)
215         {
216             whiteboard.put("started", "yes");
217         }
218     }
219 
220     private class FailingTask
221         extends Task
222     {
223         public void process(Context context)
224             throws ProcessingException
225         {
226             whiteboard.put("started", "yes");
227             throw new ProcessingException("processing failed");
228         }
229     }
230     
231     private class CleanupValve
232         implements Valve
233     {
234         public void process(Context context)
235         {
236             whiteboard.put("cleaned up", "yes");
237         }
238     }
239 
240     private class FailingCleanupValve
241         implements Valve
242     {
243         public void process(Context context)
244             throws ProcessingException
245         {
246             throw new ProcessingException("failed cleanup");
247         }
248     }
249 }