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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8