[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.46 - (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 : 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 : jsr166 1.41 private ArrayBlockingQueue<Integer> populatedQueue(int n) {
45 :     ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
46 : dl 1.1 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 : jsr166 1.44 }
298 : dl 1.1
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 : dl 1.45 delay(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 : dl 1.45 delay(SHORT_DELAY_MS);
355 : jsr166 1.27 assertEquals(q.remainingCapacity(), 0);
356 :     assertEquals(0, q.take());
357 : dl 1.45 delay(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.46 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
369 :     Thread t = newStartedThread(new CheckedRunnable() {
370 : jsr166 1.17 public void realRun() throws InterruptedException {
371 :     q.put(new Object());
372 :     q.put(new Object());
373 : jsr166 1.46 long startTime = System.nanoTime();
374 :     assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
375 :     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
376 :     pleaseInterrupt.countDown();
377 : jsr166 1.24 try {
378 : jsr166 1.46 q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
379 : jsr166 1.24 shouldThrow();
380 :     } catch (InterruptedException success) {}
381 :     }});
382 : jsr166 1.17
383 : jsr166 1.46 await(pleaseInterrupt);
384 : jsr166 1.17 t.interrupt();
385 : jsr166 1.46 awaitTermination(t);
386 : dl 1.1 }
387 :    
388 : dl 1.4 /**
389 : dl 1.5 * take retrieves elements in FIFO order
390 : dl 1.4 */
391 : jsr166 1.17 public void testTake() throws InterruptedException {
392 :     ArrayBlockingQueue q = populatedQueue(SIZE);
393 :     for (int i = 0; i < SIZE; ++i) {
394 : jsr166 1.25 assertEquals(i, q.take());
395 : jsr166 1.15 }
396 : dl 1.1 }
397 :    
398 : dl 1.4 /**
399 : dl 1.5 * Take removes existing elements until empty, then blocks interruptibly
400 : dl 1.4 */
401 : jsr166 1.17 public void testBlockingTake() throws InterruptedException {
402 : jsr166 1.25 final ArrayBlockingQueue q = populatedQueue(SIZE);
403 :     Thread t = new Thread(new CheckedRunnable() {
404 : jsr166 1.17 public void realRun() throws InterruptedException {
405 :     for (int i = 0; i < SIZE; ++i) {
406 : jsr166 1.25 assertEquals(i, q.take());
407 : jsr166 1.17 }
408 : jsr166 1.25 try {
409 :     q.take();
410 :     shouldThrow();
411 :     } catch (InterruptedException success) {}
412 :     }});
413 : jsr166 1.17
414 :     t.start();
415 : dl 1.45 delay(SHORT_DELAY_MS);
416 : jsr166 1.25 t.interrupt();
417 :     t.join();
418 : dl 1.1 }
419 :    
420 :    
421 : dl 1.4 /**
422 : dl 1.5 * poll succeeds unless empty
423 : dl 1.4 */
424 :     public void testPoll() {
425 : dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
426 :     for (int i = 0; i < SIZE; ++i) {
427 : jsr166 1.25 assertEquals(i, q.poll());
428 : dl 1.1 }
429 : jsr166 1.15 assertNull(q.poll());
430 : dl 1.1 }
431 :    
432 : dl 1.4 /**
433 : jsr166 1.37 * timed poll with zero timeout succeeds when non-empty, else times out
434 : dl 1.4 */
435 : jsr166 1.17 public void testTimedPoll0() throws InterruptedException {
436 :     ArrayBlockingQueue q = populatedQueue(SIZE);
437 :     for (int i = 0; i < SIZE; ++i) {
438 : jsr166 1.25 assertEquals(i, q.poll(0, MILLISECONDS));
439 : jsr166 1.15 }
440 : jsr166 1.17 assertNull(q.poll(0, MILLISECONDS));
441 : dl 1.1 }
442 :    
443 : dl 1.4 /**
444 : jsr166 1.37 * timed poll with nonzero timeout succeeds when non-empty, else times out
445 : dl 1.4 */
446 : jsr166 1.17 public void testTimedPoll() throws InterruptedException {
447 :     ArrayBlockingQueue q = populatedQueue(SIZE);
448 :     for (int i = 0; i < SIZE; ++i) {
449 : jsr166 1.25 assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
450 : jsr166 1.15 }
451 : jsr166 1.17 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
452 : dl 1.1 }
453 :    
454 : dl 1.4 /**
455 : dl 1.5 * Interrupted timed poll throws InterruptedException instead of
456 :     * returning timeout status
457 : dl 1.4 */
458 : jsr166 1.17 public void testInterruptedTimedPoll() throws InterruptedException {
459 : jsr166 1.42 final BlockingQueue<Integer> q = populatedQueue(SIZE);
460 :     final CountDownLatch aboutToWait = new CountDownLatch(1);
461 :     Thread t = newStartedThread(new CheckedRunnable() {
462 : jsr166 1.17 public void realRun() throws InterruptedException {
463 :     for (int i = 0; i < SIZE; ++i) {
464 : jsr166 1.42 long t0 = System.nanoTime();
465 :     assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
466 :     assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
467 : jsr166 1.17 }
468 : jsr166 1.42 long t0 = System.nanoTime();
469 :     aboutToWait.countDown();
470 : jsr166 1.21 try {
471 : jsr166 1.42 q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
472 : jsr166 1.22 shouldThrow();
473 : jsr166 1.42 } catch (InterruptedException success) {
474 :     assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
475 :     }
476 : jsr166 1.21 }});
477 : jsr166 1.17
478 : jsr166 1.42 aboutToWait.await();
479 :     waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
480 : jsr166 1.17 t.interrupt();
481 : jsr166 1.42 awaitTermination(t, MEDIUM_DELAY_MS);
482 :     checkEmpty(q);
483 : dl 1.1 }
484 :    
485 : dl 1.4 /**
486 : dl 1.5 * peek returns next element, or null if empty
487 : dl 1.4 */
488 :     public void testPeek() {
489 : dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
490 :     for (int i = 0; i < SIZE; ++i) {
491 : jsr166 1.26 assertEquals(i, q.peek());
492 :     assertEquals(i, q.poll());
493 : dl 1.1 assertTrue(q.peek() == null ||
494 : jsr166 1.26 !q.peek().equals(i));
495 : dl 1.1 }
496 : jsr166 1.15 assertNull(q.peek());
497 : dl 1.1 }
498 :    
499 : dl 1.4 /**
500 : dl 1.5 * element returns next element, or throws NSEE if empty
501 : dl 1.4 */
502 :     public void testElement() {
503 : dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
504 :     for (int i = 0; i < SIZE; ++i) {
505 : jsr166 1.26 assertEquals(i, q.element());
506 :     assertEquals(i, q.poll());
507 : dl 1.1 }
508 :     try {
509 :     q.element();
510 : dl 1.4 shouldThrow();
511 : jsr166 1.17 } catch (NoSuchElementException success) {}
512 : dl 1.1 }
513 :    
514 : dl 1.4 /**
515 : dl 1.5 * remove removes next element, or throws NSEE if empty
516 : dl 1.4 */
517 :     public void testRemove() {
518 : dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
519 :     for (int i = 0; i < SIZE; ++i) {
520 : jsr166 1.26 assertEquals(i, q.remove());
521 : dl 1.1 }
522 :     try {
523 :     q.remove();
524 : dl 1.4 shouldThrow();
525 : jsr166 1.17 } catch (NoSuchElementException success) {}
526 : dl 1.1 }
527 :    
528 : dl 1.4 /**
529 : dl 1.5 * remove(x) removes x and returns true if present
530 : dl 1.4 */
531 :     public void testRemoveElement() {
532 : dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
533 :     for (int i = 1; i < SIZE; i+=2) {
534 : dl 1.1 assertTrue(q.remove(new Integer(i)));
535 :     }
536 : dl 1.3 for (int i = 0; i < SIZE; i+=2) {
537 : dl 1.1 assertTrue(q.remove(new Integer(i)));
538 :     assertFalse(q.remove(new Integer(i+1)));
539 :     }
540 : dl 1.2 assertTrue(q.isEmpty());
541 : dl 1.1 }
542 : jsr166 1.12
543 : dl 1.4 /**
544 : dl 1.5 * contains(x) reports true when elements added but not yet removed
545 : dl 1.4 */
546 :     public void testContains() {
547 : dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
548 :     for (int i = 0; i < SIZE; ++i) {
549 : dl 1.1 assertTrue(q.contains(new Integer(i)));
550 : jsr166 1.26 assertEquals(i, q.poll());
551 : dl 1.1 assertFalse(q.contains(new Integer(i)));
552 :     }
553 :     }
554 :    
555 : dl 1.4 /**
556 : dl 1.5 * clear removes all elements
557 : dl 1.4 */
558 :     public void testClear() {
559 : dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
560 : dl 1.1 q.clear();
561 :     assertTrue(q.isEmpty());
562 :     assertEquals(0, q.size());
563 : dl 1.3 assertEquals(SIZE, q.remainingCapacity());
564 :     q.add(one);
565 : dl 1.1 assertFalse(q.isEmpty());
566 : dl 1.11 assertTrue(q.contains(one));
567 : dl 1.1 q.clear();
568 :     assertTrue(q.isEmpty());
569 :     }
570 :    
571 : dl 1.4 /**
572 : dl 1.5 * containsAll(c) is true when c contains a subset of elements
573 : dl 1.4 */
574 :     public void testContainsAll() {
575 : dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
576 :     ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE);
577 :     for (int i = 0; i < SIZE; ++i) {
578 : dl 1.1 assertTrue(q.containsAll(p));
579 :     assertFalse(p.containsAll(q));
580 :     p.add(new Integer(i));
581 :     }
582 :     assertTrue(p.containsAll(q));
583 :     }
584 :    
585 : dl 1.4 /**
586 : dl 1.5 * retainAll(c) retains only those elements of c and reports true if changed
587 : dl 1.4 */
588 :     public void testRetainAll() {
589 : dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
590 :     ArrayBlockingQueue p = populatedQueue(SIZE);
591 :     for (int i = 0; i < SIZE; ++i) {
592 : dl 1.1 boolean changed = q.retainAll(p);
593 :     if (i == 0)
594 :     assertFalse(changed);
595 :     else
596 :     assertTrue(changed);
597 :    
598 :     assertTrue(q.containsAll(p));
599 : dl 1.3 assertEquals(SIZE-i, q.size());
600 : dl 1.1 p.remove();
601 :     }
602 :     }
603 :    
604 : dl 1.4 /**
605 : dl 1.5 * removeAll(c) removes only those elements of c and reports true if changed
606 : dl 1.4 */
607 :     public void testRemoveAll() {
608 : dl 1.3 for (int i = 1; i < SIZE; ++i) {
609 :     ArrayBlockingQueue q = populatedQueue(SIZE);
610 :     ArrayBlockingQueue p = populatedQueue(i);
611 : dl 1.1 assertTrue(q.removeAll(p));
612 : dl 1.3 assertEquals(SIZE-i, q.size());
613 : dl 1.1 for (int j = 0; j < i; ++j) {
614 :     Integer I = (Integer)(p.remove());
615 :     assertFalse(q.contains(I));
616 :     }
617 :     }
618 :     }
619 :    
620 : dl 1.4 /**
621 : jsr166 1.40 * toArray contains all elements in FIFO order
622 : dl 1.4 */
623 : jsr166 1.40 public void testToArray() {
624 : dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
625 : jsr166 1.15 Object[] o = q.toArray();
626 :     for (int i = 0; i < o.length; i++)
627 : jsr166 1.40 assertSame(o[i], q.poll());
628 : dl 1.1 }
629 :    
630 : dl 1.4 /**
631 : jsr166 1.40 * toArray(a) contains all elements in FIFO order
632 : dl 1.4 */
633 : jsr166 1.40 public void testToArray2() {
634 : jsr166 1.41 ArrayBlockingQueue<Integer> q = populatedQueue(SIZE);
635 : jsr166 1.15 Integer[] ints = new Integer[SIZE];
636 : jsr166 1.41 Integer[] array = q.toArray(ints);
637 :     assertSame(ints, array);
638 : jsr166 1.17 for (int i = 0; i < ints.length; i++)
639 : jsr166 1.40 assertSame(ints[i], q.poll());
640 : dl 1.1 }
641 : dl 1.6
642 :     /**
643 : jsr166 1.39 * toArray(null) throws NullPointerException
644 : dl 1.6 */
645 : jsr166 1.39 public void testToArray_NullArg() {
646 : jsr166 1.26 ArrayBlockingQueue q = populatedQueue(SIZE);
647 : jsr166 1.15 try {
648 : jsr166 1.39 q.toArray(null);
649 : jsr166 1.15 shouldThrow();
650 :     } catch (NullPointerException success) {}
651 : dl 1.6 }
652 :    
653 :     /**
654 : jsr166 1.38 * toArray(incompatible array type) throws ArrayStoreException
655 : dl 1.6 */
656 :     public void testToArray1_BadArg() {
657 : jsr166 1.26 ArrayBlockingQueue q = populatedQueue(SIZE);
658 : jsr166 1.15 try {
659 : jsr166 1.38 q.toArray(new String[10]);
660 : jsr166 1.15 shouldThrow();
661 : jsr166 1.18 } catch (ArrayStoreException success) {}
662 : dl 1.6 }
663 :    
664 : jsr166 1.12
665 : dl 1.4 /**
666 : dl 1.5 * iterator iterates through all elements
667 : dl 1.4 */
668 : jsr166 1.17 public void testIterator() throws InterruptedException {
669 : dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
670 : jsr166 1.15 Iterator it = q.iterator();
671 : jsr166 1.17 while (it.hasNext()) {
672 :     assertEquals(it.next(), q.take());
673 : jsr166 1.15 }
674 : dl 1.1 }
675 :    
676 : dl 1.4 /**
677 : dl 1.5 * iterator.remove removes current element
678 :     */
679 : jsr166 1.29 public void testIteratorRemove() {
680 : dl 1.5 final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
681 :     q.add(two);
682 :     q.add(one);
683 :     q.add(three);
684 :    
685 :     Iterator it = q.iterator();
686 :     it.next();
687 :     it.remove();
688 : jsr166 1.12
689 : dl 1.5 it = q.iterator();
690 : jsr166 1.28 assertSame(it.next(), one);
691 :     assertSame(it.next(), three);
692 : dl 1.5 assertFalse(it.hasNext());
693 :     }
694 :    
695 :     /**
696 :     * iterator ordering is FIFO
697 : dl 1.4 */
698 : dl 1.1 public void testIteratorOrdering() {
699 :     final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
700 : dl 1.3 q.add(one);
701 :     q.add(two);
702 :     q.add(three);
703 : dl 1.1
704 :     assertEquals("queue should be full", 0, q.remainingCapacity());
705 :    
706 :     int k = 0;
707 :     for (Iterator it = q.iterator(); it.hasNext();) {
708 : jsr166 1.26 assertEquals(++k, it.next());
709 : dl 1.1 }
710 : dl 1.4 assertEquals(3, k);
711 : dl 1.1 }
712 :    
713 : dl 1.4 /**
714 : dl 1.5 * Modifications do not cause iterators to fail
715 : dl 1.4 */
716 : jsr166 1.29 public void testWeaklyConsistentIteration() {
717 : dl 1.1 final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
718 : dl 1.3 q.add(one);
719 :     q.add(two);
720 :     q.add(three);
721 : jsr166 1.17 for (Iterator it = q.iterator(); it.hasNext();) {
722 :     q.remove();
723 :     it.next();
724 : dl 1.1 }
725 : dl 1.4 assertEquals(0, q.size());
726 : dl 1.1 }
727 :    
728 :    
729 : dl 1.4 /**
730 : dl 1.5 * toString contains toStrings of elements
731 : dl 1.4 */
732 :     public void testToString() {
733 : dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
734 : dl 1.1 String s = q.toString();
735 : dl 1.3 for (int i = 0; i < SIZE; ++i) {
736 : dl 1.1 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
737 :     }
738 : jsr166 1.12 }
739 : dl 1.1
740 :    
741 : dl 1.4 /**
742 : dl 1.5 * offer transfers elements across Executor tasks
743 : dl 1.4 */
744 : dl 1.1 public void testOfferInExecutor() {
745 :     final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
746 : dl 1.3 q.add(one);
747 :     q.add(two);
748 : dl 1.1 ExecutorService executor = Executors.newFixedThreadPool(2);
749 : jsr166 1.17 executor.execute(new CheckedRunnable() {
750 :     public void realRun() throws InterruptedException {
751 : jsr166 1.27 assertFalse(q.offer(three));
752 :     assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
753 :     assertEquals(0, q.remainingCapacity());
754 : jsr166 1.17 }});
755 :    
756 :     executor.execute(new CheckedRunnable() {
757 :     public void realRun() throws InterruptedException {
758 : dl 1.45 delay(SMALL_DELAY_MS);
759 : jsr166 1.27 assertSame(one, q.take());
760 : jsr166 1.17 }});
761 : jsr166 1.12
762 : dl 1.3 joinPool(executor);
763 : dl 1.1 }
764 :    
765 : dl 1.4 /**
766 : dl 1.5 * poll retrieves elements across Executor threads
767 : dl 1.4 */
768 : dl 1.1 public void testPollInExecutor() {
769 :     final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
770 :     ExecutorService executor = Executors.newFixedThreadPool(2);
771 : jsr166 1.17 executor.execute(new CheckedRunnable() {
772 :     public void realRun() throws InterruptedException {
773 : jsr166 1.27 assertNull(q.poll());
774 :     assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
775 :     assertTrue(q.isEmpty());
776 : jsr166 1.17 }});
777 :    
778 :     executor.execute(new CheckedRunnable() {
779 :     public void realRun() throws InterruptedException {
780 : dl 1.45 delay(SMALL_DELAY_MS);
781 : jsr166 1.17 q.put(one);
782 :     }});
783 : jsr166 1.12
784 : dl 1.3 joinPool(executor);
785 : dl 1.1 }
786 : dl 1.2
787 : dl 1.4 /**
788 : dl 1.5 * A deserialized serialized queue has same elements in same order
789 : dl 1.4 */
790 : jsr166 1.17 public void testSerialization() throws Exception {
791 : dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
792 : dl 1.2
793 : jsr166 1.17 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
794 :     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
795 :     out.writeObject(q);
796 :     out.close();
797 :    
798 :     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
799 :     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
800 :     ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
801 :     assertEquals(q.size(), r.size());
802 :     while (!q.isEmpty())
803 :     assertEquals(q.remove(), r.remove());
804 : dl 1.6 }
805 :    
806 :     /**
807 :     * drainTo(null) throws NPE
808 : jsr166 1.12 */
809 : dl 1.6 public void testDrainToNull() {
810 :     ArrayBlockingQueue q = populatedQueue(SIZE);
811 :     try {
812 :     q.drainTo(null);
813 :     shouldThrow();
814 : jsr166 1.17 } catch (NullPointerException success) {}
815 : dl 1.6 }
816 :    
817 :     /**
818 :     * drainTo(this) throws IAE
819 : jsr166 1.12 */
820 : dl 1.6 public void testDrainToSelf() {
821 :     ArrayBlockingQueue q = populatedQueue(SIZE);
822 :     try {
823 :     q.drainTo(q);
824 :     shouldThrow();
825 : jsr166 1.17 } catch (IllegalArgumentException success) {}
826 : dl 1.6 }
827 :    
828 :     /**
829 :     * drainTo(c) empties queue into another collection c
830 : jsr166 1.12 */
831 : dl 1.6 public void testDrainTo() {
832 :     ArrayBlockingQueue q = populatedQueue(SIZE);
833 :     ArrayList l = new ArrayList();
834 :     q.drainTo(l);
835 :     assertEquals(q.size(), 0);
836 :     assertEquals(l.size(), SIZE);
837 : jsr166 1.12 for (int i = 0; i < SIZE; ++i)
838 : dl 1.6 assertEquals(l.get(i), new Integer(i));
839 : dl 1.11 q.add(zero);
840 :     q.add(one);
841 :     assertFalse(q.isEmpty());
842 :     assertTrue(q.contains(zero));
843 :     assertTrue(q.contains(one));
844 :     l.clear();
845 :     q.drainTo(l);
846 :     assertEquals(q.size(), 0);
847 :     assertEquals(l.size(), 2);
848 : jsr166 1.12 for (int i = 0; i < 2; ++i)
849 : dl 1.11 assertEquals(l.get(i), new Integer(i));
850 : dl 1.6 }
851 :    
852 :     /**
853 :     * drainTo empties full queue, unblocking a waiting put.
854 : jsr166 1.12 */
855 : jsr166 1.17 public void testDrainToWithActivePut() throws InterruptedException {
856 : dl 1.6 final ArrayBlockingQueue q = populatedQueue(SIZE);
857 : jsr166 1.17 Thread t = new Thread(new CheckedRunnable() {
858 :     public void realRun() throws InterruptedException {
859 :     q.put(new Integer(SIZE+1));
860 :     }});
861 :    
862 :     t.start();
863 :     ArrayList l = new ArrayList();
864 :     q.drainTo(l);
865 :     assertTrue(l.size() >= SIZE);
866 :     for (int i = 0; i < SIZE; ++i)
867 :     assertEquals(l.get(i), new Integer(i));
868 :     t.join();
869 :     assertTrue(q.size() + l.size() >= SIZE);
870 : dl 1.6 }
871 :    
872 :     /**
873 :     * drainTo(null, n) throws NPE
874 : jsr166 1.12 */
875 : dl 1.6 public void testDrainToNullN() {
876 :     ArrayBlockingQueue q = populatedQueue(SIZE);
877 :     try {
878 :     q.drainTo(null, 0);
879 :     shouldThrow();
880 : jsr166 1.17 } catch (NullPointerException success) {}
881 : dl 1.6 }
882 :    
883 :     /**
884 :     * drainTo(this, n) throws IAE
885 : jsr166 1.12 */
886 : dl 1.6 public void testDrainToSelfN() {
887 :     ArrayBlockingQueue q = populatedQueue(SIZE);
888 :     try {
889 :     q.drainTo(q, 0);
890 :     shouldThrow();
891 : jsr166 1.17 } catch (IllegalArgumentException success) {}
892 : dl 1.6 }
893 :    
894 :     /**
895 : jsr166 1.34 * drainTo(c, n) empties first min(n, size) elements of queue into c
896 : jsr166 1.12 */
897 : dl 1.6 public void testDrainToN() {
898 : dl 1.11 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
899 : dl 1.6 for (int i = 0; i < SIZE + 2; ++i) {
900 : jsr166 1.13 for (int j = 0; j < SIZE; j++)
901 : dl 1.11 assertTrue(q.offer(new Integer(j)));
902 : dl 1.6 ArrayList l = new ArrayList();
903 :     q.drainTo(l, i);
904 : jsr166 1.35 int k = (i < SIZE) ? i : SIZE;
905 : dl 1.11 assertEquals(l.size(), k);
906 : dl 1.6 assertEquals(q.size(), SIZE-k);
907 : jsr166 1.12 for (int j = 0; j < k; ++j)
908 : dl 1.6 assertEquals(l.get(j), new Integer(j));
909 : dl 1.11 while (q.poll() != null) ;
910 : dl 1.2 }
911 :     }
912 :    
913 : dl 1.1 }

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8