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

Comparing jsr166/src/test/tck/LinkedTransferQueueTest.java (file contents):
Revision 1.39 by jsr166, Fri Nov 5 00:17:22 2010 UTC vs.
Revision 1.46 by jsr166, Fri May 27 20:07:24 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   * Other contributors include John Vint
6   */
7  
# Line 40 | Line 40 | public class LinkedTransferQueueTest ext
40                              new Generic().testSuite());
41      }
42  
43    void checkEmpty(BlockingQueue q) {
44        try {
45            assertTrue(q.isEmpty());
46            assertEquals(0, q.size());
47            assertNull(q.peek());
48            assertNull(q.poll());
49            assertNull(q.poll(0, MILLISECONDS));
50            assertEquals(q.toString(), "[]");
51            assertTrue(Arrays.equals(q.toArray(), new Object[0]));
52            assertFalse(q.iterator().hasNext());
53            try {
54                q.element();
55                shouldThrow();
56            } catch (NoSuchElementException success) {}
57            try {
58                q.iterator().next();
59                shouldThrow();
60            } catch (NoSuchElementException success) {}
61            try {
62                q.remove();
63                shouldThrow();
64            } catch (NoSuchElementException success) {}
65        } catch (InterruptedException ie) {
66            threadUnexpectedException(ie);
67        }
68    }
69
43      /**
44       * Constructor builds new queue with size being zero and empty
45       * being true
# Line 281 | Line 254 | public class LinkedTransferQueueTest ext
254       * take removes existing elements until empty, then blocks interruptibly
255       */
256      public void testBlockingTake() throws InterruptedException {
257 <        final BlockingQueue<Integer> q = populatedQueue(SIZE);
258 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
257 >        final BlockingQueue q = populatedQueue(SIZE);
258 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
259          Thread t = newStartedThread(new CheckedRunnable() {
260              public void realRun() throws InterruptedException {
261                  for (int i = 0; i < SIZE; ++i) {
262 <                    assertEquals(i, (int) q.take());
262 >                    assertEquals(i, q.take());
263                  }
264 <                aboutToWait.countDown();
264 >
265 >                Thread.currentThread().interrupt();
266                  try {
267                      q.take();
268                      shouldThrow();
269                  } catch (InterruptedException success) {}
270 +                assertFalse(Thread.interrupted());
271 +
272 +                pleaseInterrupt.countDown();
273 +                try {
274 +                    q.take();
275 +                    shouldThrow();
276 +                } catch (InterruptedException success) {}
277 +                assertFalse(Thread.interrupted());
278              }});
279  
280 <        aboutToWait.await();
281 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
280 >        await(pleaseInterrupt);
281 >        assertThreadStaysAlive(t);
282          t.interrupt();
283 <        awaitTermination(t, MEDIUM_DELAY_MS);
302 <        checkEmpty(q);
283 >        awaitTermination(t);
284      }
285  
286      /**
# Line 332 | Line 313 | public class LinkedTransferQueueTest ext
313      public void testTimedPoll() throws InterruptedException {
314          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
315          for (int i = 0; i < SIZE; ++i) {
316 <            long t0 = System.nanoTime();
317 <            assertEquals(i, (int) q.poll(SMALL_DELAY_MS, MILLISECONDS));
318 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
319 <        }
320 <        long t0 = System.nanoTime();
321 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
322 <        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
316 >            long startTime = System.nanoTime();
317 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
318 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
319 >        }
320 >        long startTime = System.nanoTime();
321 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
322 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
323          checkEmpty(q);
324      }
325  
# Line 448 | Line 429 | public class LinkedTransferQueueTest ext
429       */
430      public void testRemoveElement() throws InterruptedException {
431          LinkedTransferQueue q = populatedQueue(SIZE);
432 <        for (int i = 1; i < SIZE; i += 2) {
432 >        for (int i = 1; i < SIZE; i+=2) {
433 >            assertTrue(q.contains(i));
434              assertTrue(q.remove(i));
435 +            assertFalse(q.contains(i));
436 +            assertTrue(q.contains(i-1));
437          }
438 <        for (int i = 0; i < SIZE; i += 2) {
438 >        for (int i = 0; i < SIZE; i+=2) {
439 >            assertTrue(q.contains(i));
440              assertTrue(q.remove(i));
441 <            assertFalse(q.remove(i + 1));
441 >            assertFalse(q.contains(i));
442 >            assertFalse(q.remove(i+1));
443 >            assertFalse(q.contains(i+1));
444          }
445          checkEmpty(q);
446      }
# Line 667 | Line 654 | public class LinkedTransferQueueTest ext
654          LinkedTransferQueue q = populatedQueue(SIZE);
655          String s = q.toString();
656          for (int i = 0; i < SIZE; ++i) {
657 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
657 >            assertTrue(s.contains(String.valueOf(i)));
658          }
659      }
660  
# Line 676 | Line 663 | public class LinkedTransferQueueTest ext
663       */
664      public void testOfferInExecutor() {
665          final LinkedTransferQueue q = new LinkedTransferQueue();
666 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
666 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
667          ExecutorService executor = Executors.newFixedThreadPool(2);
668  
669          executor.execute(new CheckedRunnable() {
670              public void realRun() throws InterruptedException {
684                threadsStarted.countDown();
671                  threadsStarted.await();
672 <                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
672 >                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
673              }});
674  
675          executor.execute(new CheckedRunnable() {
676              public void realRun() throws InterruptedException {
691                threadsStarted.countDown();
677                  threadsStarted.await();
678                  assertSame(one, q.take());
679                  checkEmpty(q);
# Line 702 | Line 687 | public class LinkedTransferQueueTest ext
687       */
688      public void testPollInExecutor() {
689          final LinkedTransferQueue q = new LinkedTransferQueue();
690 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
690 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
691          ExecutorService executor = Executors.newFixedThreadPool(2);
692  
693          executor.execute(new CheckedRunnable() {
694              public void realRun() throws InterruptedException {
695                  assertNull(q.poll());
711                threadsStarted.countDown();
696                  threadsStarted.await();
697 <                assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS));
697 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
698                  checkEmpty(q);
699              }});
700  
701          executor.execute(new CheckedRunnable() {
702              public void realRun() throws InterruptedException {
719                threadsStarted.countDown();
703                  threadsStarted.await();
704                  q.put(one);
705              }});
# Line 1072 | Line 1055 | public class LinkedTransferQueueTest ext
1055      }
1056  
1057      /**
1058 <     * tryTransfer waits the amount given if interrupted, and
1076 <     * throws interrupted exception
1058 >     * tryTransfer blocks interruptibly if no takers
1059       */
1060      public void testTryTransfer5() throws InterruptedException {
1061          final LinkedTransferQueue q = new LinkedTransferQueue();
1062 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1062 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1063 >        assertTrue(q.isEmpty());
1064  
1065          Thread t = newStartedThread(new CheckedRunnable() {
1066              public void realRun() throws InterruptedException {
1067 <                long t0 = System.nanoTime();
1085 <                threadStarted.countDown();
1067 >                Thread.currentThread().interrupt();
1068                  try {
1069                      q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1070                      shouldThrow();
1071                  } catch (InterruptedException success) {}
1072 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1072 >                assertFalse(Thread.interrupted());
1073 >
1074 >                pleaseInterrupt.countDown();
1075 >                try {
1076 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1077 >                    shouldThrow();
1078 >                } catch (InterruptedException success) {}
1079 >                assertFalse(Thread.interrupted());
1080              }});
1081  
1082 <        threadStarted.await();
1083 <        Thread.sleep(SHORT_DELAY_MS);
1082 >        await(pleaseInterrupt);
1083 >        assertThreadStaysAlive(t);
1084          t.interrupt();
1085 <        awaitTermination(t, MEDIUM_DELAY_MS);
1085 >        awaitTermination(t);
1086          checkEmpty(q);
1087      }
1088  
# Line 1107 | Line 1096 | public class LinkedTransferQueueTest ext
1096              public void realRun() throws InterruptedException {
1097                  long t0 = System.nanoTime();
1098                  assertFalse(q.tryTransfer(new Object(),
1099 <                                          SHORT_DELAY_MS, MILLISECONDS));
1100 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1099 >                                          timeoutMillis(), MILLISECONDS));
1100 >                assertTrue(millisElapsedSince(t0) >= timeoutMillis());
1101                  checkEmpty(q);
1102              }});
1103  
1104 <        awaitTermination(t, MEDIUM_DELAY_MS);
1104 >        awaitTermination(t);
1105          checkEmpty(q);
1106      }
1107  
# Line 1140 | Line 1129 | public class LinkedTransferQueueTest ext
1129      }
1130  
1131      /**
1132 <     * tryTransfer attempts to enqueue into the q and fails returning
1133 <     * false not enqueueing and the successive poll is null
1132 >     * tryTransfer attempts to enqueue into the queue and fails
1133 >     * returning false not enqueueing and the successive poll is null
1134       */
1135      public void testTryTransfer8() throws InterruptedException {
1136          final LinkedTransferQueue q = new LinkedTransferQueue();
1137          assertTrue(q.offer(four));
1138          assertEquals(1, q.size());
1139          long t0 = System.nanoTime();
1140 <        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1141 <        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1140 >        assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
1141 >        assertTrue(millisElapsedSince(t0) >= timeoutMillis());
1142          assertEquals(1, q.size());
1143          assertSame(four, q.poll());
1144          assertNull(q.poll());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines