ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
Revision: 1.5
Committed: Sat Sep 20 18:20:08 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.4: +200 -67 lines
Log Message:
Documentation scaffolding

File Contents

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