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

Comparing jsr166/src/test/tck/ArrayBlockingQueueTest.java (file contents):
Revision 1.18 by jsr166, Sat Nov 21 10:25:05 2009 UTC vs.
Revision 1.47 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 Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
# Line 14 | Line 13 | import static java.util.concurrent.TimeU
13   import java.io.*;
14  
15   public class ArrayBlockingQueueTest extends JSR166TestCase {
16 +
17 +    public static class Fair extends BlockingQueueTest {
18 +        protected BlockingQueue emptyCollection() {
19 +            return new ArrayBlockingQueue(20, true);
20 +        }
21 +    }
22 +
23 +    public static class NonFair extends BlockingQueueTest {
24 +        protected BlockingQueue emptyCollection() {
25 +            return new ArrayBlockingQueue(20, false);
26 +        }
27 +    }
28 +
29      public static void main(String[] args) {
30 <        junit.textui.TestRunner.run (suite());
30 >        junit.textui.TestRunner.run(suite());
31      }
32 +
33      public static Test suite() {
34 <        return new TestSuite(ArrayBlockingQueueTest.class);
34 >        return newTestSuite(ArrayBlockingQueueTest.class,
35 >                            new Fair().testSuite(),
36 >                            new NonFair().testSuite());
37      }
38  
39      /**
40       * Create a queue of given size containing consecutive
41       * Integers 0 ... n.
42       */
43 <    private ArrayBlockingQueue populatedQueue(int n) {
44 <        ArrayBlockingQueue q = new ArrayBlockingQueue(n);
43 >    private ArrayBlockingQueue<Integer> populatedQueue(int n) {
44 >        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
45          assertTrue(q.isEmpty());
46          for (int i = 0; i < n; i++)
47              assertTrue(q.offer(new Integer(i)));
# Line 104 | Line 119 | public class ArrayBlockingQueueTest exte
119       * Queue contains all elements of collection used to initialize
120       */
121      public void testConstructor7() {
122 <        try {
123 <            Integer[] ints = new Integer[SIZE];
124 <            for (int i = 0; i < SIZE; ++i)
125 <                ints[i] = new Integer(i);
126 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
127 <            for (int i = 0; i < SIZE; ++i)
113 <                assertEquals(ints[i], q.poll());
114 <        }
115 <        finally {}
122 >        Integer[] ints = new Integer[SIZE];
123 >        for (int i = 0; i < SIZE; ++i)
124 >            ints[i] = new Integer(i);
125 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
126 >        for (int i = 0; i < SIZE; ++i)
127 >            assertEquals(ints[i], q.poll());
128      }
129  
130      /**
# Line 148 | Line 160 | public class ArrayBlockingQueueTest exte
160      }
161  
162      /**
163 <     *  offer(null) throws NPE
163 >     * offer(null) throws NPE
164       */
165      public void testOfferNull() {
166          try {
# Line 159 | Line 171 | public class ArrayBlockingQueueTest exte
171      }
172  
173      /**
174 <     *  add(null) throws NPE
174 >     * add(null) throws NPE
175       */
176      public void testAddNull() {
177          try {
# Line 194 | Line 206 | public class ArrayBlockingQueueTest exte
206      }
207  
208      /**
209 <     *  addAll(null) throws NPE
209 >     * addAll(null) throws NPE
210       */
211      public void testAddAll1() {
212          try {
# Line 215 | Line 227 | public class ArrayBlockingQueueTest exte
227          } catch (IllegalArgumentException success) {}
228      }
229  
218
230      /**
231 <     *  addAll of a collection with null elements throws NPE
231 >     * addAll of a collection with null elements throws NPE
232       */
233      public void testAddAll2() {
234          try {
# Line 227 | Line 238 | public class ArrayBlockingQueueTest exte
238              shouldThrow();
239          } catch (NullPointerException success) {}
240      }
241 +
242      /**
243       * addAll of a collection with any null elements throws NPE after
244       * possibly adding some elements
# Line 241 | Line 253 | public class ArrayBlockingQueueTest exte
253              shouldThrow();
254          } catch (NullPointerException success) {}
255      }
256 +
257      /**
258       * addAll throws ISE if not enough room
259       */
# Line 254 | Line 267 | public class ArrayBlockingQueueTest exte
267              shouldThrow();
268          } catch (IllegalStateException success) {}
269      }
270 +
271      /**
272       * Queue contains all elements, in traversal order, of successful addAll
273       */
# Line 270 | Line 284 | public class ArrayBlockingQueueTest exte
284      }
285  
286      /**
287 <     *  put(null) throws NPE
287 >     * put(null) throws NPE
288       */
289      public void testPutNull() throws InterruptedException {
290          try {
# Line 278 | Line 292 | public class ArrayBlockingQueueTest exte
292              q.put(null);
293              shouldThrow();
294          } catch (NullPointerException success) {}
295 <     }
295 >    }
296  
297      /**
298       * all elements successfully put are contained
# Line 298 | Line 312 | public class ArrayBlockingQueueTest exte
312       */
313      public void testBlockingPut() throws InterruptedException {
314          final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
315 <        Thread t = new Thread(new CheckedRunnable() {
316 <            public void realRun() {
317 <                int added = 0;
315 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
316 >        Thread t = newStartedThread(new CheckedRunnable() {
317 >            public void realRun() throws InterruptedException {
318 >                for (int i = 0; i < SIZE; ++i)
319 >                    q.put(i);
320 >                assertEquals(SIZE, q.size());
321 >                assertEquals(0, q.remainingCapacity());
322 >
323 >                Thread.currentThread().interrupt();
324                  try {
325 <                    for (int i = 0; i < SIZE; ++i) {
326 <                        q.put(new Integer(i));
327 <                        ++added;
328 <                    }
309 <                    q.put(new Integer(SIZE));
310 <                    threadShouldThrow();
311 <                } catch (InterruptedException ie) {
312 <                    threadAssertEquals(added, SIZE);
313 <                }}});
325 >                    q.put(99);
326 >                    shouldThrow();
327 >                } catch (InterruptedException success) {}
328 >                assertFalse(Thread.interrupted());
329  
330 <        t.start();
331 <        Thread.sleep(MEDIUM_DELAY_MS);
330 >                pleaseInterrupt.countDown();
331 >                try {
332 >                    q.put(99);
333 >                    shouldThrow();
334 >                } catch (InterruptedException success) {}
335 >                assertFalse(Thread.interrupted());
336 >            }});
337 >
338 >        await(pleaseInterrupt);
339 >        assertThreadStaysAlive(t);
340          t.interrupt();
341 <        t.join();
341 >        awaitTermination(t);
342 >        assertEquals(SIZE, q.size());
343 >        assertEquals(0, q.remainingCapacity());
344      }
345  
346      /**
347 <     * put blocks waiting for take when full
347 >     * put blocks interruptibly waiting for take when full
348       */
349      public void testPutWithTake() throws InterruptedException {
350 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
351 <        Thread t = new Thread(new CheckedRunnable() {
352 <            public void realRun() {
353 <                int added = 0;
350 >        final int capacity = 2;
351 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity);
352 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
353 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
354 >        Thread t = newStartedThread(new CheckedRunnable() {
355 >            public void realRun() throws InterruptedException {
356 >                for (int i = 0; i < capacity; i++)
357 >                    q.put(i);
358 >                pleaseTake.countDown();
359 >                q.put(86);
360 >
361 >                pleaseInterrupt.countDown();
362                  try {
363 <                    q.put(new Object());
364 <                    ++added;
365 <                    q.put(new Object());
366 <                    ++added;
334 <                    q.put(new Object());
335 <                    ++added;
336 <                    q.put(new Object());
337 <                    ++added;
338 <                    threadShouldThrow();
339 <                } catch (InterruptedException e) {
340 <                    threadAssertTrue(added >= 2);
341 <                }
363 >                    q.put(99);
364 >                    shouldThrow();
365 >                } catch (InterruptedException success) {}
366 >                assertFalse(Thread.interrupted());
367              }});
368  
369 <        t.start();
370 <        Thread.sleep(SHORT_DELAY_MS);
371 <        q.take();
369 >        await(pleaseTake);
370 >        assertEquals(q.remainingCapacity(), 0);
371 >        assertEquals(0, q.take());
372 >
373 >        await(pleaseInterrupt);
374 >        assertThreadStaysAlive(t);
375          t.interrupt();
376 <        t.join();
376 >        awaitTermination(t);
377 >        assertEquals(q.remainingCapacity(), 0);
378      }
379  
380      /**
# Line 353 | Line 382 | public class ArrayBlockingQueueTest exte
382       */
383      public void testTimedOffer() throws InterruptedException {
384          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
385 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
385 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
386 >        Thread t = newStartedThread(new CheckedRunnable() {
387              public void realRun() throws InterruptedException {
388                  q.put(new Object());
389                  q.put(new Object());
390 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS));
391 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
392 <            }};
390 >                long startTime = System.nanoTime();
391 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
392 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
393 >                pleaseInterrupt.countDown();
394 >                try {
395 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
396 >                    shouldThrow();
397 >                } catch (InterruptedException success) {}
398 >            }});
399  
400 <        t.start();
401 <        Thread.sleep(SHORT_DELAY_MS);
400 >        await(pleaseInterrupt);
401 >        assertThreadStaysAlive(t);
402          t.interrupt();
403 <        t.join();
403 >        awaitTermination(t);
404      }
405  
406      /**
# Line 373 | Line 409 | public class ArrayBlockingQueueTest exte
409      public void testTake() throws InterruptedException {
410          ArrayBlockingQueue q = populatedQueue(SIZE);
411          for (int i = 0; i < SIZE; ++i) {
412 <            assertEquals(i, ((Integer)q.take()).intValue());
412 >            assertEquals(i, q.take());
413          }
414      }
415  
416      /**
381     * take blocks interruptibly when empty
382     */
383    public void testTakeFromEmpty() throws InterruptedException {
384        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
385        Thread t = new ThreadShouldThrow(InterruptedException.class) {
386            public void realRun() throws InterruptedException {
387                q.take();
388            }};
389
390        t.start();
391        Thread.sleep(SHORT_DELAY_MS);
392        t.interrupt();
393        t.join();
394    }
395
396    /**
417       * Take removes existing elements until empty, then blocks interruptibly
418       */
419      public void testBlockingTake() throws InterruptedException {
420 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
420 >        final ArrayBlockingQueue q = populatedQueue(SIZE);
421 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
422 >        Thread t = newStartedThread(new CheckedRunnable() {
423              public void realRun() throws InterruptedException {
402                ArrayBlockingQueue q = populatedQueue(SIZE);
424                  for (int i = 0; i < SIZE; ++i) {
425 <                    threadAssertEquals(i, ((Integer)q.take()).intValue());
425 >                    assertEquals(i, q.take());
426                  }
406                q.take();
407            }};
427  
428 <        t.start();
429 <            Thread.sleep(SHORT_DELAY_MS);
430 <            t.interrupt();
431 <            t.join();
432 <    }
428 >                Thread.currentThread().interrupt();
429 >                try {
430 >                    q.take();
431 >                    shouldThrow();
432 >                } catch (InterruptedException success) {}
433 >                assertFalse(Thread.interrupted());
434  
435 +                pleaseInterrupt.countDown();
436 +                try {
437 +                    q.take();
438 +                    shouldThrow();
439 +                } catch (InterruptedException success) {}
440 +                assertFalse(Thread.interrupted());
441 +            }});
442 +
443 +        await(pleaseInterrupt);
444 +        assertThreadStaysAlive(t);
445 +        t.interrupt();
446 +        awaitTermination(t);
447 +    }
448  
449      /**
450       * poll succeeds unless empty
# Line 419 | Line 452 | public class ArrayBlockingQueueTest exte
452      public void testPoll() {
453          ArrayBlockingQueue q = populatedQueue(SIZE);
454          for (int i = 0; i < SIZE; ++i) {
455 <            assertEquals(i, ((Integer)q.poll()).intValue());
455 >            assertEquals(i, q.poll());
456          }
457          assertNull(q.poll());
458      }
459  
460      /**
461 <     * timed pool with zero timeout succeeds when non-empty, else times out
461 >     * timed poll with zero timeout succeeds when non-empty, else times out
462       */
463      public void testTimedPoll0() throws InterruptedException {
464          ArrayBlockingQueue q = populatedQueue(SIZE);
465          for (int i = 0; i < SIZE; ++i) {
466 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
466 >            assertEquals(i, q.poll(0, MILLISECONDS));
467          }
468          assertNull(q.poll(0, MILLISECONDS));
469 +        checkEmpty(q);
470      }
471  
472      /**
473 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
473 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
474       */
475      public void testTimedPoll() throws InterruptedException {
476          ArrayBlockingQueue q = populatedQueue(SIZE);
477          for (int i = 0; i < SIZE; ++i) {
478 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
479 <        }
480 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
478 >            long startTime = System.nanoTime();
479 >            assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
480 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
481 >        }
482 >        long startTime = System.nanoTime();
483 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
484 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
485 >        checkEmpty(q);
486      }
487  
488      /**
# Line 451 | Line 490 | public class ArrayBlockingQueueTest exte
490       * returning timeout status
491       */
492      public void testInterruptedTimedPoll() throws InterruptedException {
493 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
493 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
494 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
495 >        Thread t = newStartedThread(new CheckedRunnable() {
496              public void realRun() throws InterruptedException {
456                ArrayBlockingQueue q = populatedQueue(SIZE);
497                  for (int i = 0; i < SIZE; ++i) {
498 <                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
498 >                    long t0 = System.nanoTime();
499 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
500 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
501                  }
502 <                q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
503 <            }};
504 <
505 <        t.start();
506 <        Thread.sleep(SMALL_DELAY_MS);
507 <        t.interrupt();
508 <        t.join();
509 <    }
510 <
469 <    /**
470 <     *  timed poll before a delayed offer fails; after offer succeeds;
471 <     *  on interruption throws
472 <     */
473 <    public void testTimedPollWithOffer() throws InterruptedException {
474 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
475 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
476 <            public void realRun() throws InterruptedException {
477 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
478 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
479 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
480 <            }};
502 >                long t0 = System.nanoTime();
503 >                aboutToWait.countDown();
504 >                try {
505 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
506 >                    shouldThrow();
507 >                } catch (InterruptedException success) {
508 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
509 >                }
510 >            }});
511  
512 <        t.start();
513 <        Thread.sleep(SMALL_DELAY_MS);
484 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
512 >        aboutToWait.await();
513 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
514          t.interrupt();
515 <        t.join();
515 >        awaitTermination(t, MEDIUM_DELAY_MS);
516 >        checkEmpty(q);
517      }
518  
489
519      /**
520       * peek returns next element, or null if empty
521       */
522      public void testPeek() {
523          ArrayBlockingQueue q = populatedQueue(SIZE);
524          for (int i = 0; i < SIZE; ++i) {
525 <            assertEquals(i, ((Integer)q.peek()).intValue());
526 <            q.poll();
525 >            assertEquals(i, q.peek());
526 >            assertEquals(i, q.poll());
527              assertTrue(q.peek() == null ||
528 <                       i != ((Integer)q.peek()).intValue());
528 >                       !q.peek().equals(i));
529          }
530          assertNull(q.peek());
531      }
# Line 507 | Line 536 | public class ArrayBlockingQueueTest exte
536      public void testElement() {
537          ArrayBlockingQueue q = populatedQueue(SIZE);
538          for (int i = 0; i < SIZE; ++i) {
539 <            assertEquals(i, ((Integer)q.element()).intValue());
540 <            q.poll();
539 >            assertEquals(i, q.element());
540 >            assertEquals(i, q.poll());
541          }
542          try {
543              q.element();
# Line 522 | Line 551 | public class ArrayBlockingQueueTest exte
551      public void testRemove() {
552          ArrayBlockingQueue q = populatedQueue(SIZE);
553          for (int i = 0; i < SIZE; ++i) {
554 <            assertEquals(i, ((Integer)q.remove()).intValue());
554 >            assertEquals(i, q.remove());
555          }
556          try {
557              q.remove();
# Line 552 | Line 581 | public class ArrayBlockingQueueTest exte
581          ArrayBlockingQueue q = populatedQueue(SIZE);
582          for (int i = 0; i < SIZE; ++i) {
583              assertTrue(q.contains(new Integer(i)));
584 <            q.poll();
584 >            assertEquals(i, q.poll());
585              assertFalse(q.contains(new Integer(i)));
586          }
587      }
# Line 623 | Line 652 | public class ArrayBlockingQueueTest exte
652      }
653  
654      /**
655 <     *  toArray contains all elements
655 >     * toArray contains all elements in FIFO order
656       */
657 <    public void testToArray() throws InterruptedException {
657 >    public void testToArray() {
658          ArrayBlockingQueue q = populatedQueue(SIZE);
659          Object[] o = q.toArray();
660          for (int i = 0; i < o.length; i++)
661 <            assertEquals(o[i], q.take());
661 >            assertSame(o[i], q.poll());
662      }
663  
664      /**
665 <     * toArray(a) contains all elements
665 >     * toArray(a) contains all elements in FIFO order
666       */
667 <    public void testToArray2() throws InterruptedException {
668 <        ArrayBlockingQueue q = populatedQueue(SIZE);
667 >    public void testToArray2() {
668 >        ArrayBlockingQueue<Integer> q = populatedQueue(SIZE);
669          Integer[] ints = new Integer[SIZE];
670 <        ints = (Integer[])q.toArray(ints);
670 >        Integer[] array = q.toArray(ints);
671 >        assertSame(ints, array);
672          for (int i = 0; i < ints.length; i++)
673 <            assertEquals(ints[i], q.take());
673 >            assertSame(ints[i], q.poll());
674      }
675  
676      /**
677 <     * toArray(null) throws NPE
677 >     * toArray(null) throws NullPointerException
678       */
679 <    public void testToArray_BadArg() {
679 >    public void testToArray_NullArg() {
680 >        ArrayBlockingQueue q = populatedQueue(SIZE);
681          try {
682 <            ArrayBlockingQueue q = populatedQueue(SIZE);
652 <            Object o[] = q.toArray(null);
682 >            q.toArray(null);
683              shouldThrow();
684          } catch (NullPointerException success) {}
685      }
686  
687      /**
688 <     * toArray with incompatible array type throws CCE
688 >     * toArray(incompatible array type) throws ArrayStoreException
689       */
690      public void testToArray1_BadArg() {
691 +        ArrayBlockingQueue q = populatedQueue(SIZE);
692          try {
693 <            ArrayBlockingQueue q = populatedQueue(SIZE);
663 <            Object o[] = q.toArray(new String[10] );
693 >            q.toArray(new String[10]);
694              shouldThrow();
695          } catch (ArrayStoreException success) {}
696      }
697  
668
698      /**
699       * iterator iterates through all elements
700       */
# Line 680 | Line 709 | public class ArrayBlockingQueueTest exte
709      /**
710       * iterator.remove removes current element
711       */
712 <    public void testIteratorRemove () {
712 >    public void testIteratorRemove() {
713          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
714          q.add(two);
715          q.add(one);
# Line 691 | Line 720 | public class ArrayBlockingQueueTest exte
720          it.remove();
721  
722          it = q.iterator();
723 <        assertEquals(it.next(), one);
724 <        assertEquals(it.next(), three);
723 >        assertSame(it.next(), one);
724 >        assertSame(it.next(), three);
725          assertFalse(it.hasNext());
726      }
727  
# Line 709 | Line 738 | public class ArrayBlockingQueueTest exte
738  
739          int k = 0;
740          for (Iterator it = q.iterator(); it.hasNext();) {
741 <            int i = ((Integer)(it.next())).intValue();
713 <            assertEquals(++k, i);
741 >            assertEquals(++k, it.next());
742          }
743          assertEquals(3, k);
744      }
# Line 718 | Line 746 | public class ArrayBlockingQueueTest exte
746      /**
747       * Modifications do not cause iterators to fail
748       */
749 <    public void testWeaklyConsistentIteration () {
749 >    public void testWeaklyConsistentIteration() {
750          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
751          q.add(one);
752          q.add(two);
# Line 730 | Line 758 | public class ArrayBlockingQueueTest exte
758          assertEquals(0, q.size());
759      }
760  
733
761      /**
762       * toString contains toStrings of elements
763       */
# Line 738 | Line 765 | public class ArrayBlockingQueueTest exte
765          ArrayBlockingQueue q = populatedQueue(SIZE);
766          String s = q.toString();
767          for (int i = 0; i < SIZE; ++i) {
768 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
768 >            assertTrue(s.contains(String.valueOf(i)));
769          }
770      }
771  
745
772      /**
773       * offer transfers elements across Executor tasks
774       */
# Line 751 | Line 777 | public class ArrayBlockingQueueTest exte
777          q.add(one);
778          q.add(two);
779          ExecutorService executor = Executors.newFixedThreadPool(2);
780 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
781          executor.execute(new CheckedRunnable() {
782              public void realRun() throws InterruptedException {
783 <                threadAssertFalse(q.offer(three));
784 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
785 <                threadAssertEquals(0, q.remainingCapacity());
783 >                assertFalse(q.offer(three));
784 >                threadsStarted.await();
785 >                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
786 >                assertEquals(0, q.remainingCapacity());
787              }});
788  
789          executor.execute(new CheckedRunnable() {
790              public void realRun() throws InterruptedException {
791 <                Thread.sleep(SMALL_DELAY_MS);
792 <                threadAssertEquals(one, q.take());
791 >                threadsStarted.await();
792 >                assertEquals(0, q.remainingCapacity());
793 >                assertSame(one, q.take());
794              }});
795  
796          joinPool(executor);
768
797      }
798  
799      /**
800 <     * poll retrieves elements across Executor threads
800 >     * timed poll retrieves elements across Executor threads
801       */
802      public void testPollInExecutor() {
803          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
804 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
805          ExecutorService executor = Executors.newFixedThreadPool(2);
806          executor.execute(new CheckedRunnable() {
807              public void realRun() throws InterruptedException {
808 <                threadAssertNull(q.poll());
809 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
810 <                threadAssertTrue(q.isEmpty());
808 >                assertNull(q.poll());
809 >                threadsStarted.await();
810 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
811 >                checkEmpty(q);
812              }});
813  
814          executor.execute(new CheckedRunnable() {
815              public void realRun() throws InterruptedException {
816 <                Thread.sleep(SMALL_DELAY_MS);
816 >                threadsStarted.await();
817                  q.put(one);
818              }});
819  
# Line 898 | Line 928 | public class ArrayBlockingQueueTest exte
928      }
929  
930      /**
931 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
931 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
932       */
933      public void testDrainToN() {
934          ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
# Line 907 | Line 937 | public class ArrayBlockingQueueTest exte
937                  assertTrue(q.offer(new Integer(j)));
938              ArrayList l = new ArrayList();
939              q.drainTo(l, i);
940 <            int k = (i < SIZE)? i : SIZE;
940 >            int k = (i < SIZE) ? i : SIZE;
941              assertEquals(l.size(), k);
942              assertEquals(q.size(), SIZE-k);
943              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines