33 |
|
} |
34 |
|
|
35 |
|
/** |
36 |
+ |
* A fair SynchronousQueue is both empty and full |
37 |
+ |
*/ |
38 |
+ |
public void testFairEmptyFull() { |
39 |
+ |
SynchronousQueue q = new SynchronousQueue(true); |
40 |
+ |
assertTrue(q.isEmpty()); |
41 |
+ |
assertEquals(0, q.size()); |
42 |
+ |
assertEquals(0, q.remainingCapacity()); |
43 |
+ |
assertFalse(q.offer(zero)); |
44 |
+ |
} |
45 |
+ |
|
46 |
+ |
/** |
47 |
|
* offer(null) throws NPE |
48 |
|
*/ |
49 |
|
public void testOfferNull() { |
262 |
|
} |
263 |
|
} |
264 |
|
|
265 |
+ |
|
266 |
+ |
/** |
267 |
+ |
* put blocks interruptibly if no active taker |
268 |
+ |
*/ |
269 |
+ |
public void testFairBlockingPut() { |
270 |
+ |
Thread t = new Thread(new Runnable() { |
271 |
+ |
public void run() { |
272 |
+ |
try { |
273 |
+ |
SynchronousQueue q = new SynchronousQueue(true); |
274 |
+ |
q.put(zero); |
275 |
+ |
threadShouldThrow(); |
276 |
+ |
} catch (InterruptedException ie){ |
277 |
+ |
} |
278 |
+ |
}}); |
279 |
+ |
t.start(); |
280 |
+ |
try { |
281 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
282 |
+ |
t.interrupt(); |
283 |
+ |
t.join(); |
284 |
+ |
} |
285 |
+ |
catch (InterruptedException ie) { |
286 |
+ |
unexpectedException(); |
287 |
+ |
} |
288 |
+ |
} |
289 |
+ |
|
290 |
+ |
/** |
291 |
+ |
* put blocks waiting for take |
292 |
+ |
*/ |
293 |
+ |
public void testFairPutWithTake() { |
294 |
+ |
final SynchronousQueue q = new SynchronousQueue(true); |
295 |
+ |
Thread t = new Thread(new Runnable() { |
296 |
+ |
public void run() { |
297 |
+ |
int added = 0; |
298 |
+ |
try { |
299 |
+ |
q.put(new Object()); |
300 |
+ |
++added; |
301 |
+ |
q.put(new Object()); |
302 |
+ |
++added; |
303 |
+ |
q.put(new Object()); |
304 |
+ |
++added; |
305 |
+ |
q.put(new Object()); |
306 |
+ |
++added; |
307 |
+ |
threadShouldThrow(); |
308 |
+ |
} catch (InterruptedException e){ |
309 |
+ |
assertTrue(added >= 1); |
310 |
+ |
} |
311 |
+ |
} |
312 |
+ |
}); |
313 |
+ |
try { |
314 |
+ |
t.start(); |
315 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
316 |
+ |
q.take(); |
317 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
318 |
+ |
t.interrupt(); |
319 |
+ |
t.join(); |
320 |
+ |
} catch (Exception e){ |
321 |
+ |
unexpectedException(); |
322 |
+ |
} |
323 |
+ |
} |
324 |
+ |
|
325 |
+ |
/** |
326 |
+ |
* timed offer times out if elements not taken |
327 |
+ |
*/ |
328 |
+ |
public void testFairTimedOffer() { |
329 |
+ |
final SynchronousQueue q = new SynchronousQueue(true); |
330 |
+ |
Thread t = new Thread(new Runnable() { |
331 |
+ |
public void run() { |
332 |
+ |
try { |
333 |
+ |
|
334 |
+ |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
335 |
+ |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
336 |
+ |
threadShouldThrow(); |
337 |
+ |
} catch (InterruptedException success){} |
338 |
+ |
} |
339 |
+ |
}); |
340 |
+ |
|
341 |
+ |
try { |
342 |
+ |
t.start(); |
343 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
344 |
+ |
t.interrupt(); |
345 |
+ |
t.join(); |
346 |
+ |
} catch (Exception e){ |
347 |
+ |
unexpectedException(); |
348 |
+ |
} |
349 |
+ |
} |
350 |
+ |
|
351 |
+ |
|
352 |
+ |
/** |
353 |
+ |
* take blocks interruptibly when empty |
354 |
+ |
*/ |
355 |
+ |
public void testFairTakeFromEmpty() { |
356 |
+ |
final SynchronousQueue q = new SynchronousQueue(true); |
357 |
+ |
Thread t = new Thread(new Runnable() { |
358 |
+ |
public void run() { |
359 |
+ |
try { |
360 |
+ |
q.take(); |
361 |
+ |
threadShouldThrow(); |
362 |
+ |
} catch (InterruptedException success){ } |
363 |
+ |
} |
364 |
+ |
}); |
365 |
+ |
try { |
366 |
+ |
t.start(); |
367 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
368 |
+ |
t.interrupt(); |
369 |
+ |
t.join(); |
370 |
+ |
} catch (Exception e){ |
371 |
+ |
unexpectedException(); |
372 |
+ |
} |
373 |
+ |
} |
374 |
+ |
|
375 |
|
/** |
376 |
|
* poll fails unless active taker |
377 |
|
*/ |
437 |
|
Thread t = new Thread(new Runnable() { |
438 |
|
public void run() { |
439 |
|
try { |
440 |
+ |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
441 |
+ |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
442 |
+ |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
443 |
+ |
threadShouldThrow(); |
444 |
+ |
} catch (InterruptedException success) { } |
445 |
+ |
} |
446 |
+ |
}); |
447 |
+ |
try { |
448 |
+ |
t.start(); |
449 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
450 |
+ |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
451 |
+ |
t.interrupt(); |
452 |
+ |
t.join(); |
453 |
+ |
} catch (Exception e){ |
454 |
+ |
unexpectedException(); |
455 |
+ |
} |
456 |
+ |
} |
457 |
+ |
|
458 |
+ |
/** |
459 |
+ |
* Interrupted timed poll throws InterruptedException instead of |
460 |
+ |
* returning timeout status |
461 |
+ |
*/ |
462 |
+ |
public void testFairInterruptedTimedPoll() { |
463 |
+ |
Thread t = new Thread(new Runnable() { |
464 |
+ |
public void run() { |
465 |
+ |
try { |
466 |
+ |
SynchronousQueue q = new SynchronousQueue(true); |
467 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
468 |
+ |
} catch (InterruptedException success){ |
469 |
+ |
} |
470 |
+ |
}}); |
471 |
+ |
t.start(); |
472 |
+ |
try { |
473 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
474 |
+ |
t.interrupt(); |
475 |
+ |
t.join(); |
476 |
+ |
} |
477 |
+ |
catch (InterruptedException ie) { |
478 |
+ |
unexpectedException(); |
479 |
+ |
} |
480 |
+ |
} |
481 |
+ |
|
482 |
+ |
/** |
483 |
+ |
* timed poll before a delayed offer fails; after offer succeeds; |
484 |
+ |
* on interruption throws |
485 |
+ |
*/ |
486 |
+ |
public void testFairTimedPollWithOffer() { |
487 |
+ |
final SynchronousQueue q = new SynchronousQueue(true); |
488 |
+ |
Thread t = new Thread(new Runnable() { |
489 |
+ |
public void run() { |
490 |
+ |
try { |
491 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
492 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
493 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |