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() { |
119 |
|
shouldThrow(); |
120 |
|
} catch (NullPointerException success) {} |
121 |
|
} |
122 |
+ |
|
123 |
|
/** |
124 |
|
* addAll throws ISE if no active taker |
125 |
|
*/ |
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(); |
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)); |
197 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
198 |
|
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
199 |
|
}}); |
200 |
|
|
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(); |
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)); |
274 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
275 |
|
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
276 |
|
}}); |
277 |
|
|
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)); |
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 |
|
|
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)); |
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); |
430 |
|
try { |
431 |
|
q.remove(); |
432 |
|
shouldThrow(); |
433 |
< |
} catch (NoSuchElementException success) { |
443 |
< |
} |
433 |
> |
} catch (NoSuchElementException success) {} |
434 |
|
} |
435 |
|
|
436 |
|
/** |
515 |
|
* toArray(null) throws NPE |
516 |
|
*/ |
517 |
|
public void testToArray_BadArg() { |
518 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
519 |
|
try { |
529 |
– |
SynchronousQueue q = new SynchronousQueue(); |
520 |
|
Object o[] = q.toArray(null); |
521 |
|
shouldThrow(); |
522 |
|
} catch (NullPointerException success) {} |
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()); |
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 { |
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 |
|
/** |
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()); |
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)); |
599 |
> |
Thread.sleep(SHORT_DELAY_MS); |
600 |
> |
q.put(one); |
601 |
|
}}); |
602 |
|
|
603 |
|
joinPool(executor); |
694 |
|
try { |
695 |
|
q.drainTo(q, 0); |
696 |
|
shouldThrow(); |
697 |
< |
} catch (IllegalArgumentException success) { |
710 |
< |
} |
697 |
> |
} catch (IllegalArgumentException success) {} |
698 |
|
} |
699 |
|
|
700 |
|
/** |
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(); |