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()); |
173 |
< |
++added; |
174 |
< |
q.put(new Object()); |
175 |
< |
++added; |
176 |
< |
q.put(new Object()); |
177 |
< |
++added; |
178 |
< |
q.put(new Object()); |
179 |
< |
++added; |
180 |
< |
threadShouldThrow(); |
172 |
> |
while (true) { |
173 |
> |
q.put(added); |
174 |
> |
++added; |
175 |
> |
} |
176 |
|
} catch (InterruptedException success) { |
177 |
< |
assertTrue(added >= 1); |
177 |
> |
assertTrue(added == 1); |
178 |
|
} |
179 |
|
}}); |
180 |
|
|
181 |
|
t.start(); |
182 |
|
Thread.sleep(SHORT_DELAY_MS); |
183 |
< |
q.take(); |
183 |
> |
assertEquals(0, q.take()); |
184 |
|
Thread.sleep(SHORT_DELAY_MS); |
185 |
|
t.interrupt(); |
186 |
|
t.join(); |
191 |
|
*/ |
192 |
|
public void testTimedOffer() throws InterruptedException { |
193 |
|
final SynchronousQueue q = new SynchronousQueue(); |
194 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
195 |
< |
public void realRun() throws InterruptedException { |
196 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
194 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
195 |
> |
public void realRun() throws InterruptedException { |
196 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
197 |
|
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
198 |
|
}}); |
199 |
|
|
209 |
|
*/ |
210 |
|
public void testTakeFromEmpty() throws InterruptedException { |
211 |
|
final SynchronousQueue q = new SynchronousQueue(); |
212 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
213 |
< |
public void realRun() throws InterruptedException { |
212 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
213 |
> |
public void realRun() throws InterruptedException { |
214 |
|
q.take(); |
215 |
|
}}); |
216 |
|
|
225 |
|
* put blocks interruptibly if no active taker |
226 |
|
*/ |
227 |
|
public void testFairBlockingPut() throws InterruptedException { |
228 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
229 |
< |
public void realRun() throws InterruptedException { |
228 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
229 |
> |
public void realRun() throws InterruptedException { |
230 |
|
SynchronousQueue q = new SynchronousQueue(true); |
231 |
|
q.put(zero); |
232 |
|
}}); |
242 |
|
*/ |
243 |
|
public void testFairPutWithTake() throws InterruptedException { |
244 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
245 |
< |
Thread t = new Thread(new CheckedRunnable() { |
246 |
< |
public void realRun() throws InterruptedException { |
245 |
> |
Thread t = new Thread(new CheckedRunnable() { |
246 |
> |
public void realRun() throws InterruptedException { |
247 |
|
int added = 0; |
248 |
|
try { |
249 |
< |
q.put(new Object()); |
250 |
< |
++added; |
251 |
< |
q.put(new Object()); |
252 |
< |
++added; |
258 |
< |
q.put(new Object()); |
259 |
< |
++added; |
260 |
< |
q.put(new Object()); |
261 |
< |
++added; |
262 |
< |
threadShouldThrow(); |
249 |
> |
while (true) { |
250 |
> |
q.put(added); |
251 |
> |
++added; |
252 |
> |
} |
253 |
|
} catch (InterruptedException success) { |
254 |
< |
assertTrue(added >= 1); |
254 |
> |
assertTrue(added == 1); |
255 |
|
} |
256 |
|
}}); |
257 |
|
|
258 |
|
t.start(); |
259 |
|
Thread.sleep(SHORT_DELAY_MS); |
260 |
< |
q.take(); |
260 |
> |
assertEquals(0, q.take()); |
261 |
|
Thread.sleep(SHORT_DELAY_MS); |
262 |
|
t.interrupt(); |
263 |
|
t.join(); |
268 |
|
*/ |
269 |
|
public void testFairTimedOffer() throws InterruptedException { |
270 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
271 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
272 |
< |
public void realRun() throws InterruptedException { |
273 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
271 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
272 |
> |
public void realRun() throws InterruptedException { |
273 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
274 |
|
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
275 |
|
}}); |
276 |
|
|
286 |
|
*/ |
287 |
|
public void testFairTakeFromEmpty() throws InterruptedException { |
288 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
289 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
290 |
< |
public void realRun() throws InterruptedException { |
289 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
290 |
> |
public void realRun() throws InterruptedException { |
291 |
|
q.take(); |
292 |
|
}}); |
293 |
|
|
302 |
|
*/ |
303 |
|
public void testPoll() { |
304 |
|
SynchronousQueue q = new SynchronousQueue(); |
305 |
< |
assertNull(q.poll()); |
305 |
> |
assertNull(q.poll()); |
306 |
|
} |
307 |
|
|
308 |
|
/** |
326 |
|
* returning timeout status |
327 |
|
*/ |
328 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
329 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
330 |
< |
public void realRun() throws InterruptedException { |
331 |
< |
SynchronousQueue q = new SynchronousQueue(); |
329 |
> |
final SynchronousQueue q = new SynchronousQueue(); |
330 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
331 |
> |
public void realRun() throws InterruptedException { |
332 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
333 |
|
}}); |
334 |
|
|
344 |
|
*/ |
345 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
346 |
|
final SynchronousQueue q = new SynchronousQueue(); |
347 |
< |
Thread t = new Thread(new CheckedRunnable() { |
348 |
< |
public void realRun() throws InterruptedException { |
349 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
347 |
> |
Thread t = new Thread(new CheckedRunnable() { |
348 |
> |
public void realRun() throws InterruptedException { |
349 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
350 |
|
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
351 |
|
try { |
352 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
353 |
< |
threadShouldThrow(); |
353 |
> |
shouldThrow(); |
354 |
|
} catch (InterruptedException success) {} |
355 |
|
}}); |
356 |
|
|
366 |
|
* returning timeout status |
367 |
|
*/ |
368 |
|
public void testFairInterruptedTimedPoll() throws InterruptedException { |
369 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
370 |
< |
public void realRun() throws InterruptedException { |
369 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
370 |
> |
public void realRun() throws InterruptedException { |
371 |
|
SynchronousQueue q = new SynchronousQueue(true); |
372 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
373 |
|
}}); |
384 |
|
*/ |
385 |
|
public void testFairTimedPollWithOffer() throws InterruptedException { |
386 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
387 |
< |
Thread t = new Thread(new CheckedRunnable() { |
388 |
< |
public void realRun() throws InterruptedException { |
389 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
387 |
> |
Thread t = new Thread(new CheckedRunnable() { |
388 |
> |
public void realRun() throws InterruptedException { |
389 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
390 |
|
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
391 |
|
try { |
392 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
407 |
|
*/ |
408 |
|
public void testPeek() { |
409 |
|
SynchronousQueue q = new SynchronousQueue(); |
410 |
< |
assertNull(q.peek()); |
410 |
> |
assertNull(q.peek()); |
411 |
|
} |
412 |
|
|
413 |
|
/** |
429 |
|
try { |
430 |
|
q.remove(); |
431 |
|
shouldThrow(); |
432 |
< |
} catch (NoSuchElementException success) { |
443 |
< |
} |
432 |
> |
} catch (NoSuchElementException success) {} |
433 |
|
} |
434 |
|
|
435 |
|
/** |
497 |
|
*/ |
498 |
|
public void testToArray() { |
499 |
|
SynchronousQueue q = new SynchronousQueue(); |
500 |
< |
Object[] o = q.toArray(); |
500 |
> |
Object[] o = q.toArray(); |
501 |
|
assertEquals(o.length, 0); |
502 |
|
} |
503 |
|
|
506 |
|
*/ |
507 |
|
public void testToArray2() { |
508 |
|
SynchronousQueue q = new SynchronousQueue(); |
509 |
< |
Integer[] ints = new Integer[1]; |
509 |
> |
Integer[] ints = new Integer[1]; |
510 |
|
assertNull(ints[0]); |
511 |
|
} |
512 |
|
|
514 |
|
* toArray(null) throws NPE |
515 |
|
*/ |
516 |
|
public void testToArray_BadArg() { |
517 |
< |
try { |
518 |
< |
SynchronousQueue q = new SynchronousQueue(); |
519 |
< |
Object o[] = q.toArray(null); |
520 |
< |
shouldThrow(); |
521 |
< |
} catch (NullPointerException success) {} |
517 |
> |
SynchronousQueue q = new SynchronousQueue(); |
518 |
> |
try { |
519 |
> |
Object o[] = q.toArray(null); |
520 |
> |
shouldThrow(); |
521 |
> |
} catch (NullPointerException success) {} |
522 |
|
} |
523 |
|
|
524 |
|
|
527 |
|
*/ |
528 |
|
public void testIterator() { |
529 |
|
SynchronousQueue q = new SynchronousQueue(); |
530 |
< |
Iterator it = q.iterator(); |
530 |
> |
Iterator it = q.iterator(); |
531 |
|
assertFalse(it.hasNext()); |
532 |
|
try { |
533 |
|
Object x = it.next(); |
540 |
|
*/ |
541 |
|
public void testIteratorRemove() { |
542 |
|
SynchronousQueue q = new SynchronousQueue(); |
543 |
< |
Iterator it = q.iterator(); |
543 |
> |
Iterator it = q.iterator(); |
544 |
|
try { |
545 |
|
it.remove(); |
546 |
|
shouldThrow(); |
563 |
|
public void testOfferInExecutor() { |
564 |
|
final SynchronousQueue q = new SynchronousQueue(); |
565 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
577 |
– |
final Integer one = new Integer(1); |
566 |
|
|
567 |
|
executor.execute(new CheckedRunnable() { |
568 |
< |
public void realRun() throws InterruptedException { |
569 |
< |
threadAssertFalse(q.offer(one)); |
570 |
< |
threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
571 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
568 |
> |
public void realRun() throws InterruptedException { |
569 |
> |
assertFalse(q.offer(one)); |
570 |
> |
assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
571 |
> |
assertEquals(0, q.remainingCapacity()); |
572 |
|
}}); |
573 |
|
|
574 |
|
executor.execute(new CheckedRunnable() { |
575 |
< |
public void realRun() throws InterruptedException { |
575 |
> |
public void realRun() throws InterruptedException { |
576 |
|
Thread.sleep(SMALL_DELAY_MS); |
577 |
< |
threadAssertEquals(one, q.take()); |
577 |
> |
assertSame(one, q.take()); |
578 |
|
}}); |
579 |
|
|
580 |
|
joinPool(executor); |
593 |
– |
|
581 |
|
} |
582 |
|
|
583 |
|
/** |
587 |
|
final SynchronousQueue q = new SynchronousQueue(); |
588 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
589 |
|
executor.execute(new CheckedRunnable() { |
590 |
< |
public void realRun() throws InterruptedException { |
591 |
< |
threadAssertNull(q.poll()); |
592 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
593 |
< |
threadAssertTrue(q.isEmpty()); |
590 |
> |
public void realRun() throws InterruptedException { |
591 |
> |
assertNull(q.poll()); |
592 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
593 |
> |
assertTrue(q.isEmpty()); |
594 |
|
}}); |
595 |
|
|
596 |
|
executor.execute(new CheckedRunnable() { |
597 |
< |
public void realRun() throws InterruptedException { |
598 |
< |
Thread.sleep(SMALL_DELAY_MS); |
599 |
< |
q.put(new Integer(1)); |
597 |
> |
public void realRun() throws InterruptedException { |
598 |
> |
Thread.sleep(SHORT_DELAY_MS); |
599 |
> |
q.put(one); |
600 |
|
}}); |
601 |
|
|
602 |
|
joinPool(executor); |
658 |
|
*/ |
659 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
660 |
|
final SynchronousQueue q = new SynchronousQueue(); |
661 |
< |
Thread t = new Thread(new CheckedRunnable() { |
662 |
< |
public void realRun() throws InterruptedException { |
661 |
> |
Thread t = new Thread(new CheckedRunnable() { |
662 |
> |
public void realRun() throws InterruptedException { |
663 |
|
q.put(new Integer(1)); |
664 |
|
}}); |
665 |
|
|
693 |
|
try { |
694 |
|
q.drainTo(q, 0); |
695 |
|
shouldThrow(); |
696 |
< |
} catch (IllegalArgumentException success) { |
710 |
< |
} |
696 |
> |
} catch (IllegalArgumentException success) {} |
697 |
|
} |
698 |
|
|
699 |
|
/** |
701 |
|
*/ |
702 |
|
public void testDrainToN() throws InterruptedException { |
703 |
|
final SynchronousQueue q = new SynchronousQueue(); |
704 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
705 |
< |
public void realRun() throws InterruptedException { |
704 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
705 |
> |
public void realRun() throws InterruptedException { |
706 |
|
q.put(one); |
707 |
|
}}); |
708 |
|
|
709 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
710 |
< |
public void realRun() throws InterruptedException { |
709 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
710 |
> |
public void realRun() throws InterruptedException { |
711 |
|
q.put(two); |
712 |
|
}}); |
713 |
|
|