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.11 by dl, Thu Jan 20 00:39:13 2005 UTC vs.
Revision 1.12 by jsr166, Mon Nov 2 20:28:31 2009 UTC

# Line 2 | Line 2
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
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 14 | Line 14 | import java.io.*;
14   public class LinkedBlockingQueueTest extends JSR166TestCase {
15  
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());  
17 >        junit.textui.TestRunner.run (suite());
18      }
19  
20      public static Test suite() {
# Line 36 | Line 36 | public class LinkedBlockingQueueTest ext
36          assertEquals(n, q.size());
37          return q;
38      }
39 <
39 >
40      /**
41       * A new queue has the indicated capacity, or Integer.MAX_VALUE if
42       * none given
# Line 149 | Line 149 | public class LinkedBlockingQueueTest ext
149              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
150              q.offer(null);
151              shouldThrow();
152 <        } catch (NullPointerException success) { }  
152 >        } catch (NullPointerException success) { }
153      }
154  
155      /**
# Line 160 | Line 160 | public class LinkedBlockingQueueTest ext
160              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
161              q.add(null);
162              shouldThrow();
163 <        } catch (NullPointerException success) { }  
163 >        } catch (NullPointerException success) { }
164      }
165  
166      /**
# Line 184 | Line 184 | public class LinkedBlockingQueueTest ext
184              assertEquals(0, q.remainingCapacity());
185              q.add(new Integer(SIZE));
186          } catch (IllegalStateException success){
187 <        }  
187 >        }
188      }
189  
190      /**
# Line 278 | Line 278 | public class LinkedBlockingQueueTest ext
278              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
279              q.put(null);
280              shouldThrow();
281 <        }
281 >        }
282          catch (NullPointerException success){
283 <        }  
283 >        }
284          catch (InterruptedException ie) {
285              unexpectedException();
286          }
# Line 321 | Line 321 | public class LinkedBlockingQueueTest ext
321                          threadShouldThrow();
322                      } catch (InterruptedException ie){
323                          threadAssertEquals(added, SIZE);
324 <                    }  
324 >                    }
325                  }});
326          t.start();
327 <        try {
328 <           Thread.sleep(SHORT_DELAY_MS);
327 >        try {
328 >           Thread.sleep(SHORT_DELAY_MS);
329             t.interrupt();
330             t.join();
331          }
# Line 384 | Line 384 | public class LinkedBlockingQueueTest ext
384                      } catch (InterruptedException success){}
385                  }
386              });
387 <        
387 >
388          try {
389              t.start();
390              Thread.sleep(SMALL_DELAY_MS);
# Line 406 | Line 406 | public class LinkedBlockingQueueTest ext
406              }
407          } catch (InterruptedException e){
408              unexpectedException();
409 <        }  
409 >        }
410      }
411  
412      /**
# Line 419 | Line 419 | public class LinkedBlockingQueueTest ext
419                      try {
420                          q.take();
421                          threadShouldThrow();
422 <                    } catch (InterruptedException success){ }                
422 >                    } catch (InterruptedException success){ }
423                  }
424              });
425          try {
# Line 446 | Line 446 | public class LinkedBlockingQueueTest ext
446                          q.take();
447                          threadShouldThrow();
448                      } catch (InterruptedException success){
449 <                    }  
449 >                    }
450                  }});
451          t.start();
452 <        try {
453 <           Thread.sleep(SHORT_DELAY_MS);
452 >        try {
453 >           Thread.sleep(SHORT_DELAY_MS);
454             t.interrupt();
455             t.join();
456          }
# Line 483 | Line 483 | public class LinkedBlockingQueueTest ext
483              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
484          } catch (InterruptedException e){
485              unexpectedException();
486 <        }  
486 >        }
487      }
488  
489      /**
# Line 498 | Line 498 | public class LinkedBlockingQueueTest ext
498              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
499          } catch (InterruptedException e){
500              unexpectedException();
501 <        }  
501 >        }
502      }
503  
504      /**
# Line 515 | Line 515 | public class LinkedBlockingQueueTest ext
515                          }
516                          threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
517                      } catch (InterruptedException success){
518 <                    }  
518 >                    }
519                  }});
520          t.start();
521 <        try {
522 <           Thread.sleep(SHORT_DELAY_MS);
521 >        try {
522 >           Thread.sleep(SHORT_DELAY_MS);
523             t.interrupt();
524             t.join();
525          }
# Line 541 | Line 541 | public class LinkedBlockingQueueTest ext
541                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
542                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
543                          threadShouldThrow();
544 <                    } catch (InterruptedException success) { }                
544 >                    } catch (InterruptedException success) { }
545                  }
546              });
547          try {
# Line 553 | Line 553 | public class LinkedBlockingQueueTest ext
553          } catch (Exception e){
554              unexpectedException();
555          }
556 <    }  
556 >    }
557  
558      /**
559       * peek returns next element, or null if empty
# Line 597 | Line 597 | public class LinkedBlockingQueueTest ext
597              q.remove();
598              shouldThrow();
599          } catch (NoSuchElementException success){
600 <        }  
600 >        }
601      }
602  
603      /**
# Line 631 | Line 631 | public class LinkedBlockingQueueTest ext
631              unexpectedException();
632          }
633      }
634 <        
634 >
635      /**
636       * contains(x) reports true when elements added but not yet removed
637       */
# Line 720 | Line 720 | public class LinkedBlockingQueueTest ext
720              assertEquals(o[i], q.take());
721          } catch (InterruptedException e){
722              unexpectedException();
723 <        }    
723 >        }
724      }
725  
726      /**
# Line 735 | Line 735 | public class LinkedBlockingQueueTest ext
735                  assertEquals(ints[i], q.take());
736          } catch (InterruptedException e){
737              unexpectedException();
738 <        }    
738 >        }
739      }
740  
741      /**
# Line 760 | Line 760 | public class LinkedBlockingQueueTest ext
760          } catch(ArrayStoreException  success){}
761      }
762  
763 <    
763 >
764      /**
765       * iterator iterates through all elements
766       */
# Line 773 | Line 773 | public class LinkedBlockingQueueTest ext
773              }
774          } catch (InterruptedException e){
775              unexpectedException();
776 <        }    
776 >        }
777      }
778  
779      /**
# Line 788 | Line 788 | public class LinkedBlockingQueueTest ext
788          Iterator it = q.iterator();
789          it.next();
790          it.remove();
791 <        
791 >
792          it = q.iterator();
793          assertEquals(it.next(), one);
794          assertEquals(it.next(), three);
# Line 843 | Line 843 | public class LinkedBlockingQueueTest ext
843          for (int i = 0; i < SIZE; ++i) {
844              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
845          }
846 <    }        
846 >    }
847  
848  
849      /**
# Line 878 | Line 878 | public class LinkedBlockingQueueTest ext
878                  }
879              }
880          });
881 <        
881 >
882          joinPool(executor);
883      }
884  
# Line 912 | Line 912 | public class LinkedBlockingQueueTest ext
912                  }
913              }
914          });
915 <        
915 >
916          joinPool(executor);
917      }
918  
# Line 932 | Line 932 | public class LinkedBlockingQueueTest ext
932              ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
933              LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
934              assertEquals(q.size(), r.size());
935 <            while (!q.isEmpty())
935 >            while (!q.isEmpty())
936                  assertEquals(q.remove(), r.remove());
937          } catch(Exception e){
938              unexpectedException();
# Line 941 | Line 941 | public class LinkedBlockingQueueTest ext
941  
942      /**
943       * drainTo(null) throws NPE
944 <     */
944 >     */
945      public void testDrainToNull() {
946          LinkedBlockingQueue q = populatedQueue(SIZE);
947          try {
# Line 953 | Line 953 | public class LinkedBlockingQueueTest ext
953  
954      /**
955       * drainTo(this) throws IAE
956 <     */
956 >     */
957      public void testDrainToSelf() {
958          LinkedBlockingQueue q = populatedQueue(SIZE);
959          try {
# Line 965 | Line 965 | public class LinkedBlockingQueueTest ext
965  
966      /**
967       * drainTo(c) empties queue into another collection c
968 <     */
968 >     */
969      public void testDrainTo() {
970          LinkedBlockingQueue q = populatedQueue(SIZE);
971          ArrayList l = new ArrayList();
972          q.drainTo(l);
973          assertEquals(q.size(), 0);
974          assertEquals(l.size(), SIZE);
975 <        for (int i = 0; i < SIZE; ++i)
975 >        for (int i = 0; i < SIZE; ++i)
976              assertEquals(l.get(i), new Integer(i));
977          q.add(zero);
978          q.add(one);
# Line 983 | Line 983 | public class LinkedBlockingQueueTest ext
983          q.drainTo(l);
984          assertEquals(q.size(), 0);
985          assertEquals(l.size(), 2);
986 <        for (int i = 0; i < 2; ++i)
986 >        for (int i = 0; i < 2; ++i)
987              assertEquals(l.get(i), new Integer(i));
988      }
989  
990      /**
991       * drainTo empties full queue, unblocking a waiting put.
992 <     */
992 >     */
993      public void testDrainToWithActivePut() {
994          final LinkedBlockingQueue q = populatedQueue(SIZE);
995          Thread t = new Thread(new Runnable() {
996                  public void run() {
997                      try {
998                          q.put(new Integer(SIZE+1));
999 <                    } catch (InterruptedException ie){
999 >                    } catch (InterruptedException ie){
1000                          threadUnexpectedException();
1001                      }
1002                  }
# Line 1006 | Line 1006 | public class LinkedBlockingQueueTest ext
1006              ArrayList l = new ArrayList();
1007              q.drainTo(l);
1008              assertTrue(l.size() >= SIZE);
1009 <            for (int i = 0; i < SIZE; ++i)
1009 >            for (int i = 0; i < SIZE; ++i)
1010                  assertEquals(l.get(i), new Integer(i));
1011              t.join();
1012              assertTrue(q.size() + l.size() >= SIZE);
# Line 1017 | Line 1017 | public class LinkedBlockingQueueTest ext
1017  
1018      /**
1019       * drainTo(null, n) throws NPE
1020 <     */
1020 >     */
1021      public void testDrainToNullN() {
1022          LinkedBlockingQueue q = populatedQueue(SIZE);
1023          try {
# Line 1029 | Line 1029 | public class LinkedBlockingQueueTest ext
1029  
1030      /**
1031       * drainTo(this, n) throws IAE
1032 <     */
1032 >     */
1033      public void testDrainToSelfN() {
1034          LinkedBlockingQueue q = populatedQueue(SIZE);
1035          try {
# Line 1041 | Line 1041 | public class LinkedBlockingQueueTest ext
1041  
1042      /**
1043       * drainTo(c, n) empties first max {n, size} elements of queue into c
1044 <     */
1044 >     */
1045      public void testDrainToN() {
1046          LinkedBlockingQueue q = new LinkedBlockingQueue();
1047          for (int i = 0; i < SIZE + 2; ++i) {
# Line 1052 | Line 1052 | public class LinkedBlockingQueueTest ext
1052              int k = (i < SIZE)? i : SIZE;
1053              assertEquals(l.size(), k);
1054              assertEquals(q.size(), SIZE-k);
1055 <            for (int j = 0; j < k; ++j)
1055 >            for (int j = 0; j < k; ++j)
1056                  assertEquals(l.get(j), new Integer(j));
1057              while (q.poll() != null) ;
1058          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines