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