ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CyclicBarrierTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CyclicBarrierTest.java (file contents):
Revision 1.7 by dl, Fri Jan 9 14:45:58 2004 UTC vs.
Revision 1.8 by dl, Mon Apr 25 15:04:50 2005 UTC

# Line 9 | Line 9
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 + import java.util.concurrent.locks.*;
13 + import java.util.concurrent.atomic.*;
14  
15   public class CyclicBarrierTest extends JSR166TestCase{
16      public static void main(String[] args) {
# Line 162 | Line 164 | public class CyclicBarrierTest extends J
164       * throw BrokenBarrierException
165       */
166      public void testAwait2_Interrupted_BrokenBarrier() {
167 <      final CyclicBarrier c = new CyclicBarrier(3);
167 >        final CyclicBarrier c = new CyclicBarrier(3);
168          Thread t1 = new Thread(new Runnable() {
169                  public void run() {
170                      try {
# Line 227 | Line 229 | public class CyclicBarrierTest extends J
229       * throw BrokenBarrierException
230       */
231      public void testAwait4_Timeout_BrokenBarrier() {
232 <      final CyclicBarrier c = new CyclicBarrier(3);
232 >        final CyclicBarrier c = new CyclicBarrier(3);
233          Thread t1 = new Thread(new Runnable() {
234                  public void run() {
235                      try {
# Line 265 | Line 267 | public class CyclicBarrierTest extends J
267       * throw BrokenBarrierException
268       */
269      public void testAwait5_Timeout_BrokenBarrier() {
270 <      final CyclicBarrier c = new CyclicBarrier(3);
270 >        final CyclicBarrier c = new CyclicBarrier(3);
271          Thread t1 = new Thread(new Runnable() {
272                  public void run() {
273                      try {
# Line 374 | Line 376 | public class CyclicBarrierTest extends J
376          }
377      }
378  
379 +    /**
380 +     * All threads block while a barrier is broken.
381 +     */
382 +    public void testReset_Leakage() {
383 +        try {
384 +            final CyclicBarrier c = new CyclicBarrier(2);
385 +            final AtomicBoolean done = new AtomicBoolean();
386 +            Thread t = new Thread() {
387 +                    public void run() {
388 +                        while (!done.get()) {
389 +                            try {
390 +                                while (c.isBroken())
391 +                                    c.reset();
392 +                                
393 +                                c.await();
394 +                                threadFail("await should not return");
395 +                            }
396 +                            catch (BrokenBarrierException e) {
397 +                            }
398 +                            catch (InterruptedException ie) {
399 +                            }
400 +                        }
401 +                    }
402 +                };
403 +            
404 +            t.start();
405 +            for( int i = 0; i < 4; i++) {
406 +                Thread.sleep(SHORT_DELAY_MS);
407 +                t.interrupt();
408 +            }
409 +            done.set(true);
410 +            t.interrupt();
411 +        }
412 +        catch (Exception ex) {
413 +            unexpectedException();
414 +        }
415 +    }
416 +
417 +    /**
418 +     * Reset of a non-broken barrier does not break barrier
419 +     */
420 +    public void testResetWithoutBreakage() {
421 +        try {
422 +            final CyclicBarrier start = new CyclicBarrier(3);
423 +            final CyclicBarrier barrier = new CyclicBarrier(3);
424 +            for (int i = 0; i < 3; i++) {
425 +                Thread t1 = new Thread(new Runnable() {
426 +                        public void run() {
427 +                            try { start.await(); }
428 +                            catch (Exception ie) {
429 +                                threadFail("start barrier");
430 +                            }
431 +                            try { barrier.await(); }
432 +                            catch (Throwable thrown) {
433 +                                unexpectedException();
434 +                            }}});
435 +                
436 +                Thread t2 = new Thread(new Runnable() {
437 +                        public void run() {
438 +                            try { start.await(); }
439 +                            catch (Exception ie) {
440 +                                threadFail("start barrier");
441 +                            }
442 +                            try { barrier.await(); }
443 +                            catch (Throwable thrown) {
444 +                                unexpectedException();
445 +                            }}});
446 +                
447 +                
448 +                t1.start();
449 +                t2.start();
450 +                try { start.await(); }
451 +                catch (Exception ie) { threadFail("start barrier"); }
452 +                barrier.await();
453 +                t1.join();
454 +                t2.join();
455 +                assertFalse(barrier.isBroken());
456 +                assertEquals(0, barrier.getNumberWaiting());
457 +                if (i == 1) barrier.reset();
458 +                assertFalse(barrier.isBroken());
459 +                assertEquals(0, barrier.getNumberWaiting());
460 +            }
461 +        }
462 +        catch (Exception ex) {
463 +            unexpectedException();
464 +        }
465 +    }
466 +        
467 +    /**
468 +     * Reset of a barrier after interruption reinitializes it.
469 +     */
470 +    public void testResetAfterInterrupt() {
471 +        try {
472 +            final CyclicBarrier start = new CyclicBarrier(3);
473 +            final CyclicBarrier barrier = new CyclicBarrier(3);
474 +            for (int i = 0; i < 2; i++) {
475 +                Thread t1 = new Thread(new Runnable() {
476 +                        public void run() {
477 +                            try { start.await(); }
478 +                            catch (Exception ie) {
479 +                                threadFail("start barrier");
480 +                            }
481 +                            try { barrier.await(); }
482 +                            catch(InterruptedException ok) {}
483 +                            catch (Throwable thrown) {
484 +                                unexpectedException();
485 +                            }}});
486 +                
487 +                Thread t2 = new Thread(new Runnable() {
488 +                        public void run() {
489 +                            try { start.await(); }
490 +                            catch (Exception ie) {
491 +                                threadFail("start barrier");
492 +                            }
493 +                            try { barrier.await(); }
494 +                            catch(BrokenBarrierException ok) {}
495 +                            catch (Throwable thrown) {
496 +                                unexpectedException();
497 +                            }}});
498 +                
499 +                t1.start();
500 +                t2.start();
501 +                try { start.await(); }
502 +                catch (Exception ie) { threadFail("start barrier"); }
503 +                t1.interrupt();
504 +                t1.join();
505 +                t2.join();
506 +                assertTrue(barrier.isBroken());
507 +                assertEquals(0, barrier.getNumberWaiting());
508 +                barrier.reset();
509 +                assertFalse(barrier.isBroken());
510 +                assertEquals(0, barrier.getNumberWaiting());
511 +            }
512 +        }
513 +        catch (Exception ex) {
514 +            unexpectedException();
515 +        }
516 +    }
517 +        
518 +    /**
519 +     * Reset of a barrier after timeout reinitializes it.
520 +     */
521 +    public void testResetAfterTimeout() {
522 +        try {
523 +            final CyclicBarrier start = new CyclicBarrier(3);
524 +            final CyclicBarrier barrier = new CyclicBarrier(3);
525 +            for (int i = 0; i < 2; i++) {
526 +                Thread t1 = new Thread(new Runnable() {
527 +                        public void run() {
528 +                            try { start.await(); }
529 +                            catch (Exception ie) {
530 +                                threadFail("start barrier");
531 +                            }
532 +                            try { barrier.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); }
533 +                            catch(TimeoutException ok) {}
534 +                            catch (Throwable thrown) {
535 +                                unexpectedException();
536 +                            }}});
537 +                
538 +                Thread t2 = new Thread(new Runnable() {
539 +                        public void run() {
540 +                            try { start.await(); }
541 +                            catch (Exception ie) {
542 +                                threadFail("start barrier");
543 +                            }
544 +                            try { barrier.await(); }
545 +                            catch(BrokenBarrierException ok) {}
546 +                            catch (Throwable thrown) {
547 +                                unexpectedException();
548 +                            }}});
549 +                
550 +                t1.start();
551 +                t2.start();
552 +                try { start.await(); }
553 +                catch (Exception ie) { threadFail("start barrier"); }
554 +                t1.join();
555 +                t2.join();
556 +                assertTrue(barrier.isBroken());
557 +                assertEquals(0, barrier.getNumberWaiting());
558 +                barrier.reset();
559 +                assertFalse(barrier.isBroken());
560 +                assertEquals(0, barrier.getNumberWaiting());
561 +            }
562 +        }
563 +        catch (Exception ex) {
564 +            unexpectedException();
565 +        }
566 +    }
567 +
568 +    
569 +    /**
570 +     * Reset of a barrier after a failed command reinitializes it.
571 +     */
572 +    public void testResetAfterCommandException() {
573 +        try {
574 +            final CyclicBarrier start = new CyclicBarrier(3);
575 +            final CyclicBarrier barrier =
576 +                new CyclicBarrier(3, new Runnable() {
577 +                        public void run() {
578 +                            throw new NullPointerException(); }});
579 +            for (int i = 0; i < 2; i++) {
580 +                Thread t1 = new Thread(new Runnable() {
581 +                        public void run() {
582 +                            try { start.await(); }
583 +                            catch (Exception ie) {
584 +                                threadFail("start barrier");
585 +                            }
586 +                            try { barrier.await(); }
587 +                            catch(BrokenBarrierException ok) {}
588 +                            catch (Throwable thrown) {
589 +                                unexpectedException();
590 +                            }}});
591 +                
592 +                Thread t2 = new Thread(new Runnable() {
593 +                        public void run() {
594 +                            try { start.await(); }
595 +                            catch (Exception ie) {
596 +                                threadFail("start barrier");
597 +                            }
598 +                            try { barrier.await(); }
599 +                            catch(BrokenBarrierException ok) {}
600 +                            catch (Throwable thrown) {
601 +                                unexpectedException();
602 +                            }}});
603 +                
604 +                t1.start();
605 +                t2.start();
606 +                try { start.await(); }
607 +                catch (Exception ie) { threadFail("start barrier"); }
608 +                while (barrier.getNumberWaiting() < 2) { Thread.yield(); }
609 +                try { barrier.await(); }
610 +                catch (Exception ok) { }
611 +                t1.join();
612 +                t2.join();
613 +                assertTrue(barrier.isBroken());
614 +                assertEquals(0, barrier.getNumberWaiting());
615 +                barrier.reset();
616 +                assertFalse(barrier.isBroken());
617 +                assertEquals(0, barrier.getNumberWaiting());
618 +            }
619 +        }
620 +        catch (Exception ex) {
621 +            unexpectedException();
622 +        }
623 +    }
624   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines