KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > ThreadPoolTest


1 package org.sapia.ubik.net;
2
3 import junit.framework.TestCase;
4
5
6 /**
7  * @author Yanick Duchesne
8  * <dl>
9  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
10  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
11  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
12  * </dl>
13  */

14 public class ThreadPoolTest extends TestCase {
15   /**
16    * Constructor for ThreadPoolTest.
17    * @param arg0
18    */

19   public ThreadPoolTest(String JavaDoc arg0) {
20     super(arg0);
21   }
22
23   public void testAcquire() throws Exception JavaDoc {
24     ThreadPool tp = new TestThreadPool("true", false, 5);
25     PooledThread pt = (PooledThread) tp.acquire();
26     super.assertTrue(pt.isAlive());
27     pt.exec("aTask");
28     Thread.sleep(500);
29     pt = (PooledThread) tp.acquire();
30     super.assertEquals(1, ((TestPooledThread) pt).count);
31     super.assertEquals(1, tp.getCreatedCount());
32     pt.exec("aTask");
33     Thread.sleep(500);
34     super.assertEquals(2, ((TestPooledThread) pt).count);
35   }
36
37   // /**
38
// * Constructor for ThreadPoolTest.
39
// * @param arg0
40
// */
41
// public ThreadPoolTest(String arg0) {
42
// super(arg0);
43
// }
44
//
45
// public void testAcquire() throws Exception{
46
// ThreadPool tp = new TestThreadPool("true", false, 5);
47
// PooledThread pt = (PooledThread)tp.acquire();
48
// super.assertTrue(pt.isAlive());
49
// pt.exec("aTask");
50
// Thread.sleep(500);
51
// pt = (PooledThread)tp.acquire();
52
// super.assertEquals(1, ((TestPooledThread)pt).count);
53
// super.assertEquals(1, tp.getCreatedCount());
54
// pt.exec("aTask");
55
// Thread.sleep(500);
56
// super.assertEquals(2, ((TestPooledThread)pt).count);
57
// }
58
public void testShutDown() throws Exception JavaDoc {
59     ThreadPool tp = new TestThreadPool("true", false, 5);
60     PooledThread pt1 = (PooledThread) tp.acquire();
61     PooledThread pt2 = (PooledThread) tp.acquire();
62     tp.shutdown(5000);
63     super.assertEquals(2, tp.size());
64
65     try {
66       tp.acquire();
67       throw new Exception JavaDoc(
68         "Shut down thread pool should have thrown exception on acquire()");
69     } catch (IllegalStateException JavaDoc e) {
70       //ok
71
}
72   }
73
74   class TestPooledThread extends PooledThread {
75     int count;
76
77     /**
78      * @see org.sapia.ubik.net.PooledThread#doExec(Object)
79      */

80     protected void doExec(Object JavaDoc task) {
81       count++;
82     }
83   }
84
85   class TestThreadPool extends ThreadPool {
86     /**
87      * Constructor for TestThreadPool.
88      * @param name
89      * @param daemon
90      * @param maxSize
91      */

92     public TestThreadPool(String JavaDoc name, boolean daemon, int maxSize) {
93       super(name, daemon, maxSize);
94     }
95
96     /**
97      * @see org.sapia.ubik.net.ThreadPool#newThread()
98      */

99     protected PooledThread newThread() throws Exception JavaDoc {
100       return new TestPooledThread();
101     }
102   }
103 }
104
Popular Tags