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 |
|
/** |
218 |
|
t.join(); |
219 |
|
} |
220 |
|
|
207 |
– |
|
208 |
– |
/** |
209 |
– |
* take blocks interruptibly when empty |
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 { |
215 |
– |
q.take(); |
216 |
– |
}}); |
217 |
– |
|
218 |
– |
t.start(); |
219 |
– |
Thread.sleep(SHORT_DELAY_MS); |
220 |
– |
t.interrupt(); |
221 |
– |
t.join(); |
222 |
– |
} |
223 |
– |
|
224 |
– |
|
221 |
|
/** |
222 |
|
* put blocks interruptibly if no active taker |
223 |
|
*/ |
303 |
|
} |
304 |
|
|
305 |
|
/** |
306 |
< |
* timed pool with zero timeout times out if no active taker |
306 |
> |
* timed poll with zero timeout times out if no active taker |
307 |
|
*/ |
308 |
|
public void testTimedPoll0() throws InterruptedException { |
309 |
|
SynchronousQueue q = new SynchronousQueue(); |
311 |
|
} |
312 |
|
|
313 |
|
/** |
314 |
< |
* timed pool with nonzero timeout times out if no active taker |
314 |
> |
* timed poll with nonzero timeout times out if no active taker |
315 |
|
*/ |
316 |
|
public void testTimedPoll() throws InterruptedException { |
317 |
|
SynchronousQueue q = new SynchronousQueue(); |
336 |
|
} |
337 |
|
|
338 |
|
/** |
343 |
– |
* timed poll before a delayed offer fails; after offer succeeds; |
344 |
– |
* on interruption throws |
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 |
– |
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 |
– |
shouldThrow(); |
355 |
– |
} catch (InterruptedException success) {} |
356 |
– |
}}); |
357 |
– |
|
358 |
– |
t.start(); |
359 |
– |
Thread.sleep(SMALL_DELAY_MS); |
360 |
– |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
361 |
– |
t.interrupt(); |
362 |
– |
t.join(); |
363 |
– |
} |
364 |
– |
|
365 |
– |
/** |
339 |
|
* Interrupted timed poll throws InterruptedException instead of |
340 |
|
* returning timeout status |
341 |
|
*/ |
353 |
|
} |
354 |
|
|
355 |
|
/** |
356 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
357 |
< |
* on interruption throws |
356 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
357 |
> |
* on interruption throws |
358 |
|
*/ |
359 |
|
public void testFairTimedPollWithOffer() throws InterruptedException { |
360 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
361 |
|
Thread t = new Thread(new CheckedRunnable() { |
362 |
|
public void realRun() throws InterruptedException { |
390 |
– |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
391 |
– |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
363 |
|
try { |
364 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
365 |
+ |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
366 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
367 |
|
threadShouldThrow(); |
368 |
|
} catch (InterruptedException success) {} |