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()); |
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 |
|
/** |
133 |
|
shouldThrow(); |
134 |
|
} catch (NullPointerException success) {} |
135 |
|
} |
136 |
+ |
|
137 |
|
/** |
138 |
|
* addAll throws ISE if no active taker |
139 |
|
*/ |
189 |
|
++added; |
190 |
|
} |
191 |
|
} catch (InterruptedException success) { |
192 |
< |
assertTrue(added == 1); |
192 |
> |
assertEquals(1, added); |
193 |
|
} |
194 |
|
}}); |
195 |
|
|
266 |
|
++added; |
267 |
|
} |
268 |
|
} catch (InterruptedException success) { |
269 |
< |
assertTrue(added == 1); |
269 |
> |
assertEquals(1, added); |
270 |
|
} |
271 |
|
}}); |
272 |
|
|
354 |
|
} |
355 |
|
|
356 |
|
/** |
342 |
– |
* timed poll before a delayed offer fails; after offer succeeds; |
343 |
– |
* on interruption throws |
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 |
– |
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 |
– |
shouldThrow(); |
354 |
– |
} catch (InterruptedException success) {} |
355 |
– |
}}); |
356 |
– |
|
357 |
– |
t.start(); |
358 |
– |
Thread.sleep(SMALL_DELAY_MS); |
359 |
– |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
360 |
– |
t.interrupt(); |
361 |
– |
t.join(); |
362 |
– |
} |
363 |
– |
|
364 |
– |
/** |
357 |
|
* Interrupted timed poll throws InterruptedException instead of |
358 |
|
* returning timeout status |
359 |
|
*/ |
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 { |
389 |
– |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
390 |
– |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
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) {} |
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(); |