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.21 by jsr166, Sat Nov 21 19:11:53 2009 UTC vs.
Revision 1.26 by jsr166, Sun Nov 22 18:57:16 2009 UTC

# Line 296 | Line 296 | public class ArrayBlockingQueueTest exte
296      public void testBlockingPut() throws InterruptedException {
297          final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
298          Thread t = new Thread(new CheckedRunnable() {
299 <            public void realRun() {
300 <                int added = 0;
299 >            public void realRun() throws InterruptedException {
300 >                for (int i = 0; i < SIZE; ++i)
301 >                    q.put(i);
302 >                assertEquals(SIZE, q.size());
303 >                assertEquals(0, q.remainingCapacity());
304                  try {
305 <                    for (int i = 0; i < SIZE; ++i) {
306 <                        q.put(new Integer(i));
307 <                        ++added;
308 <                    }
306 <                    q.put(new Integer(SIZE));
307 <                    threadShouldThrow();
308 <                } catch (InterruptedException success) {
309 <                    threadAssertEquals(added, SIZE);
310 <                }}});
305 >                    q.put(99);
306 >                    shouldThrow();
307 >                } catch (InterruptedException success) {}
308 >            }});
309  
310          t.start();
311 <        Thread.sleep(MEDIUM_DELAY_MS);
311 >        Thread.sleep(SHORT_DELAY_MS);
312          t.interrupt();
313          t.join();
314 +        assertEquals(SIZE, q.size());
315 +        assertEquals(0, q.remainingCapacity());
316      }
317  
318      /**
# Line 350 | Line 350 | public class ArrayBlockingQueueTest exte
350       */
351      public void testTimedOffer() throws InterruptedException {
352          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
353 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
353 >        Thread t = new Thread(new CheckedRunnable() {
354              public void realRun() throws InterruptedException {
355                  q.put(new Object());
356                  q.put(new Object());
357 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS));
358 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
359 <            }};
357 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS));
358 >                try {
359 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
360 >                    shouldThrow();
361 >                } catch (InterruptedException success) {}
362 >            }});
363  
364          t.start();
365          Thread.sleep(SHORT_DELAY_MS);
# Line 370 | Line 373 | public class ArrayBlockingQueueTest exte
373      public void testTake() throws InterruptedException {
374          ArrayBlockingQueue q = populatedQueue(SIZE);
375          for (int i = 0; i < SIZE; ++i) {
376 <            assertEquals(i, ((Integer)q.take()).intValue());
376 >            assertEquals(i, q.take());
377          }
378      }
379  
# Line 394 | Line 397 | public class ArrayBlockingQueueTest exte
397       * Take removes existing elements until empty, then blocks interruptibly
398       */
399      public void testBlockingTake() throws InterruptedException {
400 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
400 >        final ArrayBlockingQueue q = populatedQueue(SIZE);
401 >        Thread t = new Thread(new CheckedRunnable() {
402              public void realRun() throws InterruptedException {
399                ArrayBlockingQueue q = populatedQueue(SIZE);
403                  for (int i = 0; i < SIZE; ++i) {
404 <                    threadAssertEquals(i, ((Integer)q.take()).intValue());
404 >                    assertEquals(i, q.take());
405                  }
406 <                q.take();
407 <            }};
406 >                try {
407 >                    q.take();
408 >                    shouldThrow();
409 >                } catch (InterruptedException success) {}
410 >            }});
411  
412          t.start();
413 <            Thread.sleep(SHORT_DELAY_MS);
414 <            t.interrupt();
415 <            t.join();
413 >        Thread.sleep(SHORT_DELAY_MS);
414 >        t.interrupt();
415 >        t.join();
416      }
417  
418  
# Line 416 | Line 422 | public class ArrayBlockingQueueTest exte
422      public void testPoll() {
423          ArrayBlockingQueue q = populatedQueue(SIZE);
424          for (int i = 0; i < SIZE; ++i) {
425 <            assertEquals(i, ((Integer)q.poll()).intValue());
425 >            assertEquals(i, q.poll());
426          }
427          assertNull(q.poll());
428      }
# Line 427 | Line 433 | public class ArrayBlockingQueueTest exte
433      public void testTimedPoll0() throws InterruptedException {
434          ArrayBlockingQueue q = populatedQueue(SIZE);
435          for (int i = 0; i < SIZE; ++i) {
436 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
436 >            assertEquals(i, q.poll(0, MILLISECONDS));
437          }
438          assertNull(q.poll(0, MILLISECONDS));
439      }
# Line 438 | Line 444 | public class ArrayBlockingQueueTest exte
444      public void testTimedPoll() throws InterruptedException {
445          ArrayBlockingQueue q = populatedQueue(SIZE);
446          for (int i = 0; i < SIZE; ++i) {
447 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
447 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
448          }
449          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
450      }
# Line 452 | Line 458 | public class ArrayBlockingQueueTest exte
458              public void realRun() throws InterruptedException {
459                  ArrayBlockingQueue q = populatedQueue(SIZE);
460                  for (int i = 0; i < SIZE; ++i) {
461 <                    assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
461 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));;
462                  }
463                  try {
464                      q.poll(SMALL_DELAY_MS, MILLISECONDS);
465 +                    shouldThrow();
466                  } catch (InterruptedException success) {}
467              }});
468  
# Line 471 | Line 478 | public class ArrayBlockingQueueTest exte
478       */
479      public void testTimedPollWithOffer() throws InterruptedException {
480          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
481 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
481 >        Thread t = new Thread(new CheckedRunnable() {
482              public void realRun() throws InterruptedException {
483 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
484 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
485 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
486 <            }};
483 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
484 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
485 >                try {
486 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
487 >                    shouldThrow();
488 >                } catch (InterruptedException success) {}
489 >            }});
490  
491          t.start();
492          Thread.sleep(SMALL_DELAY_MS);
# Line 492 | Line 502 | public class ArrayBlockingQueueTest exte
502      public void testPeek() {
503          ArrayBlockingQueue q = populatedQueue(SIZE);
504          for (int i = 0; i < SIZE; ++i) {
505 <            assertEquals(i, ((Integer)q.peek()).intValue());
506 <            q.poll();
505 >            assertEquals(i, q.peek());
506 >            assertEquals(i, q.poll());
507              assertTrue(q.peek() == null ||
508 <                       i != ((Integer)q.peek()).intValue());
508 >                       !q.peek().equals(i));
509          }
510          assertNull(q.peek());
511      }
# Line 506 | Line 516 | public class ArrayBlockingQueueTest exte
516      public void testElement() {
517          ArrayBlockingQueue q = populatedQueue(SIZE);
518          for (int i = 0; i < SIZE; ++i) {
519 <            assertEquals(i, ((Integer)q.element()).intValue());
520 <            q.poll();
519 >            assertEquals(i, q.element());
520 >            assertEquals(i, q.poll());
521          }
522          try {
523              q.element();
# Line 521 | Line 531 | public class ArrayBlockingQueueTest exte
531      public void testRemove() {
532          ArrayBlockingQueue q = populatedQueue(SIZE);
533          for (int i = 0; i < SIZE; ++i) {
534 <            assertEquals(i, ((Integer)q.remove()).intValue());
534 >            assertEquals(i, q.remove());
535          }
536          try {
537              q.remove();
# Line 551 | Line 561 | public class ArrayBlockingQueueTest exte
561          ArrayBlockingQueue q = populatedQueue(SIZE);
562          for (int i = 0; i < SIZE; ++i) {
563              assertTrue(q.contains(new Integer(i)));
564 <            q.poll();
564 >            assertEquals(i, q.poll());
565              assertFalse(q.contains(new Integer(i)));
566          }
567      }
# Line 646 | Line 656 | public class ArrayBlockingQueueTest exte
656       * toArray(null) throws NPE
657       */
658      public void testToArray_BadArg() {
659 +        ArrayBlockingQueue q = populatedQueue(SIZE);
660          try {
650            ArrayBlockingQueue q = populatedQueue(SIZE);
661              Object o[] = q.toArray(null);
662              shouldThrow();
663          } catch (NullPointerException success) {}
# Line 657 | Line 667 | public class ArrayBlockingQueueTest exte
667       * toArray with incompatible array type throws CCE
668       */
669      public void testToArray1_BadArg() {
670 +        ArrayBlockingQueue q = populatedQueue(SIZE);
671          try {
672 <            ArrayBlockingQueue q = populatedQueue(SIZE);
662 <            Object o[] = q.toArray(new String[10] );
672 >            Object o[] = q.toArray(new String[10]);
673              shouldThrow();
674          } catch (ArrayStoreException success) {}
675      }
# Line 708 | Line 718 | public class ArrayBlockingQueueTest exte
718  
719          int k = 0;
720          for (Iterator it = q.iterator(); it.hasNext();) {
721 <            int i = ((Integer)(it.next())).intValue();
712 <            assertEquals(++k, i);
721 >            assertEquals(++k, it.next());
722          }
723          assertEquals(3, k);
724      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines