15 |
|
public class SynchronousQueueTest extends JSR166TestCase { |
16 |
|
|
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run (suite()); |
19 |
|
} |
20 |
|
|
21 |
|
public static Test suite() { |
22 |
< |
return new TestSuite(SynchronousQueueTest.class); |
22 |
> |
return new TestSuite(SynchronousQueueTest.class); |
23 |
|
} |
24 |
|
|
25 |
|
/** |
28 |
|
public void testEmptyFull() { |
29 |
|
SynchronousQueue q = new SynchronousQueue(); |
30 |
|
assertTrue(q.isEmpty()); |
31 |
< |
assertEquals(0, q.size()); |
31 |
> |
assertEquals(0, q.size()); |
32 |
|
assertEquals(0, q.remainingCapacity()); |
33 |
|
assertFalse(q.offer(zero)); |
34 |
|
} |
39 |
|
public void testFairEmptyFull() { |
40 |
|
SynchronousQueue q = new SynchronousQueue(true); |
41 |
|
assertTrue(q.isEmpty()); |
42 |
< |
assertEquals(0, q.size()); |
42 |
> |
assertEquals(0, q.size()); |
43 |
|
assertEquals(0, q.remainingCapacity()); |
44 |
|
assertFalse(q.offer(zero)); |
45 |
|
} |
48 |
|
* offer(null) throws NPE |
49 |
|
*/ |
50 |
|
public void testOfferNull() { |
51 |
< |
try { |
51 |
> |
try { |
52 |
|
SynchronousQueue q = new SynchronousQueue(); |
53 |
|
q.offer(null); |
54 |
|
shouldThrow(); |
59 |
|
* add(null) throws NPE |
60 |
|
*/ |
61 |
|
public void testAddNull() { |
62 |
< |
try { |
62 |
> |
try { |
63 |
|
SynchronousQueue q = new SynchronousQueue(); |
64 |
|
q.add(null); |
65 |
|
shouldThrow(); |
78 |
|
* add throws ISE if no active taker |
79 |
|
*/ |
80 |
|
public void testAdd() { |
81 |
< |
try { |
81 |
> |
try { |
82 |
|
SynchronousQueue q = new SynchronousQueue(); |
83 |
|
assertEquals(0, q.remainingCapacity()); |
84 |
|
q.add(one); |
137 |
|
* put(null) throws NPE |
138 |
|
*/ |
139 |
|
public void testPutNull() throws InterruptedException { |
140 |
< |
try { |
140 |
> |
try { |
141 |
|
SynchronousQueue q = new SynchronousQueue(); |
142 |
|
q.put(null); |
143 |
|
shouldThrow(); |
148 |
|
* put blocks interruptibly if no active taker |
149 |
|
*/ |
150 |
|
public void testBlockingPut() throws InterruptedException { |
151 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
152 |
< |
public void realRun() throws InterruptedException { |
151 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
152 |
> |
public void realRun() throws InterruptedException { |
153 |
|
SynchronousQueue q = new SynchronousQueue(); |
154 |
|
q.put(zero); |
155 |
|
}}); |
165 |
|
*/ |
166 |
|
public void testPutWithTake() throws InterruptedException { |
167 |
|
final SynchronousQueue q = new SynchronousQueue(); |
168 |
< |
Thread t = new Thread(new CheckedRunnable() { |
169 |
< |
public void realRun() throws InterruptedException { |
168 |
> |
Thread t = new Thread(new CheckedRunnable() { |
169 |
> |
public void realRun() throws InterruptedException { |
170 |
|
int added = 0; |
171 |
|
try { |
172 |
|
q.put(new Object()); |
196 |
|
*/ |
197 |
|
public void testTimedOffer() throws InterruptedException { |
198 |
|
final SynchronousQueue q = new SynchronousQueue(); |
199 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
200 |
< |
public void realRun() throws InterruptedException { |
199 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
200 |
> |
public void realRun() throws InterruptedException { |
201 |
|
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
202 |
|
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
203 |
|
}}); |
214 |
|
*/ |
215 |
|
public void testTakeFromEmpty() throws InterruptedException { |
216 |
|
final SynchronousQueue q = new SynchronousQueue(); |
217 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
218 |
< |
public void realRun() throws InterruptedException { |
217 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
218 |
> |
public void realRun() throws InterruptedException { |
219 |
|
q.take(); |
220 |
|
}}); |
221 |
|
|
230 |
|
* put blocks interruptibly if no active taker |
231 |
|
*/ |
232 |
|
public void testFairBlockingPut() throws InterruptedException { |
233 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
234 |
< |
public void realRun() throws InterruptedException { |
233 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
234 |
> |
public void realRun() throws InterruptedException { |
235 |
|
SynchronousQueue q = new SynchronousQueue(true); |
236 |
|
q.put(zero); |
237 |
|
}}); |
247 |
|
*/ |
248 |
|
public void testFairPutWithTake() throws InterruptedException { |
249 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
250 |
< |
Thread t = new Thread(new CheckedRunnable() { |
251 |
< |
public void realRun() throws InterruptedException { |
250 |
> |
Thread t = new Thread(new CheckedRunnable() { |
251 |
> |
public void realRun() throws InterruptedException { |
252 |
|
int added = 0; |
253 |
|
try { |
254 |
|
q.put(new Object()); |
278 |
|
*/ |
279 |
|
public void testFairTimedOffer() throws InterruptedException { |
280 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
281 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
282 |
< |
public void realRun() throws InterruptedException { |
281 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
282 |
> |
public void realRun() throws InterruptedException { |
283 |
|
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
284 |
|
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
285 |
|
}}); |
296 |
|
*/ |
297 |
|
public void testFairTakeFromEmpty() throws InterruptedException { |
298 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
299 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
300 |
< |
public void realRun() throws InterruptedException { |
299 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
300 |
> |
public void realRun() throws InterruptedException { |
301 |
|
q.take(); |
302 |
|
}}); |
303 |
|
|
312 |
|
*/ |
313 |
|
public void testPoll() { |
314 |
|
SynchronousQueue q = new SynchronousQueue(); |
315 |
< |
assertNull(q.poll()); |
315 |
> |
assertNull(q.poll()); |
316 |
|
} |
317 |
|
|
318 |
|
/** |
336 |
|
* returning timeout status |
337 |
|
*/ |
338 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
339 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
340 |
< |
public void realRun() throws InterruptedException { |
339 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
340 |
> |
public void realRun() throws InterruptedException { |
341 |
|
SynchronousQueue q = new SynchronousQueue(); |
342 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
343 |
|
}}); |
354 |
|
*/ |
355 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
356 |
|
final SynchronousQueue q = new SynchronousQueue(); |
357 |
< |
Thread t = new Thread(new CheckedRunnable() { |
358 |
< |
public void realRun() throws InterruptedException { |
357 |
> |
Thread t = new Thread(new CheckedRunnable() { |
358 |
> |
public void realRun() throws InterruptedException { |
359 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
360 |
|
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
361 |
|
try { |
376 |
|
* returning timeout status |
377 |
|
*/ |
378 |
|
public void testFairInterruptedTimedPoll() throws InterruptedException { |
379 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
380 |
< |
public void realRun() throws InterruptedException { |
379 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
380 |
> |
public void realRun() throws InterruptedException { |
381 |
|
SynchronousQueue q = new SynchronousQueue(true); |
382 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
383 |
|
}}); |
394 |
|
*/ |
395 |
|
public void testFairTimedPollWithOffer() throws InterruptedException { |
396 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
397 |
< |
Thread t = new Thread(new CheckedRunnable() { |
398 |
< |
public void realRun() throws InterruptedException { |
397 |
> |
Thread t = new Thread(new CheckedRunnable() { |
398 |
> |
public void realRun() throws InterruptedException { |
399 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
400 |
|
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
401 |
|
try { |
417 |
|
*/ |
418 |
|
public void testPeek() { |
419 |
|
SynchronousQueue q = new SynchronousQueue(); |
420 |
< |
assertNull(q.peek()); |
420 |
> |
assertNull(q.peek()); |
421 |
|
} |
422 |
|
|
423 |
|
/** |
440 |
|
q.remove(); |
441 |
|
shouldThrow(); |
442 |
|
} catch (NoSuchElementException success) { |
443 |
< |
} |
443 |
> |
} |
444 |
|
} |
445 |
|
|
446 |
|
/** |
508 |
|
*/ |
509 |
|
public void testToArray() { |
510 |
|
SynchronousQueue q = new SynchronousQueue(); |
511 |
< |
Object[] o = q.toArray(); |
511 |
> |
Object[] o = q.toArray(); |
512 |
|
assertEquals(o.length, 0); |
513 |
|
} |
514 |
|
|
517 |
|
*/ |
518 |
|
public void testToArray2() { |
519 |
|
SynchronousQueue q = new SynchronousQueue(); |
520 |
< |
Integer[] ints = new Integer[1]; |
520 |
> |
Integer[] ints = new Integer[1]; |
521 |
|
assertNull(ints[0]); |
522 |
|
} |
523 |
|
|
525 |
|
* toArray(null) throws NPE |
526 |
|
*/ |
527 |
|
public void testToArray_BadArg() { |
528 |
< |
try { |
528 |
> |
try { |
529 |
|
SynchronousQueue q = new SynchronousQueue(); |
530 |
< |
Object o[] = q.toArray(null); |
531 |
< |
shouldThrow(); |
532 |
< |
} catch (NullPointerException success) {} |
530 |
> |
Object o[] = q.toArray(null); |
531 |
> |
shouldThrow(); |
532 |
> |
} catch (NullPointerException success) {} |
533 |
|
} |
534 |
|
|
535 |
|
|
538 |
|
*/ |
539 |
|
public void testIterator() { |
540 |
|
SynchronousQueue q = new SynchronousQueue(); |
541 |
< |
Iterator it = q.iterator(); |
541 |
> |
Iterator it = q.iterator(); |
542 |
|
assertFalse(it.hasNext()); |
543 |
|
try { |
544 |
|
Object x = it.next(); |
551 |
|
*/ |
552 |
|
public void testIteratorRemove() { |
553 |
|
SynchronousQueue q = new SynchronousQueue(); |
554 |
< |
Iterator it = q.iterator(); |
554 |
> |
Iterator it = q.iterator(); |
555 |
|
try { |
556 |
|
it.remove(); |
557 |
|
shouldThrow(); |
577 |
|
final Integer one = new Integer(1); |
578 |
|
|
579 |
|
executor.execute(new CheckedRunnable() { |
580 |
< |
public void realRun() throws InterruptedException { |
580 |
> |
public void realRun() throws InterruptedException { |
581 |
|
threadAssertFalse(q.offer(one)); |
582 |
|
threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
583 |
|
threadAssertEquals(0, q.remainingCapacity()); |
584 |
|
}}); |
585 |
|
|
586 |
|
executor.execute(new CheckedRunnable() { |
587 |
< |
public void realRun() throws InterruptedException { |
587 |
> |
public void realRun() throws InterruptedException { |
588 |
|
Thread.sleep(SMALL_DELAY_MS); |
589 |
|
threadAssertEquals(one, q.take()); |
590 |
|
}}); |
600 |
|
final SynchronousQueue q = new SynchronousQueue(); |
601 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
602 |
|
executor.execute(new CheckedRunnable() { |
603 |
< |
public void realRun() throws InterruptedException { |
603 |
> |
public void realRun() throws InterruptedException { |
604 |
|
threadAssertNull(q.poll()); |
605 |
|
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
606 |
|
threadAssertTrue(q.isEmpty()); |
607 |
|
}}); |
608 |
|
|
609 |
|
executor.execute(new CheckedRunnable() { |
610 |
< |
public void realRun() throws InterruptedException { |
610 |
> |
public void realRun() throws InterruptedException { |
611 |
|
Thread.sleep(SMALL_DELAY_MS); |
612 |
|
q.put(new Integer(1)); |
613 |
|
}}); |
671 |
|
*/ |
672 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
673 |
|
final SynchronousQueue q = new SynchronousQueue(); |
674 |
< |
Thread t = new Thread(new CheckedRunnable() { |
675 |
< |
public void realRun() throws InterruptedException { |
674 |
> |
Thread t = new Thread(new CheckedRunnable() { |
675 |
> |
public void realRun() throws InterruptedException { |
676 |
|
q.put(new Integer(1)); |
677 |
|
}}); |
678 |
|
|
715 |
|
*/ |
716 |
|
public void testDrainToN() throws InterruptedException { |
717 |
|
final SynchronousQueue q = new SynchronousQueue(); |
718 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
719 |
< |
public void realRun() throws InterruptedException { |
718 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
719 |
> |
public void realRun() throws InterruptedException { |
720 |
|
q.put(one); |
721 |
|
}}); |
722 |
|
|
723 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
724 |
< |
public void realRun() throws InterruptedException { |
723 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
724 |
> |
public void realRun() throws InterruptedException { |
725 |
|
q.put(two); |
726 |
|
}}); |
727 |
|
|