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.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.5 by dl, Thu Sep 25 11:02:41 2003 UTC

# Line 36 | Line 36 | public class LinkedBlockingQueueTest ext
36          return q;
37      }
38  
39 <    public void testConstructor1(){
39 >    /**
40 >     * A new queue has the indicated capacity, or Integer.MAX_VALUE if
41 >     * none given
42 >     */
43 >    public void testConstructor1() {
44          assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity());
45 +        assertEquals(Integer.MAX_VALUE, new LinkedBlockingQueue().remainingCapacity());
46      }
47  
48 <    public void testConstructor2(){
48 >    /**
49 >     * Constructor throws IAE if  capacity argument nonpositive
50 >     */
51 >    public void testConstructor2() {
52          try {
53              LinkedBlockingQueue q = new LinkedBlockingQueue(0);
54 <            fail("Cannot make zero-sized");
54 >            shouldThrow();
55          }
56          catch (IllegalArgumentException success) {}
57      }
58  
59 <    public void testConstructor3(){
60 <
59 >    /**
60 >     * Initializing from null Collection throws NPE
61 >     */
62 >    public void testConstructor3() {
63          try {
64              LinkedBlockingQueue q = new LinkedBlockingQueue(null);
65 <            fail("Cannot make from null collection");
65 >            shouldThrow();
66          }
67          catch (NullPointerException success) {}
68      }
69  
70 <    public void testConstructor4(){
70 >    /**
71 >     * Initializing from Collection of null elements throws NPE
72 >     */
73 >    public void testConstructor4() {
74          try {
75              Integer[] ints = new Integer[SIZE];
76              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
77 <            fail("Cannot make with null elements");
77 >            shouldThrow();
78          }
79          catch (NullPointerException success) {}
80      }
81  
82 <    public void testConstructor5(){
82 >    /**
83 >     * Initializing from Collection with some null elements throws NPE
84 >     */
85 >    public void testConstructor5() {
86          try {
87              Integer[] ints = new Integer[SIZE];
88              for (int i = 0; i < SIZE-1; ++i)
89                  ints[i] = new Integer(i);
90              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
91 <            fail("Cannot make with null elements");
91 >            shouldThrow();
92          }
93          catch (NullPointerException success) {}
94      }
95  
96 <    public void testConstructor6(){
96 >    /**
97 >     * Queue contains all elements of collection used to initialize
98 >     */
99 >    public void testConstructor6() {
100          try {
101              Integer[] ints = new Integer[SIZE];
102              for (int i = 0; i < SIZE; ++i)
# Line 89 | Line 108 | public class LinkedBlockingQueueTest ext
108          finally {}
109      }
110  
111 +    /**
112 +     * Queue transitions from empty to full when elements added
113 +     */
114      public void testEmptyFull() {
115          LinkedBlockingQueue q = new LinkedBlockingQueue(2);
116          assertTrue(q.isEmpty());
# Line 97 | Line 119 | public class LinkedBlockingQueueTest ext
119          assertFalse(q.isEmpty());
120          q.add(two);
121          assertFalse(q.isEmpty());
122 <        assertEquals("queue should be full", 0, q.remainingCapacity());
123 <        assertFalse("offer should be rejected", q.offer(three));
122 >        assertEquals(0, q.remainingCapacity());
123 >        assertFalse(q.offer(three));
124      }
125  
126 <    public void testRemainingCapacity(){
126 >    /**
127 >     * remainingCapacity decreases on add, increases on remove
128 >     */
129 >    public void testRemainingCapacity() {
130          LinkedBlockingQueue q = populatedQueue(SIZE);
131          for (int i = 0; i < SIZE; ++i) {
132              assertEquals(i, q.remainingCapacity());
# Line 115 | Line 140 | public class LinkedBlockingQueueTest ext
140          }
141      }
142  
143 <    public void testOfferNull(){
143 >    /**
144 >     * offer(null) throws NPE
145 >     */
146 >    public void testOfferNull() {
147          try {
148              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
149              q.offer(null);
150 <            fail("should throw NPE");
150 >            shouldThrow();
151          } catch (NullPointerException success) { }  
152      }
153  
154 <    public void testOffer(){
154 >    /**
155 >     * Offer succeeds if not full; fails if full
156 >     */
157 >    public void testOffer() {
158          LinkedBlockingQueue q = new LinkedBlockingQueue(1);
159          assertTrue(q.offer(zero));
160          assertFalse(q.offer(one));
161      }
162  
163 <    public void testAdd(){
163 >    /**
164 >     * add succeeds if not full; throws ISE if full
165 >     */
166 >    public void testAdd() {
167          try {
168              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
169              for (int i = 0; i < SIZE; ++i) {
# Line 141 | Line 175 | public class LinkedBlockingQueueTest ext
175          }  
176      }
177  
178 <    public void testAddAll1(){
178 >    /**
179 >     * addAll(null) throws NPE
180 >     */
181 >    public void testAddAll1() {
182          try {
183              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
184              q.addAll(null);
185 <            fail("Cannot add null collection");
185 >            shouldThrow();
186          }
187          catch (NullPointerException success) {}
188      }
189 <    public void testAddAll2(){
189 >    /**
190 >     * addAll of a collection with null elements throws NPE
191 >     */
192 >    public void testAddAll2() {
193          try {
194              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
195              Integer[] ints = new Integer[SIZE];
196              q.addAll(Arrays.asList(ints));
197 <            fail("Cannot add null elements");
197 >            shouldThrow();
198          }
199          catch (NullPointerException success) {}
200      }
201 <    public void testAddAll3(){
201 >    /**
202 >     * addAll of a collection with any null elements throws NPE after
203 >     * possibly adding some elements
204 >     */
205 >    public void testAddAll3() {
206          try {
207              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
208              Integer[] ints = new Integer[SIZE];
209              for (int i = 0; i < SIZE-1; ++i)
210                  ints[i] = new Integer(i);
211              q.addAll(Arrays.asList(ints));
212 <            fail("Cannot add null elements");
212 >            shouldThrow();
213          }
214          catch (NullPointerException success) {}
215      }
216 <    public void testAddAll4(){
216 >    /**
217 >     * addAll throws ISE if not enough room
218 >     */
219 >    public void testAddAll4() {
220          try {
221              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
222              Integer[] ints = new Integer[SIZE];
223              for (int i = 0; i < SIZE; ++i)
224                  ints[i] = new Integer(i);
225              q.addAll(Arrays.asList(ints));
226 <            fail("Cannot add with insufficient capacity");
226 >            shouldThrow();
227          }
228          catch (IllegalStateException success) {}
229      }
230 <    public void testAddAll5(){
230 >    /**
231 >     * Queue contains all elements, in traversal order, of successful addAll
232 >     */
233 >    public void testAddAll5() {
234          try {
235              Integer[] empty = new Integer[0];
236              Integer[] ints = new Integer[SIZE];
# Line 195 | Line 245 | public class LinkedBlockingQueueTest ext
245          finally {}
246      }
247  
248 +    /**
249 +     * put(null) throws NPE
250 +     */
251       public void testPutNull() {
252          try {
253              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
254              q.put(null);
255 <            fail("put should throw NPE");
255 >            shouldThrow();
256          }
257          catch (NullPointerException success){
258          }  
259          catch (InterruptedException ie) {
260 <            fail("Unexpected exception");
260 >            unexpectedException();
261          }
262       }
263  
264 +    /**
265 +     * all elements successfully put are contained
266 +     */
267       public void testPut() {
268           try {
269               LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
# Line 219 | Line 275 | public class LinkedBlockingQueueTest ext
275               assertEquals(0, q.remainingCapacity());
276           }
277          catch (InterruptedException ie) {
278 <            fail("Unexpected exception");
278 >            unexpectedException();
279          }
280      }
281  
282 <    public void testBlockingPut(){
282 >    /**
283 >     * put blocks interruptibly if full
284 >     */
285 >    public void testBlockingPut() {
286          Thread t = new Thread(new Runnable() {
287                  public void run() {
288                      int added = 0;
# Line 234 | Line 293 | public class LinkedBlockingQueueTest ext
293                              ++added;
294                          }
295                          q.put(new Integer(SIZE));
296 <                        threadFail("put should block");
296 >                        threadShouldThrow();
297                      } catch (InterruptedException ie){
298                          threadAssertEquals(added, SIZE);
299                      }  
# Line 246 | Line 305 | public class LinkedBlockingQueueTest ext
305             t.join();
306          }
307          catch (InterruptedException ie) {
308 <            fail("Unexpected exception");
308 >            unexpectedException();
309          }
310      }
311  
312 +    /**
313 +     * put blocks waiting for take when full
314 +     */
315      public void testPutWithTake() {
316          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
317          Thread t = new Thread(new Runnable() {
318 <                public void run(){
318 >                public void run() {
319                      int added = 0;
320                      try {
321                          q.put(new Object());
# Line 264 | Line 326 | public class LinkedBlockingQueueTest ext
326                          ++added;
327                          q.put(new Object());
328                          ++added;
329 <                        threadFail("Should block");
329 >                        threadShouldThrow();
330                      } catch (InterruptedException e){
331                          threadAssertTrue(added >= 2);
332                      }
# Line 277 | Line 339 | public class LinkedBlockingQueueTest ext
339              t.interrupt();
340              t.join();
341          } catch (Exception e){
342 <            fail("Unexpected exception");
342 >            unexpectedException();
343          }
344      }
345  
346 +    /**
347 +     * timed offer times out if full and elements not taken
348 +     */
349      public void testTimedOffer() {
350          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
351          Thread t = new Thread(new Runnable() {
352 <                public void run(){
352 >                public void run() {
353                      try {
354                          q.put(new Object());
355                          q.put(new Object());
356                          threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
357                          q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
358 <                        threadFail("Should block");
358 >                        threadShouldThrow();
359                      } catch (InterruptedException success){}
360                  }
361              });
# Line 301 | Line 366 | public class LinkedBlockingQueueTest ext
366              t.interrupt();
367              t.join();
368          } catch (Exception e){
369 <            fail("Unexpected exception");
369 >            unexpectedException();
370          }
371      }
372  
373 <    public void testTake(){
373 >    /**
374 >     * take retrieves elements in FIFO order
375 >     */
376 >    public void testTake() {
377          try {
378              LinkedBlockingQueue q = populatedQueue(SIZE);
379              for (int i = 0; i < SIZE; ++i) {
380                  assertEquals(i, ((Integer)q.take()).intValue());
381              }
382          } catch (InterruptedException e){
383 <            fail("Unexpected exception");
383 >            unexpectedException();
384          }  
385      }
386  
387 +    /**
388 +     * take blocks interruptibly when empty
389 +     */
390      public void testTakeFromEmpty() {
391          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
392          Thread t = new Thread(new Runnable() {
393 <                public void run(){
393 >                public void run() {
394                      try {
395                          q.take();
396 <                        threadFail("Should block");
396 >                        threadShouldThrow();
397                      } catch (InterruptedException success){ }                
398                  }
399              });
# Line 332 | Line 403 | public class LinkedBlockingQueueTest ext
403              t.interrupt();
404              t.join();
405          } catch (Exception e){
406 <            fail("Unexpected exception");
406 >            unexpectedException();
407          }
408      }
409  
410 <    public void testBlockingTake(){
410 >    /**
411 >     * Take removes existing elements until empty, then blocks interruptibly
412 >     */
413 >    public void testBlockingTake() {
414          Thread t = new Thread(new Runnable() {
415                  public void run() {
416                      try {
# Line 345 | Line 419 | public class LinkedBlockingQueueTest ext
419                              assertEquals(i, ((Integer)q.take()).intValue());
420                          }
421                          q.take();
422 <                        threadFail("take should block");
422 >                        threadShouldThrow();
423                      } catch (InterruptedException success){
424                      }  
425                  }});
# Line 356 | Line 430 | public class LinkedBlockingQueueTest ext
430             t.join();
431          }
432          catch (InterruptedException ie) {
433 <            fail("Unexpected exception");
433 >            unexpectedException();
434          }
435      }
436  
437  
438 <    public void testPoll(){
438 >    /**
439 >     * poll succeeds unless empty
440 >     */
441 >    public void testPoll() {
442          LinkedBlockingQueue q = populatedQueue(SIZE);
443          for (int i = 0; i < SIZE; ++i) {
444              assertEquals(i, ((Integer)q.poll()).intValue());
# Line 369 | Line 446 | public class LinkedBlockingQueueTest ext
446          assertNull(q.poll());
447      }
448  
449 +    /**
450 +     * timed pool with zero timeout succeeds when non-empty, else times out
451 +     */
452      public void testTimedPoll0() {
453          try {
454              LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 377 | Line 457 | public class LinkedBlockingQueueTest ext
457              }
458              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
459          } catch (InterruptedException e){
460 <            fail("Unexpected exception");
460 >            unexpectedException();
461          }  
462      }
463  
464 +    /**
465 +     * timed pool with nonzero timeout succeeds when non-empty, else times out
466 +     */
467      public void testTimedPoll() {
468          try {
469              LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 389 | Line 472 | public class LinkedBlockingQueueTest ext
472              }
473              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
474          } catch (InterruptedException e){
475 <            fail("Unexpected exception");
475 >            unexpectedException();
476          }  
477      }
478  
479 <    public void testInterruptedTimedPoll(){
479 >    /**
480 >     * Interrupted timed poll throws InterruptedException instead of
481 >     * returning timeout status
482 >     */
483 >    public void testInterruptedTimedPoll() {
484          Thread t = new Thread(new Runnable() {
485                  public void run() {
486                      try {
# Line 412 | Line 499 | public class LinkedBlockingQueueTest ext
499             t.join();
500          }
501          catch (InterruptedException ie) {
502 <            fail("Unexpected exception");
502 >            unexpectedException();
503          }
504      }
505  
506 <    public void testTimedPollWithOffer(){
506 >    /**
507 >     *  timed poll before a delayed offer fails; after offer succeeds;
508 >     *  on interruption throws
509 >     */
510 >    public void testTimedPollWithOffer() {
511          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
512          Thread t = new Thread(new Runnable() {
513 <                public void run(){
513 >                public void run() {
514                      try {
515                          threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
516                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
517                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
518 <                        threadFail("Should block");
518 >                        threadShouldThrow();
519                      } catch (InterruptedException success) { }                
520                  }
521              });
# Line 435 | Line 526 | public class LinkedBlockingQueueTest ext
526              t.interrupt();
527              t.join();
528          } catch (Exception e){
529 <            fail("Unexpected exception");
529 >            unexpectedException();
530          }
531      }  
532  
533 <
534 <    public void testPeek(){
533 >    /**
534 >     * peek returns next element, or null if empty
535 >     */
536 >    public void testPeek() {
537          LinkedBlockingQueue q = populatedQueue(SIZE);
538          for (int i = 0; i < SIZE; ++i) {
539              assertEquals(i, ((Integer)q.peek()).intValue());
# Line 451 | Line 544 | public class LinkedBlockingQueueTest ext
544          assertNull(q.peek());
545      }
546  
547 <    public void testElement(){
547 >    /**
548 >     * element returns next element, or throws NSEE if empty
549 >     */
550 >    public void testElement() {
551          LinkedBlockingQueue q = populatedQueue(SIZE);
552          for (int i = 0; i < SIZE; ++i) {
553              assertEquals(i, ((Integer)q.element()).intValue());
# Line 459 | Line 555 | public class LinkedBlockingQueueTest ext
555          }
556          try {
557              q.element();
558 <            fail("no such element");
558 >            shouldThrow();
559          }
560          catch (NoSuchElementException success) {}
561      }
562  
563 <    public void testRemove(){
563 >    /**
564 >     * remove removes next element, or throws NSEE if empty
565 >     */
566 >    public void testRemove() {
567          LinkedBlockingQueue q = populatedQueue(SIZE);
568          for (int i = 0; i < SIZE; ++i) {
569              assertEquals(i, ((Integer)q.remove()).intValue());
570          }
571          try {
572              q.remove();
573 <            fail("remove should throw");
573 >            shouldThrow();
574          } catch (NoSuchElementException success){
575          }  
576      }
577  
578 <    public void testRemoveElement(){
578 >    /**
579 >     * remove(x) removes x and returns true if present
580 >     */
581 >    public void testRemoveElement() {
582          LinkedBlockingQueue q = populatedQueue(SIZE);
583          for (int i = 1; i < SIZE; i+=2) {
584              assertTrue(q.remove(new Integer(i)));
# Line 488 | Line 590 | public class LinkedBlockingQueueTest ext
590          assertTrue(q.isEmpty());
591      }
592          
593 <    public void testContains(){
593 >    /**
594 >     * contains(x) reports true when elements added but not yet removed
595 >     */
596 >    public void testContains() {
597          LinkedBlockingQueue q = populatedQueue(SIZE);
598          for (int i = 0; i < SIZE; ++i) {
599              assertTrue(q.contains(new Integer(i)));
# Line 497 | Line 602 | public class LinkedBlockingQueueTest ext
602          }
603      }
604  
605 <    public void testClear(){
605 >    /**
606 >     * clear removes all elements
607 >     */
608 >    public void testClear() {
609          LinkedBlockingQueue q = populatedQueue(SIZE);
610          q.clear();
611          assertTrue(q.isEmpty());
# Line 509 | Line 617 | public class LinkedBlockingQueueTest ext
617          assertTrue(q.isEmpty());
618      }
619  
620 <    public void testContainsAll(){
620 >    /**
621 >     * containsAll(c) is true when c contains a subset of elements
622 >     */
623 >    public void testContainsAll() {
624          LinkedBlockingQueue q = populatedQueue(SIZE);
625          LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE);
626          for (int i = 0; i < SIZE; ++i) {
# Line 520 | Line 631 | public class LinkedBlockingQueueTest ext
631          assertTrue(p.containsAll(q));
632      }
633  
634 <    public void testRetainAll(){
634 >    /**
635 >     * retainAll(c) retains only those elements of c and reports true if changed
636 >     */
637 >    public void testRetainAll() {
638          LinkedBlockingQueue q = populatedQueue(SIZE);
639          LinkedBlockingQueue p = populatedQueue(SIZE);
640          for (int i = 0; i < SIZE; ++i) {
# Line 536 | Line 650 | public class LinkedBlockingQueueTest ext
650          }
651      }
652  
653 <    public void testRemoveAll(){
653 >    /**
654 >     * removeAll(c) removes only those elements of c and reports true if changed
655 >     */
656 >    public void testRemoveAll() {
657          for (int i = 1; i < SIZE; ++i) {
658              LinkedBlockingQueue q = populatedQueue(SIZE);
659              LinkedBlockingQueue p = populatedQueue(i);
# Line 549 | Line 666 | public class LinkedBlockingQueueTest ext
666          }
667      }
668  
669 <
670 <    public void testToArray(){
669 >    /**
670 >     * toArray contains all elements
671 >     */
672 >    public void testToArray() {
673          LinkedBlockingQueue q = populatedQueue(SIZE);
674          Object[] o = q.toArray();
675          try {
676          for(int i = 0; i < o.length; i++)
677              assertEquals(o[i], q.take());
678          } catch (InterruptedException e){
679 <            fail("Unexpected exception");
679 >            unexpectedException();
680          }    
681      }
682  
683 <    public void testToArray2(){
683 >    /**
684 >     * toArray(a) contains all elements
685 >     */
686 >    public void testToArray2() {
687          LinkedBlockingQueue q = populatedQueue(SIZE);
688          Integer[] ints = new Integer[SIZE];
689          ints = (Integer[])q.toArray(ints);
# Line 569 | Line 691 | public class LinkedBlockingQueueTest ext
691              for(int i = 0; i < ints.length; i++)
692                  assertEquals(ints[i], q.take());
693          } catch (InterruptedException e){
694 <            fail("Unexpected exception");
694 >            unexpectedException();
695          }    
696      }
697      
698 <    public void testIterator(){
698 >    /**
699 >     * iterator iterates through all elements
700 >     */
701 >    public void testIterator() {
702          LinkedBlockingQueue q = populatedQueue(SIZE);
703          Iterator it = q.iterator();
704          try {
# Line 581 | Line 706 | public class LinkedBlockingQueueTest ext
706                  assertEquals(it.next(), q.take());
707              }
708          } catch (InterruptedException e){
709 <            fail("Unexpected exception");
709 >            unexpectedException();
710          }    
711      }
712  
713 <    public void testIteratorOrdering() {
714 <
713 >    /**
714 >     * iterator.remove removes current element
715 >     */
716 >    public void testIteratorRemove () {
717          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
591
592        q.add(one);
718          q.add(two);
719 +        q.add(one);
720          q.add(three);
721  
722 <        assertEquals("queue should be full", 0, q.remainingCapacity());
722 >        Iterator it = q.iterator();
723 >        it.next();
724 >        it.remove();
725 >        
726 >        it = q.iterator();
727 >        assertEquals(it.next(), one);
728 >        assertEquals(it.next(), three);
729 >        assertFalse(it.hasNext());
730 >    }
731 >
732  
733 +    /**
734 +     * iterator ordering is FIFO
735 +     */
736 +    public void testIteratorOrdering() {
737 +        final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
738 +        q.add(one);
739 +        q.add(two);
740 +        q.add(three);
741 +        assertEquals(0, q.remainingCapacity());
742          int k = 0;
743          for (Iterator it = q.iterator(); it.hasNext();) {
744              int i = ((Integer)(it.next())).intValue();
745 <            assertEquals("items should come out in order", ++k, i);
745 >            assertEquals(++k, i);
746          }
747 <
604 <        assertEquals("should go through 3 elements", 3, k);
747 >        assertEquals(3, k);
748      }
749  
750 +    /**
751 +     * Modifications do not cause iterators to fail
752 +     */
753      public void testWeaklyConsistentIteration () {
608
754          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
610
755          q.add(one);
756          q.add(two);
757          q.add(three);
614
758          try {
759              for (Iterator it = q.iterator(); it.hasNext();) {
760                  q.remove();
# Line 619 | Line 762 | public class LinkedBlockingQueueTest ext
762              }
763          }
764          catch (ConcurrentModificationException e) {
765 <            fail("weakly consistent iterator; should not get CME");
765 >            unexpectedException();
766          }
767 <
625 <        assertEquals("queue should be empty again", 0, q.size());
767 >        assertEquals(0, q.size());
768      }
769  
770  
771 <    public void testToString(){
771 >    /**
772 >     * toString contains toStrings of elements
773 >     */
774 >    public void testToString() {
775          LinkedBlockingQueue q = populatedQueue(SIZE);
776          String s = q.toString();
777          for (int i = 0; i < SIZE; ++i) {
# Line 635 | Line 780 | public class LinkedBlockingQueueTest ext
780      }        
781  
782  
783 +    /**
784 +     * offer transfers elements across Executor tasks
785 +     */
786      public void testOfferInExecutor() {
639
787          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
641
788          q.add(one);
789          q.add(two);
644
790          ExecutorService executor = Executors.newFixedThreadPool(2);
646
791          executor.execute(new Runnable() {
792              public void run() {
793                  threadAssertFalse(q.offer(three));
# Line 652 | Line 796 | public class LinkedBlockingQueueTest ext
796                      threadAssertEquals(0, q.remainingCapacity());
797                  }
798                  catch (InterruptedException e) {
799 <                    threadFail("should not be interrupted");
799 >                    threadUnexpectedException();
800                  }
801              }
802          });
# Line 664 | Line 808 | public class LinkedBlockingQueueTest ext
808                      threadAssertEquals(one, q.take());
809                  }
810                  catch (InterruptedException e) {
811 <                    threadFail("should not be interrupted");
811 >                    threadUnexpectedException();
812                  }
813              }
814          });
815          
816          joinPool(executor);
673
817      }
818  
819 +    /**
820 +     * poll retrieves elements across Executor threads
821 +     */
822      public void testPollInExecutor() {
677
823          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
679
824          ExecutorService executor = Executors.newFixedThreadPool(2);
681
825          executor.execute(new Runnable() {
826              public void run() {
827                  threadAssertNull(q.poll());
# Line 687 | Line 830 | public class LinkedBlockingQueueTest ext
830                      threadAssertTrue(q.isEmpty());
831                  }
832                  catch (InterruptedException e) {
833 <                    threadFail("should not be interrupted");
833 >                    threadUnexpectedException();
834                  }
835              }
836          });
# Line 699 | Line 842 | public class LinkedBlockingQueueTest ext
842                      q.put(one);
843                  }
844                  catch (InterruptedException e) {
845 <                    threadFail("should not be interrupted");
845 >                    threadUnexpectedException();
846                  }
847              }
848          });
# Line 707 | Line 850 | public class LinkedBlockingQueueTest ext
850          joinPool(executor);
851      }
852  
853 +    /**
854 +     * A deserialized serialized queue has same elements in same order
855 +     */
856      public void testSerialization() {
857          LinkedBlockingQueue q = populatedQueue(SIZE);
858  
# Line 723 | Line 869 | public class LinkedBlockingQueueTest ext
869              while (!q.isEmpty())
870                  assertEquals(q.remove(), r.remove());
871          } catch(Exception e){
872 <            e.printStackTrace();
727 <            fail("unexpected exception");
872 >            unexpectedException();
873          }
874      }
875  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines