1 package org.jacorb.test.notification.util; 2 3 23 24 import junit.framework.Test; 25 import junit.framework.TestCase; 26 import junit.framework.TestSuite; 27 28 import org.easymock.MockControl; 29 import org.jacorb.notification.engine.DefaultTaskExecutor; 30 31 import EDU.oswego.cs.dl.util.concurrent.Latch; 32 33 36 37 public class ThreadPoolTest extends TestCase 38 { 39 private DefaultTaskExecutor objectUnderTest_; 40 41 public void setUp() throws Exception 42 { 43 objectUnderTest_ = new DefaultTaskExecutor("Testing", 2); 44 } 45 46 public void tearDown() throws Exception 47 { 48 objectUnderTest_.dispose(); 49 } 50 51 public void testExceute() throws Exception 52 { 53 MockControl controlTask = MockControl.createControl(Runnable .class); 54 Runnable mockTask = (Runnable ) controlTask.getMock(); 55 56 mockTask.run(); 57 controlTask.replay(); 58 59 objectUnderTest_.execute(mockTask); 60 61 Thread.sleep(100); 62 63 controlTask.verify(); 64 } 65 66 public void testDirectExceute() throws Exception 67 { 68 objectUnderTest_.dispose(); 69 objectUnderTest_ = new DefaultTaskExecutor("Testing", 0); 70 71 MockControl controlTask = MockControl.createControl(Runnable .class); 72 Runnable mockTask = (Runnable ) controlTask.getMock(); 73 74 mockTask.run(); 75 controlTask.replay(); 76 77 objectUnderTest_.execute(mockTask); 78 79 controlTask.verify(); 80 } 81 82 public void testNegativeNumberOfThreadsIsInvalid() throws Exception 83 { 84 try 85 { 86 new DefaultTaskExecutor("Testing", -1); 87 fail(); 88 } catch (IllegalArgumentException e) 89 { 90 } 92 } 93 94 public void testIsTaskQueued() throws Exception 95 { 96 assertTrue(!objectUnderTest_.isTaskQueued()); 97 98 final Latch _latch1 = new Latch(); 99 final Latch _latch2 = new Latch(); 100 101 objectUnderTest_.execute(new Runnable () 102 { 103 public void run() 104 { 105 try 106 { 107 _latch1.acquire(); 108 } catch (InterruptedException e) 109 { 110 } 112 } 113 }); 114 115 objectUnderTest_.execute(new Runnable () 116 { 117 public void run() 118 { 119 try 120 { 121 _latch2.acquire(); 122 } catch (InterruptedException e) 123 { 124 } 126 } 127 }); 128 129 objectUnderTest_.execute(new Runnable () 130 { 131 public void run() 132 { 133 try 134 { 135 _latch2.acquire(); 136 } catch (InterruptedException e) 137 { 138 } 140 } 141 }); 142 143 assertTrue(objectUnderTest_.isTaskQueued()); 144 _latch1.release(); 145 Thread.sleep(100); 146 assertTrue(!objectUnderTest_.isTaskQueued()); 147 } 148 149 public ThreadPoolTest(String name) 150 { 151 super(name); 152 } 153 154 public static Test suite() 155 { 156 TestSuite suite = new TestSuite(ThreadPoolTest.class, "Tests for Class ThreadPool"); 157 158 return suite; 159 } 160 } | Popular Tags |