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

Comparing jsr166/src/test/tck/LinkedBlockingQueueTest.java (file contents):
Revision 1.13 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.16 by jsr166, Sat Nov 21 02:33:20 2009 UTC

# Line 9 | Line 9
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.io.*;
14  
15   public class LinkedBlockingQueueTest extends JSR166TestCase {
16  
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());
18 >        junit.textui.TestRunner.run (suite());
19      }
20  
21      public static Test suite() {
22 <        return new TestSuite(LinkedBlockingQueueTest.class);
22 >        return new TestSuite(LinkedBlockingQueueTest.class);
23      }
24  
25  
# Line 29 | Line 30 | public class LinkedBlockingQueueTest ext
30      private LinkedBlockingQueue populatedQueue(int n) {
31          LinkedBlockingQueue q = new LinkedBlockingQueue(n);
32          assertTrue(q.isEmpty());
33 <        for (int i = 0; i < n; i++)
34 <            assertTrue(q.offer(new Integer(i)));
33 >        for (int i = 0; i < n; i++)
34 >            assertTrue(q.offer(new Integer(i)));
35          assertFalse(q.isEmpty());
36          assertEquals(0, q.remainingCapacity());
37 <        assertEquals(n, q.size());
37 >        assertEquals(n, q.size());
38          return q;
39      }
40  
# Line 145 | Line 146 | public class LinkedBlockingQueueTest ext
146       * offer(null) throws NPE
147       */
148      public void testOfferNull() {
149 <        try {
149 >        try {
150              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
151              q.offer(null);
152              shouldThrow();
# Line 156 | Line 157 | public class LinkedBlockingQueueTest ext
157       * add(null) throws NPE
158       */
159      public void testAddNull() {
160 <        try {
160 >        try {
161              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
162              q.add(null);
163              shouldThrow();
# Line 176 | Line 177 | public class LinkedBlockingQueueTest ext
177       * add succeeds if not full; throws ISE if full
178       */
179      public void testAdd() {
180 <        try {
180 >        try {
181              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
182              for (int i = 0; i < SIZE; ++i) {
183                  assertTrue(q.add(new Integer(i)));
184              }
185              assertEquals(0, q.remainingCapacity());
186              q.add(new Integer(SIZE));
187 <        } catch (IllegalStateException success){
188 <        }
187 >        } catch (IllegalStateException success) {
188 >        }
189      }
190  
191      /**
# Line 274 | Line 275 | public class LinkedBlockingQueueTest ext
275       * put(null) throws NPE
276       */
277       public void testPutNull() {
278 <        try {
278 >        try {
279              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
280              q.put(null);
281              shouldThrow();
282          }
283 <        catch (NullPointerException success){
284 <        }
283 >        catch (NullPointerException success) {
284 >        }
285          catch (InterruptedException ie) {
286 <            unexpectedException();
286 >            unexpectedException();
287          }
288       }
289  
# Line 300 | Line 301 | public class LinkedBlockingQueueTest ext
301               assertEquals(0, q.remainingCapacity());
302           }
303          catch (InterruptedException ie) {
304 <            unexpectedException();
304 >            unexpectedException();
305          }
306      }
307  
# Line 319 | Line 320 | public class LinkedBlockingQueueTest ext
320                          }
321                          q.put(new Integer(SIZE));
322                          threadShouldThrow();
323 <                    } catch (InterruptedException ie){
323 >                    } catch (InterruptedException ie) {
324                          threadAssertEquals(added, SIZE);
325                      }
326                  }});
# Line 330 | Line 331 | public class LinkedBlockingQueueTest ext
331             t.join();
332          }
333          catch (InterruptedException ie) {
334 <            unexpectedException();
334 >            unexpectedException();
335          }
336      }
337  
# Line 351 | Line 352 | public class LinkedBlockingQueueTest ext
352                          ++added;
353                          q.put(new Object());
354                          ++added;
355 <                        threadShouldThrow();
356 <                    } catch (InterruptedException e){
355 >                        threadShouldThrow();
356 >                    } catch (InterruptedException e) {
357                          threadAssertTrue(added >= 2);
358                      }
359                  }
# Line 363 | Line 364 | public class LinkedBlockingQueueTest ext
364              q.take();
365              t.interrupt();
366              t.join();
367 <        } catch (Exception e){
367 >        } catch (Exception e) {
368              unexpectedException();
369          }
370      }
# Line 378 | Line 379 | public class LinkedBlockingQueueTest ext
379                      try {
380                          q.put(new Object());
381                          q.put(new Object());
382 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
383 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
384 <                        threadShouldThrow();
385 <                    } catch (InterruptedException success){}
382 >                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
383 >                        q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
384 >                        threadShouldThrow();
385 >                    } catch (InterruptedException success) {}
386                  }
387              });
388  
# Line 390 | Line 391 | public class LinkedBlockingQueueTest ext
391              Thread.sleep(SMALL_DELAY_MS);
392              t.interrupt();
393              t.join();
394 <        } catch (Exception e){
394 >        } catch (Exception e) {
395              unexpectedException();
396          }
397      }
# Line 399 | Line 400 | public class LinkedBlockingQueueTest ext
400       * take retrieves elements in FIFO order
401       */
402      public void testTake() {
403 <        try {
403 >        try {
404              LinkedBlockingQueue q = populatedQueue(SIZE);
405              for (int i = 0; i < SIZE; ++i) {
406                  assertEquals(i, ((Integer)q.take()).intValue());
407              }
408 <        } catch (InterruptedException e){
409 <            unexpectedException();
410 <        }
408 >        } catch (InterruptedException e) {
409 >            unexpectedException();
410 >        }
411      }
412  
413      /**
# Line 418 | Line 419 | public class LinkedBlockingQueueTest ext
419                  public void run() {
420                      try {
421                          q.take();
422 <                        threadShouldThrow();
423 <                    } catch (InterruptedException success){ }
422 >                        threadShouldThrow();
423 >                    } catch (InterruptedException success) { }
424                  }
425              });
426          try {
# Line 427 | Line 428 | public class LinkedBlockingQueueTest ext
428              Thread.sleep(SHORT_DELAY_MS);
429              t.interrupt();
430              t.join();
431 <        } catch (Exception e){
431 >        } catch (Exception e) {
432              unexpectedException();
433          }
434      }
# Line 445 | Line 446 | public class LinkedBlockingQueueTest ext
446                          }
447                          q.take();
448                          threadShouldThrow();
449 <                    } catch (InterruptedException success){
449 >                    } catch (InterruptedException success) {
450                      }
451                  }});
452          t.start();
# Line 455 | Line 456 | public class LinkedBlockingQueueTest ext
456             t.join();
457          }
458          catch (InterruptedException ie) {
459 <            unexpectedException();
459 >            unexpectedException();
460          }
461      }
462  
# Line 468 | Line 469 | public class LinkedBlockingQueueTest ext
469          for (int i = 0; i < SIZE; ++i) {
470              assertEquals(i, ((Integer)q.poll()).intValue());
471          }
472 <        assertNull(q.poll());
472 >        assertNull(q.poll());
473      }
474  
475      /**
# Line 478 | Line 479 | public class LinkedBlockingQueueTest ext
479          try {
480              LinkedBlockingQueue q = populatedQueue(SIZE);
481              for (int i = 0; i < SIZE; ++i) {
482 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
482 >                assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
483              }
484 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
485 <        } catch (InterruptedException e){
486 <            unexpectedException();
487 <        }
484 >            assertNull(q.poll(0, MILLISECONDS));
485 >        } catch (InterruptedException e) {
486 >            unexpectedException();
487 >        }
488      }
489  
490      /**
# Line 493 | Line 494 | public class LinkedBlockingQueueTest ext
494          try {
495              LinkedBlockingQueue q = populatedQueue(SIZE);
496              for (int i = 0; i < SIZE; ++i) {
497 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
497 >                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
498              }
499 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
500 <        } catch (InterruptedException e){
501 <            unexpectedException();
502 <        }
499 >            assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
500 >        } catch (InterruptedException e) {
501 >            unexpectedException();
502 >        }
503      }
504  
505      /**
# Line 511 | Line 512 | public class LinkedBlockingQueueTest ext
512                      try {
513                          LinkedBlockingQueue q = populatedQueue(SIZE);
514                          for (int i = 0; i < SIZE; ++i) {
515 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
515 >                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
516                          }
517 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
518 <                    } catch (InterruptedException success){
517 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
518 >                    } catch (InterruptedException success) {
519                      }
520                  }});
521          t.start();
# Line 524 | Line 525 | public class LinkedBlockingQueueTest ext
525             t.join();
526          }
527          catch (InterruptedException ie) {
528 <            unexpectedException();
528 >            unexpectedException();
529          }
530      }
531  
# Line 537 | Line 538 | public class LinkedBlockingQueueTest ext
538          Thread t = new Thread(new Runnable() {
539                  public void run() {
540                      try {
541 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
542 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
543 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
544 <                        threadShouldThrow();
541 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
542 >                        q.poll(LONG_DELAY_MS, MILLISECONDS);
543 >                        q.poll(LONG_DELAY_MS, MILLISECONDS);
544 >                        threadShouldThrow();
545                      } catch (InterruptedException success) { }
546                  }
547              });
548          try {
549              t.start();
550              Thread.sleep(SMALL_DELAY_MS);
551 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
551 >            assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
552              t.interrupt();
553              t.join();
554 <        } catch (Exception e){
554 >        } catch (Exception e) {
555              unexpectedException();
556          }
557      }
# Line 566 | Line 567 | public class LinkedBlockingQueueTest ext
567              assertTrue(q.peek() == null ||
568                         i != ((Integer)q.peek()).intValue());
569          }
570 <        assertNull(q.peek());
570 >        assertNull(q.peek());
571      }
572  
573      /**
# Line 596 | Line 597 | public class LinkedBlockingQueueTest ext
597          try {
598              q.remove();
599              shouldThrow();
600 <        } catch (NoSuchElementException success){
601 <        }
600 >        } catch (NoSuchElementException success) {
601 >        }
602      }
603  
604      /**
# Line 627 | Line 628 | public class LinkedBlockingQueueTest ext
628              assertTrue(q.remove(new Integer(2)));
629              assertTrue(q.add(new Integer(3)));
630              assertTrue(q.take() != null);
631 <        } catch (Exception e){
631 >        } catch (Exception e) {
632              unexpectedException();
633          }
634      }
# Line 714 | Line 715 | public class LinkedBlockingQueueTest ext
715       */
716      public void testToArray() {
717          LinkedBlockingQueue q = populatedQueue(SIZE);
718 <        Object[] o = q.toArray();
719 <        try {
720 <        for (int i = 0; i < o.length; i++)
721 <            assertEquals(o[i], q.take());
722 <        } catch (InterruptedException e){
723 <            unexpectedException();
724 <        }
718 >        Object[] o = q.toArray();
719 >        try {
720 >        for (int i = 0; i < o.length; i++)
721 >            assertEquals(o[i], q.take());
722 >        } catch (InterruptedException e) {
723 >            unexpectedException();
724 >        }
725      }
726  
727      /**
# Line 728 | Line 729 | public class LinkedBlockingQueueTest ext
729       */
730      public void testToArray2() {
731          LinkedBlockingQueue q = populatedQueue(SIZE);
732 <        Integer[] ints = new Integer[SIZE];
733 <        ints = (Integer[])q.toArray(ints);
734 <        try {
735 <            for (int i = 0; i < ints.length; i++)
736 <                assertEquals(ints[i], q.take());
737 <        } catch (InterruptedException e){
738 <            unexpectedException();
739 <        }
732 >        Integer[] ints = new Integer[SIZE];
733 >        ints = (Integer[])q.toArray(ints);
734 >        try {
735 >            for (int i = 0; i < ints.length; i++)
736 >                assertEquals(ints[i], q.take());
737 >        } catch (InterruptedException e) {
738 >            unexpectedException();
739 >        }
740      }
741  
742      /**
743       * toArray(null) throws NPE
744       */
745      public void testToArray_BadArg() {
746 <        try {
746 >        try {
747              LinkedBlockingQueue q = populatedQueue(SIZE);
748 <            Object o[] = q.toArray(null);
749 <            shouldThrow();
750 <        } catch (NullPointerException success){}
748 >            Object o[] = q.toArray(null);
749 >            shouldThrow();
750 >        } catch (NullPointerException success) {}
751      }
752  
753      /**
754       * toArray with incompatible array type throws CCE
755       */
756      public void testToArray1_BadArg() {
757 <        try {
757 >        try {
758              LinkedBlockingQueue q = populatedQueue(SIZE);
759 <            Object o[] = q.toArray(new String[10] );
760 <            shouldThrow();
761 <        } catch (ArrayStoreException  success){}
759 >            Object o[] = q.toArray(new String[10] );
760 >            shouldThrow();
761 >        } catch (ArrayStoreException  success) {}
762      }
763  
764  
# Line 766 | Line 767 | public class LinkedBlockingQueueTest ext
767       */
768      public void testIterator() {
769          LinkedBlockingQueue q = populatedQueue(SIZE);
770 <        Iterator it = q.iterator();
771 <        try {
772 <            while (it.hasNext()){
773 <                assertEquals(it.next(), q.take());
774 <            }
775 <        } catch (InterruptedException e){
776 <            unexpectedException();
777 <        }
770 >        Iterator it = q.iterator();
771 >        try {
772 >            while (it.hasNext()) {
773 >                assertEquals(it.next(), q.take());
774 >            }
775 >        } catch (InterruptedException e) {
776 >            unexpectedException();
777 >        }
778      }
779  
780      /**
# Line 858 | Line 859 | public class LinkedBlockingQueueTest ext
859              public void run() {
860                  threadAssertFalse(q.offer(three));
861                  try {
862 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
862 >                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
863                      threadAssertEquals(0, q.remainingCapacity());
864                  }
865                  catch (InterruptedException e) {
# Line 892 | Line 893 | public class LinkedBlockingQueueTest ext
893              public void run() {
894                  threadAssertNull(q.poll());
895                  try {
896 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
896 >                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
897                      threadAssertTrue(q.isEmpty());
898                  }
899                  catch (InterruptedException e) {
# Line 934 | Line 935 | public class LinkedBlockingQueueTest ext
935              assertEquals(q.size(), r.size());
936              while (!q.isEmpty())
937                  assertEquals(q.remove(), r.remove());
938 <        } catch (Exception e){
938 >        } catch (Exception e) {
939              unexpectedException();
940          }
941      }
# Line 996 | Line 997 | public class LinkedBlockingQueueTest ext
997                  public void run() {
998                      try {
999                          q.put(new Integer(SIZE+1));
1000 <                    } catch (InterruptedException ie){
1000 >                    } catch (InterruptedException ie) {
1001                          threadUnexpectedException();
1002                      }
1003                  }
# Line 1010 | Line 1011 | public class LinkedBlockingQueueTest ext
1011                  assertEquals(l.get(i), new Integer(i));
1012              t.join();
1013              assertTrue(q.size() + l.size() >= SIZE);
1014 <        } catch (Exception e){
1014 >        } catch (Exception e) {
1015              unexpectedException();
1016          }
1017      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines