13 |
|
import java.io.ObjectOutputStream; |
14 |
|
import java.util.ArrayList; |
15 |
|
import java.util.Arrays; |
16 |
– |
import java.util.ConcurrentModificationException; |
16 |
|
import java.util.Iterator; |
17 |
|
import java.util.List; |
18 |
|
import java.util.NoSuchElementException; |
354 |
|
*/ |
355 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
356 |
|
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
357 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
357 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
358 |
|
void realRun() throws InterruptedException { |
359 |
|
for (int i = 0; i < SIZE; ++i) { |
360 |
|
long t0 = System.nanoTime(); |
363 |
|
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
364 |
|
assertTrue(millisElapsed < SMALL_DELAY_MS); |
365 |
|
} |
366 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
366 |
> |
try { |
367 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
368 |
> |
} catch (InterruptedException success) {} |
369 |
|
}}); |
370 |
|
Thread.sleep(SMALL_DELAY_MS); |
371 |
|
t.interrupt(); |
546 |
|
} |
547 |
|
|
548 |
|
/** |
549 |
< |
* toArray contains all elements |
549 |
> |
* toArray() contains all elements |
550 |
|
*/ |
551 |
|
public void testToArray() throws InterruptedException { |
552 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
581 |
|
} |
582 |
|
|
583 |
|
/** |
584 |
< |
* toArray with incompatible array type throws CCE |
584 |
> |
* toArray(incompatible array type) throws CCE |
585 |
|
*/ |
586 |
|
public void testToArray1_BadArg() { |
587 |
|
try { |
693 |
|
} |
694 |
|
|
695 |
|
/** |
696 |
< |
* poll retrieves elements across Executor threads |
696 |
> |
* timed poll retrieves elements across Executor threads |
697 |
|
*/ |
698 |
|
public void testPollInExecutor() { |
699 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
791 |
|
} |
792 |
|
|
793 |
|
/** |
794 |
< |
* drainTo empties full queue, unblocking a waiting put. |
794 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
795 |
|
*/ |
796 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
797 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
856 |
|
} |
857 |
|
|
858 |
|
/** |
859 |
< |
* poll and take decrement the waiting consumer count |
859 |
> |
* timed poll() or take() increments the waiting consumer count; |
860 |
> |
* offer(e) decrements the waiting consumer count |
861 |
|
*/ |
862 |
|
public void testWaitingConsumer() throws InterruptedException { |
863 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
864 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
864 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
865 |
> |
assertFalse(q.hasWaitingConsumer()); |
866 |
|
|
867 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
868 |
|
void realRun() throws InterruptedException { |
869 |
|
Thread.sleep(SMALL_DELAY_MS); |
870 |
|
threadAssertTrue(q.hasWaitingConsumer()); |
871 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
871 |
> |
threadAssertEquals(q.getWaitingConsumerCount(), 1); |
872 |
|
threadAssertTrue(q.offer(new Object())); |
873 |
+ |
threadAssertFalse(q.hasWaitingConsumer()); |
874 |
+ |
threadAssertEquals(q.getWaitingConsumerCount(), 0); |
875 |
|
}}); |
876 |
|
|
877 |
|
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
878 |
< |
assertTrue(q.getWaitingConsumerCount() |
879 |
< |
< waiting.getWaitingConsumers()); |
878 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
879 |
> |
assertFalse(q.hasWaitingConsumer()); |
880 |
|
t.join(); |
881 |
|
} |
882 |
|
|
888 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
889 |
|
q.transfer(null); |
890 |
|
shouldThrow(); |
891 |
< |
} catch (NullPointerException ex) { |
887 |
< |
} |
891 |
> |
} catch (NullPointerException success) {} |
892 |
|
} |
893 |
|
|
894 |
|
/** |
995 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
996 |
|
q.tryTransfer(null); |
997 |
|
shouldThrow(); |
998 |
< |
} catch (NullPointerException ex) { |
995 |
< |
} |
998 |
> |
} catch (NullPointerException success) {} |
999 |
|
} |
1000 |
|
|
1001 |
|
/** |
1124 |
|
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1125 |
|
assertEquals(1, q.size()); |
1126 |
|
assertEquals(four, q.poll()); |
1124 |
– |
checkEmpty(q); |
1127 |
|
assertNull(q.poll()); |
1128 |
+ |
checkEmpty(q); |
1129 |
|
} |
1130 |
|
|
1131 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1139 |
|
assertFalse(q.isEmpty()); |
1140 |
|
return q; |
1141 |
|
} |
1139 |
– |
|
1140 |
– |
private static class ConsumerObserver { |
1141 |
– |
|
1142 |
– |
private int waitingConsumers; |
1143 |
– |
|
1144 |
– |
private ConsumerObserver() { |
1145 |
– |
} |
1146 |
– |
|
1147 |
– |
private void setWaitingConsumer(int i) { |
1148 |
– |
this.waitingConsumers = i; |
1149 |
– |
} |
1150 |
– |
|
1151 |
– |
private int getWaitingConsumers() { |
1152 |
– |
return waitingConsumers; |
1153 |
– |
} |
1154 |
– |
} |
1142 |
|
} |