ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SynchronousQueueTest.java
Revision: 1.6
Committed: Sun Oct 5 23:00:40 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE, JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.5: +167 -0 lines
Log Message:
Added tests and documentation

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 SynchronousQueueTest 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(SynchronousQueueTest.class);
21     }
22    
23 dl 1.4 /**
24 dl 1.5 * A SynchronousQueue is both empty and full
25 dl 1.4 */
26 dl 1.1 public void testEmptyFull() {
27     SynchronousQueue q = new SynchronousQueue();
28     assertTrue(q.isEmpty());
29     assertEquals(0, q.size());
30     assertEquals(0, q.remainingCapacity());
31 dl 1.5 assertFalse(q.offer(zero));
32 dl 1.1 }
33    
34 dl 1.4 /**
35 dl 1.5 * offer(null) throws NPE
36 dl 1.4 */
37     public void testOfferNull() {
38 dl 1.1 try {
39     SynchronousQueue q = new SynchronousQueue();
40     q.offer(null);
41 dl 1.4 shouldThrow();
42 dl 1.1 } catch (NullPointerException success) { }
43     }
44    
45 dl 1.4 /**
46 dl 1.6 * add(null) throws NPE
47     */
48     public void testAddNull() {
49     try {
50     SynchronousQueue q = new SynchronousQueue();
51     q.add(null);
52     shouldThrow();
53     } catch (NullPointerException success) { }
54     }
55    
56     /**
57 dl 1.5 * offer fails if no active taker
58 dl 1.4 */
59     public void testOffer() {
60 dl 1.1 SynchronousQueue q = new SynchronousQueue();
61 dl 1.5 assertFalse(q.offer(one));
62 dl 1.1 }
63    
64 dl 1.4 /**
65 dl 1.5 * add throws ISE if no active taker
66 dl 1.4 */
67     public void testAdd() {
68 dl 1.1 try {
69     SynchronousQueue q = new SynchronousQueue();
70     assertEquals(0, q.remainingCapacity());
71 dl 1.5 q.add(one);
72     shouldThrow();
73 dl 1.1 } catch (IllegalStateException success){
74     }
75     }
76    
77 dl 1.4 /**
78 dl 1.5 * addAll(null) throws NPE
79 dl 1.4 */
80     public void testAddAll1() {
81 dl 1.1 try {
82     SynchronousQueue q = new SynchronousQueue();
83     q.addAll(null);
84 dl 1.4 shouldThrow();
85 dl 1.1 }
86     catch (NullPointerException success) {}
87     }
88 dl 1.6
89     /**
90     * addAll(this) throws IAE
91     */
92     public void testAddAllSelf() {
93     try {
94     SynchronousQueue q = new SynchronousQueue();
95     q.addAll(q);
96     shouldThrow();
97     }
98     catch (IllegalArgumentException success) {}
99     }
100    
101 dl 1.4 /**
102 dl 1.5 * addAll of a collection with null elements throws NPE
103 dl 1.4 */
104     public void testAddAll2() {
105 dl 1.1 try {
106     SynchronousQueue q = new SynchronousQueue();
107 dl 1.3 Integer[] ints = new Integer[1];
108 dl 1.1 q.addAll(Arrays.asList(ints));
109 dl 1.4 shouldThrow();
110 dl 1.1 }
111     catch (NullPointerException success) {}
112     }
113 dl 1.4 /**
114 dl 1.5 * addAll throws ISE if no active taker
115 dl 1.4 */
116     public void testAddAll4() {
117 dl 1.1 try {
118     SynchronousQueue q = new SynchronousQueue();
119 dl 1.3 Integer[] ints = new Integer[1];
120     for (int i = 0; i < 1; ++i)
121 dl 1.1 ints[i] = new Integer(i);
122     q.addAll(Arrays.asList(ints));
123 dl 1.4 shouldThrow();
124 dl 1.1 }
125     catch (IllegalStateException success) {}
126     }
127    
128 dl 1.4 /**
129 dl 1.5 * put(null) throws NPE
130 dl 1.4 */
131 dl 1.1 public void testPutNull() {
132     try {
133     SynchronousQueue q = new SynchronousQueue();
134     q.put(null);
135 dl 1.4 shouldThrow();
136 dl 1.1 }
137     catch (NullPointerException success){
138     }
139     catch (InterruptedException ie) {
140 dl 1.4 unexpectedException();
141 dl 1.1 }
142     }
143    
144 dl 1.4 /**
145 dl 1.5 * put blocks interruptibly if no active taker
146 dl 1.4 */
147     public void testBlockingPut() {
148 dl 1.1 Thread t = new Thread(new Runnable() {
149     public void run() {
150     try {
151     SynchronousQueue q = new SynchronousQueue();
152 dl 1.5 q.put(zero);
153 dl 1.4 threadShouldThrow();
154 dl 1.1 } catch (InterruptedException ie){
155     }
156     }});
157     t.start();
158     try {
159     Thread.sleep(SHORT_DELAY_MS);
160     t.interrupt();
161     t.join();
162     }
163     catch (InterruptedException ie) {
164 dl 1.4 unexpectedException();
165 dl 1.1 }
166     }
167    
168 dl 1.4 /**
169 dl 1.5 * put blocks waiting for take
170 dl 1.4 */
171 dl 1.1 public void testPutWithTake() {
172     final SynchronousQueue q = new SynchronousQueue();
173     Thread t = new Thread(new Runnable() {
174 dl 1.4 public void run() {
175 dl 1.1 int added = 0;
176     try {
177     q.put(new Object());
178     ++added;
179     q.put(new Object());
180     ++added;
181     q.put(new Object());
182     ++added;
183     q.put(new Object());
184     ++added;
185 dl 1.4 threadShouldThrow();
186 dl 1.1 } catch (InterruptedException e){
187     assertTrue(added >= 1);
188     }
189     }
190     });
191     try {
192     t.start();
193     Thread.sleep(SHORT_DELAY_MS);
194     q.take();
195     Thread.sleep(SHORT_DELAY_MS);
196     t.interrupt();
197     t.join();
198     } catch (Exception e){
199 dl 1.4 unexpectedException();
200 dl 1.1 }
201     }
202    
203 dl 1.4 /**
204 dl 1.5 * timed offer times out if elements not taken
205 dl 1.4 */
206 dl 1.1 public void testTimedOffer() {
207     final SynchronousQueue q = new SynchronousQueue();
208     Thread t = new Thread(new Runnable() {
209 dl 1.4 public void run() {
210 dl 1.1 try {
211    
212 dl 1.3 threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
213 dl 1.1 q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
214 dl 1.4 threadShouldThrow();
215 dl 1.1 } catch (InterruptedException success){}
216     }
217     });
218    
219     try {
220     t.start();
221 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
222 dl 1.1 t.interrupt();
223     t.join();
224     } catch (Exception e){
225 dl 1.4 unexpectedException();
226 dl 1.1 }
227     }
228    
229    
230 dl 1.4 /**
231 dl 1.5 * take blocks interruptibly when empty
232 dl 1.4 */
233 dl 1.1 public void testTakeFromEmpty() {
234     final SynchronousQueue q = new SynchronousQueue();
235     Thread t = new Thread(new Runnable() {
236 dl 1.4 public void run() {
237 dl 1.1 try {
238     q.take();
239 dl 1.4 threadShouldThrow();
240 dl 1.1 } catch (InterruptedException success){ }
241     }
242     });
243     try {
244     t.start();
245     Thread.sleep(SHORT_DELAY_MS);
246     t.interrupt();
247     t.join();
248     } catch (Exception e){
249 dl 1.4 unexpectedException();
250 dl 1.1 }
251     }
252    
253 dl 1.4 /**
254 dl 1.5 * poll fails unless active taker
255 dl 1.4 */
256     public void testPoll() {
257 dl 1.1 SynchronousQueue q = new SynchronousQueue();
258     assertNull(q.poll());
259     }
260    
261 dl 1.4 /**
262 dl 1.5 * timed pool with zero timeout times out if no active taker
263 dl 1.4 */
264 dl 1.1 public void testTimedPoll0() {
265     try {
266     SynchronousQueue q = new SynchronousQueue();
267     assertNull(q.poll(0, TimeUnit.MILLISECONDS));
268     } catch (InterruptedException e){
269 dl 1.4 unexpectedException();
270 dl 1.1 }
271     }
272    
273 dl 1.4 /**
274 dl 1.5 * timed pool with nonzero timeout times out if no active taker
275 dl 1.4 */
276 dl 1.1 public void testTimedPoll() {
277     try {
278     SynchronousQueue q = new SynchronousQueue();
279     assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
280     } catch (InterruptedException e){
281 dl 1.4 unexpectedException();
282 dl 1.1 }
283     }
284    
285 dl 1.4 /**
286 dl 1.5 * Interrupted timed poll throws InterruptedException instead of
287     * returning timeout status
288 dl 1.4 */
289     public void testInterruptedTimedPoll() {
290 dl 1.1 Thread t = new Thread(new Runnable() {
291     public void run() {
292     try {
293     SynchronousQueue q = new SynchronousQueue();
294     assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
295     } catch (InterruptedException success){
296     }
297     }});
298     t.start();
299     try {
300     Thread.sleep(SHORT_DELAY_MS);
301     t.interrupt();
302     t.join();
303     }
304     catch (InterruptedException ie) {
305 dl 1.4 unexpectedException();
306 dl 1.1 }
307     }
308    
309 dl 1.4 /**
310 dl 1.5 * timed poll before a delayed offer fails; after offer succeeds;
311     * on interruption throws
312 dl 1.4 */
313     public void testTimedPollWithOffer() {
314 dl 1.1 final SynchronousQueue q = new SynchronousQueue();
315     Thread t = new Thread(new Runnable() {
316 dl 1.4 public void run() {
317 dl 1.1 try {
318 dl 1.3 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
319 dl 1.1 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
320     q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
321 dl 1.4 threadShouldThrow();
322 dl 1.1 } catch (InterruptedException success) { }
323     }
324     });
325     try {
326     t.start();
327 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
328 dl 1.5 assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
329 dl 1.1 t.interrupt();
330     t.join();
331     } catch (Exception e){
332 dl 1.4 unexpectedException();
333 dl 1.1 }
334     }
335    
336    
337 dl 1.4 /**
338 dl 1.5 * peek returns null
339 dl 1.4 */
340     public void testPeek() {
341 dl 1.1 SynchronousQueue q = new SynchronousQueue();
342     assertNull(q.peek());
343     }
344    
345 dl 1.4 /**
346 dl 1.5 * element throws NSEE
347 dl 1.4 */
348     public void testElement() {
349 dl 1.1 SynchronousQueue q = new SynchronousQueue();
350     try {
351     q.element();
352 dl 1.4 shouldThrow();
353 dl 1.1 }
354     catch (NoSuchElementException success) {}
355     }
356    
357 dl 1.4 /**
358 dl 1.5 * remove throws NSEE if no active taker
359 dl 1.4 */
360     public void testRemove() {
361 dl 1.1 SynchronousQueue q = new SynchronousQueue();
362     try {
363     q.remove();
364 dl 1.4 shouldThrow();
365 dl 1.1 } catch (NoSuchElementException success){
366     }
367     }
368    
369 dl 1.4 /**
370 dl 1.5 * remove(x) returns false
371 dl 1.4 */
372     public void testRemoveElement() {
373 dl 1.1 SynchronousQueue q = new SynchronousQueue();
374 dl 1.5 assertFalse(q.remove(zero));
375 dl 1.2 assertTrue(q.isEmpty());
376 dl 1.1 }
377    
378 dl 1.4 /**
379 dl 1.5 * contains returns false
380 dl 1.4 */
381     public void testContains() {
382 dl 1.1 SynchronousQueue q = new SynchronousQueue();
383 dl 1.5 assertFalse(q.contains(zero));
384 dl 1.1 }
385    
386 dl 1.4 /**
387 dl 1.5 * clear ensures isEmpty
388 dl 1.4 */
389     public void testClear() {
390 dl 1.1 SynchronousQueue q = new SynchronousQueue();
391     q.clear();
392     assertTrue(q.isEmpty());
393     }
394    
395 dl 1.4 /**
396 dl 1.5 * containsAll returns false unless empty
397 dl 1.4 */
398     public void testContainsAll() {
399 dl 1.1 SynchronousQueue q = new SynchronousQueue();
400     Integer[] empty = new Integer[0];
401 dl 1.5 assertTrue(q.containsAll(Arrays.asList(empty)));
402     Integer[] ints = new Integer[1]; ints[0] = zero;
403 dl 1.1 assertFalse(q.containsAll(Arrays.asList(ints)));
404     }
405    
406 dl 1.4 /**
407 dl 1.5 * retainAll returns false
408 dl 1.4 */
409     public void testRetainAll() {
410 dl 1.1 SynchronousQueue q = new SynchronousQueue();
411     Integer[] empty = new Integer[0];
412 dl 1.5 assertFalse(q.retainAll(Arrays.asList(empty)));
413     Integer[] ints = new Integer[1]; ints[0] = zero;
414     assertFalse(q.retainAll(Arrays.asList(ints)));
415 dl 1.1 }
416    
417 dl 1.4 /**
418 dl 1.5 * removeAll returns false
419 dl 1.4 */
420     public void testRemoveAll() {
421 dl 1.1 SynchronousQueue q = new SynchronousQueue();
422     Integer[] empty = new Integer[0];
423 dl 1.5 assertFalse(q.removeAll(Arrays.asList(empty)));
424     Integer[] ints = new Integer[1]; ints[0] = zero;
425 dl 1.1 assertFalse(q.containsAll(Arrays.asList(ints)));
426     }
427    
428    
429 dl 1.4 /**
430 dl 1.5 * toArray is empty
431 dl 1.4 */
432     public void testToArray() {
433 dl 1.1 SynchronousQueue q = new SynchronousQueue();
434     Object[] o = q.toArray();
435     assertEquals(o.length, 0);
436     }
437    
438 dl 1.4 /**
439 dl 1.5 * toArray(a) is nulled at position 0
440 dl 1.4 */
441     public void testToArray2() {
442 dl 1.1 SynchronousQueue q = new SynchronousQueue();
443     Integer[] ints = new Integer[1];
444     assertNull(ints[0]);
445     }
446    
447 dl 1.4 /**
448 dl 1.6 * toArray(null) throws NPE
449     */
450     public void testToArray_BadArg() {
451     try {
452     SynchronousQueue q = new SynchronousQueue();
453     Object o[] = q.toArray(null);
454     shouldThrow();
455     } catch(NullPointerException success){}
456     }
457    
458    
459     /**
460 dl 1.5 * iterator does not traverse any elements
461 dl 1.4 */
462     public void testIterator() {
463 dl 1.1 SynchronousQueue q = new SynchronousQueue();
464     Iterator it = q.iterator();
465     assertFalse(it.hasNext());
466     try {
467     Object x = it.next();
468 dl 1.4 shouldThrow();
469 dl 1.1 }
470     catch (NoSuchElementException success) {}
471     }
472    
473 dl 1.4 /**
474 dl 1.5 * iterator remove throws ISE
475 dl 1.4 */
476     public void testIteratorRemove() {
477 dl 1.1 SynchronousQueue q = new SynchronousQueue();
478     Iterator it = q.iterator();
479     try {
480     it.remove();
481 dl 1.4 shouldThrow();
482 dl 1.1 }
483     catch (IllegalStateException success) {}
484     }
485    
486 dl 1.4 /**
487 dl 1.5 * toString returns a non-null string
488 dl 1.4 */
489     public void testToString() {
490 dl 1.1 SynchronousQueue q = new SynchronousQueue();
491     String s = q.toString();
492 dl 1.5 assertNotNull(s);
493 dl 1.1 }
494    
495    
496 dl 1.4 /**
497 dl 1.5 * offer transfers elements across Executor tasks
498 dl 1.4 */
499 dl 1.1 public void testOfferInExecutor() {
500     final SynchronousQueue q = new SynchronousQueue();
501     ExecutorService executor = Executors.newFixedThreadPool(2);
502     final Integer one = new Integer(1);
503    
504     executor.execute(new Runnable() {
505     public void run() {
506 dl 1.3 threadAssertFalse(q.offer(one));
507 dl 1.1 try {
508 dl 1.3 threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
509     threadAssertEquals(0, q.remainingCapacity());
510 dl 1.1 }
511     catch (InterruptedException e) {
512 dl 1.4 threadUnexpectedException();
513 dl 1.1 }
514     }
515     });
516    
517     executor.execute(new Runnable() {
518     public void run() {
519     try {
520 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
521     threadAssertEquals(one, q.take());
522 dl 1.1 }
523     catch (InterruptedException e) {
524 dl 1.4 threadUnexpectedException();
525 dl 1.1 }
526     }
527     });
528    
529 dl 1.3 joinPool(executor);
530 dl 1.1
531     }
532    
533 dl 1.4 /**
534 dl 1.5 * poll retrieves elements across Executor threads
535 dl 1.4 */
536 dl 1.1 public void testPollInExecutor() {
537     final SynchronousQueue q = new SynchronousQueue();
538     ExecutorService executor = Executors.newFixedThreadPool(2);
539     executor.execute(new Runnable() {
540     public void run() {
541 dl 1.3 threadAssertNull(q.poll());
542 dl 1.1 try {
543 dl 1.3 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
544     threadAssertTrue(q.isEmpty());
545 dl 1.1 }
546     catch (InterruptedException e) {
547 dl 1.4 threadUnexpectedException();
548 dl 1.1 }
549     }
550     });
551    
552     executor.execute(new Runnable() {
553     public void run() {
554     try {
555 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
556 dl 1.1 q.put(new Integer(1));
557     }
558     catch (InterruptedException e) {
559 dl 1.4 threadUnexpectedException();
560 dl 1.1 }
561     }
562     });
563    
564 dl 1.3 joinPool(executor);
565 dl 1.2 }
566    
567 dl 1.4 /**
568 dl 1.5 * a deserialized serialized queue is usable
569 dl 1.4 */
570 dl 1.2 public void testSerialization() {
571     SynchronousQueue q = new SynchronousQueue();
572     try {
573     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
574     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
575     out.writeObject(q);
576     out.close();
577    
578     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
579     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
580     SynchronousQueue r = (SynchronousQueue)in.readObject();
581     assertEquals(q.size(), r.size());
582     while (!q.isEmpty())
583     assertEquals(q.remove(), r.remove());
584     } catch(Exception e){
585 dl 1.4 unexpectedException();
586 dl 1.2 }
587 dl 1.1 }
588 dl 1.6
589     /**
590     * drainTo(null) throws NPE
591     */
592     public void testDrainToNull() {
593     SynchronousQueue q = new SynchronousQueue();
594     try {
595     q.drainTo(null);
596     shouldThrow();
597     } catch(NullPointerException success) {
598     }
599     }
600    
601     /**
602     * drainTo(this) throws IAE
603     */
604     public void testDrainToSelf() {
605     SynchronousQueue q = new SynchronousQueue();
606     try {
607     q.drainTo(q);
608     shouldThrow();
609     } catch(IllegalArgumentException success) {
610     }
611     }
612    
613     /**
614     * drainTo(c) of empty queue doesn't transfer elements
615     */
616     public void testDrainTo() {
617     SynchronousQueue q = new SynchronousQueue();
618     ArrayList l = new ArrayList();
619     q.drainTo(l);
620     assertEquals(q.size(), 0);
621     assertEquals(l.size(), 0);
622     }
623    
624     /**
625     * drainTo empties queue, unblocking a waiting put.
626     */
627     public void testDrainToWithActivePut() {
628     final SynchronousQueue q = new SynchronousQueue();
629     Thread t = new Thread(new Runnable() {
630     public void run() {
631     try {
632     q.put(new Integer(1));
633     } catch (InterruptedException ie){
634     threadUnexpectedException();
635     }
636     }
637     });
638     try {
639     t.start();
640     ArrayList l = new ArrayList();
641     Thread.sleep(SHORT_DELAY_MS);
642     q.drainTo(l);
643     assertTrue(l.size() <= 1);
644     if (l.size() > 0)
645     assertEquals(l.get(0), new Integer(1));
646     t.join();
647     assertTrue(l.size() <= 1);
648     } catch(Exception e){
649     unexpectedException();
650     }
651     }
652    
653     /**
654     * drainTo(null, n) throws NPE
655     */
656     public void testDrainToNullN() {
657     SynchronousQueue q = new SynchronousQueue();
658     try {
659     q.drainTo(null, 0);
660     shouldThrow();
661     } catch(NullPointerException success) {
662     }
663     }
664    
665     /**
666     * drainTo(this, n) throws IAE
667     */
668     public void testDrainToSelfN() {
669     SynchronousQueue q = new SynchronousQueue();
670     try {
671     q.drainTo(q, 0);
672     shouldThrow();
673     } catch(IllegalArgumentException success) {
674     }
675     }
676    
677     /**
678     * drainTo(c, n) empties up to n elements of queue into c
679     */
680     public void testDrainToN() {
681     final SynchronousQueue q = new SynchronousQueue();
682     Thread t1 = new Thread(new Runnable() {
683     public void run() {
684     try {
685     q.put(one);
686     } catch (InterruptedException ie){
687     threadUnexpectedException();
688     }
689     }
690     });
691     Thread t2 = new Thread(new Runnable() {
692     public void run() {
693     try {
694     q.put(two);
695     } catch (InterruptedException ie){
696     threadUnexpectedException();
697     }
698     }
699     });
700    
701     try {
702     t1.start();
703     t2.start();
704     ArrayList l = new ArrayList();
705     Thread.sleep(SHORT_DELAY_MS);
706     q.drainTo(l, 1);
707     assertTrue(l.size() == 1);
708     q.drainTo(l, 1);
709     assertTrue(l.size() == 2);
710     assertTrue(l.contains(one));
711     assertTrue(l.contains(two));
712     t1.join();
713     t2.join();
714     } catch(Exception e){
715     unexpectedException();
716     }
717     }
718    
719 dl 1.1
720     }