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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines