41 |
|
} |
42 |
|
|
43 |
|
public static Test suite() { |
44 |
+ |
class Implementation implements CollectionImplementation { |
45 |
+ |
public Class<?> klazz() { return LinkedBlockingQueue.class; } |
46 |
+ |
public Collection emptyCollection() { return new LinkedBlockingQueue(); } |
47 |
+ |
public Object makeElement(int i) { return i; } |
48 |
+ |
public boolean isConcurrent() { return true; } |
49 |
+ |
public boolean permitsNulls() { return false; } |
50 |
+ |
} |
51 |
|
return newTestSuite(LinkedBlockingQueueTest.class, |
52 |
|
new Unbounded().testSuite(), |
53 |
< |
new Bounded().testSuite()); |
53 |
> |
new Bounded().testSuite(), |
54 |
> |
CollectionTest.testSuite(new Implementation())); |
55 |
|
} |
56 |
|
|
57 |
|
/** |
58 |
|
* Returns a new queue of given size containing consecutive |
59 |
< |
* Integers 0 ... n. |
59 |
> |
* Integers 0 ... n - 1. |
60 |
|
*/ |
61 |
< |
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
61 |
> |
private static LinkedBlockingQueue<Integer> populatedQueue(int n) { |
62 |
|
LinkedBlockingQueue<Integer> q = |
63 |
|
new LinkedBlockingQueue<Integer>(n); |
64 |
|
assertTrue(q.isEmpty()); |
67 |
|
assertFalse(q.isEmpty()); |
68 |
|
assertEquals(0, q.remainingCapacity()); |
69 |
|
assertEquals(n, q.size()); |
70 |
+ |
assertEquals((Integer) 0, q.peek()); |
71 |
|
return q; |
72 |
|
} |
73 |
|
|
292 |
|
}}); |
293 |
|
|
294 |
|
await(pleaseInterrupt); |
295 |
< |
assertThreadStaysAlive(t); |
295 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
296 |
|
t.interrupt(); |
297 |
|
awaitTermination(t); |
298 |
|
assertEquals(SIZE, q.size()); |
314 |
|
pleaseTake.countDown(); |
315 |
|
q.put(86); |
316 |
|
|
317 |
+ |
Thread.currentThread().interrupt(); |
318 |
+ |
try { |
319 |
+ |
q.put(99); |
320 |
+ |
shouldThrow(); |
321 |
+ |
} catch (InterruptedException success) {} |
322 |
+ |
assertFalse(Thread.interrupted()); |
323 |
+ |
|
324 |
|
pleaseInterrupt.countDown(); |
325 |
|
try { |
326 |
|
q.put(99); |
334 |
|
assertEquals(0, q.take()); |
335 |
|
|
336 |
|
await(pleaseInterrupt); |
337 |
< |
assertThreadStaysAlive(t); |
337 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
338 |
|
t.interrupt(); |
339 |
|
awaitTermination(t); |
340 |
|
assertEquals(0, q.remainingCapacity()); |
350 |
|
public void realRun() throws InterruptedException { |
351 |
|
q.put(new Object()); |
352 |
|
q.put(new Object()); |
353 |
+ |
|
354 |
|
long startTime = System.nanoTime(); |
355 |
|
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
356 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
357 |
+ |
|
358 |
+ |
Thread.currentThread().interrupt(); |
359 |
+ |
try { |
360 |
+ |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
361 |
+ |
shouldThrow(); |
362 |
+ |
} catch (InterruptedException success) {} |
363 |
+ |
assertFalse(Thread.interrupted()); |
364 |
+ |
|
365 |
|
pleaseInterrupt.countDown(); |
366 |
|
try { |
367 |
|
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
368 |
|
shouldThrow(); |
369 |
|
} catch (InterruptedException success) {} |
370 |
+ |
assertFalse(Thread.interrupted()); |
371 |
|
}}); |
372 |
|
|
373 |
|
await(pleaseInterrupt); |
374 |
< |
assertThreadStaysAlive(t); |
374 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
375 |
|
t.interrupt(); |
376 |
|
awaitTermination(t); |
377 |
|
} |
394 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
395 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
396 |
|
public void realRun() throws InterruptedException { |
397 |
< |
for (int i = 0; i < SIZE; ++i) { |
372 |
< |
assertEquals(i, q.take()); |
373 |
< |
} |
397 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); |
398 |
|
|
399 |
|
Thread.currentThread().interrupt(); |
400 |
|
try { |
412 |
|
}}); |
413 |
|
|
414 |
|
await(pleaseInterrupt); |
415 |
< |
assertThreadStaysAlive(t); |
415 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
416 |
|
t.interrupt(); |
417 |
|
awaitTermination(t); |
418 |
|
} |
461 |
|
*/ |
462 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
463 |
|
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
464 |
< |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
464 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
465 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
466 |
|
public void realRun() throws InterruptedException { |
467 |
|
long startTime = System.nanoTime(); |
468 |
< |
for (int i = 0; i < SIZE; ++i) { |
468 |
> |
for (int i = 0; i < SIZE; i++) |
469 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
470 |
< |
} |
471 |
< |
aboutToWait.countDown(); |
470 |
> |
|
471 |
> |
Thread.currentThread().interrupt(); |
472 |
> |
try { |
473 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
474 |
> |
shouldThrow(); |
475 |
> |
} catch (InterruptedException success) {} |
476 |
> |
assertFalse(Thread.interrupted()); |
477 |
> |
|
478 |
> |
pleaseInterrupt.countDown(); |
479 |
|
try { |
480 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
481 |
|
shouldThrow(); |
482 |
< |
} catch (InterruptedException success) { |
483 |
< |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
484 |
< |
} |
482 |
> |
} catch (InterruptedException success) {} |
483 |
> |
assertFalse(Thread.interrupted()); |
484 |
> |
|
485 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
486 |
|
}}); |
487 |
|
|
488 |
< |
await(aboutToWait); |
489 |
< |
waitForThreadToEnterWaitState(t, LONG_DELAY_MS); |
488 |
> |
await(pleaseInterrupt); |
489 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
490 |
|
t.interrupt(); |
491 |
|
awaitTermination(t); |
492 |
|
checkEmpty(q); |
796 |
|
} |
797 |
|
|
798 |
|
/** |
799 |
< |
* A deserialized serialized queue has same elements in same order |
799 |
> |
* A deserialized/reserialized queue has same elements in same order |
800 |
|
*/ |
801 |
|
public void testSerialization() throws Exception { |
802 |
|
Queue x = populatedQueue(SIZE); |