ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SynchronousQueueTest.java
Revision: 1.7
Committed: Sat Dec 27 19:26:44 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.6: +5 -4 lines
Log Message:
Headers reference Creative Commons

File Contents

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