ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.2
Committed: Sun Sep 7 20:39:11 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.1: +23 -1 lines
Log Message:
Added serialization and lock tests

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