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.6 by dl, Sun Oct 5 23:00:40 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 >     * add(null) throws NPE
156 >     */
157 >    public void testAddNull() {
158 >        try {
159 >            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
160 >            q.add(null);
161 >            shouldThrow();
162 >        } catch (NullPointerException success) { }  
163 >    }
164 >
165 >    /**
166 >     * Offer succeeds if not full; fails if full
167 >     */
168 >    public void testOffer() {
169          LinkedBlockingQueue q = new LinkedBlockingQueue(1);
170 <        assertTrue(q.offer(new Integer(0)));
171 <        assertFalse(q.offer(new Integer(1)));
170 >        assertTrue(q.offer(zero));
171 >        assertFalse(q.offer(one));
172      }
173  
174 <    public void testAdd(){
174 >    /**
175 >     * add succeeds if not full; throws ISE if full
176 >     */
177 >    public void testAdd() {
178          try {
179 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
180 <            for (int i = 0; i < N; ++i) {
179 >            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
180 >            for (int i = 0; i < SIZE; ++i) {
181                  assertTrue(q.add(new Integer(i)));
182              }
183              assertEquals(0, q.remainingCapacity());
184 <            q.add(new Integer(N));
184 >            q.add(new Integer(SIZE));
185          } catch (IllegalStateException success){
186          }  
187      }
188  
189 <    public void testAddAll1(){
189 >    /**
190 >     * addAll(null) throws NPE
191 >     */
192 >    public void testAddAll1() {
193          try {
194              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
195              q.addAll(null);
196 <            fail("Cannot add null collection");
196 >            shouldThrow();
197          }
198          catch (NullPointerException success) {}
199      }
200 <    public void testAddAll2(){
200 >
201 >    /**
202 >     * addAll(this) throws IAE
203 >     */
204 >    public void testAddAllSelf() {
205 >        try {
206 >            LinkedBlockingQueue q = populatedQueue(SIZE);
207 >            q.addAll(q);
208 >            shouldThrow();
209 >        }
210 >        catch (IllegalArgumentException success) {}
211 >    }
212 >
213 >    /**
214 >     * addAll of a collection with null elements throws NPE
215 >     */
216 >    public void testAddAll2() {
217          try {
218 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
219 <            Integer[] ints = new Integer[N];
218 >            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
219 >            Integer[] ints = new Integer[SIZE];
220              q.addAll(Arrays.asList(ints));
221 <            fail("Cannot add null elements");
221 >            shouldThrow();
222          }
223          catch (NullPointerException success) {}
224      }
225 <    public void testAddAll3(){
225 >    /**
226 >     * addAll of a collection with any null elements throws NPE after
227 >     * possibly adding some elements
228 >     */
229 >    public void testAddAll3() {
230          try {
231 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
232 <            Integer[] ints = new Integer[N];
233 <            for (int i = 0; i < N-1; ++i)
231 >            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
232 >            Integer[] ints = new Integer[SIZE];
233 >            for (int i = 0; i < SIZE-1; ++i)
234                  ints[i] = new Integer(i);
235              q.addAll(Arrays.asList(ints));
236 <            fail("Cannot add null elements");
236 >            shouldThrow();
237          }
238          catch (NullPointerException success) {}
239      }
240 <    public void testAddAll4(){
240 >    /**
241 >     * addAll throws ISE if not enough room
242 >     */
243 >    public void testAddAll4() {
244          try {
245              LinkedBlockingQueue q = new LinkedBlockingQueue(1);
246 <            Integer[] ints = new Integer[N];
247 <            for (int i = 0; i < N; ++i)
246 >            Integer[] ints = new Integer[SIZE];
247 >            for (int i = 0; i < SIZE; ++i)
248                  ints[i] = new Integer(i);
249              q.addAll(Arrays.asList(ints));
250 <            fail("Cannot add with insufficient capacity");
250 >            shouldThrow();
251          }
252          catch (IllegalStateException success) {}
253      }
254 <    public void testAddAll5(){
254 >    /**
255 >     * Queue contains all elements, in traversal order, of successful addAll
256 >     */
257 >    public void testAddAll5() {
258          try {
259              Integer[] empty = new Integer[0];
260 <            Integer[] ints = new Integer[N];
261 <            for (int i = 0; i < N; ++i)
260 >            Integer[] ints = new Integer[SIZE];
261 >            for (int i = 0; i < SIZE; ++i)
262                  ints[i] = new Integer(i);
263 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
263 >            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
264              assertFalse(q.addAll(Arrays.asList(empty)));
265              assertTrue(q.addAll(Arrays.asList(ints)));
266 <            for (int i = 0; i < N; ++i)
266 >            for (int i = 0; i < SIZE; ++i)
267                  assertEquals(ints[i], q.poll());
268          }
269          finally {}
270      }
271  
272 +    /**
273 +     * put(null) throws NPE
274 +     */
275       public void testPutNull() {
276          try {
277 <            LinkedBlockingQueue q = new LinkedBlockingQueue(N);
277 >            LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
278              q.put(null);
279 <            fail("put should throw NPE");
279 >            shouldThrow();
280          }
281          catch (NullPointerException success){
282          }  
283          catch (InterruptedException ie) {
284 <            fail("Unexpected exception");
284 >            unexpectedException();
285          }
286       }
287  
288 +    /**
289 +     * all elements successfully put are contained
290 +     */
291       public void testPut() {
292           try {
293 <             LinkedBlockingQueue q = new LinkedBlockingQueue(N);
294 <             for (int i = 0; i < N; ++i) {
293 >             LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
294 >             for (int i = 0; i < SIZE; ++i) {
295                   Integer I = new Integer(i);
296                   q.put(I);
297                   assertTrue(q.contains(I));
# Line 222 | Line 299 | public class LinkedBlockingQueueTest ext
299               assertEquals(0, q.remainingCapacity());
300           }
301          catch (InterruptedException ie) {
302 <            fail("Unexpected exception");
302 >            unexpectedException();
303          }
304      }
305  
306 <    public void testBlockingPut(){
306 >    /**
307 >     * put blocks interruptibly if full
308 >     */
309 >    public void testBlockingPut() {
310          Thread t = new Thread(new Runnable() {
311                  public void run() {
312                      int added = 0;
313                      try {
314 <                        LinkedBlockingQueue q = new LinkedBlockingQueue(N);
315 <                        for (int i = 0; i < N; ++i) {
314 >                        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
315 >                        for (int i = 0; i < SIZE; ++i) {
316                              q.put(new Integer(i));
317                              ++added;
318                          }
319 <                        q.put(new Integer(N));
320 <                        fail("put should block");
319 >                        q.put(new Integer(SIZE));
320 >                        threadShouldThrow();
321                      } catch (InterruptedException ie){
322 <                        assertEquals(added, N);
322 >                        threadAssertEquals(added, SIZE);
323                      }  
324                  }});
325          t.start();
# Line 249 | Line 329 | public class LinkedBlockingQueueTest ext
329             t.join();
330          }
331          catch (InterruptedException ie) {
332 <            fail("Unexpected exception");
332 >            unexpectedException();
333          }
334      }
335  
336 +    /**
337 +     * put blocks waiting for take when full
338 +     */
339      public void testPutWithTake() {
340          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
341          Thread t = new Thread(new Runnable() {
342 <                public void run(){
342 >                public void run() {
343                      int added = 0;
344                      try {
345                          q.put(new Object());
# Line 267 | Line 350 | public class LinkedBlockingQueueTest ext
350                          ++added;
351                          q.put(new Object());
352                          ++added;
353 <                        fail("Should block");
353 >                        threadShouldThrow();
354                      } catch (InterruptedException e){
355 <                        assertTrue(added >= 2);
355 >                        threadAssertTrue(added >= 2);
356                      }
357                  }
358              });
# Line 280 | Line 363 | public class LinkedBlockingQueueTest ext
363              t.interrupt();
364              t.join();
365          } catch (Exception e){
366 <            fail("Unexpected exception");
366 >            unexpectedException();
367          }
368      }
369  
370 +    /**
371 +     * timed offer times out if full and elements not taken
372 +     */
373      public void testTimedOffer() {
374          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
375          Thread t = new Thread(new Runnable() {
376 <                public void run(){
376 >                public void run() {
377                      try {
378                          q.put(new Object());
379                          q.put(new Object());
380 <                        assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
380 >                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
381                          q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
382 <                        fail("Should block");
382 >                        threadShouldThrow();
383                      } catch (InterruptedException success){}
384                  }
385              });
386          
387          try {
388              t.start();
389 <            Thread.sleep(SHORT_DELAY_MS);
389 >            Thread.sleep(SMALL_DELAY_MS);
390              t.interrupt();
391              t.join();
392          } catch (Exception e){
393 <            fail("Unexpected exception");
393 >            unexpectedException();
394          }
395      }
396  
397 <    public void testTake(){
397 >    /**
398 >     * take retrieves elements in FIFO order
399 >     */
400 >    public void testTake() {
401          try {
402 <            LinkedBlockingQueue q = fullQueue(N);
403 <            for (int i = 0; i < N; ++i) {
402 >            LinkedBlockingQueue q = populatedQueue(SIZE);
403 >            for (int i = 0; i < SIZE; ++i) {
404                  assertEquals(i, ((Integer)q.take()).intValue());
405              }
406          } catch (InterruptedException e){
407 <            fail("Unexpected exception");
407 >            unexpectedException();
408          }  
409      }
410  
411 +    /**
412 +     * take blocks interruptibly when empty
413 +     */
414      public void testTakeFromEmpty() {
415          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
416          Thread t = new Thread(new Runnable() {
417 <                public void run(){
417 >                public void run() {
418                      try {
419                          q.take();
420 <                        fail("Should block");
420 >                        threadShouldThrow();
421                      } catch (InterruptedException success){ }                
422                  }
423              });
# Line 335 | Line 427 | public class LinkedBlockingQueueTest ext
427              t.interrupt();
428              t.join();
429          } catch (Exception e){
430 <            fail("Unexpected exception");
430 >            unexpectedException();
431          }
432      }
433  
434 <    public void testBlockingTake(){
434 >    /**
435 >     * Take removes existing elements until empty, then blocks interruptibly
436 >     */
437 >    public void testBlockingTake() {
438          Thread t = new Thread(new Runnable() {
439                  public void run() {
440                      try {
441 <                        LinkedBlockingQueue q = fullQueue(N);
442 <                        for (int i = 0; i < N; ++i) {
441 >                        LinkedBlockingQueue q = populatedQueue(SIZE);
442 >                        for (int i = 0; i < SIZE; ++i) {
443                              assertEquals(i, ((Integer)q.take()).intValue());
444                          }
445                          q.take();
446 <                        fail("take should block");
446 >                        threadShouldThrow();
447                      } catch (InterruptedException success){
448                      }  
449                  }});
# Line 359 | Line 454 | public class LinkedBlockingQueueTest ext
454             t.join();
455          }
456          catch (InterruptedException ie) {
457 <            fail("Unexpected exception");
457 >            unexpectedException();
458          }
459      }
460  
461  
462 <    public void testPoll(){
463 <        LinkedBlockingQueue q = fullQueue(N);
464 <        for (int i = 0; i < N; ++i) {
462 >    /**
463 >     * poll succeeds unless empty
464 >     */
465 >    public void testPoll() {
466 >        LinkedBlockingQueue q = populatedQueue(SIZE);
467 >        for (int i = 0; i < SIZE; ++i) {
468              assertEquals(i, ((Integer)q.poll()).intValue());
469          }
470          assertNull(q.poll());
471      }
472  
473 +    /**
474 +     * timed pool with zero timeout succeeds when non-empty, else times out
475 +     */
476      public void testTimedPoll0() {
477          try {
478 <            LinkedBlockingQueue q = fullQueue(N);
479 <            for (int i = 0; i < N; ++i) {
478 >            LinkedBlockingQueue q = populatedQueue(SIZE);
479 >            for (int i = 0; i < SIZE; ++i) {
480                  assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
481              }
482              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
483          } catch (InterruptedException e){
484 <            fail("Unexpected exception");
484 >            unexpectedException();
485          }  
486      }
487  
488 +    /**
489 +     * timed pool with nonzero timeout succeeds when non-empty, else times out
490 +     */
491      public void testTimedPoll() {
492          try {
493 <            LinkedBlockingQueue q = fullQueue(N);
494 <            for (int i = 0; i < N; ++i) {
493 >            LinkedBlockingQueue q = populatedQueue(SIZE);
494 >            for (int i = 0; i < SIZE; ++i) {
495                  assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
496              }
497              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
498          } catch (InterruptedException e){
499 <            fail("Unexpected exception");
499 >            unexpectedException();
500          }  
501      }
502  
503 <    public void testInterruptedTimedPoll(){
503 >    /**
504 >     * Interrupted timed poll throws InterruptedException instead of
505 >     * returning timeout status
506 >     */
507 >    public void testInterruptedTimedPoll() {
508          Thread t = new Thread(new Runnable() {
509                  public void run() {
510                      try {
511 <                        LinkedBlockingQueue q = fullQueue(N);
512 <                        for (int i = 0; i < N; ++i) {
513 <                            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
511 >                        LinkedBlockingQueue q = populatedQueue(SIZE);
512 >                        for (int i = 0; i < SIZE; ++i) {
513 >                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
514                          }
515 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
515 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
516                      } catch (InterruptedException success){
517                      }  
518                  }});
# Line 415 | Line 523 | public class LinkedBlockingQueueTest ext
523             t.join();
524          }
525          catch (InterruptedException ie) {
526 <            fail("Unexpected exception");
526 >            unexpectedException();
527          }
528      }
529  
530 <    public void testTimedPollWithOffer(){
530 >    /**
531 >     *  timed poll before a delayed offer fails; after offer succeeds;
532 >     *  on interruption throws
533 >     */
534 >    public void testTimedPollWithOffer() {
535          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
536          Thread t = new Thread(new Runnable() {
537 <                public void run(){
537 >                public void run() {
538                      try {
539 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
539 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
540                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
541                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
542 <                        fail("Should block");
542 >                        threadShouldThrow();
543                      } catch (InterruptedException success) { }                
544                  }
545              });
546          try {
547              t.start();
548 <            Thread.sleep(SHORT_DELAY_MS * 2);
549 <            assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
548 >            Thread.sleep(SMALL_DELAY_MS);
549 >            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
550              t.interrupt();
551              t.join();
552          } catch (Exception e){
553 <            fail("Unexpected exception");
553 >            unexpectedException();
554          }
555      }  
556  
557 <
558 <    public void testPeek(){
559 <        LinkedBlockingQueue q = fullQueue(N);
560 <        for (int i = 0; i < N; ++i) {
557 >    /**
558 >     * peek returns next element, or null if empty
559 >     */
560 >    public void testPeek() {
561 >        LinkedBlockingQueue q = populatedQueue(SIZE);
562 >        for (int i = 0; i < SIZE; ++i) {
563              assertEquals(i, ((Integer)q.peek()).intValue());
564              q.poll();
565              assertTrue(q.peek() == null ||
# Line 454 | Line 568 | public class LinkedBlockingQueueTest ext
568          assertNull(q.peek());
569      }
570  
571 <    public void testElement(){
572 <        LinkedBlockingQueue q = fullQueue(N);
573 <        for (int i = 0; i < N; ++i) {
571 >    /**
572 >     * element returns next element, or throws NSEE if empty
573 >     */
574 >    public void testElement() {
575 >        LinkedBlockingQueue q = populatedQueue(SIZE);
576 >        for (int i = 0; i < SIZE; ++i) {
577              assertEquals(i, ((Integer)q.element()).intValue());
578              q.poll();
579          }
580          try {
581              q.element();
582 <            fail("no such element");
582 >            shouldThrow();
583          }
584          catch (NoSuchElementException success) {}
585      }
586  
587 <    public void testRemove(){
588 <        LinkedBlockingQueue q = fullQueue(N);
589 <        for (int i = 0; i < N; ++i) {
587 >    /**
588 >     * remove removes next element, or throws NSEE if empty
589 >     */
590 >    public void testRemove() {
591 >        LinkedBlockingQueue q = populatedQueue(SIZE);
592 >        for (int i = 0; i < SIZE; ++i) {
593              assertEquals(i, ((Integer)q.remove()).intValue());
594          }
595          try {
596              q.remove();
597 <            fail("remove should throw");
597 >            shouldThrow();
598          } catch (NoSuchElementException success){
599          }  
600      }
601  
602 <    public void testRemoveElement(){
603 <        LinkedBlockingQueue q = fullQueue(N);
604 <        for (int i = 1; i < N; i+=2) {
602 >    /**
603 >     * remove(x) removes x and returns true if present
604 >     */
605 >    public void testRemoveElement() {
606 >        LinkedBlockingQueue q = populatedQueue(SIZE);
607 >        for (int i = 1; i < SIZE; i+=2) {
608              assertTrue(q.remove(new Integer(i)));
609          }
610 <        for (int i = 0; i < N; i+=2) {
610 >        for (int i = 0; i < SIZE; i+=2) {
611              assertTrue(q.remove(new Integer(i)));
612              assertFalse(q.remove(new Integer(i+1)));
613          }
614 <        assert(q.isEmpty());
614 >        assertTrue(q.isEmpty());
615      }
616          
617 <    public void testContains(){
618 <        LinkedBlockingQueue q = fullQueue(N);
619 <        for (int i = 0; i < N; ++i) {
617 >    /**
618 >     * contains(x) reports true when elements added but not yet removed
619 >     */
620 >    public void testContains() {
621 >        LinkedBlockingQueue q = populatedQueue(SIZE);
622 >        for (int i = 0; i < SIZE; ++i) {
623              assertTrue(q.contains(new Integer(i)));
624              q.poll();
625              assertFalse(q.contains(new Integer(i)));
626          }
627      }
628  
629 <    public void testClear(){
630 <        LinkedBlockingQueue q = fullQueue(N);
629 >    /**
630 >     * clear removes all elements
631 >     */
632 >    public void testClear() {
633 >        LinkedBlockingQueue q = populatedQueue(SIZE);
634          q.clear();
635          assertTrue(q.isEmpty());
636          assertEquals(0, q.size());
637 <        assertEquals(N, q.remainingCapacity());
638 <        q.add(new Integer(1));
637 >        assertEquals(SIZE, q.remainingCapacity());
638 >        q.add(one);
639          assertFalse(q.isEmpty());
640          q.clear();
641          assertTrue(q.isEmpty());
642      }
643  
644 <    public void testContainsAll(){
645 <        LinkedBlockingQueue q = fullQueue(N);
646 <        LinkedBlockingQueue p = new LinkedBlockingQueue(N);
647 <        for (int i = 0; i < N; ++i) {
644 >    /**
645 >     * containsAll(c) is true when c contains a subset of elements
646 >     */
647 >    public void testContainsAll() {
648 >        LinkedBlockingQueue q = populatedQueue(SIZE);
649 >        LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE);
650 >        for (int i = 0; i < SIZE; ++i) {
651              assertTrue(q.containsAll(p));
652              assertFalse(p.containsAll(q));
653              p.add(new Integer(i));
# Line 523 | Line 655 | public class LinkedBlockingQueueTest ext
655          assertTrue(p.containsAll(q));
656      }
657  
658 <    public void testRetainAll(){
659 <        LinkedBlockingQueue q = fullQueue(N);
660 <        LinkedBlockingQueue p = fullQueue(N);
661 <        for (int i = 0; i < N; ++i) {
658 >    /**
659 >     * retainAll(c) retains only those elements of c and reports true if changed
660 >     */
661 >    public void testRetainAll() {
662 >        LinkedBlockingQueue q = populatedQueue(SIZE);
663 >        LinkedBlockingQueue p = populatedQueue(SIZE);
664 >        for (int i = 0; i < SIZE; ++i) {
665              boolean changed = q.retainAll(p);
666              if (i == 0)
667                  assertFalse(changed);
# Line 534 | Line 669 | public class LinkedBlockingQueueTest ext
669                  assertTrue(changed);
670  
671              assertTrue(q.containsAll(p));
672 <            assertEquals(N-i, q.size());
672 >            assertEquals(SIZE-i, q.size());
673              p.remove();
674          }
675      }
676  
677 <    public void testRemoveAll(){
678 <        for (int i = 1; i < N; ++i) {
679 <            LinkedBlockingQueue q = fullQueue(N);
680 <            LinkedBlockingQueue p = fullQueue(i);
677 >    /**
678 >     * removeAll(c) removes only those elements of c and reports true if changed
679 >     */
680 >    public void testRemoveAll() {
681 >        for (int i = 1; i < SIZE; ++i) {
682 >            LinkedBlockingQueue q = populatedQueue(SIZE);
683 >            LinkedBlockingQueue p = populatedQueue(i);
684              assertTrue(q.removeAll(p));
685 <            assertEquals(N-i, q.size());
685 >            assertEquals(SIZE-i, q.size());
686              for (int j = 0; j < i; ++j) {
687                  Integer I = (Integer)(p.remove());
688                  assertFalse(q.contains(I));
# Line 552 | Line 690 | public class LinkedBlockingQueueTest ext
690          }
691      }
692  
693 <
694 <    public void testToArray(){
695 <        LinkedBlockingQueue q = fullQueue(N);
693 >    /**
694 >     * toArray contains all elements
695 >     */
696 >    public void testToArray() {
697 >        LinkedBlockingQueue q = populatedQueue(SIZE);
698          Object[] o = q.toArray();
699          try {
700          for(int i = 0; i < o.length; i++)
701              assertEquals(o[i], q.take());
702          } catch (InterruptedException e){
703 <            fail("Unexpected exception");
703 >            unexpectedException();
704          }    
705      }
706  
707 <    public void testToArray2(){
708 <        LinkedBlockingQueue q = fullQueue(N);
709 <        Integer[] ints = new Integer[N];
707 >    /**
708 >     * toArray(a) contains all elements
709 >     */
710 >    public void testToArray2() {
711 >        LinkedBlockingQueue q = populatedQueue(SIZE);
712 >        Integer[] ints = new Integer[SIZE];
713          ints = (Integer[])q.toArray(ints);
714          try {
715              for(int i = 0; i < ints.length; i++)
716                  assertEquals(ints[i], q.take());
717          } catch (InterruptedException e){
718 <            fail("Unexpected exception");
718 >            unexpectedException();
719          }    
720      }
721 +
722 +    /**
723 +     * toArray(null) throws NPE
724 +     */
725 +    public void testToArray_BadArg() {
726 +        try {
727 +            LinkedBlockingQueue q = populatedQueue(SIZE);
728 +            Object o[] = q.toArray(null);
729 +            shouldThrow();
730 +        } catch(NullPointerException success){}
731 +    }
732 +
733 +    /**
734 +     * toArray with incompatable array type throws CCE
735 +     */
736 +    public void testToArray1_BadArg() {
737 +        try {
738 +            LinkedBlockingQueue q = populatedQueue(SIZE);
739 +            Object o[] = q.toArray(new String[10] );
740 +            shouldThrow();
741 +        } catch(ArrayStoreException  success){}
742 +    }
743 +
744      
745 <    public void testIterator(){
746 <        LinkedBlockingQueue q = fullQueue(N);
745 >    /**
746 >     * iterator iterates through all elements
747 >     */
748 >    public void testIterator() {
749 >        LinkedBlockingQueue q = populatedQueue(SIZE);
750          Iterator it = q.iterator();
751          try {
752              while(it.hasNext()){
753                  assertEquals(it.next(), q.take());
754              }
755          } catch (InterruptedException e){
756 <            fail("Unexpected exception");
756 >            unexpectedException();
757          }    
758      }
759  
760 <    public void testIteratorOrdering() {
761 <
760 >    /**
761 >     * iterator.remove removes current element
762 >     */
763 >    public void testIteratorRemove () {
764          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
765 +        q.add(two);
766 +        q.add(one);
767 +        q.add(three);
768 +
769 +        Iterator it = q.iterator();
770 +        it.next();
771 +        it.remove();
772 +        
773 +        it = q.iterator();
774 +        assertEquals(it.next(), one);
775 +        assertEquals(it.next(), three);
776 +        assertFalse(it.hasNext());
777 +    }
778  
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());
779  
780 +    /**
781 +     * iterator ordering is FIFO
782 +     */
783 +    public void testIteratorOrdering() {
784 +        final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
785 +        q.add(one);
786 +        q.add(two);
787 +        q.add(three);
788 +        assertEquals(0, q.remainingCapacity());
789          int k = 0;
790          for (Iterator it = q.iterator(); it.hasNext();) {
791              int i = ((Integer)(it.next())).intValue();
792 <            assertEquals("items should come out in order", ++k, i);
792 >            assertEquals(++k, i);
793          }
794 <
607 <        assertEquals("should go through 3 elements", 3, k);
794 >        assertEquals(3, k);
795      }
796  
797 +    /**
798 +     * Modifications do not cause iterators to fail
799 +     */
800      public void testWeaklyConsistentIteration () {
611
801          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
802 <
803 <        q.add(new Integer(1));
804 <        q.add(new Integer(2));
616 <        q.add(new Integer(3));
617 <
802 >        q.add(one);
803 >        q.add(two);
804 >        q.add(three);
805          try {
806              for (Iterator it = q.iterator(); it.hasNext();) {
807                  q.remove();
# Line 622 | Line 809 | public class LinkedBlockingQueueTest ext
809              }
810          }
811          catch (ConcurrentModificationException e) {
812 <            fail("weakly consistent iterator; should not get CME");
812 >            unexpectedException();
813          }
814 <
628 <        assertEquals("queue should be empty again", 0, q.size());
814 >        assertEquals(0, q.size());
815      }
816  
817  
818 <    public void testToString(){
819 <        LinkedBlockingQueue q = fullQueue(N);
818 >    /**
819 >     * toString contains toStrings of elements
820 >     */
821 >    public void testToString() {
822 >        LinkedBlockingQueue q = populatedQueue(SIZE);
823          String s = q.toString();
824 <        for (int i = 0; i < N; ++i) {
824 >        for (int i = 0; i < SIZE; ++i) {
825              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
826          }
827      }        
828  
829  
830 +    /**
831 +     * offer transfers elements across Executor tasks
832 +     */
833      public void testOfferInExecutor() {
642
834          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
835 <
836 <        q.add(new Integer(1));
646 <        q.add(new Integer(2));
647 <
835 >        q.add(one);
836 >        q.add(two);
837          ExecutorService executor = Executors.newFixedThreadPool(2);
649
838          executor.execute(new Runnable() {
839              public void run() {
840 <                assertFalse("offer should be rejected", q.offer(new Integer(3)));
840 >                threadAssertFalse(q.offer(three));
841                  try {
842 <                    assertTrue("offer should be accepted", q.offer(new Integer(3), MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
843 <                    assertEquals(0, q.remainingCapacity());
842 >                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
843 >                    threadAssertEquals(0, q.remainingCapacity());
844                  }
845                  catch (InterruptedException e) {
846 <                    fail("should not be interrupted");
846 >                    threadUnexpectedException();
847                  }
848              }
849          });
# Line 663 | Line 851 | public class LinkedBlockingQueueTest ext
851          executor.execute(new Runnable() {
852              public void run() {
853                  try {
854 <                    Thread.sleep(MEDIUM_DELAY_MS);
855 <                    assertEquals("first item in queue should be 1", new Integer(1), q.take());
854 >                    Thread.sleep(SMALL_DELAY_MS);
855 >                    threadAssertEquals(one, q.take());
856                  }
857                  catch (InterruptedException e) {
858 <                    fail("should not be interrupted");
858 >                    threadUnexpectedException();
859                  }
860              }
861          });
862          
863 <        executor.shutdown();
676 <
863 >        joinPool(executor);
864      }
865  
866 +    /**
867 +     * poll retrieves elements across Executor threads
868 +     */
869      public void testPollInExecutor() {
680
870          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
682
871          ExecutorService executor = Executors.newFixedThreadPool(2);
684
872          executor.execute(new Runnable() {
873              public void run() {
874 <                assertNull("poll should fail", q.poll());
874 >                threadAssertNull(q.poll());
875                  try {
876 <                    assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
877 <                    assertTrue(q.isEmpty());
876 >                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
877 >                    threadAssertTrue(q.isEmpty());
878                  }
879                  catch (InterruptedException e) {
880 <                    fail("should not be interrupted");
880 >                    threadUnexpectedException();
881                  }
882              }
883          });
# Line 698 | Line 885 | public class LinkedBlockingQueueTest ext
885          executor.execute(new Runnable() {
886              public void run() {
887                  try {
888 <                    Thread.sleep(MEDIUM_DELAY_MS);
889 <                    q.put(new Integer(1));
888 >                    Thread.sleep(SMALL_DELAY_MS);
889 >                    q.put(one);
890                  }
891                  catch (InterruptedException e) {
892 <                    fail("should not be interrupted");
892 >                    threadUnexpectedException();
893                  }
894              }
895          });
896          
897 <        executor.shutdown();
897 >        joinPool(executor);
898 >    }
899 >
900 >    /**
901 >     * A deserialized serialized queue has same elements in same order
902 >     */
903 >    public void testSerialization() {
904 >        LinkedBlockingQueue q = populatedQueue(SIZE);
905  
906 +        try {
907 +            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
908 +            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
909 +            out.writeObject(q);
910 +            out.close();
911 +
912 +            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
913 +            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
914 +            LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
915 +            assertEquals(q.size(), r.size());
916 +            while (!q.isEmpty())
917 +                assertEquals(q.remove(), r.remove());
918 +        } catch(Exception e){
919 +            unexpectedException();
920 +        }
921 +    }
922 +
923 +    /**
924 +     * drainTo(null) throws NPE
925 +     */
926 +    public void testDrainToNull() {
927 +        LinkedBlockingQueue q = populatedQueue(SIZE);
928 +        try {
929 +            q.drainTo(null);
930 +            shouldThrow();
931 +        } catch(NullPointerException success) {
932 +        }
933 +    }
934 +
935 +    /**
936 +     * drainTo(this) throws IAE
937 +     */
938 +    public void testDrainToSelf() {
939 +        LinkedBlockingQueue q = populatedQueue(SIZE);
940 +        try {
941 +            q.drainTo(q);
942 +            shouldThrow();
943 +        } catch(IllegalArgumentException success) {
944 +        }
945 +    }
946 +
947 +    /**
948 +     * drainTo(c) empties queue into another collection c
949 +     */
950 +    public void testDrainTo() {
951 +        LinkedBlockingQueue q = populatedQueue(SIZE);
952 +        ArrayList l = new ArrayList();
953 +        q.drainTo(l);
954 +        assertEquals(q.size(), 0);
955 +        assertEquals(l.size(), SIZE);
956 +        for (int i = 0; i < SIZE; ++i)
957 +            assertEquals(l.get(i), new Integer(i));
958 +    }
959 +
960 +    /**
961 +     * drainTo empties full queue, unblocking a waiting put.
962 +     */
963 +    public void testDrainToWithActivePut() {
964 +        final LinkedBlockingQueue q = populatedQueue(SIZE);
965 +        Thread t = new Thread(new Runnable() {
966 +                public void run() {
967 +                    try {
968 +                        q.put(new Integer(SIZE+1));
969 +                    } catch (InterruptedException ie){
970 +                        threadUnexpectedException();
971 +                    }
972 +                }
973 +            });
974 +        try {
975 +            t.start();
976 +            ArrayList l = new ArrayList();
977 +            q.drainTo(l);
978 +            assertTrue(l.size() >= SIZE);
979 +            for (int i = 0; i < SIZE; ++i)
980 +                assertEquals(l.get(i), new Integer(i));
981 +            t.join();
982 +            assertTrue(q.size() + l.size() == SIZE+1);
983 +        } catch(Exception e){
984 +            unexpectedException();
985 +        }
986 +    }
987 +
988 +    /**
989 +     * drainTo(null, n) throws NPE
990 +     */
991 +    public void testDrainToNullN() {
992 +        LinkedBlockingQueue q = populatedQueue(SIZE);
993 +        try {
994 +            q.drainTo(null, 0);
995 +            shouldThrow();
996 +        } catch(NullPointerException success) {
997 +        }
998 +    }
999 +
1000 +    /**
1001 +     * drainTo(this, n) throws IAE
1002 +     */
1003 +    public void testDrainToSelfN() {
1004 +        LinkedBlockingQueue q = populatedQueue(SIZE);
1005 +        try {
1006 +            q.drainTo(q, 0);
1007 +            shouldThrow();
1008 +        } catch(IllegalArgumentException success) {
1009 +        }
1010 +    }
1011 +
1012 +    /**
1013 +     * drainTo(c, n) empties first max {n, size} elements of queue into c
1014 +     */
1015 +    public void testDrainToN() {
1016 +        for (int i = 0; i < SIZE + 2; ++i) {
1017 +            LinkedBlockingQueue q = populatedQueue(SIZE);
1018 +            ArrayList l = new ArrayList();
1019 +            q.drainTo(l, i);
1020 +            int k = (i < SIZE)? i : SIZE;
1021 +            assertEquals(q.size(), SIZE-k);
1022 +            assertEquals(l.size(), k);
1023 +            for (int j = 0; j < k; ++j)
1024 +                assertEquals(l.get(j), new Integer(j));
1025 +        }
1026      }
1027  
1028   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines