1 package org.sapia.ubik.net; 2 3 import junit.framework.TestCase; 4 5 6 14 public class ThreadPoolTest extends TestCase { 15 19 public ThreadPoolTest(String arg0) { 20 super(arg0); 21 } 22 23 public void testAcquire() throws Exception { 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 public void testShutDown() throws Exception { 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 ( 68 "Shut down thread pool should have thrown exception on acquire()"); 69 } catch (IllegalStateException e) { 70 } 72 } 73 74 class TestPooledThread extends PooledThread { 75 int count; 76 77 80 protected void doExec(Object task) { 81 count++; 82 } 83 } 84 85 class TestThreadPool extends ThreadPool { 86 92 public TestThreadPool(String name, boolean daemon, int maxSize) { 93 super(name, daemon, maxSize); 94 } 95 96 99 protected PooledThread newThread() throws Exception { 100 return new TestPooledThread(); 101 } 102 } 103 } 104 | Popular Tags |