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.1 by dl, Sun Aug 31 19:24:55 2003 UTC vs.
Revision 1.5 by dl, Thu Sep 25 11:02:41 2003 UTC

# Line 8 | Line 8
8   import junit.framework.*;
9   import java.util.*;
10   import java.util.concurrent.*;
11 + import java.io.*;
12  
13 < public class LinkedBlockingQueueTest extends TestCase {
13 <
14 <    private static int N = 10;
15 <    private static long SHORT_DELAY_MS = 100;
16 <    private static long MEDIUM_DELAY_MS = 1000;
17 <    private static long LONG_DELAY_MS = 10000;
13 > public class LinkedBlockingQueueTest extends JSR166TestCase {
14  
15      public static void main(String[] args) {
16          junit.textui.TestRunner.run (suite());  
# Line 24 | Line 20 | public class LinkedBlockingQueueTest ext
20          return new TestSuite(LinkedBlockingQueueTest.class);
21      }
22  
23 +
24      /**
25       * Create a queue of given size containing consecutive
26       * Integers 0 ... n.
27       */
28 <    private LinkedBlockingQueue fullQueue(int n) {
28 >    private LinkedBlockingQueue populatedQueue(int n) {
29          LinkedBlockingQueue q = new LinkedBlockingQueue(n);
30          assertTrue(q.isEmpty());
31          for(int i = 0; i < n; i++)
# Line 39 | Line 36 | public class LinkedBlockingQueueTest ext
36          return q;
37      }
38  
39 <    public void testConstructor1(){
40 <        assertEquals(N, new LinkedBlockingQueue(N).remainingCapacity());
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[N];
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[N];
88 <            for (int i = 0; i < N-1; ++i)
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[N];
102 <            for (int i = 0; i < N; ++i)
101 >            Integer[] ints = new Integer[SIZE];
102 >            for (int i = 0; i < SIZE; ++i)
103                  ints[i] = new Integer(i);
104              LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
105 <            for (int i = 0; i < N; ++i)
105 >            for (int i = 0; i < SIZE; ++i)
106                  assertEquals(ints[i], q.poll());
107          }
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());
117          assertEquals("should have room for 2", 2, q.remainingCapacity());
118 <        q.add(new Integer(1));
118 >        q.add(one);
119          assertFalse(q.isEmpty());
120 <        q.add(new Integer(2));
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(new Integer(3)));
122 >        assertEquals(0, q.remainingCapacity());
123 >        assertFalse(q.offer(three));
124      }
125  
126 <    public void testRemainingCapacity(){
127 <        LinkedBlockingQueue q = fullQueue(N);
128 <        for (int i = 0; i < N; ++i) {
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());
133 <            assertEquals(N-i, q.size());
133 >            assertEquals(SIZE-i, q.size());
134              q.remove();
135          }
136 <        for (int i = 0; i < N; ++i) {
137 <            assertEquals(N-i, q.remainingCapacity());
136 >        for (int i = 0; i < SIZE; ++i) {
137 >            assertEquals(SIZE-i, q.remainingCapacity());
138              assertEquals(i, q.size());
139              q.add(new Integer(i));
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(new Integer(0)));
160 <        assertFalse(q.offer(new Integer(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(N);
169 <            for (int i = 0; i < N; ++i) {
168 >            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
169 >            for (int i = 0; i < SIZE; ++i) {
170                  assertTrue(q.add(new Integer(i)));
171              }
172              assertEquals(0, q.remainingCapacity());
173 <            q.add(new Integer(N));
173 >            q.add(new Integer(SIZE));
174          } catch (IllegalStateException success){
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(N);
195 <            Integer[] ints = new Integer[N];
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(N);
208 <            Integer[] ints = new Integer[N];
209 <            for (int i = 0; i < N-1; ++i)
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[N];
223 <            for (int i = 0; i < N; ++i)
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[N];
237 <            for (int i = 0; i < N; ++i)
236 >            Integer[] ints = new Integer[SIZE];
237 >            for (int i = 0; i < SIZE; ++i)
238                  ints[i] = new Integer(i);
239 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
239 >            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
240              assertFalse(q.addAll(Arrays.asList(empty)));
241              assertTrue(q.addAll(Arrays.asList(ints)));
242 <            for (int i = 0; i < N; ++i)
242 >            for (int i = 0; i < SIZE; ++i)
243                  assertEquals(ints[i], q.poll());
244          }
245          finally {}
246      }
247  
248 +    /**
249 +     * put(null) throws NPE
250 +     */
251       public void testPutNull() {
252          try {
253 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
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(N);
270 <             for (int i = 0; i < N; ++i) {
269 >             LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
270 >             for (int i = 0; i < SIZE; ++i) {
271                   Integer I = new Integer(i);
272                   q.put(I);
273                   assertTrue(q.contains(I));
# Line 222 | 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;
289                      try {
290 <                        LinkedBlockingQueue q = new LinkedBlockingQueue(N);
291 <                        for (int i = 0; i < N; ++i) {
290 >                        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
291 >                        for (int i = 0; i < SIZE; ++i) {
292                              q.put(new Integer(i));
293                              ++added;
294                          }
295 <                        q.put(new Integer(N));
296 <                        fail("put should block");
295 >                        q.put(new Integer(SIZE));
296 >                        threadShouldThrow();
297                      } catch (InterruptedException ie){
298 <                        assertEquals(added, N);
298 >                        threadAssertEquals(added, SIZE);
299                      }  
300                  }});
301          t.start();
# Line 249 | 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 267 | Line 326 | public class LinkedBlockingQueueTest ext
326                          ++added;
327                          q.put(new Object());
328                          ++added;
329 <                        fail("Should block");
329 >                        threadShouldThrow();
330                      } catch (InterruptedException e){
331 <                        assertTrue(added >= 2);
331 >                        threadAssertTrue(added >= 2);
332                      }
333                  }
334              });
# Line 280 | 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 <                        assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
356 >                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
357                          q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
358 <                        fail("Should block");
358 >                        threadShouldThrow();
359                      } catch (InterruptedException success){}
360                  }
361              });
362          
363          try {
364              t.start();
365 <            Thread.sleep(SHORT_DELAY_MS);
365 >            Thread.sleep(SMALL_DELAY_MS);
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 = fullQueue(N);
379 <            for (int i = 0; i < N; ++i) {
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 <                        fail("Should block");
396 >                        threadShouldThrow();
397                      } catch (InterruptedException success){ }                
398                  }
399              });
# Line 335 | 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 {
417 <                        LinkedBlockingQueue q = fullQueue(N);
418 <                        for (int i = 0; i < N; ++i) {
417 >                        LinkedBlockingQueue q = populatedQueue(SIZE);
418 >                        for (int i = 0; i < SIZE; ++i) {
419                              assertEquals(i, ((Integer)q.take()).intValue());
420                          }
421                          q.take();
422 <                        fail("take should block");
422 >                        threadShouldThrow();
423                      } catch (InterruptedException success){
424                      }  
425                  }});
# Line 359 | 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(){
439 <        LinkedBlockingQueue q = fullQueue(N);
440 <        for (int i = 0; i < N; ++i) {
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());
445          }
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 = fullQueue(N);
455 <            for (int i = 0; i < N; ++i) {
454 >            LinkedBlockingQueue q = populatedQueue(SIZE);
455 >            for (int i = 0; i < SIZE; ++i) {
456                  assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
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 = fullQueue(N);
470 <            for (int i = 0; i < N; ++i) {
469 >            LinkedBlockingQueue q = populatedQueue(SIZE);
470 >            for (int i = 0; i < SIZE; ++i) {
471                  assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
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 {
487 <                        LinkedBlockingQueue q = fullQueue(N);
488 <                        for (int i = 0; i < N; ++i) {
489 <                            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
487 >                        LinkedBlockingQueue q = populatedQueue(SIZE);
488 >                        for (int i = 0; i < SIZE; ++i) {
489 >                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
490                          }
491 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
491 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
492                      } catch (InterruptedException success){
493                      }  
494                  }});
# Line 415 | 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 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
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 <                        fail("Should block");
518 >                        threadShouldThrow();
519                      } catch (InterruptedException success) { }                
520                  }
521              });
522          try {
523              t.start();
524 <            Thread.sleep(SHORT_DELAY_MS * 2);
525 <            assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
524 >            Thread.sleep(SMALL_DELAY_MS);
525 >            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
526              t.interrupt();
527              t.join();
528          } catch (Exception e){
529 <            fail("Unexpected exception");
529 >            unexpectedException();
530          }
531      }  
532  
533 <
534 <    public void testPeek(){
535 <        LinkedBlockingQueue q = fullQueue(N);
536 <        for (int i = 0; i < N; ++i) {
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());
540              q.poll();
541              assertTrue(q.peek() == null ||
# Line 454 | Line 544 | public class LinkedBlockingQueueTest ext
544          assertNull(q.peek());
545      }
546  
547 <    public void testElement(){
548 <        LinkedBlockingQueue q = fullQueue(N);
549 <        for (int i = 0; i < N; ++i) {
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());
554              q.poll();
555          }
556          try {
557              q.element();
558 <            fail("no such element");
558 >            shouldThrow();
559          }
560          catch (NoSuchElementException success) {}
561      }
562  
563 <    public void testRemove(){
564 <        LinkedBlockingQueue q = fullQueue(N);
565 <        for (int i = 0; i < N; ++i) {
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(){
579 <        LinkedBlockingQueue q = fullQueue(N);
580 <        for (int i = 1; i < N; i+=2) {
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)));
585          }
586 <        for (int i = 0; i < N; i+=2) {
586 >        for (int i = 0; i < SIZE; i+=2) {
587              assertTrue(q.remove(new Integer(i)));
588              assertFalse(q.remove(new Integer(i+1)));
589          }
590 <        assert(q.isEmpty());
590 >        assertTrue(q.isEmpty());
591      }
592          
593 <    public void testContains(){
594 <        LinkedBlockingQueue q = fullQueue(N);
595 <        for (int i = 0; i < N; ++i) {
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)));
600              q.poll();
601              assertFalse(q.contains(new Integer(i)));
602          }
603      }
604  
605 <    public void testClear(){
606 <        LinkedBlockingQueue q = fullQueue(N);
605 >    /**
606 >     * clear removes all elements
607 >     */
608 >    public void testClear() {
609 >        LinkedBlockingQueue q = populatedQueue(SIZE);
610          q.clear();
611          assertTrue(q.isEmpty());
612          assertEquals(0, q.size());
613 <        assertEquals(N, q.remainingCapacity());
614 <        q.add(new Integer(1));
613 >        assertEquals(SIZE, q.remainingCapacity());
614 >        q.add(one);
615          assertFalse(q.isEmpty());
616          q.clear();
617          assertTrue(q.isEmpty());
618      }
619  
620 <    public void testContainsAll(){
621 <        LinkedBlockingQueue q = fullQueue(N);
622 <        LinkedBlockingQueue p = new LinkedBlockingQueue(N);
623 <        for (int i = 0; i < N; ++i) {
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) {
627              assertTrue(q.containsAll(p));
628              assertFalse(p.containsAll(q));
629              p.add(new Integer(i));
# Line 523 | Line 631 | public class LinkedBlockingQueueTest ext
631          assertTrue(p.containsAll(q));
632      }
633  
634 <    public void testRetainAll(){
635 <        LinkedBlockingQueue q = fullQueue(N);
636 <        LinkedBlockingQueue p = fullQueue(N);
637 <        for (int i = 0; i < N; ++i) {
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) {
641              boolean changed = q.retainAll(p);
642              if (i == 0)
643                  assertFalse(changed);
# Line 534 | Line 645 | public class LinkedBlockingQueueTest ext
645                  assertTrue(changed);
646  
647              assertTrue(q.containsAll(p));
648 <            assertEquals(N-i, q.size());
648 >            assertEquals(SIZE-i, q.size());
649              p.remove();
650          }
651      }
652  
653 <    public void testRemoveAll(){
654 <        for (int i = 1; i < N; ++i) {
655 <            LinkedBlockingQueue q = fullQueue(N);
656 <            LinkedBlockingQueue p = fullQueue(i);
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);
660              assertTrue(q.removeAll(p));
661 <            assertEquals(N-i, q.size());
661 >            assertEquals(SIZE-i, q.size());
662              for (int j = 0; j < i; ++j) {
663                  Integer I = (Integer)(p.remove());
664                  assertFalse(q.contains(I));
# Line 552 | Line 666 | public class LinkedBlockingQueueTest ext
666          }
667      }
668  
669 <
670 <    public void testToArray(){
671 <        LinkedBlockingQueue q = fullQueue(N);
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(){
684 <        LinkedBlockingQueue q = fullQueue(N);
685 <        Integer[] ints = new Integer[N];
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);
690          try {
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(){
699 <        LinkedBlockingQueue q = fullQueue(N);
698 >    /**
699 >     * iterator iterates through all elements
700 >     */
701 >    public void testIterator() {
702 >        LinkedBlockingQueue q = populatedQueue(SIZE);
703          Iterator it = q.iterator();
704          try {
705              while(it.hasNext()){
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);
718 +        q.add(two);
719 +        q.add(one);
720 +        q.add(three);
721 +
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  
595        q.add(new Integer(1));
596        q.add(new Integer(2));
597        q.add(new Integer(3));
598
599        assertEquals("queue should be full", 0, q.remainingCapacity());
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 <
607 <        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 () {
611
754          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
755 <
756 <        q.add(new Integer(1));
757 <        q.add(new Integer(2));
616 <        q.add(new Integer(3));
617 <
755 >        q.add(one);
756 >        q.add(two);
757 >        q.add(three);
758          try {
759              for (Iterator it = q.iterator(); it.hasNext();) {
760                  q.remove();
# Line 622 | 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 <
628 <        assertEquals("queue should be empty again", 0, q.size());
767 >        assertEquals(0, q.size());
768      }
769  
770  
771 <    public void testToString(){
772 <        LinkedBlockingQueue q = fullQueue(N);
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 < N; ++i) {
777 >        for (int i = 0; i < SIZE; ++i) {
778              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
779          }
780      }        
781  
782  
783 +    /**
784 +     * offer transfers elements across Executor tasks
785 +     */
786      public void testOfferInExecutor() {
642
787          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
788 <
789 <        q.add(new Integer(1));
646 <        q.add(new Integer(2));
647 <
788 >        q.add(one);
789 >        q.add(two);
790          ExecutorService executor = Executors.newFixedThreadPool(2);
649
791          executor.execute(new Runnable() {
792              public void run() {
793 <                assertFalse("offer should be rejected", q.offer(new Integer(3)));
793 >                threadAssertFalse(q.offer(three));
794                  try {
795 <                    assertTrue("offer should be accepted", q.offer(new Integer(3), MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
796 <                    assertEquals(0, q.remainingCapacity());
795 >                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
796 >                    threadAssertEquals(0, q.remainingCapacity());
797                  }
798                  catch (InterruptedException e) {
799 <                    fail("should not be interrupted");
799 >                    threadUnexpectedException();
800                  }
801              }
802          });
# Line 663 | Line 804 | public class LinkedBlockingQueueTest ext
804          executor.execute(new Runnable() {
805              public void run() {
806                  try {
807 <                    Thread.sleep(MEDIUM_DELAY_MS);
808 <                    assertEquals("first item in queue should be 1", new Integer(1), q.take());
807 >                    Thread.sleep(SMALL_DELAY_MS);
808 >                    threadAssertEquals(one, q.take());
809                  }
810                  catch (InterruptedException e) {
811 <                    fail("should not be interrupted");
811 >                    threadUnexpectedException();
812                  }
813              }
814          });
815          
816 <        executor.shutdown();
676 <
816 >        joinPool(executor);
817      }
818  
819 +    /**
820 +     * poll retrieves elements across Executor threads
821 +     */
822      public void testPollInExecutor() {
680
823          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
682
824          ExecutorService executor = Executors.newFixedThreadPool(2);
684
825          executor.execute(new Runnable() {
826              public void run() {
827 <                assertNull("poll should fail", q.poll());
827 >                threadAssertNull(q.poll());
828                  try {
829 <                    assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
830 <                    assertTrue(q.isEmpty());
829 >                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
830 >                    threadAssertTrue(q.isEmpty());
831                  }
832                  catch (InterruptedException e) {
833 <                    fail("should not be interrupted");
833 >                    threadUnexpectedException();
834                  }
835              }
836          });
# Line 698 | Line 838 | public class LinkedBlockingQueueTest ext
838          executor.execute(new Runnable() {
839              public void run() {
840                  try {
841 <                    Thread.sleep(MEDIUM_DELAY_MS);
842 <                    q.put(new Integer(1));
841 >                    Thread.sleep(SMALL_DELAY_MS);
842 >                    q.put(one);
843                  }
844                  catch (InterruptedException e) {
845 <                    fail("should not be interrupted");
845 >                    threadUnexpectedException();
846                  }
847              }
848          });
849          
850 <        executor.shutdown();
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  
859 +        try {
860 +            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
861 +            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
862 +            out.writeObject(q);
863 +            out.close();
864 +
865 +            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
866 +            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
867 +            LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
868 +            assertEquals(q.size(), r.size());
869 +            while (!q.isEmpty())
870 +                assertEquals(q.remove(), r.remove());
871 +        } catch(Exception e){
872 +            unexpectedException();
873 +        }
874      }
875  
876   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines