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

Comparing jsr166/src/test/tck/ArrayBlockingQueueTest.java (file contents):
Revision 1.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.12 by jsr166, Mon Nov 2 20:28:31 2009 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9 +
10   import junit.framework.*;
11   import java.util.*;
12   import java.util.concurrent.*;
13   import java.io.*;
14  
15 < public class ArrayBlockingQueueTest extends TestCase {
14 <
15 <    private static int N = 10;
16 <    private static long SHORT_DELAY_MS = 100;
17 <    private static long MEDIUM_DELAY_MS = 1000;
18 <    private static long LONG_DELAY_MS = 10000;
19 <
15 > public class ArrayBlockingQueueTest extends JSR166TestCase {
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());  
17 >        junit.textui.TestRunner.run (suite());
18      }
23
19      public static Test suite() {
20          return new TestSuite(ArrayBlockingQueueTest.class);
21      }
# Line 29 | Line 24 | public class ArrayBlockingQueueTest exte
24       * Create a queue of given size containing consecutive
25       * Integers 0 ... n.
26       */
27 <    private ArrayBlockingQueue fullQueue(int n) {
27 >    private ArrayBlockingQueue populatedQueue(int n) {
28          ArrayBlockingQueue q = new ArrayBlockingQueue(n);
29          assertTrue(q.isEmpty());
30          for(int i = 0; i < n; i++)
# Line 39 | Line 34 | public class ArrayBlockingQueueTest exte
34          assertEquals(n, q.size());
35          return q;
36      }
37 <
38 <    public void testConstructor1(){
39 <        assertEquals(N, new ArrayBlockingQueue(N).remainingCapacity());
37 >
38 >    /**
39 >     * A new queue has the indicated capacity
40 >     */
41 >    public void testConstructor1() {
42 >        assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity());
43      }
44  
45 <    public void testConstructor2(){
45 >    /**
46 >     * Constructor throws IAE if  capacity argument nonpositive
47 >     */
48 >    public void testConstructor2() {
49          try {
50              ArrayBlockingQueue q = new ArrayBlockingQueue(0);
51 <            fail("Cannot make zero-sized");
51 >            shouldThrow();
52          }
53          catch (IllegalArgumentException success) {}
54      }
55  
56 <    public void testConstructor3(){
57 <
56 >    /**
57 >     * Initializing from null Collection throws NPE
58 >     */
59 >    public void testConstructor3() {
60          try {
61              ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
62 <            fail("Cannot make from null collection");
62 >            shouldThrow();
63          }
64          catch (NullPointerException success) {}
65      }
66  
67 <    public void testConstructor4(){
67 >    /**
68 >     * Initializing from Collection of null elements throws NPE
69 >     */
70 >    public void testConstructor4() {
71          try {
72 <            Integer[] ints = new Integer[N];
73 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N, false, Arrays.asList(ints));
74 <            fail("Cannot make with null elements");
72 >            Integer[] ints = new Integer[SIZE];
73 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
74 >            shouldThrow();
75          }
76          catch (NullPointerException success) {}
77      }
78  
79 <    public void testConstructor5(){
79 >    /**
80 >     * Initializing from Collection with some null elements throws NPE
81 >     */
82 >    public void testConstructor5() {
83          try {
84 <            Integer[] ints = new Integer[N];
85 <            for (int i = 0; i < N-1; ++i)
84 >            Integer[] ints = new Integer[SIZE];
85 >            for (int i = 0; i < SIZE-1; ++i)
86                  ints[i] = new Integer(i);
87 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N, false, Arrays.asList(ints));
88 <            fail("Cannot make with null elements");
87 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
88 >            shouldThrow();
89          }
90          catch (NullPointerException success) {}
91      }
92  
93 <    public void testConstructor6(){
93 >    /**
94 >     * Initializing from too large collection throws IAE
95 >     */
96 >    public void testConstructor6() {
97          try {
98 <            Integer[] ints = new Integer[N];
99 <            for (int i = 0; i < N; ++i)
98 >            Integer[] ints = new Integer[SIZE];
99 >            for (int i = 0; i < SIZE; ++i)
100                  ints[i] = new Integer(i);
101              ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
102 <            fail("Cannot make with insufficient capacity");
102 >            shouldThrow();
103          }
104          catch (IllegalArgumentException success) {}
105      }
106  
107 <    public void testConstructor7(){
107 >    /**
108 >     * Queue contains all elements of collection used to initialize
109 >     */
110 >    public void testConstructor7() {
111          try {
112 <            Integer[] ints = new Integer[N];
113 <            for (int i = 0; i < N; ++i)
112 >            Integer[] ints = new Integer[SIZE];
113 >            for (int i = 0; i < SIZE; ++i)
114                  ints[i] = new Integer(i);
115 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N, true, Arrays.asList(ints));
116 <            for (int i = 0; i < N; ++i)
115 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
116 >            for (int i = 0; i < SIZE; ++i)
117                  assertEquals(ints[i], q.poll());
118          }
119          finally {}
120      }
121  
122 +    /**
123 +     * Queue transitions from empty to full when elements added
124 +     */
125      public void testEmptyFull() {
126          ArrayBlockingQueue q = new ArrayBlockingQueue(2);
127          assertTrue(q.isEmpty());
128 <        assertEquals("should have room for 2", 2, q.remainingCapacity());
129 <        q.add(new Integer(1));
128 >        assertEquals(2, q.remainingCapacity());
129 >        q.add(one);
130          assertFalse(q.isEmpty());
131 <        q.add(new Integer(2));
131 >        q.add(two);
132          assertFalse(q.isEmpty());
133 <        assertEquals("queue should be full", 0, q.remainingCapacity());
134 <        assertFalse("offer should be rejected", q.offer(new Integer(3)));
133 >        assertEquals(0, q.remainingCapacity());
134 >        assertFalse(q.offer(three));
135      }
136  
137 <    public void testRemainingCapacity(){
138 <        ArrayBlockingQueue q = fullQueue(N);
139 <        for (int i = 0; i < N; ++i) {
137 >    /**
138 >     * remainingCapacity decreases on add, increases on remove
139 >     */
140 >    public void testRemainingCapacity() {
141 >        ArrayBlockingQueue q = populatedQueue(SIZE);
142 >        for (int i = 0; i < SIZE; ++i) {
143              assertEquals(i, q.remainingCapacity());
144 <            assertEquals(N-i, q.size());
144 >            assertEquals(SIZE-i, q.size());
145              q.remove();
146          }
147 <        for (int i = 0; i < N; ++i) {
148 <            assertEquals(N-i, q.remainingCapacity());
147 >        for (int i = 0; i < SIZE; ++i) {
148 >            assertEquals(SIZE-i, q.remainingCapacity());
149              assertEquals(i, q.size());
150              q.add(new Integer(i));
151          }
152      }
153  
154 <    public void testOfferNull(){
154 >    /**
155 >     *  offer(null) throws NPE
156 >     */
157 >    public void testOfferNull() {
158          try {
159              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
160              q.offer(null);
161 <            fail("should throw NPE");
162 <        } catch (NullPointerException success) { }  
161 >            shouldThrow();
162 >        } catch (NullPointerException success) { }
163 >    }
164 >
165 >    /**
166 >     *  add(null) throws NPE
167 >     */
168 >    public void testAddNull() {
169 >        try {
170 >            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
171 >            q.add(null);
172 >            shouldThrow();
173 >        } catch (NullPointerException success) { }
174      }
175  
176 <    public void testOffer(){
176 >    /**
177 >     * Offer succeeds if not full; fails if full
178 >     */
179 >    public void testOffer() {
180          ArrayBlockingQueue q = new ArrayBlockingQueue(1);
181 <        assertTrue(q.offer(new Integer(0)));
182 <        assertFalse(q.offer(new Integer(1)));
181 >        assertTrue(q.offer(zero));
182 >        assertFalse(q.offer(one));
183      }
184  
185 <    public void testAdd(){
185 >    /**
186 >     * add succeeds if not full; throws ISE if full
187 >     */
188 >    public void testAdd() {
189          try {
190 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
191 <            for (int i = 0; i < N; ++i) {
190 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
191 >            for (int i = 0; i < SIZE; ++i) {
192                  assertTrue(q.add(new Integer(i)));
193              }
194              assertEquals(0, q.remainingCapacity());
195 <            q.add(new Integer(N));
195 >            q.add(new Integer(SIZE));
196          } catch (IllegalStateException success){
197 <        }  
197 >        }
198      }
199  
200 <    public void testAddAll1(){
200 >    /**
201 >     *  addAll(null) throws NPE
202 >     */
203 >    public void testAddAll1() {
204          try {
205              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
206              q.addAll(null);
207 <            fail("Cannot add null collection");
207 >            shouldThrow();
208          }
209          catch (NullPointerException success) {}
210      }
211 <    public void testAddAll2(){
211 >
212 >    /**
213 >     * addAll(this) throws IAE
214 >     */
215 >    public void testAddAllSelf() {
216          try {
217 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
218 <            Integer[] ints = new Integer[N];
217 >            ArrayBlockingQueue q = populatedQueue(SIZE);
218 >            q.addAll(q);
219 >            shouldThrow();
220 >        }
221 >        catch (IllegalArgumentException success) {}
222 >    }
223 >
224 >
225 >    /**
226 >     *  addAll of a collection with null elements throws NPE
227 >     */
228 >    public void testAddAll2() {
229 >        try {
230 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
231 >            Integer[] ints = new Integer[SIZE];
232              q.addAll(Arrays.asList(ints));
233 <            fail("Cannot add null elements");
233 >            shouldThrow();
234          }
235          catch (NullPointerException success) {}
236      }
237 <    public void testAddAll3(){
237 >    /**
238 >     * addAll of a collection with any null elements throws NPE after
239 >     * possibly adding some elements
240 >     */
241 >    public void testAddAll3() {
242          try {
243 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
244 <            Integer[] ints = new Integer[N];
245 <            for (int i = 0; i < N-1; ++i)
243 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
244 >            Integer[] ints = new Integer[SIZE];
245 >            for (int i = 0; i < SIZE-1; ++i)
246                  ints[i] = new Integer(i);
247              q.addAll(Arrays.asList(ints));
248 <            fail("Cannot add null elements");
248 >            shouldThrow();
249          }
250          catch (NullPointerException success) {}
251      }
252 <    public void testAddAll4(){
252 >    /**
253 >     * addAll throws ISE if not enough room
254 >     */
255 >    public void testAddAll4() {
256          try {
257              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
258 <            Integer[] ints = new Integer[N];
259 <            for (int i = 0; i < N; ++i)
258 >            Integer[] ints = new Integer[SIZE];
259 >            for (int i = 0; i < SIZE; ++i)
260                  ints[i] = new Integer(i);
261              q.addAll(Arrays.asList(ints));
262 <            fail("Cannot add with insufficient capacity");
262 >            shouldThrow();
263          }
264          catch (IllegalStateException success) {}
265      }
266 <    public void testAddAll5(){
266 >    /**
267 >     * Queue contains all elements, in traversal order, of successful addAll
268 >     */
269 >    public void testAddAll5() {
270          try {
271              Integer[] empty = new Integer[0];
272 <            Integer[] ints = new Integer[N];
273 <            for (int i = 0; i < N; ++i)
272 >            Integer[] ints = new Integer[SIZE];
273 >            for (int i = 0; i < SIZE; ++i)
274                  ints[i] = new Integer(i);
275 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
275 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
276              assertFalse(q.addAll(Arrays.asList(empty)));
277              assertTrue(q.addAll(Arrays.asList(ints)));
278 <            for (int i = 0; i < N; ++i)
278 >            for (int i = 0; i < SIZE; ++i)
279                  assertEquals(ints[i], q.poll());
280          }
281          finally {}
282      }
283  
284 +    /**
285 +     *  put(null) throws NPE
286 +     */
287       public void testPutNull() {
288          try {
289 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
289 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
290              q.put(null);
291 <            fail("put should throw NPE");
292 <        }
291 >            shouldThrow();
292 >        }
293          catch (NullPointerException success){
294 <        }  
294 >        }
295          catch (InterruptedException ie) {
296 <            fail("Unexpected exception");
296 >            unexpectedException();
297          }
298       }
299  
300 +    /**
301 +     * all elements successfully put are contained
302 +     */
303       public void testPut() {
304           try {
305 <             ArrayBlockingQueue q = new ArrayBlockingQueue(N);
306 <             for (int i = 0; i < N; ++i) {
305 >             ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
306 >             for (int i = 0; i < SIZE; ++i) {
307                   Integer I = new Integer(i);
308                   q.put(I);
309                   assertTrue(q.contains(I));
# Line 234 | Line 311 | public class ArrayBlockingQueueTest exte
311               assertEquals(0, q.remainingCapacity());
312           }
313          catch (InterruptedException ie) {
314 <            fail("Unexpected exception");
314 >            unexpectedException();
315          }
316      }
317  
318 <    public void testBlockingPut(){
318 >    /**
319 >     * put blocks interruptibly if full
320 >     */
321 >    public void testBlockingPut() {
322 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
323          Thread t = new Thread(new Runnable() {
324                  public void run() {
325                      int added = 0;
326                      try {
327 <                        ArrayBlockingQueue q = new ArrayBlockingQueue(N);
247 <                        for (int i = 0; i < N; ++i) {
327 >                        for (int i = 0; i < SIZE; ++i) {
328                              q.put(new Integer(i));
329                              ++added;
330                          }
331 <                        q.put(new Integer(N));
332 <                        fail("put should block");
331 >                        q.put(new Integer(SIZE));
332 >                        threadShouldThrow();
333                      } catch (InterruptedException ie){
334 <                        assertEquals(added, N);
335 <                    }  
334 >                        threadAssertEquals(added, SIZE);
335 >                    }
336                  }});
337 <        t.start();
338 <        try {
339 <           Thread.sleep(SHORT_DELAY_MS);
337 >        try {
338 >            t.start();
339 >           Thread.sleep(MEDIUM_DELAY_MS);
340             t.interrupt();
341             t.join();
342          }
343          catch (InterruptedException ie) {
344 <            fail("Unexpected exception");
344 >            unexpectedException();
345          }
346      }
347  
348 +    /**
349 +     * put blocks waiting for take when full
350 +     */
351      public void testPutWithTake() {
352          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
353          Thread t = new Thread(new Runnable() {
354 <                public void run(){
354 >                public void run() {
355                      int added = 0;
356                      try {
357                          q.put(new Object());
# Line 279 | Line 362 | public class ArrayBlockingQueueTest exte
362                          ++added;
363                          q.put(new Object());
364                          ++added;
365 <                        fail("Should block");
365 >                        threadShouldThrow();
366                      } catch (InterruptedException e){
367 <                        assertTrue(added >= 2);
367 >                        threadAssertTrue(added >= 2);
368                      }
369                  }
370              });
# Line 292 | Line 375 | public class ArrayBlockingQueueTest exte
375              t.interrupt();
376              t.join();
377          } catch (Exception e){
378 <            fail("Unexpected exception");
378 >            unexpectedException();
379          }
380      }
381  
382 +    /**
383 +     * timed offer times out if full and elements not taken
384 +     */
385      public void testTimedOffer() {
386          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
387          Thread t = new Thread(new Runnable() {
388 <                public void run(){
388 >                public void run() {
389                      try {
390                          q.put(new Object());
391                          q.put(new Object());
392 <                        assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
392 >                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
393                          q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
394 <                        fail("Should block");
394 >                        threadShouldThrow();
395                      } catch (InterruptedException success){}
396                  }
397              });
398 <        
398 >
399          try {
400              t.start();
401              Thread.sleep(SHORT_DELAY_MS);
402              t.interrupt();
403              t.join();
404          } catch (Exception e){
405 <            fail("Unexpected exception");
405 >            unexpectedException();
406          }
407      }
408  
409 <    public void testTake(){
409 >    /**
410 >     * take retrieves elements in FIFO order
411 >     */
412 >    public void testTake() {
413          try {
414 <            ArrayBlockingQueue q = fullQueue(N);
415 <            for (int i = 0; i < N; ++i) {
414 >            ArrayBlockingQueue q = populatedQueue(SIZE);
415 >            for (int i = 0; i < SIZE; ++i) {
416                  assertEquals(i, ((Integer)q.take()).intValue());
417              }
418          } catch (InterruptedException e){
419 <            fail("Unexpected exception");
420 <        }  
419 >            unexpectedException();
420 >        }
421      }
422  
423 +    /**
424 +     * take blocks interruptibly when empty
425 +     */
426      public void testTakeFromEmpty() {
427          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
428          Thread t = new Thread(new Runnable() {
429 <                public void run(){
429 >                public void run() {
430                      try {
431                          q.take();
432 <                        fail("Should block");
433 <                    } catch (InterruptedException success){ }                
432 >                        threadShouldThrow();
433 >                    } catch (InterruptedException success){ }
434                  }
435              });
436          try {
# Line 347 | Line 439 | public class ArrayBlockingQueueTest exte
439              t.interrupt();
440              t.join();
441          } catch (Exception e){
442 <            fail("Unexpected exception");
442 >            unexpectedException();
443          }
444      }
445  
446 <    public void testBlockingTake(){
446 >    /**
447 >     * Take removes existing elements until empty, then blocks interruptibly
448 >     */
449 >    public void testBlockingTake() {
450          Thread t = new Thread(new Runnable() {
451                  public void run() {
452                      try {
453 <                        ArrayBlockingQueue q = fullQueue(N);
454 <                        for (int i = 0; i < N; ++i) {
455 <                            assertEquals(i, ((Integer)q.take()).intValue());
453 >                        ArrayBlockingQueue q = populatedQueue(SIZE);
454 >                        for (int i = 0; i < SIZE; ++i) {
455 >                            threadAssertEquals(i, ((Integer)q.take()).intValue());
456                          }
457                          q.take();
458 <                        fail("take should block");
458 >                        threadShouldThrow();
459                      } catch (InterruptedException success){
460 <                    }  
460 >                    }
461                  }});
462 <        t.start();
463 <        try {
464 <           Thread.sleep(SHORT_DELAY_MS);
465 <           t.interrupt();
466 <           t.join();
462 >        try {
463 >            t.start();
464 >            Thread.sleep(SHORT_DELAY_MS);
465 >            t.interrupt();
466 >            t.join();
467          }
468          catch (InterruptedException ie) {
469 <            fail("Unexpected exception");
469 >            unexpectedException();
470          }
471      }
472  
473  
474 <    public void testPoll(){
475 <        ArrayBlockingQueue q = fullQueue(N);
476 <        for (int i = 0; i < N; ++i) {
474 >    /**
475 >     * poll succeeds unless empty
476 >     */
477 >    public void testPoll() {
478 >        ArrayBlockingQueue q = populatedQueue(SIZE);
479 >        for (int i = 0; i < SIZE; ++i) {
480              assertEquals(i, ((Integer)q.poll()).intValue());
481          }
482          assertNull(q.poll());
483      }
484  
485 +    /**
486 +     * timed pool with zero timeout succeeds when non-empty, else times out
487 +     */
488      public void testTimedPoll0() {
489          try {
490 <            ArrayBlockingQueue q = fullQueue(N);
491 <            for (int i = 0; i < N; ++i) {
490 >            ArrayBlockingQueue q = populatedQueue(SIZE);
491 >            for (int i = 0; i < SIZE; ++i) {
492                  assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
493              }
494              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
495          } catch (InterruptedException e){
496 <            fail("Unexpected exception");
497 <        }  
496 >            unexpectedException();
497 >        }
498      }
499  
500 +    /**
501 +     * timed pool with nonzero timeout succeeds when non-empty, else times out
502 +     */
503      public void testTimedPoll() {
504          try {
505 <            ArrayBlockingQueue q = fullQueue(N);
506 <            for (int i = 0; i < N; ++i) {
505 >            ArrayBlockingQueue q = populatedQueue(SIZE);
506 >            for (int i = 0; i < SIZE; ++i) {
507                  assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
508              }
509              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
510          } catch (InterruptedException e){
511 <            fail("Unexpected exception");
512 <        }  
511 >            unexpectedException();
512 >        }
513      }
514  
515 <    public void testInterruptedTimedPoll(){
515 >    /**
516 >     * Interrupted timed poll throws InterruptedException instead of
517 >     * returning timeout status
518 >     */
519 >    public void testInterruptedTimedPoll() {
520          Thread t = new Thread(new Runnable() {
521                  public void run() {
522                      try {
523 <                        ArrayBlockingQueue q = fullQueue(N);
524 <                        for (int i = 0; i < N; ++i) {
525 <                            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
523 >                        ArrayBlockingQueue q = populatedQueue(SIZE);
524 >                        for (int i = 0; i < SIZE; ++i) {
525 >                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
526                          }
527 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
527 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
528                      } catch (InterruptedException success){
529 <                    }  
529 >                    }
530                  }});
531 <        t.start();
532 <        try {
533 <           Thread.sleep(SHORT_DELAY_MS);
534 <           t.interrupt();
535 <           t.join();
531 >        try {
532 >            t.start();
533 >            Thread.sleep(SHORT_DELAY_MS);
534 >            t.interrupt();
535 >            t.join();
536          }
537          catch (InterruptedException ie) {
538 <            fail("Unexpected exception");
538 >            unexpectedException();
539          }
540      }
541  
542 <    public void testTimedPollWithOffer(){
542 >    /**
543 >     *  timed poll before a delayed offer fails; after offer succeeds;
544 >     *  on interruption throws
545 >     */
546 >    public void testTimedPollWithOffer() {
547          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
548          Thread t = new Thread(new Runnable() {
549 <                public void run(){
549 >                public void run() {
550                      try {
551 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
551 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
552                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
553                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
554 <                        fail("Should block");
555 <                    } catch (InterruptedException success) { }                
554 >                        threadShouldThrow();
555 >                    } catch (InterruptedException success) { }
556                  }
557              });
558          try {
559              t.start();
560 <            Thread.sleep(SHORT_DELAY_MS * 2);
561 <            assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
560 >            Thread.sleep(SMALL_DELAY_MS);
561 >            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
562              t.interrupt();
563              t.join();
564          } catch (Exception e){
565 <            fail("Unexpected exception");
565 >            unexpectedException();
566          }
567 <    }  
567 >    }
568  
569  
570 <    public void testPeek(){
571 <        ArrayBlockingQueue q = fullQueue(N);
572 <        for (int i = 0; i < N; ++i) {
570 >    /**
571 >     * peek returns next element, or null if empty
572 >     */
573 >    public void testPeek() {
574 >        ArrayBlockingQueue q = populatedQueue(SIZE);
575 >        for (int i = 0; i < SIZE; ++i) {
576              assertEquals(i, ((Integer)q.peek()).intValue());
577              q.poll();
578              assertTrue(q.peek() == null ||
# Line 466 | Line 581 | public class ArrayBlockingQueueTest exte
581          assertNull(q.peek());
582      }
583  
584 <    public void testElement(){
585 <        ArrayBlockingQueue q = fullQueue(N);
586 <        for (int i = 0; i < N; ++i) {
584 >    /**
585 >     * element returns next element, or throws NSEE if empty
586 >     */
587 >    public void testElement() {
588 >        ArrayBlockingQueue q = populatedQueue(SIZE);
589 >        for (int i = 0; i < SIZE; ++i) {
590              assertEquals(i, ((Integer)q.element()).intValue());
591              q.poll();
592          }
593          try {
594              q.element();
595 <            fail("no such element");
595 >            shouldThrow();
596          }
597          catch (NoSuchElementException success) {}
598      }
599  
600 <    public void testRemove(){
601 <        ArrayBlockingQueue q = fullQueue(N);
602 <        for (int i = 0; i < N; ++i) {
600 >    /**
601 >     * remove removes next element, or throws NSEE if empty
602 >     */
603 >    public void testRemove() {
604 >        ArrayBlockingQueue q = populatedQueue(SIZE);
605 >        for (int i = 0; i < SIZE; ++i) {
606              assertEquals(i, ((Integer)q.remove()).intValue());
607          }
608          try {
609              q.remove();
610 <            fail("remove should throw");
610 >            shouldThrow();
611          } catch (NoSuchElementException success){
612 <        }  
612 >        }
613      }
614  
615 <    public void testRemoveElement(){
616 <        ArrayBlockingQueue q = fullQueue(N);
617 <        for (int i = 1; i < N; i+=2) {
615 >    /**
616 >     * remove(x) removes x and returns true if present
617 >     */
618 >    public void testRemoveElement() {
619 >        ArrayBlockingQueue q = populatedQueue(SIZE);
620 >        for (int i = 1; i < SIZE; i+=2) {
621              assertTrue(q.remove(new Integer(i)));
622          }
623 <        for (int i = 0; i < N; i+=2) {
623 >        for (int i = 0; i < SIZE; i+=2) {
624              assertTrue(q.remove(new Integer(i)));
625              assertFalse(q.remove(new Integer(i+1)));
626          }
627          assertTrue(q.isEmpty());
628      }
629 <        
630 <    public void testContains(){
631 <        ArrayBlockingQueue q = fullQueue(N);
632 <        for (int i = 0; i < N; ++i) {
629 >
630 >    /**
631 >     * contains(x) reports true when elements added but not yet removed
632 >     */
633 >    public void testContains() {
634 >        ArrayBlockingQueue q = populatedQueue(SIZE);
635 >        for (int i = 0; i < SIZE; ++i) {
636              assertTrue(q.contains(new Integer(i)));
637              q.poll();
638              assertFalse(q.contains(new Integer(i)));
639          }
640      }
641  
642 <    public void testClear(){
643 <        ArrayBlockingQueue q = fullQueue(N);
642 >    /**
643 >     * clear removes all elements
644 >     */
645 >    public void testClear() {
646 >        ArrayBlockingQueue q = populatedQueue(SIZE);
647          q.clear();
648          assertTrue(q.isEmpty());
649          assertEquals(0, q.size());
650 <        assertEquals(N, q.remainingCapacity());
651 <        q.add(new Integer(1));
650 >        assertEquals(SIZE, q.remainingCapacity());
651 >        q.add(one);
652          assertFalse(q.isEmpty());
653 +        assertTrue(q.contains(one));
654          q.clear();
655          assertTrue(q.isEmpty());
656      }
657  
658 <    public void testContainsAll(){
659 <        ArrayBlockingQueue q = fullQueue(N);
660 <        ArrayBlockingQueue p = new ArrayBlockingQueue(N);
661 <        for (int i = 0; i < N; ++i) {
658 >    /**
659 >     * containsAll(c) is true when c contains a subset of elements
660 >     */
661 >    public void testContainsAll() {
662 >        ArrayBlockingQueue q = populatedQueue(SIZE);
663 >        ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE);
664 >        for (int i = 0; i < SIZE; ++i) {
665              assertTrue(q.containsAll(p));
666              assertFalse(p.containsAll(q));
667              p.add(new Integer(i));
# Line 535 | Line 669 | public class ArrayBlockingQueueTest exte
669          assertTrue(p.containsAll(q));
670      }
671  
672 <    public void testRetainAll(){
673 <        ArrayBlockingQueue q = fullQueue(N);
674 <        ArrayBlockingQueue p = fullQueue(N);
675 <        for (int i = 0; i < N; ++i) {
672 >    /**
673 >     * retainAll(c) retains only those elements of c and reports true if changed
674 >     */
675 >    public void testRetainAll() {
676 >        ArrayBlockingQueue q = populatedQueue(SIZE);
677 >        ArrayBlockingQueue p = populatedQueue(SIZE);
678 >        for (int i = 0; i < SIZE; ++i) {
679              boolean changed = q.retainAll(p);
680              if (i == 0)
681                  assertFalse(changed);
# Line 546 | Line 683 | public class ArrayBlockingQueueTest exte
683                  assertTrue(changed);
684  
685              assertTrue(q.containsAll(p));
686 <            assertEquals(N-i, q.size());
686 >            assertEquals(SIZE-i, q.size());
687              p.remove();
688          }
689      }
690  
691 <    public void testRemoveAll(){
692 <        for (int i = 1; i < N; ++i) {
693 <            ArrayBlockingQueue q = fullQueue(N);
694 <            ArrayBlockingQueue p = fullQueue(i);
691 >    /**
692 >     * removeAll(c) removes only those elements of c and reports true if changed
693 >     */
694 >    public void testRemoveAll() {
695 >        for (int i = 1; i < SIZE; ++i) {
696 >            ArrayBlockingQueue q = populatedQueue(SIZE);
697 >            ArrayBlockingQueue p = populatedQueue(i);
698              assertTrue(q.removeAll(p));
699 <            assertEquals(N-i, q.size());
699 >            assertEquals(SIZE-i, q.size());
700              for (int j = 0; j < i; ++j) {
701                  Integer I = (Integer)(p.remove());
702                  assertFalse(q.contains(I));
# Line 564 | Line 704 | public class ArrayBlockingQueueTest exte
704          }
705      }
706  
707 <
708 <    public void testToArray(){
709 <        ArrayBlockingQueue q = fullQueue(N);
707 >    /**
708 >     *  toArray contains all elements
709 >     */
710 >    public void testToArray() {
711 >        ArrayBlockingQueue q = populatedQueue(SIZE);
712          Object[] o = q.toArray();
713          try {
714          for(int i = 0; i < o.length; i++)
715              assertEquals(o[i], q.take());
716          } catch (InterruptedException e){
717 <            fail("Unexpected exception");
718 <        }    
717 >            unexpectedException();
718 >        }
719      }
720  
721 <    public void testToArray2(){
722 <        ArrayBlockingQueue q = fullQueue(N);
723 <        Integer[] ints = new Integer[N];
721 >    /**
722 >     * toArray(a) contains all elements
723 >     */
724 >    public void testToArray2() {
725 >        ArrayBlockingQueue q = populatedQueue(SIZE);
726 >        Integer[] ints = new Integer[SIZE];
727          ints = (Integer[])q.toArray(ints);
728          try {
729              for(int i = 0; i < ints.length; i++)
730                  assertEquals(ints[i], q.take());
731          } catch (InterruptedException e){
732 <            fail("Unexpected exception");
733 <        }    
732 >            unexpectedException();
733 >        }
734 >    }
735 >
736 >    /**
737 >     * toArray(null) throws NPE
738 >     */
739 >    public void testToArray_BadArg() {
740 >        try {
741 >            ArrayBlockingQueue q = populatedQueue(SIZE);
742 >            Object o[] = q.toArray(null);
743 >            shouldThrow();
744 >        } catch(NullPointerException success){}
745      }
746 <    
747 <    public void testIterator(){
748 <        ArrayBlockingQueue q = fullQueue(N);
746 >
747 >    /**
748 >     * toArray with incompatible array type throws CCE
749 >     */
750 >    public void testToArray1_BadArg() {
751 >        try {
752 >            ArrayBlockingQueue q = populatedQueue(SIZE);
753 >            Object o[] = q.toArray(new String[10] );
754 >            shouldThrow();
755 >        } catch(ArrayStoreException  success){}
756 >    }
757 >
758 >
759 >    /**
760 >     * iterator iterates through all elements
761 >     */
762 >    public void testIterator() {
763 >        ArrayBlockingQueue q = populatedQueue(SIZE);
764          Iterator it = q.iterator();
765          try {
766              while(it.hasNext()){
767                  assertEquals(it.next(), q.take());
768              }
769          } catch (InterruptedException e){
770 <            fail("Unexpected exception");
771 <        }    
770 >            unexpectedException();
771 >        }
772      }
773  
774 <    public void testIteratorOrdering() {
775 <
774 >    /**
775 >     * iterator.remove removes current element
776 >     */
777 >    public void testIteratorRemove () {
778          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
779 +        q.add(two);
780 +        q.add(one);
781 +        q.add(three);
782 +
783 +        Iterator it = q.iterator();
784 +        it.next();
785 +        it.remove();
786 +
787 +        it = q.iterator();
788 +        assertEquals(it.next(), one);
789 +        assertEquals(it.next(), three);
790 +        assertFalse(it.hasNext());
791 +    }
792  
793 <        q.add(new Integer(1));
794 <        q.add(new Integer(2));
795 <        q.add(new Integer(3));
793 >    /**
794 >     * iterator ordering is FIFO
795 >     */
796 >    public void testIteratorOrdering() {
797 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
798 >        q.add(one);
799 >        q.add(two);
800 >        q.add(three);
801  
802          assertEquals("queue should be full", 0, q.remainingCapacity());
803  
804          int k = 0;
805          for (Iterator it = q.iterator(); it.hasNext();) {
806              int i = ((Integer)(it.next())).intValue();
807 <            assertEquals("items should come out in order", ++k, i);
807 >            assertEquals(++k, i);
808          }
809 <
619 <        assertEquals("should go through 3 elements", 3, k);
809 >        assertEquals(3, k);
810      }
811  
812 +    /**
813 +     * Modifications do not cause iterators to fail
814 +     */
815      public void testWeaklyConsistentIteration () {
623
816          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
817 <
818 <        q.add(new Integer(1));
819 <        q.add(new Integer(2));
628 <        q.add(new Integer(3));
629 <
817 >        q.add(one);
818 >        q.add(two);
819 >        q.add(three);
820          try {
821              for (Iterator it = q.iterator(); it.hasNext();) {
822                  q.remove();
# Line 634 | Line 824 | public class ArrayBlockingQueueTest exte
824              }
825          }
826          catch (ConcurrentModificationException e) {
827 <            fail("weakly consistent iterator; should not get CME");
827 >            unexpectedException();
828          }
829 <
640 <        assertEquals("queue should be empty again", 0, q.size());
829 >        assertEquals(0, q.size());
830      }
831  
832  
833 <    public void testToString(){
834 <        ArrayBlockingQueue q = fullQueue(N);
833 >    /**
834 >     * toString contains toStrings of elements
835 >     */
836 >    public void testToString() {
837 >        ArrayBlockingQueue q = populatedQueue(SIZE);
838          String s = q.toString();
839 <        for (int i = 0; i < N; ++i) {
839 >        for (int i = 0; i < SIZE; ++i) {
840              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
841          }
842 <    }        
842 >    }
843  
844  
845 +    /**
846 +     * offer transfers elements across Executor tasks
847 +     */
848      public void testOfferInExecutor() {
654
849          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
850 <
851 <        q.add(new Integer(1));
658 <        q.add(new Integer(2));
659 <
850 >        q.add(one);
851 >        q.add(two);
852          ExecutorService executor = Executors.newFixedThreadPool(2);
661
853          executor.execute(new Runnable() {
854              public void run() {
855 <                assertFalse("offer should be rejected", q.offer(new Integer(3)));
855 >                threadAssertFalse(q.offer(three));
856                  try {
857 <                    assertTrue("offer should be accepted", q.offer(new Integer(3), MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
858 <                    assertEquals(0, q.remainingCapacity());
857 >                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
858 >                    threadAssertEquals(0, q.remainingCapacity());
859                  }
860                  catch (InterruptedException e) {
861 <                    fail("should not be interrupted");
861 >                    threadUnexpectedException();
862                  }
863              }
864          });
# Line 675 | Line 866 | public class ArrayBlockingQueueTest exte
866          executor.execute(new Runnable() {
867              public void run() {
868                  try {
869 <                    Thread.sleep(MEDIUM_DELAY_MS);
870 <                    assertEquals("first item in queue should be 1", new Integer(1), q.take());
869 >                    Thread.sleep(SMALL_DELAY_MS);
870 >                    threadAssertEquals(one, q.take());
871                  }
872                  catch (InterruptedException e) {
873 <                    fail("should not be interrupted");
873 >                    threadUnexpectedException();
874                  }
875              }
876          });
877 <        
878 <        executor.shutdown();
877 >
878 >        joinPool(executor);
879  
880      }
881  
882 +    /**
883 +     * poll retrieves elements across Executor threads
884 +     */
885      public void testPollInExecutor() {
692
886          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
694
887          ExecutorService executor = Executors.newFixedThreadPool(2);
696
888          executor.execute(new Runnable() {
889              public void run() {
890 <                assertNull("poll should fail", q.poll());
890 >                threadAssertNull(q.poll());
891                  try {
892 <                    assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
893 <                    assertTrue(q.isEmpty());
892 >                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
893 >                    threadAssertTrue(q.isEmpty());
894                  }
895                  catch (InterruptedException e) {
896 <                    fail("should not be interrupted");
896 >                    threadUnexpectedException();
897                  }
898              }
899          });
# Line 710 | Line 901 | public class ArrayBlockingQueueTest exte
901          executor.execute(new Runnable() {
902              public void run() {
903                  try {
904 <                    Thread.sleep(MEDIUM_DELAY_MS);
905 <                    q.put(new Integer(1));
904 >                    Thread.sleep(SMALL_DELAY_MS);
905 >                    q.put(one);
906                  }
907                  catch (InterruptedException e) {
908 <                    fail("should not be interrupted");
908 >                    threadUnexpectedException();
909                  }
910              }
911          });
721        
722        executor.shutdown();
912  
913 +        joinPool(executor);
914      }
915  
916 +    /**
917 +     * A deserialized serialized queue has same elements in same order
918 +     */
919      public void testSerialization() {
920 <        ArrayBlockingQueue q = fullQueue(N);
920 >        ArrayBlockingQueue q = populatedQueue(SIZE);
921  
922          try {
923              ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
# Line 736 | Line 929 | public class ArrayBlockingQueueTest exte
929              ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
930              ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
931              assertEquals(q.size(), r.size());
932 <            while (!q.isEmpty())
932 >            while (!q.isEmpty())
933                  assertEquals(q.remove(), r.remove());
934          } catch(Exception e){
935 <            fail("unexpected exception");
935 >            unexpectedException();
936 >        }
937 >    }
938 >
939 >    /**
940 >     * drainTo(null) throws NPE
941 >     */
942 >    public void testDrainToNull() {
943 >        ArrayBlockingQueue q = populatedQueue(SIZE);
944 >        try {
945 >            q.drainTo(null);
946 >            shouldThrow();
947 >        } catch(NullPointerException success) {
948 >        }
949 >    }
950 >
951 >    /**
952 >     * drainTo(this) throws IAE
953 >     */
954 >    public void testDrainToSelf() {
955 >        ArrayBlockingQueue q = populatedQueue(SIZE);
956 >        try {
957 >            q.drainTo(q);
958 >            shouldThrow();
959 >        } catch(IllegalArgumentException success) {
960 >        }
961 >    }
962 >
963 >    /**
964 >     * drainTo(c) empties queue into another collection c
965 >     */
966 >    public void testDrainTo() {
967 >        ArrayBlockingQueue q = populatedQueue(SIZE);
968 >        ArrayList l = new ArrayList();
969 >        q.drainTo(l);
970 >        assertEquals(q.size(), 0);
971 >        assertEquals(l.size(), SIZE);
972 >        for (int i = 0; i < SIZE; ++i)
973 >            assertEquals(l.get(i), new Integer(i));
974 >        q.add(zero);
975 >        q.add(one);
976 >        assertFalse(q.isEmpty());
977 >        assertTrue(q.contains(zero));
978 >        assertTrue(q.contains(one));
979 >        l.clear();
980 >        q.drainTo(l);
981 >        assertEquals(q.size(), 0);
982 >        assertEquals(l.size(), 2);
983 >        for (int i = 0; i < 2; ++i)
984 >            assertEquals(l.get(i), new Integer(i));
985 >    }
986 >
987 >    /**
988 >     * drainTo empties full queue, unblocking a waiting put.
989 >     */
990 >    public void testDrainToWithActivePut() {
991 >        final ArrayBlockingQueue q = populatedQueue(SIZE);
992 >        Thread t = new Thread(new Runnable() {
993 >                public void run() {
994 >                    try {
995 >                        q.put(new Integer(SIZE+1));
996 >                    } catch (InterruptedException ie){
997 >                        threadUnexpectedException();
998 >                    }
999 >                }
1000 >            });
1001 >        try {
1002 >            t.start();
1003 >            ArrayList l = new ArrayList();
1004 >            q.drainTo(l);
1005 >            assertTrue(l.size() >= SIZE);
1006 >            for (int i = 0; i < SIZE; ++i)
1007 >                assertEquals(l.get(i), new Integer(i));
1008 >            t.join();
1009 >            assertTrue(q.size() + l.size() >= SIZE);
1010 >        } catch(Exception e){
1011 >            unexpectedException();
1012 >        }
1013 >    }
1014 >
1015 >    /**
1016 >     * drainTo(null, n) throws NPE
1017 >     */
1018 >    public void testDrainToNullN() {
1019 >        ArrayBlockingQueue q = populatedQueue(SIZE);
1020 >        try {
1021 >            q.drainTo(null, 0);
1022 >            shouldThrow();
1023 >        } catch(NullPointerException success) {
1024 >        }
1025 >    }
1026 >
1027 >    /**
1028 >     * drainTo(this, n) throws IAE
1029 >     */
1030 >    public void testDrainToSelfN() {
1031 >        ArrayBlockingQueue q = populatedQueue(SIZE);
1032 >        try {
1033 >            q.drainTo(q, 0);
1034 >            shouldThrow();
1035 >        } catch(IllegalArgumentException success) {
1036 >        }
1037 >    }
1038 >
1039 >    /**
1040 >     * drainTo(c, n) empties first max {n, size} elements of queue into c
1041 >     */
1042 >    public void testDrainToN() {
1043 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
1044 >        for (int i = 0; i < SIZE + 2; ++i) {
1045 >            for(int j = 0; j < SIZE; j++)
1046 >                assertTrue(q.offer(new Integer(j)));
1047 >            ArrayList l = new ArrayList();
1048 >            q.drainTo(l, i);
1049 >            int k = (i < SIZE)? i : SIZE;
1050 >            assertEquals(l.size(), k);
1051 >            assertEquals(q.size(), SIZE-k);
1052 >            for (int j = 0; j < k; ++j)
1053 >                assertEquals(l.get(j), new Integer(j));
1054 >            while (q.poll() != null) ;
1055          }
1056      }
1057  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines