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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines