59 |
59 |
60 public static Test suite() { |
60 public static Test suite() { |
61 class Implementation implements CollectionImplementation { |
61 class Implementation implements CollectionImplementation { |
62 public Class<?> klazz() { return ArrayBlockingQueue.class; } |
62 public Class<?> klazz() { return ArrayBlockingQueue.class; } |
63 public Collection emptyCollection() { |
63 public Collection emptyCollection() { |
64 boolean fair = ThreadLocalRandom.current().nextBoolean(); |
64 boolean fair = randomBoolean(); |
65 return populatedQueue(0, SIZE, 2 * SIZE, fair); |
65 return populatedQueue(0, SIZE, 2 * SIZE, fair); |
66 } |
66 } |
67 public Object makeElement(int i) { return i; } |
67 public Object makeElement(int i) { return i; } |
68 public boolean isConcurrent() { return true; } |
68 public boolean isConcurrent() { return true; } |
69 public boolean permitsNulls() { return false; } |
69 public boolean permitsNulls() { return false; } |
365 } catch (InterruptedException success) {} |
365 } catch (InterruptedException success) {} |
366 assertFalse(Thread.interrupted()); |
366 assertFalse(Thread.interrupted()); |
367 }}); |
367 }}); |
368 |
368 |
369 await(pleaseInterrupt); |
369 await(pleaseInterrupt); |
370 assertThreadBlocks(t, Thread.State.WAITING); |
370 if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
371 t.interrupt(); |
371 t.interrupt(); |
372 awaitTermination(t); |
372 awaitTermination(t); |
373 assertEquals(SIZE, q.size()); |
373 assertEquals(SIZE, q.size()); |
374 assertEquals(0, q.remainingCapacity()); |
374 assertEquals(0, q.remainingCapacity()); |
375 } |
375 } |
407 await(pleaseTake); |
407 await(pleaseTake); |
408 assertEquals(0, q.remainingCapacity()); |
408 assertEquals(0, q.remainingCapacity()); |
409 assertEquals(0, q.take()); |
409 assertEquals(0, q.take()); |
410 |
410 |
411 await(pleaseInterrupt); |
411 await(pleaseInterrupt); |
412 assertThreadBlocks(t, Thread.State.WAITING); |
412 if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
413 t.interrupt(); |
413 t.interrupt(); |
414 awaitTermination(t); |
414 awaitTermination(t); |
415 assertEquals(0, q.remainingCapacity()); |
415 assertEquals(0, q.remainingCapacity()); |
416 } |
416 } |
417 |
417 |
429 assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
429 assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
430 assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
430 assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
431 |
431 |
432 Thread.currentThread().interrupt(); |
432 Thread.currentThread().interrupt(); |
433 try { |
433 try { |
434 q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
434 q.offer(new Object(), randomTimeout(), randomTimeUnit()); |
435 shouldThrow(); |
435 shouldThrow(); |
436 } catch (InterruptedException success) {} |
436 } catch (InterruptedException success) {} |
437 assertFalse(Thread.interrupted()); |
437 assertFalse(Thread.interrupted()); |
438 |
438 |
439 pleaseInterrupt.countDown(); |
439 pleaseInterrupt.countDown(); |
440 try { |
440 try { |
441 q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
441 q.offer(new Object(), LONGER_DELAY_MS, MILLISECONDS); |
442 shouldThrow(); |
442 shouldThrow(); |
443 } catch (InterruptedException success) {} |
443 } catch (InterruptedException success) {} |
444 assertFalse(Thread.interrupted()); |
444 assertFalse(Thread.interrupted()); |
445 }}); |
445 }}); |
446 |
446 |
447 await(pleaseInterrupt); |
447 await(pleaseInterrupt); |
448 assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
448 if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
449 t.interrupt(); |
449 t.interrupt(); |
450 awaitTermination(t); |
450 awaitTermination(t); |
451 } |
451 } |
452 |
452 |
453 /** |
453 /** |
537 public void testInterruptedTimedPoll() throws InterruptedException { |
537 public void testInterruptedTimedPoll() throws InterruptedException { |
538 final BlockingQueue<Integer> q = populatedQueue(SIZE); |
538 final BlockingQueue<Integer> q = populatedQueue(SIZE); |
539 final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
539 final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
540 Thread t = newStartedThread(new CheckedRunnable() { |
540 Thread t = newStartedThread(new CheckedRunnable() { |
541 public void realRun() throws InterruptedException { |
541 public void realRun() throws InterruptedException { |
542 long startTime = System.nanoTime(); |
|
543 for (int i = 0; i < SIZE; i++) |
542 for (int i = 0; i < SIZE; i++) |
544 assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
543 assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
545 |
544 |
546 Thread.currentThread().interrupt(); |
545 Thread.currentThread().interrupt(); |
547 try { |
546 try { |
548 q.poll(LONG_DELAY_MS, MILLISECONDS); |
547 q.poll(randomTimeout(), randomTimeUnit()); |
549 shouldThrow(); |
548 shouldThrow(); |
550 } catch (InterruptedException success) {} |
549 } catch (InterruptedException success) {} |
551 assertFalse(Thread.interrupted()); |
550 assertFalse(Thread.interrupted()); |
552 |
551 |
553 pleaseInterrupt.countDown(); |
552 pleaseInterrupt.countDown(); |
554 try { |
553 try { |
555 q.poll(LONG_DELAY_MS, MILLISECONDS); |
554 q.poll(LONGER_DELAY_MS, MILLISECONDS); |
556 shouldThrow(); |
555 shouldThrow(); |
557 } catch (InterruptedException success) {} |
556 } catch (InterruptedException success) {} |
558 assertFalse(Thread.interrupted()); |
557 assertFalse(Thread.interrupted()); |
559 |
|
560 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
|
561 }}); |
558 }}); |
562 |
559 |
563 await(pleaseInterrupt); |
560 await(pleaseInterrupt); |
564 assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
561 if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
565 t.interrupt(); |
562 t.interrupt(); |
566 awaitTermination(t); |
563 awaitTermination(t); |
567 checkEmpty(q); |
564 checkEmpty(q); |
568 } |
565 } |
569 |
566 |