21 |
|
} |
22 |
|
|
23 |
|
/** |
24 |
< |
* |
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(new Integer(3))); |
31 |
> |
assertFalse(q.offer(zero)); |
32 |
|
} |
33 |
|
|
34 |
|
/** |
35 |
< |
* |
35 |
> |
* offer(null) throws NPE |
36 |
|
*/ |
37 |
|
public void testOfferNull() { |
38 |
|
try { |
43 |
|
} |
44 |
|
|
45 |
|
/** |
46 |
< |
* |
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(new Integer(1))); |
61 |
> |
assertFalse(q.offer(one)); |
62 |
|
} |
63 |
|
|
64 |
|
/** |
65 |
< |
* |
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(new Integer(0)); |
71 |
> |
q.add(one); |
72 |
> |
shouldThrow(); |
73 |
|
} catch (IllegalStateException success){ |
74 |
|
} |
75 |
|
} |
76 |
|
|
77 |
|
/** |
78 |
< |
* |
78 |
> |
* addAll(null) throws NPE |
79 |
|
*/ |
80 |
|
public void testAddAll1() { |
81 |
|
try { |
85 |
|
} |
86 |
|
catch (NullPointerException success) {} |
87 |
|
} |
88 |
+ |
|
89 |
|
/** |
90 |
< |
* |
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 { |
111 |
|
catch (NullPointerException success) {} |
112 |
|
} |
113 |
|
/** |
114 |
< |
* |
114 |
> |
* addAll throws ISE if no active taker |
115 |
|
*/ |
116 |
|
public void testAddAll4() { |
117 |
|
try { |
126 |
|
} |
127 |
|
|
128 |
|
/** |
129 |
< |
* |
129 |
> |
* put(null) throws NPE |
130 |
|
*/ |
131 |
|
public void testPutNull() { |
132 |
|
try { |
142 |
|
} |
143 |
|
|
144 |
|
/** |
145 |
< |
* |
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(new Integer(0)); |
152 |
> |
q.put(zero); |
153 |
|
threadShouldThrow(); |
154 |
|
} catch (InterruptedException ie){ |
155 |
|
} |
166 |
|
} |
167 |
|
|
168 |
|
/** |
169 |
< |
* |
169 |
> |
* put blocks waiting for take |
170 |
|
*/ |
171 |
|
public void testPutWithTake() { |
172 |
|
final SynchronousQueue q = new SynchronousQueue(); |
201 |
|
} |
202 |
|
|
203 |
|
/** |
204 |
< |
* |
204 |
> |
* timed offer times out if elements not taken |
205 |
|
*/ |
206 |
|
public void testTimedOffer() { |
207 |
|
final SynchronousQueue q = new SynchronousQueue(); |
228 |
|
|
229 |
|
|
230 |
|
/** |
231 |
< |
* |
231 |
> |
* take blocks interruptibly when empty |
232 |
|
*/ |
233 |
|
public void testTakeFromEmpty() { |
234 |
|
final SynchronousQueue q = new SynchronousQueue(); |
251 |
|
} |
252 |
|
|
253 |
|
/** |
254 |
< |
* |
254 |
> |
* poll fails unless active taker |
255 |
|
*/ |
256 |
|
public void testPoll() { |
257 |
|
SynchronousQueue q = new SynchronousQueue(); |
259 |
|
} |
260 |
|
|
261 |
|
/** |
262 |
< |
* |
262 |
> |
* timed pool with zero timeout times out if no active taker |
263 |
|
*/ |
264 |
|
public void testTimedPoll0() { |
265 |
|
try { |
271 |
|
} |
272 |
|
|
273 |
|
/** |
274 |
< |
* |
274 |
> |
* timed pool with nonzero timeout times out if no active taker |
275 |
|
*/ |
276 |
|
public void testTimedPoll() { |
277 |
|
try { |
283 |
|
} |
284 |
|
|
285 |
|
/** |
286 |
< |
* |
286 |
> |
* Interrupted timed poll throws InterruptedException instead of |
287 |
> |
* returning timeout status |
288 |
|
*/ |
289 |
|
public void testInterruptedTimedPoll() { |
290 |
|
Thread t = new Thread(new Runnable() { |
307 |
|
} |
308 |
|
|
309 |
|
/** |
310 |
< |
* |
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(); |
325 |
|
try { |
326 |
|
t.start(); |
327 |
|
Thread.sleep(SMALL_DELAY_MS); |
328 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
328 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
329 |
|
t.interrupt(); |
330 |
|
t.join(); |
331 |
|
} catch (Exception e){ |
335 |
|
|
336 |
|
|
337 |
|
/** |
338 |
< |
* |
338 |
> |
* peek returns null |
339 |
|
*/ |
340 |
|
public void testPeek() { |
341 |
|
SynchronousQueue q = new SynchronousQueue(); |
343 |
|
} |
344 |
|
|
345 |
|
/** |
346 |
< |
* |
346 |
> |
* element throws NSEE |
347 |
|
*/ |
348 |
|
public void testElement() { |
349 |
|
SynchronousQueue q = new SynchronousQueue(); |
355 |
|
} |
356 |
|
|
357 |
|
/** |
358 |
< |
* |
358 |
> |
* remove throws NSEE if no active taker |
359 |
|
*/ |
360 |
|
public void testRemove() { |
361 |
|
SynchronousQueue q = new SynchronousQueue(); |
367 |
|
} |
368 |
|
|
369 |
|
/** |
370 |
< |
* |
370 |
> |
* remove(x) returns false |
371 |
|
*/ |
372 |
|
public void testRemoveElement() { |
373 |
|
SynchronousQueue q = new SynchronousQueue(); |
374 |
< |
assertFalse(q.remove(new Integer(0))); |
374 |
> |
assertFalse(q.remove(zero)); |
375 |
|
assertTrue(q.isEmpty()); |
376 |
|
} |
377 |
|
|
378 |
|
/** |
379 |
< |
* |
379 |
> |
* contains returns false |
380 |
|
*/ |
381 |
|
public void testContains() { |
382 |
|
SynchronousQueue q = new SynchronousQueue(); |
383 |
< |
assertFalse(q.contains(new Integer(0))); |
383 |
> |
assertFalse(q.contains(zero)); |
384 |
|
} |
385 |
|
|
386 |
|
/** |
387 |
< |
* |
387 |
> |
* clear ensures isEmpty |
388 |
|
*/ |
389 |
|
public void testClear() { |
390 |
|
SynchronousQueue q = new SynchronousQueue(); |
393 |
|
} |
394 |
|
|
395 |
|
/** |
396 |
< |
* |
396 |
> |
* containsAll returns false unless empty |
397 |
|
*/ |
398 |
|
public void testContainsAll() { |
399 |
|
SynchronousQueue q = new SynchronousQueue(); |
400 |
|
Integer[] empty = new Integer[0]; |
401 |
< |
Integer[] ints = new Integer[1]; ints[0] = 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 |
< |
* |
407 |
> |
* retainAll returns false |
408 |
|
*/ |
409 |
|
public void testRetainAll() { |
410 |
|
SynchronousQueue q = new SynchronousQueue(); |
411 |
|
Integer[] empty = new Integer[0]; |
412 |
< |
Integer[] ints = new Integer[1]; ints[0] = new Integer(0); |
413 |
< |
q.retainAll(Arrays.asList(ints)); |
414 |
< |
assertFalse(q.containsAll(Arrays.asList(ints))); |
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 |
< |
* |
418 |
> |
* removeAll returns false |
419 |
|
*/ |
420 |
|
public void testRemoveAll() { |
421 |
|
SynchronousQueue q = new SynchronousQueue(); |
422 |
|
Integer[] empty = new Integer[0]; |
423 |
< |
Integer[] ints = new Integer[1]; ints[0] = new Integer(0); |
424 |
< |
q.removeAll(Arrays.asList(ints)); |
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 |
< |
* |
430 |
> |
* toArray is empty |
431 |
|
*/ |
432 |
|
public void testToArray() { |
433 |
|
SynchronousQueue q = new SynchronousQueue(); |
436 |
|
} |
437 |
|
|
438 |
|
/** |
439 |
< |
* |
439 |
> |
* toArray(a) is nulled at position 0 |
440 |
|
*/ |
441 |
|
public void testToArray2() { |
442 |
|
SynchronousQueue q = new SynchronousQueue(); |
445 |
|
} |
446 |
|
|
447 |
|
/** |
448 |
< |
* |
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(); |
471 |
|
} |
472 |
|
|
473 |
|
/** |
474 |
< |
* |
474 |
> |
* iterator remove throws ISE |
475 |
|
*/ |
476 |
|
public void testIteratorRemove() { |
477 |
|
SynchronousQueue q = new SynchronousQueue(); |
484 |
|
} |
485 |
|
|
486 |
|
/** |
487 |
< |
* |
487 |
> |
* toString returns a non-null string |
488 |
|
*/ |
489 |
|
public void testToString() { |
490 |
|
SynchronousQueue q = new SynchronousQueue(); |
491 |
|
String s = q.toString(); |
492 |
< |
assertTrue(s != null); |
492 |
> |
assertNotNull(s); |
493 |
|
} |
494 |
|
|
495 |
|
|
496 |
|
/** |
497 |
< |
* |
497 |
> |
* offer transfers elements across Executor tasks |
498 |
|
*/ |
499 |
|
public void testOfferInExecutor() { |
500 |
|
final SynchronousQueue q = new SynchronousQueue(); |
531 |
|
} |
532 |
|
|
533 |
|
/** |
534 |
< |
* |
534 |
> |
* poll retrieves elements across Executor threads |
535 |
|
*/ |
536 |
|
public void testPollInExecutor() { |
497 |
– |
|
537 |
|
final SynchronousQueue q = new SynchronousQueue(); |
499 |
– |
|
538 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
501 |
– |
|
539 |
|
executor.execute(new Runnable() { |
540 |
|
public void run() { |
541 |
|
threadAssertNull(q.poll()); |
562 |
|
}); |
563 |
|
|
564 |
|
joinPool(executor); |
528 |
– |
|
565 |
|
} |
566 |
|
|
567 |
|
/** |
568 |
< |
* |
568 |
> |
* a deserialized serialized queue is usable |
569 |
|
*/ |
570 |
|
public void testSerialization() { |
571 |
|
SynchronousQueue q = new SynchronousQueue(); |
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 |
|
} |