[cvs] / jsr166 / src / test / tck / ArrayBlockingQueueTest.java Repository:
ViewVC logotype

Annotation of /jsr166/src/test/tck/ArrayBlockingQueueTest.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.48 - (view) (download)

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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8