KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > foxtrot > test > SingleWorkerThreadTest


1 /**
2  * Copyright (c) 2002-2005, Simone Bordet
3  * All rights reserved.
4  *
5  * This software is distributable under the BSD license.
6  * See the terms of the BSD license in the documentation provided with this software.
7  */

8
9 package foxtrot.test;
10
11 import foxtrot.workers.SingleWorkerThread;
12 import foxtrot.Task;
13
14 /**
15  * @version $Revision: 1.3 $
16  */

17 public class SingleWorkerThreadTest extends FoxtrotTestCase
18 {
19    public SingleWorkerThreadTest(String JavaDoc s)
20    {
21       super(s);
22    }
23
24    private class TestSingleWorkerThread extends SingleWorkerThread
25    {
26       public void stop()
27       {
28          super.stop();
29       }
30    }
31
32    public void testStart() throws Exception JavaDoc
33    {
34       invokeTest(null, new Runnable JavaDoc()
35       {
36          public void run()
37          {
38             TestSingleWorkerThread worker = new TestSingleWorkerThread();
39             if (worker.isAlive()) fail();
40             worker.start();
41             if (!worker.isAlive()) fail();
42          }
43       }, null);
44    }
45
46    public void testStop() throws Exception JavaDoc
47    {
48       invokeTest(null, new Runnable JavaDoc()
49       {
50          public void run()
51          {
52             TestSingleWorkerThread worker = new TestSingleWorkerThread();
53             worker.start();
54             worker.stop();
55             if (worker.isAlive()) fail();
56          }
57       }, null);
58    }
59
60    public void testStartStopStart() throws Exception JavaDoc
61    {
62       invokeTest(null, new Runnable JavaDoc()
63       {
64          public void run()
65          {
66             TestSingleWorkerThread worker = new TestSingleWorkerThread();
67             worker.start();
68             worker.stop();
69             worker.start();
70             if (!worker.isAlive()) fail();
71          }
72       }, null);
73    }
74
75    public void testStopBeforeStart() throws Exception JavaDoc
76    {
77       invokeTest(null, new Runnable JavaDoc()
78       {
79          public void run()
80          {
81             TestSingleWorkerThread worker = new TestSingleWorkerThread();
82             worker.stop();
83          }
84       }, null);
85    }
86
87    public void testStartStart() throws Exception JavaDoc
88    {
89       invokeTest(null, new Runnable JavaDoc()
90       {
91          public void run()
92          {
93             final MutableHolder thread = new MutableHolder(null);
94             TestSingleWorkerThread worker = new TestSingleWorkerThread()
95             {
96                public void run()
97                {
98                   thread.set(Thread.currentThread());
99                   super.run();
100                }
101             };
102             worker.start();
103             sleep(500);
104             Thread JavaDoc foxtrot = (Thread JavaDoc)thread.get();
105             worker.start();
106             sleep(500);
107             if (foxtrot != thread.get()) fail();
108          }
109       }, null);
110    }
111
112    public void testNoStartAllowsPost() throws Exception JavaDoc
113    {
114       final TestSingleWorkerThread worker = new TestSingleWorkerThread();
115       invokeTest(worker, new Runnable JavaDoc()
116       {
117          public void run()
118          {
119             final MutableInteger pass = new MutableInteger(0);
120             worker.postTask(new Task()
121             {
122                public Object JavaDoc run() throws Exception JavaDoc
123                {
124                   pass.set(1);
125                   return null;
126                }
127             });
128             sleep(500);
129             if (pass.get() != 1) fail();
130          }
131       }, null);
132    }
133
134    public void testPost() throws Exception JavaDoc
135    {
136       final TestSingleWorkerThread worker = new TestSingleWorkerThread();
137       invokeTest(worker, new Runnable JavaDoc()
138       {
139          public void run()
140          {
141             worker.start();
142             final MutableInteger pass = new MutableInteger(0);
143             worker.postTask(new Task()
144             {
145                public Object JavaDoc run() throws Exception JavaDoc
146                {
147                   pass.set(1);
148                   return null;
149                }
150             });
151             sleep(500);
152             if (pass.get() != 1) fail();
153          }
154       }, null);
155    }
156
157    public void testManyPosts() throws Exception JavaDoc
158    {
159       final TestSingleWorkerThread worker = new TestSingleWorkerThread();
160       invokeTest(worker, new Runnable JavaDoc()
161       {
162          public void run()
163          {
164             worker.start();
165             worker.postTask(new Task()
166             {
167                public Object JavaDoc run() throws Exception JavaDoc
168                {
169                   sleep(500);
170                   return null;
171                }
172             });
173             final MutableInteger pass = new MutableInteger(0);
174             worker.postTask(new Task()
175             {
176                public Object JavaDoc run() throws Exception JavaDoc
177                {
178                   pass.set(pass.get() + 1);
179                   return null;
180                }
181             });
182             worker.postTask(new Task()
183             {
184                public Object JavaDoc run() throws Exception JavaDoc
185                {
186                   pass.set(pass.get() + 1);
187                   return null;
188                }
189             });
190             sleep(1000);
191             if (pass.get() != 2) fail();
192          }
193       }, null);
194    }
195
196    public void testTaskIsExecutedAfterIgnoredInterruptInTask() throws Exception JavaDoc
197    {
198       final TestSingleWorkerThread worker = new TestSingleWorkerThread();
199       invokeTest(worker, new Runnable JavaDoc()
200       {
201          public void run()
202          {
203             final MutableHolder thread = new MutableHolder(null);
204             final MutableInteger pass = new MutableInteger(0);
205             worker.postTask(new Task()
206             {
207                public Object JavaDoc run() throws Exception JavaDoc
208                {
209                   thread.set(Thread.currentThread());
210
211                   try
212                   {
213                      Thread.sleep(1000);
214                   }
215                   catch (InterruptedException JavaDoc x)
216                   {
217                      // Swallow the exception and don't restore the interrupted status of the thread
218
// Be sure we pass here
219
pass.set(1);
220                   }
221
222                   return null;
223                }
224             });
225             sleep(500);
226
227             // Interrupt the WorkerThread.
228
// The task will not restore the status of the thread, thereby allowing it to continue
229
Thread JavaDoc foxtrot = (Thread JavaDoc)thread.get();
230             foxtrot.interrupt();
231             sleep(1000);
232             if (pass.get() != 1) fail();
233
234             // Be sure another Task can be posted and executed on the same worker thread as above
235
worker.postTask(new Task()
236             {
237                public Object JavaDoc run() throws Exception JavaDoc
238                {
239                   thread.set(Thread.currentThread());
240                   pass.set(2);
241                   return null;
242                }
243             });
244             sleep(500);
245             if (thread.get() != foxtrot) fail();
246             if (pass.get() != 2) fail();
247          }
248       }, null);
249    }
250
251    public void testTaskIsExecutedAfterNotIgnoredInterruptInTask() throws Exception JavaDoc
252    {
253       final TestSingleWorkerThread worker = new TestSingleWorkerThread();
254       invokeTest(worker, new Runnable JavaDoc()
255       {
256          public void run()
257          {
258             final MutableHolder thread = new MutableHolder(null);
259             final MutableInteger pass = new MutableInteger(0);
260             worker.postTask(new Task()
261             {
262                public Object JavaDoc run() throws Exception JavaDoc
263                {
264                   thread.set(Thread.currentThread());
265
266                   try
267                   {
268                      Thread.sleep(1000);
269                   }
270                   catch (InterruptedException JavaDoc x)
271                   {
272                      // Restore the interrupted status of the thread
273
Thread.currentThread().interrupt();
274                      // Be sure we pass here
275
pass.set(1);
276                   }
277
278                   return null;
279                }
280             });
281             sleep(500);
282
283             // Interrupt the WorkerThread.
284
// The task will not restore the status of the thread, thereby allowing it to continue
285
Thread JavaDoc foxtrot = (Thread JavaDoc)thread.get();
286             foxtrot.interrupt();
287             sleep(1000);
288             if (pass.get() != 1) fail();
289
290             // Be sure another Task can be posted and executed on a different worker thread
291
worker.postTask(new Task()
292             {
293                public Object JavaDoc run() throws Exception JavaDoc
294                {
295                   thread.set(Thread.currentThread());
296                   pass.set(2);
297                   return null;
298                }
299             });
300             sleep(500);
301             if (thread.get() == foxtrot) fail();
302             if (pass.get() != 2) fail();
303          }
304       }, null);
305    }
306
307    public void testTaskIsExecutedAfterInterruptInTask() throws Exception JavaDoc
308    {
309       final TestSingleWorkerThread worker = new TestSingleWorkerThread();
310       invokeTest(worker, new Runnable JavaDoc()
311       {
312          public void run()
313          {
314             final MutableHolder thread = new MutableHolder(null);
315             final MutableInteger pass = new MutableInteger(0);
316             worker.postTask(new Task()
317             {
318                public Object JavaDoc run() throws Exception JavaDoc
319                {
320                   thread.set(Thread.currentThread());
321
322                   // Allow InterruptedException to unwind the stack frames
323
Thread.sleep(1000);
324
325                   return null;
326                }
327             });
328             sleep(500);
329
330             Thread JavaDoc foxtrot = (Thread JavaDoc)thread.get();
331             foxtrot.interrupt();
332             sleep(1000);
333
334             // Be sure another Task can be posted and executed on a different worker thread
335
worker.postTask(new Task()
336             {
337                public Object JavaDoc run() throws Exception JavaDoc
338                {
339                   thread.set(Thread.currentThread());
340                   pass.set(1);
341                   return null;
342                }
343             });
344             sleep(500);
345             if (thread.get() == foxtrot) fail();
346             if (pass.get() != 1) fail();
347          }
348       }, null);
349    }
350
351    public void testPendingTasksAreExecutedAfterRestart() throws Exception JavaDoc
352    {
353       final TestSingleWorkerThread worker = new TestSingleWorkerThread();
354       invokeTest(worker, new Runnable JavaDoc()
355       {
356          public void run()
357          {
358             worker.start();
359             worker.postTask(new Task()
360             {
361                public Object JavaDoc run() throws Exception JavaDoc
362                {
363                   Thread.sleep(500);
364                   return null;
365                }
366             });
367             final MutableInteger pass = new MutableInteger(0);
368             worker.postTask(new Task()
369             {
370                public Object JavaDoc run() throws Exception JavaDoc
371                {
372                   pass.set(1);
373                   return null;
374                }
375             });
376             sleep(250);
377             worker.stop();
378             sleep(1000);
379             // Be sure 2nd Task not yet executed
380
if (pass.get() != 0) fail();
381             worker.start();
382             sleep(500);
383             if (pass.get() != 1) fail();
384          }
385       }, null);
386    }
387 }
388
Popular Tags