1 37 package net.sourceforge.cruisecontrol.util.threadpool; 38 39 import junit.framework.TestCase; 40 41 46 public class ThreadQueueTest extends TestCase { 47 private static final String TASK_NAME = "TASK:"; 48 private static final int TASK_COUNT = 5; 49 private static final int TENTH_OF_SECOND = 100; 50 51 protected void setUp() throws Exception { 52 for (int i = 1; i < TASK_COUNT + 1; i++) { 53 final String taskName = TASK_NAME + i; 54 55 IdleThreadQueueClient task = new IdleThreadQueueClient(taskName); 56 ThreadQueue.addTask(task); 57 assertEquals(i, ThreadQueue.numTotalTasks()); 58 } 59 60 sleep(TENTH_OF_SECOND); 61 } 62 63 protected void tearDown() { 64 ThreadQueue.terminate(); 65 } 66 67 public void testIsIdle() throws Exception { 68 assertFalse(ThreadQueue.isIdle(TASK_NAME + 1)); 69 assertTrue(ThreadQueue.isIdle(TASK_NAME + 2)); 70 assertTrue(ThreadQueue.isIdle(TASK_NAME + 3)); 71 72 tasksThatCompleteShouldNotBeIdle(); 73 74 caseOfNameShouldNotMatter(); 75 76 tasksThatDontExistShouldNotBeIdle(); 77 } 78 79 private void tasksThatCompleteShouldNotBeIdle() { 80 ThreadQueue.waitFor(TASK_NAME + 2); 81 assertFalse(ThreadQueue.isIdle(TASK_NAME + 2)); 82 } 83 84 private void caseOfNameShouldNotMatter() { 85 String taskName = TASK_NAME + TASK_COUNT; 86 assertTrue(ThreadQueue.isIdle(taskName.toLowerCase())); 87 } 88 89 private void tasksThatDontExistShouldNotBeIdle() { 90 assertFalse(ThreadQueue.isIdle(TASK_NAME + 42)); 91 } 92 93 public void testInterrupt() throws Exception { 94 assertFalse(ThreadQueue.isIdle(TASK_NAME + 1)); 95 ThreadQueue.interrupt(TASK_NAME + 1); 96 assertInterrupted(TASK_NAME + 1); 97 98 assertTrue(ThreadQueue.isIdle(TASK_NAME + TASK_COUNT)); 99 ThreadQueue.interrupt(TASK_NAME + TASK_COUNT); 100 assertInterrupted(TASK_NAME + TASK_COUNT); 101 } 102 103 public void testExecution() { 104 verifyCountOfRunningAndIdleTasksCorrect(); 105 106 for (int i = 1; i < TASK_COUNT + 1; i++) { 107 String taskName = TASK_NAME + i; 108 assertTrue(ThreadQueue.taskExists(taskName)); 109 assertTrue(ThreadQueue.isActive(taskName)); 110 } 111 112 ThreadQueue.waitForAll(); 114 115 assertEquals(0, ThreadQueue.numRunningTasks()); 116 assertEquals(0, ThreadQueue.numWaitingTasks()); 117 118 for (int i = 1; i < TASK_COUNT + 1; i++) { 119 String taskName = TASK_NAME + i; 120 121 assertTrue(ThreadQueue.taskExists(taskName)); 122 assertFalse(ThreadQueue.isActive(taskName)); 123 124 Object rawResult = ThreadQueue.getResult(taskName); 126 assertTrue(rawResult instanceof String ); 127 assertEquals("DONE WITH " + taskName, (String ) rawResult); 128 } 129 } 130 131 private void verifyCountOfRunningAndIdleTasksCorrect() { 132 int numRunningTasks = ThreadQueue.numRunningTasks(); 136 int numWorkerThreads = ThreadQueue.getMaxNumWorkerThreads(); 137 assertEquals(numWorkerThreads, numRunningTasks); 138 139 int numThatShouldBeWaiting = TASK_COUNT - ThreadQueue.getMaxNumWorkerThreads(); 143 if (ThreadQueue.getMaxNumWorkerThreads() > TASK_COUNT) { 145 numThatShouldBeWaiting = 0; 146 } 147 assertEquals(numThatShouldBeWaiting, ThreadQueue.numWaitingTasks()); 148 } 149 150 private static void sleep(int ms) { 151 try { 152 Thread.sleep(ms); 153 } catch (Exception e) { 154 } 155 } 156 157 private static void assertInterrupted(String taskName) { 158 assertFalse(ThreadQueue.isActive(taskName)); 159 assertFalse(ThreadQueue.isDone(taskName)); 160 assertFalse(ThreadQueue.isIdle(taskName)); 161 } 162 163 } 164 | Popular Tags |