284 |
|
* put blocks interruptibly if full |
285 |
|
*/ |
286 |
|
public void testBlockingPut() throws InterruptedException { |
287 |
+ |
final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
288 |
|
Thread t = new Thread(new CheckedRunnable() { |
289 |
< |
public void realRun() { |
290 |
< |
int added = 0; |
289 |
> |
public void realRun() throws InterruptedException { |
290 |
> |
for (int i = 0; i < SIZE; ++i) |
291 |
> |
q.put(i); |
292 |
> |
assertEquals(SIZE, q.size()); |
293 |
> |
assertEquals(0, q.remainingCapacity()); |
294 |
|
try { |
295 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
296 |
< |
for (int i = 0; i < SIZE; ++i) { |
297 |
< |
q.put(new Integer(i)); |
294 |
< |
++added; |
295 |
< |
} |
296 |
< |
q.put(new Integer(SIZE)); |
297 |
< |
threadShouldThrow(); |
298 |
< |
} catch (InterruptedException success) { |
299 |
< |
threadAssertEquals(added, SIZE); |
300 |
< |
} |
295 |
> |
q.put(99); |
296 |
> |
shouldThrow(); |
297 |
> |
} catch (InterruptedException success) {} |
298 |
|
}}); |
299 |
|
|
300 |
|
t.start(); |
301 |
|
Thread.sleep(SHORT_DELAY_MS); |
302 |
|
t.interrupt(); |
303 |
|
t.join(); |
304 |
+ |
assertEquals(SIZE, q.size()); |
305 |
+ |
assertEquals(0, q.remainingCapacity()); |
306 |
|
} |
307 |
|
|
308 |
|
/** |
309 |
|
* put blocks waiting for take when full |
310 |
|
*/ |
311 |
|
public void testPutWithTake() throws InterruptedException { |
312 |
+ |
final int capacity = 2; |
313 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
314 |
|
Thread t = new Thread(new CheckedRunnable() { |
315 |
< |
public void realRun() { |
316 |
< |
int added = 0; |
315 |
> |
public void realRun() throws InterruptedException { |
316 |
> |
for (int i = 0; i < capacity + 1; i++) |
317 |
> |
q.put(i); |
318 |
|
try { |
319 |
< |
q.put(new Object()); |
320 |
< |
++added; |
321 |
< |
q.put(new Object()); |
321 |
< |
++added; |
322 |
< |
q.put(new Object()); |
323 |
< |
++added; |
324 |
< |
q.put(new Object()); |
325 |
< |
++added; |
326 |
< |
threadShouldThrow(); |
327 |
< |
} catch (InterruptedException success) { |
328 |
< |
threadAssertTrue(added >= 2); |
329 |
< |
} |
319 |
> |
q.put(99); |
320 |
> |
shouldThrow(); |
321 |
> |
} catch (InterruptedException success) {} |
322 |
|
}}); |
323 |
|
|
324 |
|
t.start(); |
325 |
|
Thread.sleep(SHORT_DELAY_MS); |
326 |
< |
q.take(); |
326 |
> |
assertEquals(q.remainingCapacity(), 0); |
327 |
> |
assertEquals(0, q.take()); |
328 |
> |
Thread.sleep(SHORT_DELAY_MS); |
329 |
|
t.interrupt(); |
330 |
|
t.join(); |
331 |
+ |
assertEquals(q.remainingCapacity(), 0); |
332 |
|
} |
333 |
|
|
334 |
|
/** |
383 |
|
* Take removes existing elements until empty, then blocks interruptibly |
384 |
|
*/ |
385 |
|
public void testBlockingTake() throws InterruptedException { |
386 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
386 |
> |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
387 |
> |
Thread t = new Thread(new CheckedRunnable() { |
388 |
|
public void realRun() throws InterruptedException { |
393 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
389 |
|
for (int i = 0; i < SIZE; ++i) { |
390 |
|
assertEquals(i, ((Integer)q.take()).intValue()); |
391 |
|
} |
392 |
< |
q.take(); |
393 |
< |
}}; |
392 |
> |
try { |
393 |
> |
q.take(); |
394 |
> |
shouldThrow(); |
395 |
> |
} catch (InterruptedException success) {} |
396 |
> |
}}); |
397 |
|
|
398 |
|
t.start(); |
399 |
|
Thread.sleep(SHORT_DELAY_MS); |
401 |
|
t.join(); |
402 |
|
} |
403 |
|
|
406 |
– |
|
404 |
|
/** |
405 |
|
* poll succeeds unless empty |
406 |
|
*/ |