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

# 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 SynchronousQueueTest extends JSR166TestCase {
14
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 /**
24 * A SynchronousQueue is both empty and full
25 */
26 public void testEmptyFull() {
27 SynchronousQueue q = new SynchronousQueue();
28 assertTrue(q.isEmpty());
29 assertEquals(0, q.size());
30 assertEquals(0, q.remainingCapacity());
31 assertFalse(q.offer(zero));
32 }
33
34 /**
35 * offer(null) throws NPE
36 */
37 public void testOfferNull() {
38 try {
39 SynchronousQueue q = new SynchronousQueue();
40 q.offer(null);
41 shouldThrow();
42 } catch (NullPointerException success) { }
43 }
44
45 /**
46 * 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 * offer fails if no active taker
58 */
59 public void testOffer() {
60 SynchronousQueue q = new SynchronousQueue();
61 assertFalse(q.offer(one));
62 }
63
64 /**
65 * add throws ISE if no active taker
66 */
67 public void testAdd() {
68 try {
69 SynchronousQueue q = new SynchronousQueue();
70 assertEquals(0, q.remainingCapacity());
71 q.add(one);
72 shouldThrow();
73 } catch (IllegalStateException success){
74 }
75 }
76
77 /**
78 * addAll(null) throws NPE
79 */
80 public void testAddAll1() {
81 try {
82 SynchronousQueue q = new SynchronousQueue();
83 q.addAll(null);
84 shouldThrow();
85 }
86 catch (NullPointerException success) {}
87 }
88
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 /**
102 * addAll of a collection with null elements throws NPE
103 */
104 public void testAddAll2() {
105 try {
106 SynchronousQueue q = new SynchronousQueue();
107 Integer[] ints = new Integer[1];
108 q.addAll(Arrays.asList(ints));
109 shouldThrow();
110 }
111 catch (NullPointerException success) {}
112 }
113 /**
114 * addAll throws ISE if no active taker
115 */
116 public void testAddAll4() {
117 try {
118 SynchronousQueue q = new SynchronousQueue();
119 Integer[] ints = new Integer[1];
120 for (int i = 0; i < 1; ++i)
121 ints[i] = new Integer(i);
122 q.addAll(Arrays.asList(ints));
123 shouldThrow();
124 }
125 catch (IllegalStateException success) {}
126 }
127
128 /**
129 * put(null) throws NPE
130 */
131 public void testPutNull() {
132 try {
133 SynchronousQueue q = new SynchronousQueue();
134 q.put(null);
135 shouldThrow();
136 }
137 catch (NullPointerException success){
138 }
139 catch (InterruptedException ie) {
140 unexpectedException();
141 }
142 }
143
144 /**
145 * put blocks interruptibly if no active taker
146 */
147 public void testBlockingPut() {
148 Thread t = new Thread(new Runnable() {
149 public void run() {
150 try {
151 SynchronousQueue q = new SynchronousQueue();
152 q.put(zero);
153 threadShouldThrow();
154 } 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 unexpectedException();
165 }
166 }
167
168 /**
169 * put blocks waiting for take
170 */
171 public void testPutWithTake() {
172 final SynchronousQueue q = new SynchronousQueue();
173 Thread t = new Thread(new Runnable() {
174 public void run() {
175 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 threadShouldThrow();
186 } 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 unexpectedException();
200 }
201 }
202
203 /**
204 * timed offer times out if elements not taken
205 */
206 public void testTimedOffer() {
207 final SynchronousQueue q = new SynchronousQueue();
208 Thread t = new Thread(new Runnable() {
209 public void run() {
210 try {
211
212 threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
213 q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
214 threadShouldThrow();
215 } catch (InterruptedException success){}
216 }
217 });
218
219 try {
220 t.start();
221 Thread.sleep(SMALL_DELAY_MS);
222 t.interrupt();
223 t.join();
224 } catch (Exception e){
225 unexpectedException();
226 }
227 }
228
229
230 /**
231 * take blocks interruptibly when empty
232 */
233 public void testTakeFromEmpty() {
234 final SynchronousQueue q = new SynchronousQueue();
235 Thread t = new Thread(new Runnable() {
236 public void run() {
237 try {
238 q.take();
239 threadShouldThrow();
240 } 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 unexpectedException();
250 }
251 }
252
253 /**
254 * poll fails unless active taker
255 */
256 public void testPoll() {
257 SynchronousQueue q = new SynchronousQueue();
258 assertNull(q.poll());
259 }
260
261 /**
262 * timed pool with zero timeout times out if no active taker
263 */
264 public void testTimedPoll0() {
265 try {
266 SynchronousQueue q = new SynchronousQueue();
267 assertNull(q.poll(0, TimeUnit.MILLISECONDS));
268 } catch (InterruptedException e){
269 unexpectedException();
270 }
271 }
272
273 /**
274 * timed pool with nonzero timeout times out if no active taker
275 */
276 public void testTimedPoll() {
277 try {
278 SynchronousQueue q = new SynchronousQueue();
279 assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
280 } catch (InterruptedException e){
281 unexpectedException();
282 }
283 }
284
285 /**
286 * Interrupted timed poll throws InterruptedException instead of
287 * returning timeout status
288 */
289 public void testInterruptedTimedPoll() {
290 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 unexpectedException();
306 }
307 }
308
309 /**
310 * timed poll before a delayed offer fails; after offer succeeds;
311 * on interruption throws
312 */
313 public void testTimedPollWithOffer() {
314 final SynchronousQueue q = new SynchronousQueue();
315 Thread t = new Thread(new Runnable() {
316 public void run() {
317 try {
318 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
319 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
320 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
321 threadShouldThrow();
322 } catch (InterruptedException success) { }
323 }
324 });
325 try {
326 t.start();
327 Thread.sleep(SMALL_DELAY_MS);
328 assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
329 t.interrupt();
330 t.join();
331 } catch (Exception e){
332 unexpectedException();
333 }
334 }
335
336
337 /**
338 * peek returns null
339 */
340 public void testPeek() {
341 SynchronousQueue q = new SynchronousQueue();
342 assertNull(q.peek());
343 }
344
345 /**
346 * element throws NSEE
347 */
348 public void testElement() {
349 SynchronousQueue q = new SynchronousQueue();
350 try {
351 q.element();
352 shouldThrow();
353 }
354 catch (NoSuchElementException success) {}
355 }
356
357 /**
358 * remove throws NSEE if no active taker
359 */
360 public void testRemove() {
361 SynchronousQueue q = new SynchronousQueue();
362 try {
363 q.remove();
364 shouldThrow();
365 } catch (NoSuchElementException success){
366 }
367 }
368
369 /**
370 * remove(x) returns false
371 */
372 public void testRemoveElement() {
373 SynchronousQueue q = new SynchronousQueue();
374 assertFalse(q.remove(zero));
375 assertTrue(q.isEmpty());
376 }
377
378 /**
379 * contains returns false
380 */
381 public void testContains() {
382 SynchronousQueue q = new SynchronousQueue();
383 assertFalse(q.contains(zero));
384 }
385
386 /**
387 * clear ensures isEmpty
388 */
389 public void testClear() {
390 SynchronousQueue q = new SynchronousQueue();
391 q.clear();
392 assertTrue(q.isEmpty());
393 }
394
395 /**
396 * containsAll returns false unless empty
397 */
398 public void testContainsAll() {
399 SynchronousQueue q = new SynchronousQueue();
400 Integer[] empty = new Integer[0];
401 assertTrue(q.containsAll(Arrays.asList(empty)));
402 Integer[] ints = new Integer[1]; ints[0] = zero;
403 assertFalse(q.containsAll(Arrays.asList(ints)));
404 }
405
406 /**
407 * retainAll returns false
408 */
409 public void testRetainAll() {
410 SynchronousQueue q = new SynchronousQueue();
411 Integer[] empty = new Integer[0];
412 assertFalse(q.retainAll(Arrays.asList(empty)));
413 Integer[] ints = new Integer[1]; ints[0] = zero;
414 assertFalse(q.retainAll(Arrays.asList(ints)));
415 }
416
417 /**
418 * removeAll returns false
419 */
420 public void testRemoveAll() {
421 SynchronousQueue q = new SynchronousQueue();
422 Integer[] empty = new Integer[0];
423 assertFalse(q.removeAll(Arrays.asList(empty)));
424 Integer[] ints = new Integer[1]; ints[0] = zero;
425 assertFalse(q.containsAll(Arrays.asList(ints)));
426 }
427
428
429 /**
430 * toArray is empty
431 */
432 public void testToArray() {
433 SynchronousQueue q = new SynchronousQueue();
434 Object[] o = q.toArray();
435 assertEquals(o.length, 0);
436 }
437
438 /**
439 * toArray(a) is nulled at position 0
440 */
441 public void testToArray2() {
442 SynchronousQueue q = new SynchronousQueue();
443 Integer[] ints = new Integer[1];
444 assertNull(ints[0]);
445 }
446
447 /**
448 * 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 * iterator does not traverse any elements
461 */
462 public void testIterator() {
463 SynchronousQueue q = new SynchronousQueue();
464 Iterator it = q.iterator();
465 assertFalse(it.hasNext());
466 try {
467 Object x = it.next();
468 shouldThrow();
469 }
470 catch (NoSuchElementException success) {}
471 }
472
473 /**
474 * iterator remove throws ISE
475 */
476 public void testIteratorRemove() {
477 SynchronousQueue q = new SynchronousQueue();
478 Iterator it = q.iterator();
479 try {
480 it.remove();
481 shouldThrow();
482 }
483 catch (IllegalStateException success) {}
484 }
485
486 /**
487 * toString returns a non-null string
488 */
489 public void testToString() {
490 SynchronousQueue q = new SynchronousQueue();
491 String s = q.toString();
492 assertNotNull(s);
493 }
494
495
496 /**
497 * offer transfers elements across Executor tasks
498 */
499 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 threadAssertFalse(q.offer(one));
507 try {
508 threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
509 threadAssertEquals(0, q.remainingCapacity());
510 }
511 catch (InterruptedException e) {
512 threadUnexpectedException();
513 }
514 }
515 });
516
517 executor.execute(new Runnable() {
518 public void run() {
519 try {
520 Thread.sleep(SMALL_DELAY_MS);
521 threadAssertEquals(one, q.take());
522 }
523 catch (InterruptedException e) {
524 threadUnexpectedException();
525 }
526 }
527 });
528
529 joinPool(executor);
530
531 }
532
533 /**
534 * poll retrieves elements across Executor threads
535 */
536 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 threadAssertNull(q.poll());
542 try {
543 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
544 threadAssertTrue(q.isEmpty());
545 }
546 catch (InterruptedException e) {
547 threadUnexpectedException();
548 }
549 }
550 });
551
552 executor.execute(new Runnable() {
553 public void run() {
554 try {
555 Thread.sleep(SMALL_DELAY_MS);
556 q.put(new Integer(1));
557 }
558 catch (InterruptedException e) {
559 threadUnexpectedException();
560 }
561 }
562 });
563
564 joinPool(executor);
565 }
566
567 /**
568 * a deserialized serialized queue is usable
569 */
570 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 unexpectedException();
586 }
587 }
588
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
720 }