ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.1
Committed: Sun Aug 31 19:24:52 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Log Message:
First check-in of tests to be in tck

File Contents

# Content
1 /*
2 * Written by members of JCP JSR-166 Expert Group and released to the
3 * public domain. Use, modify, and redistribute this code in any way
4 * without acknowledgement. Other contributors include Andrew Wright,
5 * Jeffrey Hayes, Pat Fischer, Mike Judd.
6 */
7
8 import junit.framework.*;
9 import java.util.*;
10 import java.util.concurrent.*;
11
12 public class ArrayBlockingQueueTest extends TestCase {
13
14 private static int N = 10;
15 private static long SHORT_DELAY_MS = 100;
16 private static long MEDIUM_DELAY_MS = 1000;
17 private static long LONG_DELAY_MS = 10000;
18
19 public static void main(String[] args) {
20 junit.textui.TestRunner.run (suite());
21 }
22
23 public static Test suite() {
24 return new TestSuite(ArrayBlockingQueueTest.class);
25 }
26
27 /**
28 * Create a queue of given size containing consecutive
29 * Integers 0 ... n.
30 */
31 private ArrayBlockingQueue fullQueue(int n) {
32 ArrayBlockingQueue q = new ArrayBlockingQueue(n);
33 assertTrue(q.isEmpty());
34 for(int i = 0; i < n; i++)
35 assertTrue(q.offer(new Integer(i)));
36 assertFalse(q.isEmpty());
37 assertEquals(0, q.remainingCapacity());
38 assertEquals(n, q.size());
39 return q;
40 }
41
42 public void testConstructor1(){
43 assertEquals(N, new ArrayBlockingQueue(N).remainingCapacity());
44 }
45
46 public void testConstructor2(){
47 try {
48 ArrayBlockingQueue q = new ArrayBlockingQueue(0);
49 fail("Cannot make zero-sized");
50 }
51 catch (IllegalArgumentException success) {}
52 }
53
54 public void testConstructor3(){
55
56 try {
57 ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
58 fail("Cannot make from null collection");
59 }
60 catch (NullPointerException success) {}
61 }
62
63 public void testConstructor4(){
64 try {
65 Integer[] ints = new Integer[N];
66 ArrayBlockingQueue q = new ArrayBlockingQueue(N, false, Arrays.asList(ints));
67 fail("Cannot make with null elements");
68 }
69 catch (NullPointerException success) {}
70 }
71
72 public void testConstructor5(){
73 try {
74 Integer[] ints = new Integer[N];
75 for (int i = 0; i < N-1; ++i)
76 ints[i] = new Integer(i);
77 ArrayBlockingQueue q = new ArrayBlockingQueue(N, false, Arrays.asList(ints));
78 fail("Cannot make with null elements");
79 }
80 catch (NullPointerException success) {}
81 }
82
83 public void testConstructor6(){
84 try {
85 Integer[] ints = new Integer[N];
86 for (int i = 0; i < N; ++i)
87 ints[i] = new Integer(i);
88 ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
89 fail("Cannot make with insufficient capacity");
90 }
91 catch (IllegalArgumentException success) {}
92 }
93
94 public void testConstructor7(){
95 try {
96 Integer[] ints = new Integer[N];
97 for (int i = 0; i < N; ++i)
98 ints[i] = new Integer(i);
99 ArrayBlockingQueue q = new ArrayBlockingQueue(N, true, Arrays.asList(ints));
100 for (int i = 0; i < N; ++i)
101 assertEquals(ints[i], q.poll());
102 }
103 finally {}
104 }
105
106 public void testEmptyFull() {
107 ArrayBlockingQueue q = new ArrayBlockingQueue(2);
108 assertTrue(q.isEmpty());
109 assertEquals("should have room for 2", 2, q.remainingCapacity());
110 q.add(new Integer(1));
111 assertFalse(q.isEmpty());
112 q.add(new Integer(2));
113 assertFalse(q.isEmpty());
114 assertEquals("queue should be full", 0, q.remainingCapacity());
115 assertFalse("offer should be rejected", q.offer(new Integer(3)));
116 }
117
118 public void testRemainingCapacity(){
119 ArrayBlockingQueue q = fullQueue(N);
120 for (int i = 0; i < N; ++i) {
121 assertEquals(i, q.remainingCapacity());
122 assertEquals(N-i, q.size());
123 q.remove();
124 }
125 for (int i = 0; i < N; ++i) {
126 assertEquals(N-i, q.remainingCapacity());
127 assertEquals(i, q.size());
128 q.add(new Integer(i));
129 }
130 }
131
132 public void testOfferNull(){
133 try {
134 ArrayBlockingQueue q = new ArrayBlockingQueue(1);
135 q.offer(null);
136 fail("should throw NPE");
137 } catch (NullPointerException success) { }
138 }
139
140 public void testOffer(){
141 ArrayBlockingQueue q = new ArrayBlockingQueue(1);
142 assertTrue(q.offer(new Integer(0)));
143 assertFalse(q.offer(new Integer(1)));
144 }
145
146 public void testAdd(){
147 try {
148 ArrayBlockingQueue q = new ArrayBlockingQueue(N);
149 for (int i = 0; i < N; ++i) {
150 assertTrue(q.add(new Integer(i)));
151 }
152 assertEquals(0, q.remainingCapacity());
153 q.add(new Integer(N));
154 } catch (IllegalStateException success){
155 }
156 }
157
158 public void testAddAll1(){
159 try {
160 ArrayBlockingQueue q = new ArrayBlockingQueue(1);
161 q.addAll(null);
162 fail("Cannot add null collection");
163 }
164 catch (NullPointerException success) {}
165 }
166 public void testAddAll2(){
167 try {
168 ArrayBlockingQueue q = new ArrayBlockingQueue(N);
169 Integer[] ints = new Integer[N];
170 q.addAll(Arrays.asList(ints));
171 fail("Cannot add null elements");
172 }
173 catch (NullPointerException success) {}
174 }
175 public void testAddAll3(){
176 try {
177 ArrayBlockingQueue q = new ArrayBlockingQueue(N);
178 Integer[] ints = new Integer[N];
179 for (int i = 0; i < N-1; ++i)
180 ints[i] = new Integer(i);
181 q.addAll(Arrays.asList(ints));
182 fail("Cannot add null elements");
183 }
184 catch (NullPointerException success) {}
185 }
186 public void testAddAll4(){
187 try {
188 ArrayBlockingQueue q = new ArrayBlockingQueue(1);
189 Integer[] ints = new Integer[N];
190 for (int i = 0; i < N; ++i)
191 ints[i] = new Integer(i);
192 q.addAll(Arrays.asList(ints));
193 fail("Cannot add with insufficient capacity");
194 }
195 catch (IllegalStateException success) {}
196 }
197 public void testAddAll5(){
198 try {
199 Integer[] empty = new Integer[0];
200 Integer[] ints = new Integer[N];
201 for (int i = 0; i < N; ++i)
202 ints[i] = new Integer(i);
203 ArrayBlockingQueue q = new ArrayBlockingQueue(N);
204 assertFalse(q.addAll(Arrays.asList(empty)));
205 assertTrue(q.addAll(Arrays.asList(ints)));
206 for (int i = 0; i < N; ++i)
207 assertEquals(ints[i], q.poll());
208 }
209 finally {}
210 }
211
212 public void testPutNull() {
213 try {
214 ArrayBlockingQueue q = new ArrayBlockingQueue(N);
215 q.put(null);
216 fail("put should throw NPE");
217 }
218 catch (NullPointerException success){
219 }
220 catch (InterruptedException ie) {
221 fail("Unexpected exception");
222 }
223 }
224
225 public void testPut() {
226 try {
227 ArrayBlockingQueue q = new ArrayBlockingQueue(N);
228 for (int i = 0; i < N; ++i) {
229 Integer I = new Integer(i);
230 q.put(I);
231 assertTrue(q.contains(I));
232 }
233 assertEquals(0, q.remainingCapacity());
234 }
235 catch (InterruptedException ie) {
236 fail("Unexpected exception");
237 }
238 }
239
240 public void testBlockingPut(){
241 Thread t = new Thread(new Runnable() {
242 public void run() {
243 int added = 0;
244 try {
245 ArrayBlockingQueue q = new ArrayBlockingQueue(N);
246 for (int i = 0; i < N; ++i) {
247 q.put(new Integer(i));
248 ++added;
249 }
250 q.put(new Integer(N));
251 fail("put should block");
252 } catch (InterruptedException ie){
253 assertEquals(added, N);
254 }
255 }});
256 t.start();
257 try {
258 Thread.sleep(SHORT_DELAY_MS);
259 t.interrupt();
260 t.join();
261 }
262 catch (InterruptedException ie) {
263 fail("Unexpected exception");
264 }
265 }
266
267 public void testPutWithTake() {
268 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
269 Thread t = new Thread(new Runnable() {
270 public void run(){
271 int added = 0;
272 try {
273 q.put(new Object());
274 ++added;
275 q.put(new Object());
276 ++added;
277 q.put(new Object());
278 ++added;
279 q.put(new Object());
280 ++added;
281 fail("Should block");
282 } catch (InterruptedException e){
283 assertTrue(added >= 2);
284 }
285 }
286 });
287 try {
288 t.start();
289 Thread.sleep(SHORT_DELAY_MS);
290 q.take();
291 t.interrupt();
292 t.join();
293 } catch (Exception e){
294 fail("Unexpected exception");
295 }
296 }
297
298 public void testTimedOffer() {
299 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
300 Thread t = new Thread(new Runnable() {
301 public void run(){
302 try {
303 q.put(new Object());
304 q.put(new Object());
305 assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
306 q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
307 fail("Should block");
308 } catch (InterruptedException success){}
309 }
310 });
311
312 try {
313 t.start();
314 Thread.sleep(SHORT_DELAY_MS);
315 t.interrupt();
316 t.join();
317 } catch (Exception e){
318 fail("Unexpected exception");
319 }
320 }
321
322 public void testTake(){
323 try {
324 ArrayBlockingQueue q = fullQueue(N);
325 for (int i = 0; i < N; ++i) {
326 assertEquals(i, ((Integer)q.take()).intValue());
327 }
328 } catch (InterruptedException e){
329 fail("Unexpected exception");
330 }
331 }
332
333 public void testTakeFromEmpty() {
334 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
335 Thread t = new Thread(new Runnable() {
336 public void run(){
337 try {
338 q.take();
339 fail("Should block");
340 } catch (InterruptedException success){ }
341 }
342 });
343 try {
344 t.start();
345 Thread.sleep(SHORT_DELAY_MS);
346 t.interrupt();
347 t.join();
348 } catch (Exception e){
349 fail("Unexpected exception");
350 }
351 }
352
353 public void testBlockingTake(){
354 Thread t = new Thread(new Runnable() {
355 public void run() {
356 try {
357 ArrayBlockingQueue q = fullQueue(N);
358 for (int i = 0; i < N; ++i) {
359 assertEquals(i, ((Integer)q.take()).intValue());
360 }
361 q.take();
362 fail("take should block");
363 } catch (InterruptedException success){
364 }
365 }});
366 t.start();
367 try {
368 Thread.sleep(SHORT_DELAY_MS);
369 t.interrupt();
370 t.join();
371 }
372 catch (InterruptedException ie) {
373 fail("Unexpected exception");
374 }
375 }
376
377
378 public void testPoll(){
379 ArrayBlockingQueue q = fullQueue(N);
380 for (int i = 0; i < N; ++i) {
381 assertEquals(i, ((Integer)q.poll()).intValue());
382 }
383 assertNull(q.poll());
384 }
385
386 public void testTimedPoll0() {
387 try {
388 ArrayBlockingQueue q = fullQueue(N);
389 for (int i = 0; i < N; ++i) {
390 assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
391 }
392 assertNull(q.poll(0, TimeUnit.MILLISECONDS));
393 } catch (InterruptedException e){
394 fail("Unexpected exception");
395 }
396 }
397
398 public void testTimedPoll() {
399 try {
400 ArrayBlockingQueue q = fullQueue(N);
401 for (int i = 0; i < N; ++i) {
402 assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
403 }
404 assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
405 } catch (InterruptedException e){
406 fail("Unexpected exception");
407 }
408 }
409
410 public void testInterruptedTimedPoll(){
411 Thread t = new Thread(new Runnable() {
412 public void run() {
413 try {
414 ArrayBlockingQueue q = fullQueue(N);
415 for (int i = 0; i < N; ++i) {
416 assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
417 }
418 assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
419 } catch (InterruptedException success){
420 }
421 }});
422 t.start();
423 try {
424 Thread.sleep(SHORT_DELAY_MS);
425 t.interrupt();
426 t.join();
427 }
428 catch (InterruptedException ie) {
429 fail("Unexpected exception");
430 }
431 }
432
433 public void testTimedPollWithOffer(){
434 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
435 Thread t = new Thread(new Runnable() {
436 public void run(){
437 try {
438 assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
439 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
440 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
441 fail("Should block");
442 } catch (InterruptedException success) { }
443 }
444 });
445 try {
446 t.start();
447 Thread.sleep(SHORT_DELAY_MS * 2);
448 assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
449 t.interrupt();
450 t.join();
451 } catch (Exception e){
452 fail("Unexpected exception");
453 }
454 }
455
456
457 public void testPeek(){
458 ArrayBlockingQueue q = fullQueue(N);
459 for (int i = 0; i < N; ++i) {
460 assertEquals(i, ((Integer)q.peek()).intValue());
461 q.poll();
462 assertTrue(q.peek() == null ||
463 i != ((Integer)q.peek()).intValue());
464 }
465 assertNull(q.peek());
466 }
467
468 public void testElement(){
469 ArrayBlockingQueue q = fullQueue(N);
470 for (int i = 0; i < N; ++i) {
471 assertEquals(i, ((Integer)q.element()).intValue());
472 q.poll();
473 }
474 try {
475 q.element();
476 fail("no such element");
477 }
478 catch (NoSuchElementException success) {}
479 }
480
481 public void testRemove(){
482 ArrayBlockingQueue q = fullQueue(N);
483 for (int i = 0; i < N; ++i) {
484 assertEquals(i, ((Integer)q.remove()).intValue());
485 }
486 try {
487 q.remove();
488 fail("remove should throw");
489 } catch (NoSuchElementException success){
490 }
491 }
492
493 public void testRemoveElement(){
494 ArrayBlockingQueue q = fullQueue(N);
495 for (int i = 1; i < N; i+=2) {
496 assertTrue(q.remove(new Integer(i)));
497 }
498 for (int i = 0; i < N; i+=2) {
499 assertTrue(q.remove(new Integer(i)));
500 assertFalse(q.remove(new Integer(i+1)));
501 }
502 assert(q.isEmpty());
503 }
504
505 public void testContains(){
506 ArrayBlockingQueue q = fullQueue(N);
507 for (int i = 0; i < N; ++i) {
508 assertTrue(q.contains(new Integer(i)));
509 q.poll();
510 assertFalse(q.contains(new Integer(i)));
511 }
512 }
513
514 public void testClear(){
515 ArrayBlockingQueue q = fullQueue(N);
516 q.clear();
517 assertTrue(q.isEmpty());
518 assertEquals(0, q.size());
519 assertEquals(N, q.remainingCapacity());
520 q.add(new Integer(1));
521 assertFalse(q.isEmpty());
522 q.clear();
523 assertTrue(q.isEmpty());
524 }
525
526 public void testContainsAll(){
527 ArrayBlockingQueue q = fullQueue(N);
528 ArrayBlockingQueue p = new ArrayBlockingQueue(N);
529 for (int i = 0; i < N; ++i) {
530 assertTrue(q.containsAll(p));
531 assertFalse(p.containsAll(q));
532 p.add(new Integer(i));
533 }
534 assertTrue(p.containsAll(q));
535 }
536
537 public void testRetainAll(){
538 ArrayBlockingQueue q = fullQueue(N);
539 ArrayBlockingQueue p = fullQueue(N);
540 for (int i = 0; i < N; ++i) {
541 boolean changed = q.retainAll(p);
542 if (i == 0)
543 assertFalse(changed);
544 else
545 assertTrue(changed);
546
547 assertTrue(q.containsAll(p));
548 assertEquals(N-i, q.size());
549 p.remove();
550 }
551 }
552
553 public void testRemoveAll(){
554 for (int i = 1; i < N; ++i) {
555 ArrayBlockingQueue q = fullQueue(N);
556 ArrayBlockingQueue p = fullQueue(i);
557 assertTrue(q.removeAll(p));
558 assertEquals(N-i, q.size());
559 for (int j = 0; j < i; ++j) {
560 Integer I = (Integer)(p.remove());
561 assertFalse(q.contains(I));
562 }
563 }
564 }
565
566
567 public void testToArray(){
568 ArrayBlockingQueue q = fullQueue(N);
569 Object[] o = q.toArray();
570 try {
571 for(int i = 0; i < o.length; i++)
572 assertEquals(o[i], q.take());
573 } catch (InterruptedException e){
574 fail("Unexpected exception");
575 }
576 }
577
578 public void testToArray2(){
579 ArrayBlockingQueue q = fullQueue(N);
580 Integer[] ints = new Integer[N];
581 ints = (Integer[])q.toArray(ints);
582 try {
583 for(int i = 0; i < ints.length; i++)
584 assertEquals(ints[i], q.take());
585 } catch (InterruptedException e){
586 fail("Unexpected exception");
587 }
588 }
589
590 public void testIterator(){
591 ArrayBlockingQueue q = fullQueue(N);
592 Iterator it = q.iterator();
593 try {
594 while(it.hasNext()){
595 assertEquals(it.next(), q.take());
596 }
597 } catch (InterruptedException e){
598 fail("Unexpected exception");
599 }
600 }
601
602 public void testIteratorOrdering() {
603
604 final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
605
606 q.add(new Integer(1));
607 q.add(new Integer(2));
608 q.add(new Integer(3));
609
610 assertEquals("queue should be full", 0, q.remainingCapacity());
611
612 int k = 0;
613 for (Iterator it = q.iterator(); it.hasNext();) {
614 int i = ((Integer)(it.next())).intValue();
615 assertEquals("items should come out in order", ++k, i);
616 }
617
618 assertEquals("should go through 3 elements", 3, k);
619 }
620
621 public void testWeaklyConsistentIteration () {
622
623 final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
624
625 q.add(new Integer(1));
626 q.add(new Integer(2));
627 q.add(new Integer(3));
628
629 try {
630 for (Iterator it = q.iterator(); it.hasNext();) {
631 q.remove();
632 it.next();
633 }
634 }
635 catch (ConcurrentModificationException e) {
636 fail("weakly consistent iterator; should not get CME");
637 }
638
639 assertEquals("queue should be empty again", 0, q.size());
640 }
641
642
643 public void testToString(){
644 ArrayBlockingQueue q = fullQueue(N);
645 String s = q.toString();
646 for (int i = 0; i < N; ++i) {
647 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
648 }
649 }
650
651
652 public void testOfferInExecutor() {
653
654 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
655
656 q.add(new Integer(1));
657 q.add(new Integer(2));
658
659 ExecutorService executor = Executors.newFixedThreadPool(2);
660
661 executor.execute(new Runnable() {
662 public void run() {
663 assertFalse("offer should be rejected", q.offer(new Integer(3)));
664 try {
665 assertTrue("offer should be accepted", q.offer(new Integer(3), MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
666 assertEquals(0, q.remainingCapacity());
667 }
668 catch (InterruptedException e) {
669 fail("should not be interrupted");
670 }
671 }
672 });
673
674 executor.execute(new Runnable() {
675 public void run() {
676 try {
677 Thread.sleep(MEDIUM_DELAY_MS);
678 assertEquals("first item in queue should be 1", new Integer(1), q.take());
679 }
680 catch (InterruptedException e) {
681 fail("should not be interrupted");
682 }
683 }
684 });
685
686 executor.shutdown();
687
688 }
689
690 public void testPollInExecutor() {
691
692 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
693
694 ExecutorService executor = Executors.newFixedThreadPool(2);
695
696 executor.execute(new Runnable() {
697 public void run() {
698 assertNull("poll should fail", q.poll());
699 try {
700 assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
701 assertTrue(q.isEmpty());
702 }
703 catch (InterruptedException e) {
704 fail("should not be interrupted");
705 }
706 }
707 });
708
709 executor.execute(new Runnable() {
710 public void run() {
711 try {
712 Thread.sleep(MEDIUM_DELAY_MS);
713 q.put(new Integer(1));
714 }
715 catch (InterruptedException e) {
716 fail("should not be interrupted");
717 }
718 }
719 });
720
721 executor.shutdown();
722
723 }
724
725 }