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.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 }