1 45 package org.exolab.jms.common.threads; 46 47 import java.util.HashSet ; 48 49 import junit.framework.Test; 50 import junit.framework.TestCase; 51 import junit.framework.TestSuite; 52 53 import org.apache.commons.logging.Log; 54 import org.apache.commons.logging.LogFactory; 55 56 import org.exolab.jms.common.threads.CompletionListener; 57 import org.exolab.jms.common.threads.ThreadPool; 58 59 60 69 public class ThreadPoolTest extends TestCase { 70 71 74 private static final Log _log = LogFactory.getLog(ThreadPoolTest.class); 75 76 77 82 public ThreadPoolTest(String name) { 83 super(name); 84 } 85 86 91 public static Test suite() { 92 return new TestSuite(ThreadPoolTest.class); 93 } 94 95 101 public void testExecute() throws Exception { 102 103 ThreadPool pool = new ThreadPool(3); 104 105 Runnable ra = RunnableHelper.makeRunnable("Worker A", 3000); 106 pool.execute(ra); 107 108 Runnable rb = RunnableHelper.makeRunnable("Worker B", 1000); 109 pool.execute(rb); 110 111 Runnable rc = RunnableHelper.makeRunnable("Worker C", 2000); 112 pool.execute(rc); 113 114 Runnable rd = RunnableHelper.makeRunnable("Worker D", 6000); 115 pool.execute(rd); 116 117 Runnable re = RunnableHelper.makeRunnable("Worker E", 1000); 118 pool.execute(re); 119 120 Thread.currentThread().sleep(15000); 121 pool.stopRequestAllWorkers(); 122 } 123 124 130 public void testQueue() throws Exception { 131 ThreadPool pool = new ThreadPool(3); 132 133 TestListener listener = new TestListener(); 134 Runnable ra = RunnableHelper.makeRunnable("Worker A", 3000); 135 listener.addTarget(ra); 136 pool.queue(ra, listener); 137 138 Runnable rb = RunnableHelper.makeRunnable("Worker B", 1000); 139 listener.addTarget(rb); 140 pool.queue(rb, listener); 141 142 Runnable rc = RunnableHelper.makeRunnable("Worker C", 2000); 143 listener.addTarget(rc); 144 pool.queue(rc, listener); 145 146 Runnable rd = RunnableHelper.makeRunnable("Worker D", 6000); 147 listener.addTarget(rd); 148 pool.queue(rd, listener); 149 150 Runnable re = RunnableHelper.makeRunnable("Worker E", 1000); 151 listener.addTarget(re); 152 pool.queue(re, listener); 153 154 Thread.currentThread().sleep(15000); 155 pool.stopRequestAllWorkers(); 156 157 if (listener.getCompleted() != 5) { 158 fail("Only " + listener.getCompleted() 159 + " workers completed in the allocated time"); 160 } 161 162 if (listener.getErrors() != 0) { 163 fail("CompletionListener detected " + listener.getErrors() + 164 " errors"); 165 } 166 } 167 168 private class TestListener implements CompletionListener { 169 170 private HashSet _targets = new HashSet (); 171 private HashSet _completed = new HashSet (); 172 int _errors = 0; 173 174 public void addTarget(Runnable target) { 175 _targets.add(target); 176 } 177 178 public synchronized void completed(Runnable target) { 179 if (_targets.contains(target)) { 180 if (!_completed.contains(target)) { 181 _completed.add(target); 182 _log.debug(target + " has completed"); 183 } else { 184 _log.error(target + " has already completed"); 185 ++_errors; 186 } 187 } else { 188 _log.error("Target=" + target + " not registered " 189 + "with the completion listener"); 190 ++_errors; 191 } 192 } 193 194 public synchronized int getCompleted() { 195 return _completed.size(); 196 } 197 198 public synchronized int getErrors() { 199 return _errors; 200 } 201 202 } 204 public static class RunnableHelper { 205 206 214 public static Runnable makeRunnable(final String name, 215 final long firstDelay) { 216 return new Runnable () { 217 public void run() { 218 try { 219 _log.debug(name + ": starting up"); 220 Thread.sleep(firstDelay); 221 _log.debug(name + ": doing some stuff"); 222 Thread.sleep(2000); 223 _log.debug(name + ": leaving"); 224 } catch (InterruptedException exception) { 225 _log.debug(name + ": got interrupted!"); 226 } catch (Exception exception) { 227 _log.error(exception.getMessage(), exception); 228 } 229 } 230 231 public String toString() { 232 return name; 233 } 234 }; 235 } 236 } 238 } | Popular Tags |