1 8 9 package foxtrot.test; 10 11 import foxtrot.workers.SingleWorkerThread; 12 import foxtrot.Task; 13 14 17 public class SingleWorkerThreadTest extends FoxtrotTestCase 18 { 19 public SingleWorkerThreadTest(String s) 20 { 21 super(s); 22 } 23 24 private class TestSingleWorkerThread extends SingleWorkerThread 25 { 26 public void stop() 27 { 28 super.stop(); 29 } 30 } 31 32 public void testStart() throws Exception 33 { 34 invokeTest(null, new Runnable () 35 { 36 public void run() 37 { 38 TestSingleWorkerThread worker = new TestSingleWorkerThread(); 39 if (worker.isAlive()) fail(); 40 worker.start(); 41 if (!worker.isAlive()) fail(); 42 } 43 }, null); 44 } 45 46 public void testStop() throws Exception 47 { 48 invokeTest(null, new Runnable () 49 { 50 public void run() 51 { 52 TestSingleWorkerThread worker = new TestSingleWorkerThread(); 53 worker.start(); 54 worker.stop(); 55 if (worker.isAlive()) fail(); 56 } 57 }, null); 58 } 59 60 public void testStartStopStart() throws Exception 61 { 62 invokeTest(null, new Runnable () 63 { 64 public void run() 65 { 66 TestSingleWorkerThread worker = new TestSingleWorkerThread(); 67 worker.start(); 68 worker.stop(); 69 worker.start(); 70 if (!worker.isAlive()) fail(); 71 } 72 }, null); 73 } 74 75 public void testStopBeforeStart() throws Exception 76 { 77 invokeTest(null, new Runnable () 78 { 79 public void run() 80 { 81 TestSingleWorkerThread worker = new TestSingleWorkerThread(); 82 worker.stop(); 83 } 84 }, null); 85 } 86 87 public void testStartStart() throws Exception 88 { 89 invokeTest(null, new Runnable () 90 { 91 public void run() 92 { 93 final MutableHolder thread = new MutableHolder(null); 94 TestSingleWorkerThread worker = new TestSingleWorkerThread() 95 { 96 public void run() 97 { 98 thread.set(Thread.currentThread()); 99 super.run(); 100 } 101 }; 102 worker.start(); 103 sleep(500); 104 Thread foxtrot = (Thread )thread.get(); 105 worker.start(); 106 sleep(500); 107 if (foxtrot != thread.get()) fail(); 108 } 109 }, null); 110 } 111 112 public void testNoStartAllowsPost() throws Exception 113 { 114 final TestSingleWorkerThread worker = new TestSingleWorkerThread(); 115 invokeTest(worker, new Runnable () 116 { 117 public void run() 118 { 119 final MutableInteger pass = new MutableInteger(0); 120 worker.postTask(new Task() 121 { 122 public Object run() throws Exception 123 { 124 pass.set(1); 125 return null; 126 } 127 }); 128 sleep(500); 129 if (pass.get() != 1) fail(); 130 } 131 }, null); 132 } 133 134 public void testPost() throws Exception 135 { 136 final TestSingleWorkerThread worker = new TestSingleWorkerThread(); 137 invokeTest(worker, new Runnable () 138 { 139 public void run() 140 { 141 worker.start(); 142 final MutableInteger pass = new MutableInteger(0); 143 worker.postTask(new Task() 144 { 145 public Object run() throws Exception 146 { 147 pass.set(1); 148 return null; 149 } 150 }); 151 sleep(500); 152 if (pass.get() != 1) fail(); 153 } 154 }, null); 155 } 156 157 public void testManyPosts() throws Exception 158 { 159 final TestSingleWorkerThread worker = new TestSingleWorkerThread(); 160 invokeTest(worker, new Runnable () 161 { 162 public void run() 163 { 164 worker.start(); 165 worker.postTask(new Task() 166 { 167 public Object run() throws Exception 168 { 169 sleep(500); 170 return null; 171 } 172 }); 173 final MutableInteger pass = new MutableInteger(0); 174 worker.postTask(new Task() 175 { 176 public Object run() throws Exception 177 { 178 pass.set(pass.get() + 1); 179 return null; 180 } 181 }); 182 worker.postTask(new Task() 183 { 184 public Object run() throws Exception 185 { 186 pass.set(pass.get() + 1); 187 return null; 188 } 189 }); 190 sleep(1000); 191 if (pass.get() != 2) fail(); 192 } 193 }, null); 194 } 195 196 public void testTaskIsExecutedAfterIgnoredInterruptInTask() throws Exception 197 { 198 final TestSingleWorkerThread worker = new TestSingleWorkerThread(); 199 invokeTest(worker, new Runnable () 200 { 201 public void run() 202 { 203 final MutableHolder thread = new MutableHolder(null); 204 final MutableInteger pass = new MutableInteger(0); 205 worker.postTask(new Task() 206 { 207 public Object run() throws Exception 208 { 209 thread.set(Thread.currentThread()); 210 211 try 212 { 213 Thread.sleep(1000); 214 } 215 catch (InterruptedException x) 216 { 217 pass.set(1); 220 } 221 222 return null; 223 } 224 }); 225 sleep(500); 226 227 Thread foxtrot = (Thread )thread.get(); 230 foxtrot.interrupt(); 231 sleep(1000); 232 if (pass.get() != 1) fail(); 233 234 worker.postTask(new Task() 236 { 237 public Object run() throws Exception 238 { 239 thread.set(Thread.currentThread()); 240 pass.set(2); 241 return null; 242 } 243 }); 244 sleep(500); 245 if (thread.get() != foxtrot) fail(); 246 if (pass.get() != 2) fail(); 247 } 248 }, null); 249 } 250 251 public void testTaskIsExecutedAfterNotIgnoredInterruptInTask() throws Exception 252 { 253 final TestSingleWorkerThread worker = new TestSingleWorkerThread(); 254 invokeTest(worker, new Runnable () 255 { 256 public void run() 257 { 258 final MutableHolder thread = new MutableHolder(null); 259 final MutableInteger pass = new MutableInteger(0); 260 worker.postTask(new Task() 261 { 262 public Object run() throws Exception 263 { 264 thread.set(Thread.currentThread()); 265 266 try 267 { 268 Thread.sleep(1000); 269 } 270 catch (InterruptedException x) 271 { 272 Thread.currentThread().interrupt(); 274 pass.set(1); 276 } 277 278 return null; 279 } 280 }); 281 sleep(500); 282 283 Thread foxtrot = (Thread )thread.get(); 286 foxtrot.interrupt(); 287 sleep(1000); 288 if (pass.get() != 1) fail(); 289 290 worker.postTask(new Task() 292 { 293 public Object run() throws Exception 294 { 295 thread.set(Thread.currentThread()); 296 pass.set(2); 297 return null; 298 } 299 }); 300 sleep(500); 301 if (thread.get() == foxtrot) fail(); 302 if (pass.get() != 2) fail(); 303 } 304 }, null); 305 } 306 307 public void testTaskIsExecutedAfterInterruptInTask() throws Exception 308 { 309 final TestSingleWorkerThread worker = new TestSingleWorkerThread(); 310 invokeTest(worker, new Runnable () 311 { 312 public void run() 313 { 314 final MutableHolder thread = new MutableHolder(null); 315 final MutableInteger pass = new MutableInteger(0); 316 worker.postTask(new Task() 317 { 318 public Object run() throws Exception 319 { 320 thread.set(Thread.currentThread()); 321 322 Thread.sleep(1000); 324 325 return null; 326 } 327 }); 328 sleep(500); 329 330 Thread foxtrot = (Thread )thread.get(); 331 foxtrot.interrupt(); 332 sleep(1000); 333 334 worker.postTask(new Task() 336 { 337 public Object run() throws Exception 338 { 339 thread.set(Thread.currentThread()); 340 pass.set(1); 341 return null; 342 } 343 }); 344 sleep(500); 345 if (thread.get() == foxtrot) fail(); 346 if (pass.get() != 1) fail(); 347 } 348 }, null); 349 } 350 351 public void testPendingTasksAreExecutedAfterRestart() throws Exception 352 { 353 final TestSingleWorkerThread worker = new TestSingleWorkerThread(); 354 invokeTest(worker, new Runnable () 355 { 356 public void run() 357 { 358 worker.start(); 359 worker.postTask(new Task() 360 { 361 public Object run() throws Exception 362 { 363 Thread.sleep(500); 364 return null; 365 } 366 }); 367 final MutableInteger pass = new MutableInteger(0); 368 worker.postTask(new Task() 369 { 370 public Object run() throws Exception 371 { 372 pass.set(1); 373 return null; 374 } 375 }); 376 sleep(250); 377 worker.stop(); 378 sleep(1000); 379 if (pass.get() != 0) fail(); 381 worker.start(); 382 sleep(500); 383 if (pass.get() != 1) fail(); 384 } 385 }, null); 386 } 387 } 388 | Popular Tags |