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.42 by jsr166, Sun Nov 28 08:43:53 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 TestCase {
16 > public class ArrayBlockingQueueTest extends JSR166TestCase {
17  
18 <    private static int N = 10;
19 <    private static long SHORT_DELAY_MS = 100;
20 <    private static long MEDIUM_DELAY_MS = 1000;
21 <    private static long LONG_DELAY_MS = 10000;
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      /**
41       * Create a queue of given size containing consecutive
42       * Integers 0 ... n.
43       */
44 <    private ArrayBlockingQueue fullQueue(int n) {
45 <        ArrayBlockingQueue q = new ArrayBlockingQueue(n);
44 >    private ArrayBlockingQueue<Integer> populatedQueue(int n) {
45 >        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(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(){
56 <        assertEquals(N, new ArrayBlockingQueue(N).remainingCapacity());
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 <        }
52 <        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 <        }
61 <        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[N];
88 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N, false, Arrays.asList(ints));
89 <            fail("Cannot make with null elements");
90 <        }
70 <        catch (NullPointerException success) {}
87 >            Integer[] ints = new Integer[SIZE];
88 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
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[N];
99 <            for (int i = 0; i < N-1; ++i)
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(N, false, Arrays.asList(ints));
102 <            fail("Cannot make with null elements");
103 <        }
81 <        catch (NullPointerException success) {}
101 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
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[N];
112 <            for (int i = 0; i < N; ++i)
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 <        }
92 <        catch (IllegalArgumentException success) {}
115 >            shouldThrow();
116 >        } catch (IllegalArgumentException success) {}
117      }
118  
119 <    public void testConstructor7(){
120 <        try {
121 <            Integer[] ints = new Integer[N];
122 <            for (int i = 0; i < N; ++i)
123 <                ints[i] = new Integer(i);
124 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N, true, Arrays.asList(ints));
125 <            for (int i = 0; i < N; ++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());
138 <        q.add(new Integer(1));
137 >        assertEquals(2, q.remainingCapacity());
138 >        q.add(one);
139          assertFalse(q.isEmpty());
140 <        q.add(new Integer(2));
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(new Integer(3)));
142 >        assertEquals(0, q.remainingCapacity());
143 >        assertFalse(q.offer(three));
144      }
145  
146 <    public void testRemainingCapacity(){
147 <        ArrayBlockingQueue q = fullQueue(N);
148 <        for (int i = 0; i < N; ++i) {
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());
153 <            assertEquals(N-i, q.size());
153 >            assertEquals(SIZE-i, q.size());
154              q.remove();
155          }
156 <        for (int i = 0; i < N; ++i) {
157 <            assertEquals(N-i, q.remainingCapacity());
156 >        for (int i = 0; i < SIZE; ++i) {
157 >            assertEquals(SIZE-i, q.remainingCapacity());
158              assertEquals(i, q.size());
159              q.add(new Integer(i));
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(new Integer(0)));
191 <        assertFalse(q.offer(new Integer(1)));
190 >        assertTrue(q.offer(zero));
191 >        assertFalse(q.offer(one));
192      }
193  
194 <    public void testAdd(){
195 <        try {
196 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
197 <            for (int i = 0; i < N; ++i) {
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(N));
205 <        } catch (IllegalStateException success){
206 <        }  
204 >            q.add(new Integer(SIZE));
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 <        }
218 <        catch (NullPointerException success) {}
216 >            shouldThrow();
217 >        } catch (NullPointerException success) {}
218 >    }
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 <    public void testAddAll2(){
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(N);
238 <            Integer[] ints = new Integer[N];
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 <        }
174 <        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(N);
251 <            Integer[] ints = new Integer[N];
252 <            for (int i = 0; i < N-1; ++i)
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 <        }
185 <        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[N];
266 <            for (int i = 0; i < N; ++i)
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 <        }
196 <        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[N];
276 <            for (int i = 0; i < N; ++i)
277 <                ints[i] = new Integer(i);
278 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
279 <            assertFalse(q.addAll(Arrays.asList(empty)));
280 <            assertTrue(q.addAll(Arrays.asList(ints)));
281 <            for (int i = 0; i < N; ++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 {
290 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
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 <        }
219 <        catch (NullPointerException success){
220 <        }  
221 <        catch (InterruptedException ie) {
222 <            fail("Unexpected exception");
223 <        }
295 >            shouldThrow();
296 >        } catch (NullPointerException success) {}
297       }
298  
299 <     public void testPut() {
300 <         try {
301 <             ArrayBlockingQueue q = new ArrayBlockingQueue(N);
302 <             for (int i = 0; i < N; ++i) {
303 <                 Integer I = new Integer(i);
304 <                 q.put(I);
305 <                 assertTrue(q.contains(I));
306 <             }
307 <             assertEquals(0, q.remainingCapacity());
235 <         }
236 <        catch (InterruptedException ie) {
237 <            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(N);
318 <                        for (int i = 0; i < N; ++i) {
319 <                            q.put(new Integer(i));
320 <                            ++added;
321 <                        }
322 <                        q.put(new Integer(N));
323 <                        fail("put should block");
324 <                    } catch (InterruptedException ie){
325 <                        assertEquals(added, N);
326 <                    }  
327 <                }});
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 <        try {
331 <           Thread.sleep(SHORT_DELAY_MS);
332 <           t.interrupt();
333 <           t.join();
334 <        }
263 <        catch (InterruptedException ie) {
264 <            fail("Unexpected exception");
265 <        }
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 <                        fail("Should block");
352 <                    } catch (InterruptedException e){
353 <                        assertTrue(added >= 2);
354 <                    }
355 <                }
356 <            });
357 <        try {
358 <            t.start();
359 <            Thread.sleep(SHORT_DELAY_MS);
360 <            q.take();
292 <            t.interrupt();
293 <            t.join();
294 <        } catch (Exception e){
295 <            fail("Unexpected exception");
296 <        }
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 <                        assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
374 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
375 <                        fail("Should block");
376 <                    } catch (InterruptedException success){}
377 <                }
311 <            });
312 <        
313 <        try {
314 <            t.start();
315 <            Thread.sleep(SHORT_DELAY_MS);
316 <            t.interrupt();
317 <            t.join();
318 <        } catch (Exception e){
319 <            fail("Unexpected exception");
320 <        }
321 <    }
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 <    public void testTake(){
380 <        try {
381 <            ArrayBlockingQueue q = fullQueue(N);
382 <            for (int i = 0; i < N; ++i) {
327 <                assertEquals(i, ((Integer)q.take()).intValue());
328 <            }
329 <        } catch (InterruptedException e){
330 <            fail("Unexpected exception");
331 <        }  
379 >        t.start();
380 >        Thread.sleep(SHORT_DELAY_MS);
381 >        t.interrupt();
382 >        t.join();
383      }
384  
385 <    public void testTakeFromEmpty() {
386 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
387 <        Thread t = new Thread(new Runnable() {
388 <                public void run(){
389 <                    try {
390 <                        q.take();
391 <                        fail("Should block");
341 <                    } catch (InterruptedException success){ }                
342 <                }
343 <            });
344 <        try {
345 <            t.start();
346 <            Thread.sleep(SHORT_DELAY_MS);
347 <            t.interrupt();
348 <            t.join();
349 <        } catch (Exception e){
350 <            fail("Unexpected exception");
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 testBlockingTake(){
396 <        Thread t = new Thread(new Runnable() {
397 <                public void run() {
398 <                    try {
399 <                        ArrayBlockingQueue q = fullQueue(N);
400 <                        for (int i = 0; i < N; ++i) {
401 <                            assertEquals(i, ((Integer)q.take()).intValue());
402 <                        }
403 <                        q.take();
404 <                        fail("take should block");
405 <                    } catch (InterruptedException success){
406 <                    }  
407 <                }});
395 >    /**
396 >     * Take removes existing elements until empty, then blocks interruptibly
397 >     */
398 >    public void testBlockingTake() throws InterruptedException {
399 >        final ArrayBlockingQueue q = populatedQueue(SIZE);
400 >        Thread t = new Thread(new CheckedRunnable() {
401 >            public void realRun() throws InterruptedException {
402 >                for (int i = 0; i < SIZE; ++i) {
403 >                    assertEquals(i, q.take());
404 >                }
405 >                try {
406 >                    q.take();
407 >                    shouldThrow();
408 >                } catch (InterruptedException success) {}
409 >            }});
410 >
411          t.start();
412 <        try {
413 <           Thread.sleep(SHORT_DELAY_MS);
414 <           t.interrupt();
371 <           t.join();
372 <        }
373 <        catch (InterruptedException ie) {
374 <            fail("Unexpected exception");
375 <        }
412 >        Thread.sleep(SHORT_DELAY_MS);
413 >        t.interrupt();
414 >        t.join();
415      }
416  
417  
418 <    public void testPoll(){
419 <        ArrayBlockingQueue q = fullQueue(N);
420 <        for (int i = 0; i < N; ++i) {
421 <            assertEquals(i, ((Integer)q.poll()).intValue());
418 >    /**
419 >     * poll succeeds unless empty
420 >     */
421 >    public void testPoll() {
422 >        ArrayBlockingQueue q = populatedQueue(SIZE);
423 >        for (int i = 0; i < SIZE; ++i) {
424 >            assertEquals(i, q.poll());
425          }
426 <        assertNull(q.poll());
426 >        assertNull(q.poll());
427      }
428  
429 <    public void testTimedPoll0() {
430 <        try {
431 <            ArrayBlockingQueue q = fullQueue(N);
432 <            for (int i = 0; i < N; ++i) {
433 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
434 <            }
435 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
436 <        } catch (InterruptedException e){
437 <            fail("Unexpected exception");
396 <        }  
429 >    /**
430 >     * timed poll with zero timeout succeeds when non-empty, else times out
431 >     */
432 >    public void testTimedPoll0() throws InterruptedException {
433 >        ArrayBlockingQueue q = populatedQueue(SIZE);
434 >        for (int i = 0; i < SIZE; ++i) {
435 >            assertEquals(i, q.poll(0, MILLISECONDS));
436 >        }
437 >        assertNull(q.poll(0, MILLISECONDS));
438      }
439  
440 <    public void testTimedPoll() {
441 <        try {
442 <            ArrayBlockingQueue q = fullQueue(N);
443 <            for (int i = 0; i < N; ++i) {
444 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
445 <            }
446 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
406 <        } catch (InterruptedException e){
407 <            fail("Unexpected exception");
408 <        }  
409 <    }
410 <
411 <    public void testInterruptedTimedPoll(){
412 <        Thread t = new Thread(new Runnable() {
413 <                public void run() {
414 <                    try {
415 <                        ArrayBlockingQueue q = fullQueue(N);
416 <                        for (int i = 0; i < N; ++i) {
417 <                            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
418 <                        }
419 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
420 <                    } catch (InterruptedException success){
421 <                    }  
422 <                }});
423 <        t.start();
424 <        try {
425 <           Thread.sleep(SHORT_DELAY_MS);
426 <           t.interrupt();
427 <           t.join();
428 <        }
429 <        catch (InterruptedException ie) {
430 <            fail("Unexpected exception");
440 >    /**
441 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
442 >     */
443 >    public void testTimedPoll() throws InterruptedException {
444 >        ArrayBlockingQueue q = populatedQueue(SIZE);
445 >        for (int i = 0; i < SIZE; ++i) {
446 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
447          }
448 +        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
449      }
450  
451 <    public void testTimedPollWithOffer(){
452 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
453 <        Thread t = new Thread(new Runnable() {
454 <                public void run(){
455 <                    try {
456 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
457 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
458 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
459 <                        fail("Should block");
460 <                    } catch (InterruptedException success) { }                
451 >    /**
452 >     * Interrupted timed poll throws InterruptedException instead of
453 >     * returning timeout status
454 >     */
455 >    public void testInterruptedTimedPoll() throws InterruptedException {
456 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
457 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
458 >        Thread t = newStartedThread(new CheckedRunnable() {
459 >            public void realRun() throws InterruptedException {
460 >                for (int i = 0; i < SIZE; ++i) {
461 >                    long t0 = System.nanoTime();
462 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
463 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
464                  }
465 <            });
466 <        try {
467 <            t.start();
468 <            Thread.sleep(SHORT_DELAY_MS * 2);
469 <            assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
470 <            t.interrupt();
471 <            t.join();
472 <        } catch (Exception e){
473 <            fail("Unexpected exception");
474 <        }
475 <    }  
476 <
477 <
478 <    public void testPeek(){
479 <        ArrayBlockingQueue q = fullQueue(N);
480 <        for (int i = 0; i < N; ++i) {
481 <            assertEquals(i, ((Integer)q.peek()).intValue());
482 <            q.poll();
465 >                long t0 = System.nanoTime();
466 >                aboutToWait.countDown();
467 >                try {
468 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
469 >                    shouldThrow();
470 >                } catch (InterruptedException success) {
471 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
472 >                }
473 >            }});
474 >
475 >        aboutToWait.await();
476 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
477 >        t.interrupt();
478 >        awaitTermination(t, MEDIUM_DELAY_MS);
479 >        checkEmpty(q);
480 >    }
481 >
482 >    /**
483 >     * peek returns next element, or null if empty
484 >     */
485 >    public void testPeek() {
486 >        ArrayBlockingQueue q = populatedQueue(SIZE);
487 >        for (int i = 0; i < SIZE; ++i) {
488 >            assertEquals(i, q.peek());
489 >            assertEquals(i, q.poll());
490              assertTrue(q.peek() == null ||
491 <                       i != ((Integer)q.peek()).intValue());
491 >                       !q.peek().equals(i));
492          }
493 <        assertNull(q.peek());
493 >        assertNull(q.peek());
494      }
495  
496 <    public void testElement(){
497 <        ArrayBlockingQueue q = fullQueue(N);
498 <        for (int i = 0; i < N; ++i) {
499 <            assertEquals(i, ((Integer)q.element()).intValue());
500 <            q.poll();
496 >    /**
497 >     * element returns next element, or throws NSEE if empty
498 >     */
499 >    public void testElement() {
500 >        ArrayBlockingQueue q = populatedQueue(SIZE);
501 >        for (int i = 0; i < SIZE; ++i) {
502 >            assertEquals(i, q.element());
503 >            assertEquals(i, q.poll());
504          }
505          try {
506              q.element();
507 <            fail("no such element");
508 <        }
479 <        catch (NoSuchElementException success) {}
507 >            shouldThrow();
508 >        } catch (NoSuchElementException success) {}
509      }
510  
511 <    public void testRemove(){
512 <        ArrayBlockingQueue q = fullQueue(N);
513 <        for (int i = 0; i < N; ++i) {
514 <            assertEquals(i, ((Integer)q.remove()).intValue());
511 >    /**
512 >     * remove removes next element, or throws NSEE if empty
513 >     */
514 >    public void testRemove() {
515 >        ArrayBlockingQueue q = populatedQueue(SIZE);
516 >        for (int i = 0; i < SIZE; ++i) {
517 >            assertEquals(i, q.remove());
518          }
519          try {
520              q.remove();
521 <            fail("remove should throw");
522 <        } catch (NoSuchElementException success){
491 <        }  
521 >            shouldThrow();
522 >        } catch (NoSuchElementException success) {}
523      }
524  
525 <    public void testRemoveElement(){
526 <        ArrayBlockingQueue q = fullQueue(N);
527 <        for (int i = 1; i < N; i+=2) {
525 >    /**
526 >     * remove(x) removes x and returns true if present
527 >     */
528 >    public void testRemoveElement() {
529 >        ArrayBlockingQueue q = populatedQueue(SIZE);
530 >        for (int i = 1; i < SIZE; i+=2) {
531              assertTrue(q.remove(new Integer(i)));
532          }
533 <        for (int i = 0; i < N; i+=2) {
533 >        for (int i = 0; i < SIZE; i+=2) {
534              assertTrue(q.remove(new Integer(i)));
535              assertFalse(q.remove(new Integer(i+1)));
536          }
537          assertTrue(q.isEmpty());
538      }
539 <        
540 <    public void testContains(){
541 <        ArrayBlockingQueue q = fullQueue(N);
542 <        for (int i = 0; i < N; ++i) {
539 >
540 >    /**
541 >     * contains(x) reports true when elements added but not yet removed
542 >     */
543 >    public void testContains() {
544 >        ArrayBlockingQueue q = populatedQueue(SIZE);
545 >        for (int i = 0; i < SIZE; ++i) {
546              assertTrue(q.contains(new Integer(i)));
547 <            q.poll();
547 >            assertEquals(i, q.poll());
548              assertFalse(q.contains(new Integer(i)));
549          }
550      }
551  
552 <    public void testClear(){
553 <        ArrayBlockingQueue q = fullQueue(N);
552 >    /**
553 >     * clear removes all elements
554 >     */
555 >    public void testClear() {
556 >        ArrayBlockingQueue q = populatedQueue(SIZE);
557          q.clear();
558          assertTrue(q.isEmpty());
559          assertEquals(0, q.size());
560 <        assertEquals(N, q.remainingCapacity());
561 <        q.add(new Integer(1));
560 >        assertEquals(SIZE, q.remainingCapacity());
561 >        q.add(one);
562          assertFalse(q.isEmpty());
563 +        assertTrue(q.contains(one));
564          q.clear();
565          assertTrue(q.isEmpty());
566      }
567  
568 <    public void testContainsAll(){
569 <        ArrayBlockingQueue q = fullQueue(N);
570 <        ArrayBlockingQueue p = new ArrayBlockingQueue(N);
571 <        for (int i = 0; i < N; ++i) {
568 >    /**
569 >     * containsAll(c) is true when c contains a subset of elements
570 >     */
571 >    public void testContainsAll() {
572 >        ArrayBlockingQueue q = populatedQueue(SIZE);
573 >        ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE);
574 >        for (int i = 0; i < SIZE; ++i) {
575              assertTrue(q.containsAll(p));
576              assertFalse(p.containsAll(q));
577              p.add(new Integer(i));
# Line 535 | Line 579 | public class ArrayBlockingQueueTest exte
579          assertTrue(p.containsAll(q));
580      }
581  
582 <    public void testRetainAll(){
583 <        ArrayBlockingQueue q = fullQueue(N);
584 <        ArrayBlockingQueue p = fullQueue(N);
585 <        for (int i = 0; i < N; ++i) {
582 >    /**
583 >     * retainAll(c) retains only those elements of c and reports true if changed
584 >     */
585 >    public void testRetainAll() {
586 >        ArrayBlockingQueue q = populatedQueue(SIZE);
587 >        ArrayBlockingQueue p = populatedQueue(SIZE);
588 >        for (int i = 0; i < SIZE; ++i) {
589              boolean changed = q.retainAll(p);
590              if (i == 0)
591                  assertFalse(changed);
# Line 546 | Line 593 | public class ArrayBlockingQueueTest exte
593                  assertTrue(changed);
594  
595              assertTrue(q.containsAll(p));
596 <            assertEquals(N-i, q.size());
596 >            assertEquals(SIZE-i, q.size());
597              p.remove();
598          }
599      }
600  
601 <    public void testRemoveAll(){
602 <        for (int i = 1; i < N; ++i) {
603 <            ArrayBlockingQueue q = fullQueue(N);
604 <            ArrayBlockingQueue p = fullQueue(i);
601 >    /**
602 >     * removeAll(c) removes only those elements of c and reports true if changed
603 >     */
604 >    public void testRemoveAll() {
605 >        for (int i = 1; i < SIZE; ++i) {
606 >            ArrayBlockingQueue q = populatedQueue(SIZE);
607 >            ArrayBlockingQueue p = populatedQueue(i);
608              assertTrue(q.removeAll(p));
609 <            assertEquals(N-i, q.size());
609 >            assertEquals(SIZE-i, q.size());
610              for (int j = 0; j < i; ++j) {
611                  Integer I = (Integer)(p.remove());
612                  assertFalse(q.contains(I));
# Line 564 | Line 614 | public class ArrayBlockingQueueTest exte
614          }
615      }
616  
617 +    /**
618 +     * toArray contains all elements in FIFO order
619 +     */
620 +    public void testToArray() {
621 +        ArrayBlockingQueue q = populatedQueue(SIZE);
622 +        Object[] o = q.toArray();
623 +        for (int i = 0; i < o.length; i++)
624 +            assertSame(o[i], q.poll());
625 +    }
626  
627 <    public void testToArray(){
628 <        ArrayBlockingQueue q = fullQueue(N);
629 <        Object[] o = q.toArray();
630 <        try {
631 <        for(int i = 0; i < o.length; i++)
632 <            assertEquals(o[i], q.take());
633 <        } catch (InterruptedException e){
634 <            fail("Unexpected exception");
635 <        }    
636 <    }
578 <
579 <    public void testToArray2(){
580 <        ArrayBlockingQueue q = fullQueue(N);
581 <        Integer[] ints = new Integer[N];
582 <        ints = (Integer[])q.toArray(ints);
583 <        try {
584 <            for(int i = 0; i < ints.length; i++)
585 <                assertEquals(ints[i], q.take());
586 <        } catch (InterruptedException e){
587 <            fail("Unexpected exception");
588 <        }    
589 <    }
590 <    
591 <    public void testIterator(){
592 <        ArrayBlockingQueue q = fullQueue(N);
593 <        Iterator it = q.iterator();
594 <        try {
595 <            while(it.hasNext()){
596 <                assertEquals(it.next(), q.take());
597 <            }
598 <        } catch (InterruptedException e){
599 <            fail("Unexpected exception");
600 <        }    
627 >    /**
628 >     * toArray(a) contains all elements in FIFO order
629 >     */
630 >    public void testToArray2() {
631 >        ArrayBlockingQueue<Integer> q = populatedQueue(SIZE);
632 >        Integer[] ints = new Integer[SIZE];
633 >        Integer[] array = q.toArray(ints);
634 >        assertSame(ints, array);
635 >        for (int i = 0; i < ints.length; i++)
636 >            assertSame(ints[i], q.poll());
637      }
638  
639 <    public void testIteratorOrdering() {
639 >    /**
640 >     * toArray(null) throws NullPointerException
641 >     */
642 >    public void testToArray_NullArg() {
643 >        ArrayBlockingQueue q = populatedQueue(SIZE);
644 >        try {
645 >            q.toArray(null);
646 >            shouldThrow();
647 >        } catch (NullPointerException success) {}
648 >    }
649 >
650 >    /**
651 >     * toArray(incompatible array type) throws ArrayStoreException
652 >     */
653 >    public void testToArray1_BadArg() {
654 >        ArrayBlockingQueue q = populatedQueue(SIZE);
655 >        try {
656 >            q.toArray(new String[10]);
657 >            shouldThrow();
658 >        } catch (ArrayStoreException success) {}
659 >    }
660 >
661 >
662 >    /**
663 >     * iterator iterates through all elements
664 >     */
665 >    public void testIterator() throws InterruptedException {
666 >        ArrayBlockingQueue q = populatedQueue(SIZE);
667 >        Iterator it = q.iterator();
668 >        while (it.hasNext()) {
669 >            assertEquals(it.next(), q.take());
670 >        }
671 >    }
672  
673 +    /**
674 +     * iterator.remove removes current element
675 +     */
676 +    public void testIteratorRemove() {
677          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
678 +        q.add(two);
679 +        q.add(one);
680 +        q.add(three);
681 +
682 +        Iterator it = q.iterator();
683 +        it.next();
684 +        it.remove();
685 +
686 +        it = q.iterator();
687 +        assertSame(it.next(), one);
688 +        assertSame(it.next(), three);
689 +        assertFalse(it.hasNext());
690 +    }
691  
692 <        q.add(new Integer(1));
693 <        q.add(new Integer(2));
694 <        q.add(new Integer(3));
692 >    /**
693 >     * iterator ordering is FIFO
694 >     */
695 >    public void testIteratorOrdering() {
696 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
697 >        q.add(one);
698 >        q.add(two);
699 >        q.add(three);
700  
701          assertEquals("queue should be full", 0, q.remainingCapacity());
702  
703          int k = 0;
704          for (Iterator it = q.iterator(); it.hasNext();) {
705 <            int i = ((Integer)(it.next())).intValue();
616 <            assertEquals("items should come out in order", ++k, i);
705 >            assertEquals(++k, it.next());
706          }
707 <
619 <        assertEquals("should go through 3 elements", 3, k);
707 >        assertEquals(3, k);
708      }
709  
710 <    public void testWeaklyConsistentIteration () {
711 <
710 >    /**
711 >     * Modifications do not cause iterators to fail
712 >     */
713 >    public void testWeaklyConsistentIteration() {
714          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
715 <
716 <        q.add(new Integer(1));
717 <        q.add(new Integer(2));
718 <        q.add(new Integer(3));
719 <
720 <        try {
631 <            for (Iterator it = q.iterator(); it.hasNext();) {
632 <                q.remove();
633 <                it.next();
634 <            }
635 <        }
636 <        catch (ConcurrentModificationException e) {
637 <            fail("weakly consistent iterator; should not get CME");
715 >        q.add(one);
716 >        q.add(two);
717 >        q.add(three);
718 >        for (Iterator it = q.iterator(); it.hasNext();) {
719 >            q.remove();
720 >            it.next();
721          }
722 <
640 <        assertEquals("queue should be empty again", 0, q.size());
722 >        assertEquals(0, q.size());
723      }
724  
725  
726 <    public void testToString(){
727 <        ArrayBlockingQueue q = fullQueue(N);
726 >    /**
727 >     * toString contains toStrings of elements
728 >     */
729 >    public void testToString() {
730 >        ArrayBlockingQueue q = populatedQueue(SIZE);
731          String s = q.toString();
732 <        for (int i = 0; i < N; ++i) {
732 >        for (int i = 0; i < SIZE; ++i) {
733              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
734          }
735 <    }        
735 >    }
736  
737  
738 +    /**
739 +     * offer transfers elements across Executor tasks
740 +     */
741      public void testOfferInExecutor() {
654
742          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
743 +        q.add(one);
744 +        q.add(two);
745 +        ExecutorService executor = Executors.newFixedThreadPool(2);
746 +        executor.execute(new CheckedRunnable() {
747 +            public void realRun() throws InterruptedException {
748 +                assertFalse(q.offer(three));
749 +                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
750 +                assertEquals(0, q.remainingCapacity());
751 +            }});
752 +
753 +        executor.execute(new CheckedRunnable() {
754 +            public void realRun() throws InterruptedException {
755 +                Thread.sleep(SMALL_DELAY_MS);
756 +                assertSame(one, q.take());
757 +            }});
758  
759 <        q.add(new Integer(1));
760 <        q.add(new Integer(2));
759 >        joinPool(executor);
760 >    }
761  
762 +    /**
763 +     * poll retrieves elements across Executor threads
764 +     */
765 +    public void testPollInExecutor() {
766 +        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
767          ExecutorService executor = Executors.newFixedThreadPool(2);
768 +        executor.execute(new CheckedRunnable() {
769 +            public void realRun() throws InterruptedException {
770 +                assertNull(q.poll());
771 +                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
772 +                assertTrue(q.isEmpty());
773 +            }});
774 +
775 +        executor.execute(new CheckedRunnable() {
776 +            public void realRun() throws InterruptedException {
777 +                Thread.sleep(SMALL_DELAY_MS);
778 +                q.put(one);
779 +            }});
780  
781 <        executor.execute(new Runnable() {
782 <            public void run() {
664 <                assertFalse("offer should be rejected", q.offer(new Integer(3)));
665 <                try {
666 <                    assertTrue("offer should be accepted", q.offer(new Integer(3), MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
667 <                    assertEquals(0, q.remainingCapacity());
668 <                }
669 <                catch (InterruptedException e) {
670 <                    fail("should not be interrupted");
671 <                }
672 <            }
673 <        });
781 >        joinPool(executor);
782 >    }
783  
784 <        executor.execute(new Runnable() {
785 <            public void run() {
786 <                try {
787 <                    Thread.sleep(MEDIUM_DELAY_MS);
788 <                    assertEquals("first item in queue should be 1", new Integer(1), q.take());
680 <                }
681 <                catch (InterruptedException e) {
682 <                    fail("should not be interrupted");
683 <                }
684 <            }
685 <        });
686 <        
687 <        executor.shutdown();
784 >    /**
785 >     * A deserialized serialized queue has same elements in same order
786 >     */
787 >    public void testSerialization() throws Exception {
788 >        ArrayBlockingQueue q = populatedQueue(SIZE);
789  
790 +        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
791 +        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
792 +        out.writeObject(q);
793 +        out.close();
794 +
795 +        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
796 +        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
797 +        ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
798 +        assertEquals(q.size(), r.size());
799 +        while (!q.isEmpty())
800 +            assertEquals(q.remove(), r.remove());
801      }
802  
803 <    public void testPollInExecutor() {
804 <
805 <        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
803 >    /**
804 >     * drainTo(null) throws NPE
805 >     */
806 >    public void testDrainToNull() {
807 >        ArrayBlockingQueue q = populatedQueue(SIZE);
808 >        try {
809 >            q.drainTo(null);
810 >            shouldThrow();
811 >        } catch (NullPointerException success) {}
812 >    }
813  
814 <        ExecutorService executor = Executors.newFixedThreadPool(2);
814 >    /**
815 >     * drainTo(this) throws IAE
816 >     */
817 >    public void testDrainToSelf() {
818 >        ArrayBlockingQueue q = populatedQueue(SIZE);
819 >        try {
820 >            q.drainTo(q);
821 >            shouldThrow();
822 >        } catch (IllegalArgumentException success) {}
823 >    }
824  
825 <        executor.execute(new Runnable() {
826 <            public void run() {
827 <                assertNull("poll should fail", q.poll());
828 <                try {
829 <                    assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
830 <                    assertTrue(q.isEmpty());
831 <                }
832 <                catch (InterruptedException e) {
833 <                    fail("should not be interrupted");
834 <                }
835 <            }
836 <        });
825 >    /**
826 >     * drainTo(c) empties queue into another collection c
827 >     */
828 >    public void testDrainTo() {
829 >        ArrayBlockingQueue q = populatedQueue(SIZE);
830 >        ArrayList l = new ArrayList();
831 >        q.drainTo(l);
832 >        assertEquals(q.size(), 0);
833 >        assertEquals(l.size(), SIZE);
834 >        for (int i = 0; i < SIZE; ++i)
835 >            assertEquals(l.get(i), new Integer(i));
836 >        q.add(zero);
837 >        q.add(one);
838 >        assertFalse(q.isEmpty());
839 >        assertTrue(q.contains(zero));
840 >        assertTrue(q.contains(one));
841 >        l.clear();
842 >        q.drainTo(l);
843 >        assertEquals(q.size(), 0);
844 >        assertEquals(l.size(), 2);
845 >        for (int i = 0; i < 2; ++i)
846 >            assertEquals(l.get(i), new Integer(i));
847 >    }
848  
849 <        executor.execute(new Runnable() {
850 <            public void run() {
851 <                try {
852 <                    Thread.sleep(MEDIUM_DELAY_MS);
853 <                    q.put(new Integer(1));
854 <                }
855 <                catch (InterruptedException e) {
856 <                    fail("should not be interrupted");
857 <                }
719 <            }
720 <        });
721 <        
722 <        executor.shutdown();
849 >    /**
850 >     * drainTo empties full queue, unblocking a waiting put.
851 >     */
852 >    public void testDrainToWithActivePut() throws InterruptedException {
853 >        final ArrayBlockingQueue q = populatedQueue(SIZE);
854 >        Thread t = new Thread(new CheckedRunnable() {
855 >            public void realRun() throws InterruptedException {
856 >                q.put(new Integer(SIZE+1));
857 >            }});
858  
859 +        t.start();
860 +        ArrayList l = new ArrayList();
861 +        q.drainTo(l);
862 +        assertTrue(l.size() >= SIZE);
863 +        for (int i = 0; i < SIZE; ++i)
864 +            assertEquals(l.get(i), new Integer(i));
865 +        t.join();
866 +        assertTrue(q.size() + l.size() >= SIZE);
867      }
868  
869 <    public void testSerialization() {
870 <        ArrayBlockingQueue q = fullQueue(N);
869 >    /**
870 >     * drainTo(null, n) throws NPE
871 >     */
872 >    public void testDrainToNullN() {
873 >        ArrayBlockingQueue q = populatedQueue(SIZE);
874 >        try {
875 >            q.drainTo(null, 0);
876 >            shouldThrow();
877 >        } catch (NullPointerException success) {}
878 >    }
879  
880 +    /**
881 +     * drainTo(this, n) throws IAE
882 +     */
883 +    public void testDrainToSelfN() {
884 +        ArrayBlockingQueue q = populatedQueue(SIZE);
885          try {
886 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
887 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
888 <            out.writeObject(q);
889 <            out.close();
886 >            q.drainTo(q, 0);
887 >            shouldThrow();
888 >        } catch (IllegalArgumentException success) {}
889 >    }
890  
891 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
892 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
893 <            ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
894 <            assertEquals(q.size(), r.size());
895 <            while (!q.isEmpty())
896 <                assertEquals(q.remove(), r.remove());
897 <        } catch(Exception e){
898 <            fail("unexpected exception");
891 >    /**
892 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
893 >     */
894 >    public void testDrainToN() {
895 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
896 >        for (int i = 0; i < SIZE + 2; ++i) {
897 >            for (int j = 0; j < SIZE; j++)
898 >                assertTrue(q.offer(new Integer(j)));
899 >            ArrayList l = new ArrayList();
900 >            q.drainTo(l, i);
901 >            int k = (i < SIZE) ? i : SIZE;
902 >            assertEquals(l.size(), k);
903 >            assertEquals(q.size(), SIZE-k);
904 >            for (int j = 0; j < k; ++j)
905 >                assertEquals(l.get(j), new Integer(j));
906 >            while (q.poll() != null) ;
907          }
908      }
909  
746
910   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines