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

Comparing jsr166/src/test/tck/LinkedBlockingDequeTest.java (file contents):
Revision 1.32 by jsr166, Fri Nov 5 00:17:22 2010 UTC vs.
Revision 1.38 by jsr166, Sat May 21 06:24:33 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   import junit.framework.*;
# Line 617 | Line 617 | public class LinkedBlockingDequeTest ext
617              }});
618  
619          t.start();
620 <        Thread.sleep(SHORT_DELAY_MS);
620 >        delay(SHORT_DELAY_MS);
621          t.interrupt();
622          t.join();
623          assertEquals(SIZE, q.size());
# Line 641 | Line 641 | public class LinkedBlockingDequeTest ext
641              }});
642  
643          t.start();
644 <        Thread.sleep(SHORT_DELAY_MS);
644 >        delay(SHORT_DELAY_MS);
645          assertEquals(q.remainingCapacity(), 0);
646          assertEquals(0, q.take());
647 <        Thread.sleep(SHORT_DELAY_MS);
647 >        delay(SHORT_DELAY_MS);
648          t.interrupt();
649          t.join();
650          assertEquals(q.remainingCapacity(), 0);
# Line 655 | Line 655 | public class LinkedBlockingDequeTest ext
655       */
656      public void testTimedOffer() throws InterruptedException {
657          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
658 <        Thread t = new Thread(new CheckedRunnable() {
658 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
659 >        Thread t = newStartedThread(new CheckedRunnable() {
660              public void realRun() throws InterruptedException {
661                  q.put(new Object());
662                  q.put(new Object());
663 <                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
663 >                long startTime = System.nanoTime();
664 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
665 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
666 >                pleaseInterrupt.countDown();
667                  try {
668 <                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
668 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
669                      shouldThrow();
670                  } catch (InterruptedException success) {}
671              }});
672  
673 <        t.start();
670 <        Thread.sleep(SMALL_DELAY_MS);
673 >        await(pleaseInterrupt);
674          t.interrupt();
675 <        t.join();
675 >        awaitTermination(t);
676      }
677  
678      /**
# Line 699 | Line 702 | public class LinkedBlockingDequeTest ext
702              }});
703  
704          t.start();
705 <        Thread.sleep(SHORT_DELAY_MS);
705 >        delay(SHORT_DELAY_MS);
706          t.interrupt();
707          t.join();
708      }
# Line 743 | Line 746 | public class LinkedBlockingDequeTest ext
746       * returning timeout status
747       */
748      public void testInterruptedTimedPoll() throws InterruptedException {
749 <        Thread t = new Thread(new CheckedRunnable() {
749 >        final BlockingQueue<Integer> q = populatedDeque(SIZE);
750 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
751 >        Thread t = newStartedThread(new CheckedRunnable() {
752              public void realRun() throws InterruptedException {
748                LinkedBlockingDeque q = populatedDeque(SIZE);
753                  for (int i = 0; i < SIZE; ++i) {
754 <                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
754 >                    long t0 = System.nanoTime();
755 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
756 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
757                  }
758 +                long t0 = System.nanoTime();
759 +                aboutToWait.countDown();
760                  try {
761 <                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
761 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
762                      shouldThrow();
763 <                } catch (InterruptedException success) {}
763 >                } catch (InterruptedException success) {
764 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
765 >                }
766              }});
767  
768 <        t.start();
769 <        Thread.sleep(SHORT_DELAY_MS);
768 >        aboutToWait.await();
769 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
770          t.interrupt();
771 <        t.join();
771 >        awaitTermination(t, MEDIUM_DELAY_MS);
772 >        checkEmpty(q);
773      }
774  
775      /**
776       * putFirst(null) throws NPE
777       */
778 <     public void testPutFirstNull() throws InterruptedException {
778 >    public void testPutFirstNull() throws InterruptedException {
779          try {
780              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
781              q.putFirst(null);
782              shouldThrow();
783          } catch (NullPointerException success) {}
784 <     }
784 >    }
785  
786      /**
787       * all elements successfully putFirst are contained
788       */
789 <     public void testPutFirst() throws InterruptedException {
790 <         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
791 <         for (int i = 0; i < SIZE; ++i) {
792 <             Integer I = new Integer(i);
793 <             q.putFirst(I);
794 <             assertTrue(q.contains(I));
795 <         }
796 <         assertEquals(0, q.remainingCapacity());
789 >    public void testPutFirst() throws InterruptedException {
790 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
791 >        for (int i = 0; i < SIZE; ++i) {
792 >            Integer I = new Integer(i);
793 >            q.putFirst(I);
794 >            assertTrue(q.contains(I));
795 >        }
796 >        assertEquals(0, q.remainingCapacity());
797      }
798  
799      /**
# Line 803 | Line 814 | public class LinkedBlockingDequeTest ext
814              }});
815  
816          t.start();
817 <        Thread.sleep(SHORT_DELAY_MS);
817 >        delay(SHORT_DELAY_MS);
818          t.interrupt();
819          t.join();
820          assertEquals(SIZE, q.size());
# Line 827 | Line 838 | public class LinkedBlockingDequeTest ext
838              }});
839  
840          t.start();
841 <        Thread.sleep(SHORT_DELAY_MS);
841 >        delay(SHORT_DELAY_MS);
842          assertEquals(q.remainingCapacity(), 0);
843          assertEquals(capacity - 1, q.take());
844 <        Thread.sleep(SHORT_DELAY_MS);
844 >        delay(SHORT_DELAY_MS);
845          t.interrupt();
846          t.join();
847          assertEquals(q.remainingCapacity(), 0);
# Line 841 | Line 852 | public class LinkedBlockingDequeTest ext
852       */
853      public void testTimedOfferFirst() throws InterruptedException {
854          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
855 <        Thread t = new Thread(new CheckedRunnable() {
855 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
856 >        Thread t = newStartedThread(new CheckedRunnable() {
857              public void realRun() throws InterruptedException {
858                  q.putFirst(new Object());
859                  q.putFirst(new Object());
860 <                assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
860 >                long startTime = System.nanoTime();
861 >                assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS));
862 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
863 >                pleaseInterrupt.countDown();
864                  try {
865 <                    q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
865 >                    q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
866                      shouldThrow();
867                  } catch (InterruptedException success) {}
868              }});
869  
870 <        t.start();
856 <        Thread.sleep(SMALL_DELAY_MS);
870 >        await(pleaseInterrupt);
871          t.interrupt();
872 <        t.join();
872 >        awaitTermination(t);
873      }
874  
875      /**
# Line 879 | Line 893 | public class LinkedBlockingDequeTest ext
893              }};
894  
895          t.start();
896 <        Thread.sleep(SHORT_DELAY_MS);
896 >        delay(SHORT_DELAY_MS);
897          t.interrupt();
898          t.join();
899      }
# Line 900 | Line 914 | public class LinkedBlockingDequeTest ext
914              }});
915  
916          t.start();
917 <        Thread.sleep(SHORT_DELAY_MS);
917 >        delay(SHORT_DELAY_MS);
918          t.interrupt();
919          t.join();
920      }
# Line 946 | Line 960 | public class LinkedBlockingDequeTest ext
960              }});
961  
962          t.start();
963 <        Thread.sleep(SHORT_DELAY_MS);
963 >        delay(SHORT_DELAY_MS);
964          t.interrupt();
965          t.join();
966      }
# Line 968 | Line 982 | public class LinkedBlockingDequeTest ext
982              }});
983  
984          t.start();
985 <        Thread.sleep(SMALL_DELAY_MS);
985 >        delay(SMALL_DELAY_MS);
986          assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
987          t.interrupt();
988          t.join();
# Line 977 | Line 991 | public class LinkedBlockingDequeTest ext
991      /**
992       * putLast(null) throws NPE
993       */
994 <     public void testPutLastNull() throws InterruptedException {
994 >    public void testPutLastNull() throws InterruptedException {
995          try {
996              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
997              q.putLast(null);
998              shouldThrow();
999          } catch (NullPointerException success) {}
1000 <     }
1000 >    }
1001  
1002      /**
1003       * all elements successfully putLast are contained
1004       */
1005 <     public void testPutLast() throws InterruptedException {
1006 <         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1007 <         for (int i = 0; i < SIZE; ++i) {
1008 <             Integer I = new Integer(i);
1009 <             q.putLast(I);
1010 <             assertTrue(q.contains(I));
1011 <         }
1012 <         assertEquals(0, q.remainingCapacity());
1005 >    public void testPutLast() throws InterruptedException {
1006 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1007 >        for (int i = 0; i < SIZE; ++i) {
1008 >            Integer I = new Integer(i);
1009 >            q.putLast(I);
1010 >            assertTrue(q.contains(I));
1011 >        }
1012 >        assertEquals(0, q.remainingCapacity());
1013      }
1014  
1015      /**
# Line 1016 | Line 1030 | public class LinkedBlockingDequeTest ext
1030              }});
1031  
1032          t.start();
1033 <        Thread.sleep(SHORT_DELAY_MS);
1033 >        delay(SHORT_DELAY_MS);
1034          t.interrupt();
1035          t.join();
1036          assertEquals(SIZE, q.size());
# Line 1040 | Line 1054 | public class LinkedBlockingDequeTest ext
1054              }});
1055  
1056          t.start();
1057 <        Thread.sleep(SHORT_DELAY_MS);
1057 >        delay(SHORT_DELAY_MS);
1058          assertEquals(q.remainingCapacity(), 0);
1059          assertEquals(0, q.take());
1060 <        Thread.sleep(SHORT_DELAY_MS);
1060 >        delay(SHORT_DELAY_MS);
1061          t.interrupt();
1062          t.join();
1063          assertEquals(q.remainingCapacity(), 0);
# Line 1054 | Line 1068 | public class LinkedBlockingDequeTest ext
1068       */
1069      public void testTimedOfferLast() throws InterruptedException {
1070          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1071 <        Thread t = new Thread(new CheckedRunnable() {
1071 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1072 >        Thread t = newStartedThread(new CheckedRunnable() {
1073              public void realRun() throws InterruptedException {
1074                  q.putLast(new Object());
1075                  q.putLast(new Object());
1076 <                assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1076 >                long startTime = System.nanoTime();
1077 >                assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS));
1078 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1079 >                pleaseInterrupt.countDown();
1080                  try {
1081 <                    q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1081 >                    q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
1082                      shouldThrow();
1083                  } catch (InterruptedException success) {}
1084              }});
1085  
1086 <        t.start();
1069 <        Thread.sleep(SMALL_DELAY_MS);
1086 >        await(pleaseInterrupt);
1087          t.interrupt();
1088 <        t.join();
1088 >        awaitTermination(t);
1089      }
1090  
1091      /**
# Line 1092 | Line 1109 | public class LinkedBlockingDequeTest ext
1109              }};
1110  
1111          t.start();
1112 <        Thread.sleep(SHORT_DELAY_MS);
1112 >        delay(SHORT_DELAY_MS);
1113          t.interrupt();
1114          t.join();
1115      }
# Line 1113 | Line 1130 | public class LinkedBlockingDequeTest ext
1130              }});
1131  
1132          t.start();
1133 <        Thread.sleep(SHORT_DELAY_MS);
1133 >        delay(SHORT_DELAY_MS);
1134          t.interrupt();
1135          t.join();
1136      }
# Line 1158 | Line 1175 | public class LinkedBlockingDequeTest ext
1175              }});
1176  
1177          t.start();
1178 <        Thread.sleep(SHORT_DELAY_MS);
1178 >        delay(SHORT_DELAY_MS);
1179          t.interrupt();
1180          t.join();
1181      }
# Line 1180 | Line 1197 | public class LinkedBlockingDequeTest ext
1197              }});
1198  
1199          t.start();
1200 <        Thread.sleep(SMALL_DELAY_MS);
1200 >        delay(SMALL_DELAY_MS);
1201          assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1202          t.interrupt();
1203          t.join();
# Line 1208 | Line 1225 | public class LinkedBlockingDequeTest ext
1225      public void testRemoveElement() {
1226          LinkedBlockingDeque q = populatedDeque(SIZE);
1227          for (int i = 1; i < SIZE; i+=2) {
1228 <            assertTrue(q.remove(new Integer(i)));
1228 >            assertTrue(q.contains(i));
1229 >            assertTrue(q.remove(i));
1230 >            assertFalse(q.contains(i));
1231 >            assertTrue(q.contains(i-1));
1232          }
1233          for (int i = 0; i < SIZE; i+=2) {
1234 <            assertTrue(q.remove(new Integer(i)));
1235 <            assertFalse(q.remove(new Integer(i+1)));
1234 >            assertTrue(q.contains(i));
1235 >            assertTrue(q.remove(i));
1236 >            assertFalse(q.contains(i));
1237 >            assertFalse(q.remove(i+1));
1238 >            assertFalse(q.contains(i+1));
1239          }
1240          assertTrue(q.isEmpty());
1241      }
# Line 1494 | Line 1517 | public class LinkedBlockingDequeTest ext
1517  
1518          executor.execute(new CheckedRunnable() {
1519              public void realRun() throws InterruptedException {
1520 <                Thread.sleep(SMALL_DELAY_MS);
1520 >                delay(SMALL_DELAY_MS);
1521                  assertSame(one, q.take());
1522              }});
1523  
# Line 1516 | Line 1539 | public class LinkedBlockingDequeTest ext
1539  
1540          executor.execute(new CheckedRunnable() {
1541              public void realRun() throws InterruptedException {
1542 <                Thread.sleep(SMALL_DELAY_MS);
1542 >                delay(SMALL_DELAY_MS);
1543                  q.put(one);
1544              }});
1545  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines