ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.100
Committed: Wed Jan 27 02:55:18 2021 UTC (3 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.99: +1 -0 lines
Log Message:
Suppress all new errorprone "errors"

File Contents

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