14 |
|
|
15 |
|
public class SynchronousQueueTest extends JSR166TestCase { |
16 |
|
|
17 |
+ |
public static class Fair extends BlockingQueueTest { |
18 |
+ |
protected BlockingQueue emptyCollection() { |
19 |
+ |
return new SynchronousQueue(true); |
20 |
+ |
} |
21 |
+ |
} |
22 |
+ |
|
23 |
+ |
public static class NonFair extends BlockingQueueTest { |
24 |
+ |
protected BlockingQueue emptyCollection() { |
25 |
+ |
return new SynchronousQueue(false); |
26 |
+ |
} |
27 |
+ |
} |
28 |
+ |
|
29 |
|
public static void main(String[] args) { |
30 |
< |
junit.textui.TestRunner.run (suite()); |
30 |
> |
junit.textui.TestRunner.run(suite()); |
31 |
|
} |
32 |
|
|
33 |
|
public static Test suite() { |
34 |
< |
return new TestSuite(SynchronousQueueTest.class); |
34 |
> |
return newTestSuite(SynchronousQueueTest.class, |
35 |
> |
new Fair().testSuite(), |
36 |
> |
new NonFair().testSuite()); |
37 |
|
} |
38 |
|
|
39 |
|
/** |
42 |
|
public void testEmptyFull() { |
43 |
|
SynchronousQueue q = new SynchronousQueue(); |
44 |
|
assertTrue(q.isEmpty()); |
45 |
< |
assertEquals(0, q.size()); |
45 |
> |
assertEquals(0, q.size()); |
46 |
|
assertEquals(0, q.remainingCapacity()); |
47 |
|
assertFalse(q.offer(zero)); |
48 |
|
} |
53 |
|
public void testFairEmptyFull() { |
54 |
|
SynchronousQueue q = new SynchronousQueue(true); |
55 |
|
assertTrue(q.isEmpty()); |
56 |
< |
assertEquals(0, q.size()); |
56 |
> |
assertEquals(0, q.size()); |
57 |
|
assertEquals(0, q.remainingCapacity()); |
58 |
|
assertFalse(q.offer(zero)); |
59 |
|
} |
62 |
|
* offer(null) throws NPE |
63 |
|
*/ |
64 |
|
public void testOfferNull() { |
65 |
< |
try { |
65 |
> |
try { |
66 |
|
SynchronousQueue q = new SynchronousQueue(); |
67 |
|
q.offer(null); |
68 |
|
shouldThrow(); |
73 |
|
* add(null) throws NPE |
74 |
|
*/ |
75 |
|
public void testAddNull() { |
76 |
< |
try { |
76 |
> |
try { |
77 |
|
SynchronousQueue q = new SynchronousQueue(); |
78 |
|
q.add(null); |
79 |
|
shouldThrow(); |
92 |
|
* add throws ISE if no active taker |
93 |
|
*/ |
94 |
|
public void testAdd() { |
95 |
< |
try { |
95 |
> |
try { |
96 |
|
SynchronousQueue q = new SynchronousQueue(); |
97 |
|
assertEquals(0, q.remainingCapacity()); |
98 |
|
q.add(one); |
133 |
|
shouldThrow(); |
134 |
|
} catch (NullPointerException success) {} |
135 |
|
} |
136 |
+ |
|
137 |
|
/** |
138 |
|
* addAll throws ISE if no active taker |
139 |
|
*/ |
152 |
|
* put(null) throws NPE |
153 |
|
*/ |
154 |
|
public void testPutNull() throws InterruptedException { |
155 |
< |
try { |
155 |
> |
try { |
156 |
|
SynchronousQueue q = new SynchronousQueue(); |
157 |
|
q.put(null); |
158 |
|
shouldThrow(); |
163 |
|
* put blocks interruptibly if no active taker |
164 |
|
*/ |
165 |
|
public void testBlockingPut() throws InterruptedException { |
166 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
167 |
< |
public void realRun() throws InterruptedException { |
166 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
167 |
> |
public void realRun() throws InterruptedException { |
168 |
|
SynchronousQueue q = new SynchronousQueue(); |
169 |
|
q.put(zero); |
170 |
|
}}); |
180 |
|
*/ |
181 |
|
public void testPutWithTake() throws InterruptedException { |
182 |
|
final SynchronousQueue q = new SynchronousQueue(); |
183 |
< |
Thread t = new Thread(new CheckedRunnable() { |
184 |
< |
public void realRun() throws InterruptedException { |
183 |
> |
Thread t = new Thread(new CheckedRunnable() { |
184 |
> |
public void realRun() throws InterruptedException { |
185 |
|
int added = 0; |
186 |
|
try { |
187 |
< |
q.put(new Object()); |
188 |
< |
++added; |
189 |
< |
q.put(new Object()); |
190 |
< |
++added; |
176 |
< |
q.put(new Object()); |
177 |
< |
++added; |
178 |
< |
q.put(new Object()); |
179 |
< |
++added; |
180 |
< |
threadShouldThrow(); |
187 |
> |
while (true) { |
188 |
> |
q.put(added); |
189 |
> |
++added; |
190 |
> |
} |
191 |
|
} catch (InterruptedException success) { |
192 |
< |
assertTrue(added >= 1); |
192 |
> |
assertEquals(1, added); |
193 |
|
} |
194 |
|
}}); |
195 |
|
|
196 |
|
t.start(); |
197 |
|
Thread.sleep(SHORT_DELAY_MS); |
198 |
< |
q.take(); |
198 |
> |
assertEquals(0, q.take()); |
199 |
|
Thread.sleep(SHORT_DELAY_MS); |
200 |
|
t.interrupt(); |
201 |
|
t.join(); |
206 |
|
*/ |
207 |
|
public void testTimedOffer() throws InterruptedException { |
208 |
|
final SynchronousQueue q = new SynchronousQueue(); |
209 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
210 |
< |
public void realRun() throws InterruptedException { |
211 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
209 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
210 |
> |
public void realRun() throws InterruptedException { |
211 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
212 |
|
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
213 |
|
}}); |
214 |
|
|
224 |
|
*/ |
225 |
|
public void testTakeFromEmpty() throws InterruptedException { |
226 |
|
final SynchronousQueue q = new SynchronousQueue(); |
227 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
228 |
< |
public void realRun() throws InterruptedException { |
227 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
228 |
> |
public void realRun() throws InterruptedException { |
229 |
|
q.take(); |
230 |
|
}}); |
231 |
|
|
240 |
|
* put blocks interruptibly if no active taker |
241 |
|
*/ |
242 |
|
public void testFairBlockingPut() throws InterruptedException { |
243 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
244 |
< |
public void realRun() throws InterruptedException { |
243 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
244 |
> |
public void realRun() throws InterruptedException { |
245 |
|
SynchronousQueue q = new SynchronousQueue(true); |
246 |
|
q.put(zero); |
247 |
|
}}); |
257 |
|
*/ |
258 |
|
public void testFairPutWithTake() throws InterruptedException { |
259 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
260 |
< |
Thread t = new Thread(new CheckedRunnable() { |
261 |
< |
public void realRun() throws InterruptedException { |
260 |
> |
Thread t = new Thread(new CheckedRunnable() { |
261 |
> |
public void realRun() throws InterruptedException { |
262 |
|
int added = 0; |
263 |
|
try { |
264 |
< |
q.put(new Object()); |
265 |
< |
++added; |
266 |
< |
q.put(new Object()); |
267 |
< |
++added; |
258 |
< |
q.put(new Object()); |
259 |
< |
++added; |
260 |
< |
q.put(new Object()); |
261 |
< |
++added; |
262 |
< |
threadShouldThrow(); |
264 |
> |
while (true) { |
265 |
> |
q.put(added); |
266 |
> |
++added; |
267 |
> |
} |
268 |
|
} catch (InterruptedException success) { |
269 |
< |
assertTrue(added >= 1); |
269 |
> |
assertEquals(1, added); |
270 |
|
} |
271 |
|
}}); |
272 |
|
|
273 |
|
t.start(); |
274 |
|
Thread.sleep(SHORT_DELAY_MS); |
275 |
< |
q.take(); |
275 |
> |
assertEquals(0, q.take()); |
276 |
|
Thread.sleep(SHORT_DELAY_MS); |
277 |
|
t.interrupt(); |
278 |
|
t.join(); |
283 |
|
*/ |
284 |
|
public void testFairTimedOffer() throws InterruptedException { |
285 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
286 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
287 |
< |
public void realRun() throws InterruptedException { |
288 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
286 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
287 |
> |
public void realRun() throws InterruptedException { |
288 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
289 |
|
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
290 |
|
}}); |
291 |
|
|
301 |
|
*/ |
302 |
|
public void testFairTakeFromEmpty() throws InterruptedException { |
303 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
304 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
305 |
< |
public void realRun() throws InterruptedException { |
304 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
305 |
> |
public void realRun() throws InterruptedException { |
306 |
|
q.take(); |
307 |
|
}}); |
308 |
|
|
317 |
|
*/ |
318 |
|
public void testPoll() { |
319 |
|
SynchronousQueue q = new SynchronousQueue(); |
320 |
< |
assertNull(q.poll()); |
320 |
> |
assertNull(q.poll()); |
321 |
|
} |
322 |
|
|
323 |
|
/** |
341 |
|
* returning timeout status |
342 |
|
*/ |
343 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
344 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
345 |
< |
public void realRun() throws InterruptedException { |
346 |
< |
SynchronousQueue q = new SynchronousQueue(); |
344 |
> |
final SynchronousQueue q = new SynchronousQueue(); |
345 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
346 |
> |
public void realRun() throws InterruptedException { |
347 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
348 |
|
}}); |
349 |
|
|
354 |
|
} |
355 |
|
|
356 |
|
/** |
352 |
– |
* timed poll before a delayed offer fails; after offer succeeds; |
353 |
– |
* on interruption throws |
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 { |
359 |
– |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
360 |
– |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
361 |
– |
try { |
362 |
– |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
363 |
– |
threadShouldThrow(); |
364 |
– |
} catch (InterruptedException success) {} |
365 |
– |
}}); |
366 |
– |
|
367 |
– |
t.start(); |
368 |
– |
Thread.sleep(SMALL_DELAY_MS); |
369 |
– |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
370 |
– |
t.interrupt(); |
371 |
– |
t.join(); |
372 |
– |
} |
373 |
– |
|
374 |
– |
/** |
357 |
|
* Interrupted timed poll throws InterruptedException instead of |
358 |
|
* returning timeout status |
359 |
|
*/ |
360 |
|
public void testFairInterruptedTimedPoll() throws InterruptedException { |
361 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
362 |
< |
public void realRun() throws InterruptedException { |
361 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
362 |
> |
public void realRun() throws InterruptedException { |
363 |
|
SynchronousQueue q = new SynchronousQueue(true); |
364 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
365 |
|
}}); |
371 |
|
} |
372 |
|
|
373 |
|
/** |
374 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
375 |
< |
* on interruption throws |
374 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
375 |
> |
* on interruption throws |
376 |
|
*/ |
377 |
|
public void testFairTimedPollWithOffer() throws InterruptedException { |
378 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
379 |
< |
Thread t = new Thread(new CheckedRunnable() { |
380 |
< |
public void realRun() throws InterruptedException { |
399 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
400 |
< |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
379 |
> |
Thread t = new Thread(new CheckedRunnable() { |
380 |
> |
public void realRun() throws InterruptedException { |
381 |
|
try { |
382 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
383 |
+ |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
384 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
385 |
|
threadShouldThrow(); |
386 |
|
} catch (InterruptedException success) {} |
399 |
|
*/ |
400 |
|
public void testPeek() { |
401 |
|
SynchronousQueue q = new SynchronousQueue(); |
402 |
< |
assertNull(q.peek()); |
402 |
> |
assertNull(q.peek()); |
403 |
|
} |
404 |
|
|
405 |
|
/** |
421 |
|
try { |
422 |
|
q.remove(); |
423 |
|
shouldThrow(); |
424 |
< |
} catch (NoSuchElementException success) { |
443 |
< |
} |
424 |
> |
} catch (NoSuchElementException success) {} |
425 |
|
} |
426 |
|
|
427 |
|
/** |
489 |
|
*/ |
490 |
|
public void testToArray() { |
491 |
|
SynchronousQueue q = new SynchronousQueue(); |
492 |
< |
Object[] o = q.toArray(); |
492 |
> |
Object[] o = q.toArray(); |
493 |
|
assertEquals(o.length, 0); |
494 |
|
} |
495 |
|
|
498 |
|
*/ |
499 |
|
public void testToArray2() { |
500 |
|
SynchronousQueue q = new SynchronousQueue(); |
501 |
< |
Integer[] ints = new Integer[1]; |
501 |
> |
Integer[] ints = new Integer[1]; |
502 |
|
assertNull(ints[0]); |
503 |
|
} |
504 |
|
|
506 |
|
* toArray(null) throws NPE |
507 |
|
*/ |
508 |
|
public void testToArray_BadArg() { |
509 |
< |
try { |
510 |
< |
SynchronousQueue q = new SynchronousQueue(); |
511 |
< |
Object o[] = q.toArray(null); |
512 |
< |
shouldThrow(); |
513 |
< |
} catch (NullPointerException success) {} |
509 |
> |
SynchronousQueue q = new SynchronousQueue(); |
510 |
> |
try { |
511 |
> |
Object o[] = q.toArray(null); |
512 |
> |
shouldThrow(); |
513 |
> |
} catch (NullPointerException success) {} |
514 |
|
} |
515 |
|
|
516 |
|
|
519 |
|
*/ |
520 |
|
public void testIterator() { |
521 |
|
SynchronousQueue q = new SynchronousQueue(); |
522 |
< |
Iterator it = q.iterator(); |
522 |
> |
Iterator it = q.iterator(); |
523 |
|
assertFalse(it.hasNext()); |
524 |
|
try { |
525 |
|
Object x = it.next(); |
532 |
|
*/ |
533 |
|
public void testIteratorRemove() { |
534 |
|
SynchronousQueue q = new SynchronousQueue(); |
535 |
< |
Iterator it = q.iterator(); |
535 |
> |
Iterator it = q.iterator(); |
536 |
|
try { |
537 |
|
it.remove(); |
538 |
|
shouldThrow(); |
555 |
|
public void testOfferInExecutor() { |
556 |
|
final SynchronousQueue q = new SynchronousQueue(); |
557 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
577 |
– |
final Integer one = new Integer(1); |
558 |
|
|
559 |
|
executor.execute(new CheckedRunnable() { |
560 |
< |
public void realRun() throws InterruptedException { |
561 |
< |
threadAssertFalse(q.offer(one)); |
562 |
< |
threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
563 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
560 |
> |
public void realRun() throws InterruptedException { |
561 |
> |
assertFalse(q.offer(one)); |
562 |
> |
assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
563 |
> |
assertEquals(0, q.remainingCapacity()); |
564 |
|
}}); |
565 |
|
|
566 |
|
executor.execute(new CheckedRunnable() { |
567 |
< |
public void realRun() throws InterruptedException { |
567 |
> |
public void realRun() throws InterruptedException { |
568 |
|
Thread.sleep(SMALL_DELAY_MS); |
569 |
< |
threadAssertEquals(one, q.take()); |
569 |
> |
assertSame(one, q.take()); |
570 |
|
}}); |
571 |
|
|
572 |
|
joinPool(executor); |
593 |
– |
|
573 |
|
} |
574 |
|
|
575 |
|
/** |
579 |
|
final SynchronousQueue q = new SynchronousQueue(); |
580 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
581 |
|
executor.execute(new CheckedRunnable() { |
582 |
< |
public void realRun() throws InterruptedException { |
583 |
< |
threadAssertNull(q.poll()); |
584 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
585 |
< |
threadAssertTrue(q.isEmpty()); |
582 |
> |
public void realRun() throws InterruptedException { |
583 |
> |
assertNull(q.poll()); |
584 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
585 |
> |
assertTrue(q.isEmpty()); |
586 |
|
}}); |
587 |
|
|
588 |
|
executor.execute(new CheckedRunnable() { |
589 |
< |
public void realRun() throws InterruptedException { |
590 |
< |
Thread.sleep(SMALL_DELAY_MS); |
591 |
< |
q.put(new Integer(1)); |
589 |
> |
public void realRun() throws InterruptedException { |
590 |
> |
Thread.sleep(SHORT_DELAY_MS); |
591 |
> |
q.put(one); |
592 |
|
}}); |
593 |
|
|
594 |
|
joinPool(executor); |
650 |
|
*/ |
651 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
652 |
|
final SynchronousQueue q = new SynchronousQueue(); |
653 |
< |
Thread t = new Thread(new CheckedRunnable() { |
654 |
< |
public void realRun() throws InterruptedException { |
653 |
> |
Thread t = new Thread(new CheckedRunnable() { |
654 |
> |
public void realRun() throws InterruptedException { |
655 |
|
q.put(new Integer(1)); |
656 |
|
}}); |
657 |
|
|
685 |
|
try { |
686 |
|
q.drainTo(q, 0); |
687 |
|
shouldThrow(); |
688 |
< |
} catch (IllegalArgumentException success) { |
710 |
< |
} |
688 |
> |
} catch (IllegalArgumentException success) {} |
689 |
|
} |
690 |
|
|
691 |
|
/** |
693 |
|
*/ |
694 |
|
public void testDrainToN() throws InterruptedException { |
695 |
|
final SynchronousQueue q = new SynchronousQueue(); |
696 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
697 |
< |
public void realRun() throws InterruptedException { |
696 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
697 |
> |
public void realRun() throws InterruptedException { |
698 |
|
q.put(one); |
699 |
|
}}); |
700 |
|
|
701 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
702 |
< |
public void realRun() throws InterruptedException { |
701 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
702 |
> |
public void realRun() throws InterruptedException { |
703 |
|
q.put(two); |
704 |
|
}}); |
705 |
|
|
708 |
|
ArrayList l = new ArrayList(); |
709 |
|
Thread.sleep(SHORT_DELAY_MS); |
710 |
|
q.drainTo(l, 1); |
711 |
< |
assertTrue(l.size() == 1); |
711 |
> |
assertEquals(1, l.size()); |
712 |
|
q.drainTo(l, 1); |
713 |
< |
assertTrue(l.size() == 2); |
713 |
> |
assertEquals(2, l.size()); |
714 |
|
assertTrue(l.contains(one)); |
715 |
|
assertTrue(l.contains(two)); |
716 |
|
t1.join(); |