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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines