1 16 package org.apache.commons.collections.buffer; 17 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 import java.util.HashSet ; 21 import java.util.LinkedList ; 22 import java.util.Set ; 23 24 import junit.framework.Test; 25 import junit.framework.TestSuite; 26 27 import org.apache.commons.collections.AbstractTestObject; 28 import org.apache.commons.collections.Buffer; 29 import org.apache.commons.collections.BufferUnderflowException; 30 31 41 public class TestBlockingBuffer extends AbstractTestObject { 42 43 public TestBlockingBuffer(String testName) { 44 super(testName); 45 } 46 47 public static Test suite() { 48 return new TestSuite(TestBlockingBuffer.class); 49 } 50 51 public static void main(String args[]) { 52 String [] testCaseName = { TestBlockingBuffer.class.getName()}; 53 junit.textui.TestRunner.main(testCaseName); 54 } 55 56 public Object makeObject() { 57 return BlockingBuffer.decorate(new MyBuffer()); 58 } 59 60 public boolean isEqualsCheckable() { 61 return false; 62 } 63 64 68 public void testGetWithAdd() { 69 70 Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); 71 Object obj = new Object (); 72 73 new DelayedAdd(blockingBuffer, obj).start(); 74 75 assertSame(obj, blockingBuffer.get()); 77 } 78 79 83 public void testGetWithAddAll() { 84 85 Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); 86 Object obj = new Object (); 87 88 new DelayedAddAll(blockingBuffer, obj).start(); 89 90 assertSame(obj, blockingBuffer.get()); 92 } 93 94 98 public void testRemoveWithAdd() { 99 100 Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); 101 Object obj = new Object (); 102 103 new DelayedAdd(blockingBuffer, obj).start(); 104 105 assertSame(obj, blockingBuffer.remove()); 107 } 108 109 113 public void testRemoveWithAddAll() { 114 115 Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); 116 Object obj = new Object (); 117 118 new DelayedAddAll(blockingBuffer, obj).start(); 119 120 assertSame(obj, blockingBuffer.remove()); 122 } 123 124 131 public void testBlockedGetWithAdd() { 132 133 Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); 134 Object obj = new Object (); 135 136 Thread thread1 = new ReadThread(blockingBuffer, obj); 138 Thread thread2 = new ReadThread(blockingBuffer, obj); 139 thread1.start(); 140 thread2.start(); 141 142 delay(); 144 145 blockingBuffer.add(obj); 147 148 delay(); 150 151 if (thread1.isAlive() || thread2.isAlive()) 153 fail("Live thread(s) when both should be dead."); 154 } 155 156 163 public void testBlockedGetWithAddAll() { 164 165 Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); 166 Object obj = new Object (); 167 168 Thread thread1 = new ReadThread(blockingBuffer, obj); 170 Thread thread2 = new ReadThread(blockingBuffer, obj); 171 thread1.start(); 172 thread2.start(); 173 174 delay(); 176 177 blockingBuffer.addAll(Collections.singleton(obj)); 179 180 delay(); 182 183 if (thread1.isAlive() || thread2.isAlive()) 185 fail("Live thread(s) when both should be dead."); 186 } 187 188 192 public void testInterruptedGet() { 193 194 Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); 195 Object obj = new Object (); 196 197 ArrayList exceptionList = new ArrayList (); 199 Thread thread = new ReadThread(blockingBuffer, obj, exceptionList); 200 thread.start(); 201 202 thread.interrupt(); 204 205 delay(); 207 208 assertTrue("Thread interrupt should have led to underflow", 209 exceptionList.contains("BufferUnderFlow")); 210 211 if (thread.isAlive()) { 212 fail("Read thread has hung."); 213 } 214 215 } 216 217 225 public void testBlockedRemoveWithAdd() { 226 227 Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); 228 Object obj = new Object (); 229 230 Thread thread1 = new ReadThread(blockingBuffer, obj, null, "remove"); 232 Thread thread2 = new ReadThread(blockingBuffer, obj, null, "remove"); 233 thread1.start(); 234 thread2.start(); 235 236 delay(); 238 239 blockingBuffer.add(obj); 240 241 delay(); 243 244 assertTrue ("There is one thread waiting", thread1.isAlive() ^ thread2.isAlive()); 246 247 blockingBuffer.add(obj); 248 249 delay(); 251 252 if(thread1.isAlive() || thread2.isAlive()) 254 fail("Live thread(s) when both should be dead."); 255 } 256 257 266 public void testBlockedRemoveWithAddAll1() { 267 268 Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); 269 Object obj = new Object (); 270 271 Thread thread1 = new ReadThread(blockingBuffer, obj, null, "remove"); 273 Thread thread2 = new ReadThread(blockingBuffer, obj, null, "remove"); 274 thread1.start(); 275 thread2.start(); 276 277 delay(); 279 280 blockingBuffer.addAll(Collections.singleton(obj)); 281 282 delay(); 284 285 assertTrue ("There is one thread waiting", thread1.isAlive() ^ thread2.isAlive()); 287 288 blockingBuffer.addAll(Collections.singleton(obj)); 289 290 delay(); 292 293 if(thread1.isAlive() || thread2.isAlive()) 295 fail("Live thread(s) when both should be dead."); 296 } 297 298 299 308 public void testBlockedRemoveWithAddAll2() { 309 310 Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); 311 Object obj1 = new Object (); 312 Object obj2 = new Object (); 313 314 Set objs = Collections.synchronizedSet(new HashSet ()); 315 objs.add(obj1); 316 objs.add(obj2); 317 318 Thread thread1 = new ReadThread(blockingBuffer, objs, "remove"); 320 Thread thread2 = new ReadThread(blockingBuffer, objs, "remove"); 321 thread1.start(); 322 thread2.start(); 323 324 delay(); 326 327 blockingBuffer.addAll(objs); 328 329 delay(); 331 332 assertEquals("Both objects were removed", 0, objs.size()); 333 334 if(thread1.isAlive() || thread2.isAlive()) 336 fail("Live thread(s) when both should be dead."); 337 } 338 339 343 public void testInterruptedRemove() { 344 345 Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); 346 Object obj = new Object (); 347 348 ArrayList exceptionList = new ArrayList (); 350 Thread thread = new ReadThread(blockingBuffer, obj, exceptionList, "remove"); 351 thread.start(); 352 353 thread.interrupt(); 355 356 delay(); 358 359 assertTrue("Thread interrupt should have led to underflow", 360 exceptionList.contains("BufferUnderFlow")); 361 362 if (thread.isAlive()) { 363 fail("Read thread has hung."); 364 } 365 366 } 367 368 protected static class DelayedAdd extends Thread { 369 370 Buffer buffer; 371 Object obj; 372 373 DelayedAdd (Buffer buffer, Object obj) { 374 super(); 375 this.buffer = buffer; 376 this.obj = obj; 377 } 378 379 public void run() { 380 381 try { 382 Thread.currentThread().sleep(100); 384 } 385 catch (InterruptedException e) {} 386 387 buffer.add(obj); 388 } 389 } 390 391 protected static class DelayedAddAll extends Thread { 392 393 Buffer buffer; 394 Object obj; 395 396 DelayedAddAll (Buffer buffer, Object obj) { 397 super(); 398 this.buffer = buffer; 399 this.obj = obj; 400 } 401 402 public void run() { 403 404 try { 405 Thread.currentThread().sleep(100); 407 } 408 catch (InterruptedException e) {} 409 410 buffer.addAll(Collections.singleton(obj)); 411 } 412 } 413 414 protected static class ReadThread extends Thread { 415 416 Buffer buffer; 417 Object obj; 418 ArrayList exceptionList = null; 419 String action = "get"; 420 Set objs; 421 422 ReadThread (Buffer buffer, Object obj) { 423 super(); 424 this.buffer = buffer; 425 this.obj = obj; 426 } 427 428 ReadThread (Buffer buffer, Object obj, ArrayList exceptionList) { 429 super(); 430 this.buffer = buffer; 431 this.obj = obj; 432 this.exceptionList = exceptionList; 433 } 434 435 ReadThread (Buffer buffer, Object obj, ArrayList exceptionList, String action) { 436 super(); 437 this.buffer = buffer; 438 this.obj = obj; 439 this.exceptionList = exceptionList; 440 this.action = action; 441 } 442 443 ReadThread (Buffer buffer, Set objs, String action) { 444 super(); 445 this.buffer = buffer; 446 this.objs = objs; 447 this.action = action; 448 } 449 450 public void run() { 451 try { 452 if (action == "get") { 453 assertSame(obj, buffer.get()); 454 } else { 455 if (null != obj) 456 assertSame(obj, buffer.remove()); 457 else 458 assertTrue(objs.remove(buffer.remove())); 459 } 460 } catch (BufferUnderflowException ex) { 461 exceptionList.add("BufferUnderFlow"); 462 } 463 } 464 } 465 466 467 protected static class MyBuffer extends LinkedList implements Buffer { 468 469 public Object get() { 470 if(isEmpty()) 471 throw new BufferUnderflowException(); 472 return get(0); 473 } 474 475 public Object remove() { 476 if(isEmpty()) 477 throw new BufferUnderflowException(); 478 return remove(0); 479 } 480 } 481 482 private void delay(){ 483 try { 484 Thread.currentThread().sleep(100); 485 } catch (InterruptedException e) {} 486 } 487 488 public String getCompatibilityVersion() { 489 return "3.1"; 490 } 491 492 502 } 503 | Popular Tags |