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

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