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.Valve;
39  import org.objectledge.threads.Task;
40  
41  /***
42   * 
43   * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
44   * @version $Id: WorkerPoolTest.java,v 1.1 2004/02/02 14:24:44 fil Exp $
45   */
46  public class WorkerPoolTest extends TestCase
47  {
48      private static final int TASK_DURATION = 300;
49      
50      private static Map whiteboard = new HashMap();
51      
52      private WorkerPool pool;
53      
54      private Daemon scheduler;
55  
56      private static Logger log = 
57          new Log4JLogger(org.apache.log4j.Logger.getLogger(WorkerTest.class));
58  
59      /***
60       * Constructor for WorkerPoolTest.
61       * @param arg0
62       */
63      public WorkerPoolTest(String arg0)
64      {
65          super(arg0);
66      }
67  
68      public void setUp(int capacity)
69      {
70          int priority = Thread.NORM_PRIORITY;
71          ThreadGroup threadGroup = new ThreadGroup("Test group");
72          Context context = new Context();
73          Valve cleanup = new CleanupValve();
74          pool = new WorkerPool(capacity, priority, threadGroup, log, context, cleanup);
75          Task schedulerTask = pool.getSchedulingTask();
76          scheduler = new Daemon(schedulerTask, priority, threadGroup, log, context, cleanup);
77          whiteboard.clear();
78      }
79      
80      public void tearDown()
81      {
82          scheduler.stop();
83          scheduler = null;
84          pool = null;        
85      }
86      
87      public void testSequential()
88          throws Exception
89      {
90          synchronized(whiteboard)
91          {
92              setUp(1);
93              pool.dispatch(new Test1Task());
94              pool.dispatch(new Test2Task());
95              Thread.sleep(1000);
96              assertNotNull(whiteboard.get("started test1"));
97              assertNotNull(whiteboard.get("started test2"));
98          }
99      }
100 
101     public void testConcurrent()
102         throws Exception
103     {
104         synchronized(whiteboard)
105         {
106             setUp(2);
107             pool.dispatch(new Test1Task());
108             pool.dispatch(new Test2Task());
109             Thread.sleep(1000);
110             assertNotNull(whiteboard.get("started test1"));
111             assertNotNull(whiteboard.get("started test2"));
112         }
113     }
114 
115     private class Test1Task
116         extends Task
117     {
118         public synchronized void process(Context context)
119         {
120            try
121            {
122                whiteboard.put("started test1", "yes");
123                this.wait(TASK_DURATION);
124            }
125            catch(InterruptedException e)
126            {
127                whiteboard.put("interrupted test1", "yes");
128                return;
129            }
130         }
131     }
132 
133     private class Test2Task
134         extends Task
135     {
136         public synchronized void process(Context context)
137         {
138            try
139            {
140                whiteboard.put("started test2", "yes");
141                this.wait(TASK_DURATION);
142            }
143            catch(InterruptedException e)
144            {
145                whiteboard.put("interrupted test2", "yes");
146                return;
147            }
148         }
149     }
150 
151     private class CleanupValve
152         implements Valve
153     {
154         public void process(Context context)
155         {
156             whiteboard.put("cleaned up", "yes");
157         }
158     }
159 }