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); |
119 |
|
shouldThrow(); |
120 |
|
} catch (NullPointerException success) {} |
121 |
|
} |
122 |
+ |
|
123 |
|
/** |
124 |
|
* addAll throws ISE if no active taker |
125 |
|
*/ |
138 |
|
* put(null) throws NPE |
139 |
|
*/ |
140 |
|
public void testPutNull() throws InterruptedException { |
141 |
< |
try { |
141 |
> |
try { |
142 |
|
SynchronousQueue q = new SynchronousQueue(); |
143 |
|
q.put(null); |
144 |
|
shouldThrow(); |
149 |
|
* put blocks interruptibly if no active taker |
150 |
|
*/ |
151 |
|
public void testBlockingPut() throws InterruptedException { |
152 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
153 |
< |
public void realRun() throws InterruptedException { |
152 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
153 |
> |
public void realRun() throws InterruptedException { |
154 |
|
SynchronousQueue q = new SynchronousQueue(); |
155 |
|
q.put(zero); |
156 |
|
}}); |
166 |
|
*/ |
167 |
|
public void testPutWithTake() throws InterruptedException { |
168 |
|
final SynchronousQueue q = new SynchronousQueue(); |
169 |
< |
Thread t = new Thread(new CheckedRunnable() { |
170 |
< |
public void realRun() throws InterruptedException { |
169 |
> |
Thread t = new Thread(new CheckedRunnable() { |
170 |
> |
public void realRun() throws InterruptedException { |
171 |
|
int added = 0; |
172 |
|
try { |
173 |
< |
q.put(new Object()); |
174 |
< |
++added; |
175 |
< |
q.put(new Object()); |
176 |
< |
++added; |
176 |
< |
q.put(new Object()); |
177 |
< |
++added; |
178 |
< |
q.put(new Object()); |
179 |
< |
++added; |
180 |
< |
threadShouldThrow(); |
173 |
> |
while (true) { |
174 |
> |
q.put(added); |
175 |
> |
++added; |
176 |
> |
} |
177 |
|
} catch (InterruptedException success) { |
178 |
< |
assertTrue(added >= 1); |
178 |
> |
assertEquals(1, added); |
179 |
|
} |
180 |
|
}}); |
181 |
|
|
182 |
|
t.start(); |
183 |
|
Thread.sleep(SHORT_DELAY_MS); |
184 |
< |
q.take(); |
184 |
> |
assertEquals(0, q.take()); |
185 |
|
Thread.sleep(SHORT_DELAY_MS); |
186 |
|
t.interrupt(); |
187 |
|
t.join(); |
192 |
|
*/ |
193 |
|
public void testTimedOffer() throws InterruptedException { |
194 |
|
final SynchronousQueue q = new SynchronousQueue(); |
195 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
196 |
< |
public void realRun() throws InterruptedException { |
197 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
195 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
196 |
> |
public void realRun() throws InterruptedException { |
197 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
198 |
|
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
199 |
|
}}); |
200 |
|
|
210 |
|
*/ |
211 |
|
public void testTakeFromEmpty() throws InterruptedException { |
212 |
|
final SynchronousQueue q = new SynchronousQueue(); |
213 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
214 |
< |
public void realRun() throws InterruptedException { |
213 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
214 |
> |
public void realRun() throws InterruptedException { |
215 |
|
q.take(); |
216 |
|
}}); |
217 |
|
|
226 |
|
* put blocks interruptibly if no active taker |
227 |
|
*/ |
228 |
|
public void testFairBlockingPut() throws InterruptedException { |
229 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
230 |
< |
public void realRun() throws InterruptedException { |
229 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
230 |
> |
public void realRun() throws InterruptedException { |
231 |
|
SynchronousQueue q = new SynchronousQueue(true); |
232 |
|
q.put(zero); |
233 |
|
}}); |
243 |
|
*/ |
244 |
|
public void testFairPutWithTake() throws InterruptedException { |
245 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
246 |
< |
Thread t = new Thread(new CheckedRunnable() { |
247 |
< |
public void realRun() throws InterruptedException { |
246 |
> |
Thread t = new Thread(new CheckedRunnable() { |
247 |
> |
public void realRun() throws InterruptedException { |
248 |
|
int added = 0; |
249 |
|
try { |
250 |
< |
q.put(new Object()); |
251 |
< |
++added; |
252 |
< |
q.put(new Object()); |
253 |
< |
++added; |
258 |
< |
q.put(new Object()); |
259 |
< |
++added; |
260 |
< |
q.put(new Object()); |
261 |
< |
++added; |
262 |
< |
threadShouldThrow(); |
250 |
> |
while (true) { |
251 |
> |
q.put(added); |
252 |
> |
++added; |
253 |
> |
} |
254 |
|
} catch (InterruptedException success) { |
255 |
< |
assertTrue(added >= 1); |
255 |
> |
assertEquals(1, added); |
256 |
|
} |
257 |
|
}}); |
258 |
|
|
259 |
|
t.start(); |
260 |
|
Thread.sleep(SHORT_DELAY_MS); |
261 |
< |
q.take(); |
261 |
> |
assertEquals(0, q.take()); |
262 |
|
Thread.sleep(SHORT_DELAY_MS); |
263 |
|
t.interrupt(); |
264 |
|
t.join(); |
269 |
|
*/ |
270 |
|
public void testFairTimedOffer() throws InterruptedException { |
271 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
272 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
273 |
< |
public void realRun() throws InterruptedException { |
274 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
272 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
273 |
> |
public void realRun() throws InterruptedException { |
274 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
275 |
|
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
276 |
|
}}); |
277 |
|
|
287 |
|
*/ |
288 |
|
public void testFairTakeFromEmpty() throws InterruptedException { |
289 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
290 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
291 |
< |
public void realRun() throws InterruptedException { |
290 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
291 |
> |
public void realRun() throws InterruptedException { |
292 |
|
q.take(); |
293 |
|
}}); |
294 |
|
|
303 |
|
*/ |
304 |
|
public void testPoll() { |
305 |
|
SynchronousQueue q = new SynchronousQueue(); |
306 |
< |
assertNull(q.poll()); |
306 |
> |
assertNull(q.poll()); |
307 |
|
} |
308 |
|
|
309 |
|
/** |
327 |
|
* returning timeout status |
328 |
|
*/ |
329 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
330 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
331 |
< |
public void realRun() throws InterruptedException { |
332 |
< |
SynchronousQueue q = new SynchronousQueue(); |
330 |
> |
final SynchronousQueue q = new SynchronousQueue(); |
331 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
332 |
> |
public void realRun() throws InterruptedException { |
333 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
334 |
|
}}); |
335 |
|
|
345 |
|
*/ |
346 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
347 |
|
final SynchronousQueue q = new SynchronousQueue(); |
348 |
< |
Thread t = new Thread(new CheckedRunnable() { |
349 |
< |
public void realRun() throws InterruptedException { |
350 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
348 |
> |
Thread t = new Thread(new CheckedRunnable() { |
349 |
> |
public void realRun() throws InterruptedException { |
350 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
351 |
|
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
352 |
|
try { |
353 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
354 |
< |
threadShouldThrow(); |
354 |
> |
shouldThrow(); |
355 |
|
} catch (InterruptedException success) {} |
356 |
|
}}); |
357 |
|
|
367 |
|
* returning timeout status |
368 |
|
*/ |
369 |
|
public void testFairInterruptedTimedPoll() throws InterruptedException { |
370 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
371 |
< |
public void realRun() throws InterruptedException { |
370 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
371 |
> |
public void realRun() throws InterruptedException { |
372 |
|
SynchronousQueue q = new SynchronousQueue(true); |
373 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
374 |
|
}}); |
385 |
|
*/ |
386 |
|
public void testFairTimedPollWithOffer() throws InterruptedException { |
387 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
388 |
< |
Thread t = new Thread(new CheckedRunnable() { |
389 |
< |
public void realRun() throws InterruptedException { |
390 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
388 |
> |
Thread t = new Thread(new CheckedRunnable() { |
389 |
> |
public void realRun() throws InterruptedException { |
390 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
391 |
|
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
392 |
|
try { |
393 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
408 |
|
*/ |
409 |
|
public void testPeek() { |
410 |
|
SynchronousQueue q = new SynchronousQueue(); |
411 |
< |
assertNull(q.peek()); |
411 |
> |
assertNull(q.peek()); |
412 |
|
} |
413 |
|
|
414 |
|
/** |
430 |
|
try { |
431 |
|
q.remove(); |
432 |
|
shouldThrow(); |
433 |
< |
} catch (NoSuchElementException success) { |
443 |
< |
} |
433 |
> |
} catch (NoSuchElementException success) {} |
434 |
|
} |
435 |
|
|
436 |
|
/** |
498 |
|
*/ |
499 |
|
public void testToArray() { |
500 |
|
SynchronousQueue q = new SynchronousQueue(); |
501 |
< |
Object[] o = q.toArray(); |
501 |
> |
Object[] o = q.toArray(); |
502 |
|
assertEquals(o.length, 0); |
503 |
|
} |
504 |
|
|
507 |
|
*/ |
508 |
|
public void testToArray2() { |
509 |
|
SynchronousQueue q = new SynchronousQueue(); |
510 |
< |
Integer[] ints = new Integer[1]; |
510 |
> |
Integer[] ints = new Integer[1]; |
511 |
|
assertNull(ints[0]); |
512 |
|
} |
513 |
|
|
515 |
|
* toArray(null) throws NPE |
516 |
|
*/ |
517 |
|
public void testToArray_BadArg() { |
518 |
< |
try { |
519 |
< |
SynchronousQueue q = new SynchronousQueue(); |
520 |
< |
Object o[] = q.toArray(null); |
521 |
< |
shouldThrow(); |
522 |
< |
} catch (NullPointerException success) {} |
518 |
> |
SynchronousQueue q = new SynchronousQueue(); |
519 |
> |
try { |
520 |
> |
Object o[] = q.toArray(null); |
521 |
> |
shouldThrow(); |
522 |
> |
} catch (NullPointerException success) {} |
523 |
|
} |
524 |
|
|
525 |
|
|
528 |
|
*/ |
529 |
|
public void testIterator() { |
530 |
|
SynchronousQueue q = new SynchronousQueue(); |
531 |
< |
Iterator it = q.iterator(); |
531 |
> |
Iterator it = q.iterator(); |
532 |
|
assertFalse(it.hasNext()); |
533 |
|
try { |
534 |
|
Object x = it.next(); |
541 |
|
*/ |
542 |
|
public void testIteratorRemove() { |
543 |
|
SynchronousQueue q = new SynchronousQueue(); |
544 |
< |
Iterator it = q.iterator(); |
544 |
> |
Iterator it = q.iterator(); |
545 |
|
try { |
546 |
|
it.remove(); |
547 |
|
shouldThrow(); |
564 |
|
public void testOfferInExecutor() { |
565 |
|
final SynchronousQueue q = new SynchronousQueue(); |
566 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
577 |
– |
final Integer one = new Integer(1); |
567 |
|
|
568 |
|
executor.execute(new CheckedRunnable() { |
569 |
< |
public void realRun() throws InterruptedException { |
570 |
< |
threadAssertFalse(q.offer(one)); |
571 |
< |
threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
572 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
569 |
> |
public void realRun() throws InterruptedException { |
570 |
> |
assertFalse(q.offer(one)); |
571 |
> |
assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
572 |
> |
assertEquals(0, q.remainingCapacity()); |
573 |
|
}}); |
574 |
|
|
575 |
|
executor.execute(new CheckedRunnable() { |
576 |
< |
public void realRun() throws InterruptedException { |
576 |
> |
public void realRun() throws InterruptedException { |
577 |
|
Thread.sleep(SMALL_DELAY_MS); |
578 |
< |
threadAssertEquals(one, q.take()); |
578 |
> |
assertSame(one, q.take()); |
579 |
|
}}); |
580 |
|
|
581 |
|
joinPool(executor); |
593 |
– |
|
582 |
|
} |
583 |
|
|
584 |
|
/** |
588 |
|
final SynchronousQueue q = new SynchronousQueue(); |
589 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
590 |
|
executor.execute(new CheckedRunnable() { |
591 |
< |
public void realRun() throws InterruptedException { |
592 |
< |
threadAssertNull(q.poll()); |
593 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
594 |
< |
threadAssertTrue(q.isEmpty()); |
591 |
> |
public void realRun() throws InterruptedException { |
592 |
> |
assertNull(q.poll()); |
593 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
594 |
> |
assertTrue(q.isEmpty()); |
595 |
|
}}); |
596 |
|
|
597 |
|
executor.execute(new CheckedRunnable() { |
598 |
< |
public void realRun() throws InterruptedException { |
599 |
< |
Thread.sleep(SMALL_DELAY_MS); |
600 |
< |
q.put(new Integer(1)); |
598 |
> |
public void realRun() throws InterruptedException { |
599 |
> |
Thread.sleep(SHORT_DELAY_MS); |
600 |
> |
q.put(one); |
601 |
|
}}); |
602 |
|
|
603 |
|
joinPool(executor); |
659 |
|
*/ |
660 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
661 |
|
final SynchronousQueue q = new SynchronousQueue(); |
662 |
< |
Thread t = new Thread(new CheckedRunnable() { |
663 |
< |
public void realRun() throws InterruptedException { |
662 |
> |
Thread t = new Thread(new CheckedRunnable() { |
663 |
> |
public void realRun() throws InterruptedException { |
664 |
|
q.put(new Integer(1)); |
665 |
|
}}); |
666 |
|
|
694 |
|
try { |
695 |
|
q.drainTo(q, 0); |
696 |
|
shouldThrow(); |
697 |
< |
} catch (IllegalArgumentException success) { |
710 |
< |
} |
697 |
> |
} catch (IllegalArgumentException success) {} |
698 |
|
} |
699 |
|
|
700 |
|
/** |
702 |
|
*/ |
703 |
|
public void testDrainToN() throws InterruptedException { |
704 |
|
final SynchronousQueue q = new SynchronousQueue(); |
705 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
706 |
< |
public void realRun() throws InterruptedException { |
705 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
706 |
> |
public void realRun() throws InterruptedException { |
707 |
|
q.put(one); |
708 |
|
}}); |
709 |
|
|
710 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
711 |
< |
public void realRun() throws InterruptedException { |
710 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
711 |
> |
public void realRun() throws InterruptedException { |
712 |
|
q.put(two); |
713 |
|
}}); |
714 |
|
|
717 |
|
ArrayList l = new ArrayList(); |
718 |
|
Thread.sleep(SHORT_DELAY_MS); |
719 |
|
q.drainTo(l, 1); |
720 |
< |
assertTrue(l.size() == 1); |
720 |
> |
assertEquals(1, l.size()); |
721 |
|
q.drainTo(l, 1); |
722 |
< |
assertTrue(l.size() == 2); |
722 |
> |
assertEquals(2, l.size()); |
723 |
|
assertTrue(l.contains(one)); |
724 |
|
assertTrue(l.contains(two)); |
725 |
|
t1.join(); |