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; |
44 |
|
try { |
45 |
|
q.element(); |
46 |
|
shouldThrow(); |
47 |
< |
} catch (NoSuchElementException success) { |
49 |
< |
} |
47 |
> |
} catch (NoSuchElementException success) {} |
48 |
|
try { |
49 |
|
q.iterator().next(); |
50 |
|
shouldThrow(); |
51 |
< |
} catch (NoSuchElementException success) { |
54 |
< |
} |
51 |
> |
} catch (NoSuchElementException success) {} |
52 |
|
try { |
53 |
|
q.remove(); |
54 |
|
shouldThrow(); |
55 |
< |
} catch (NoSuchElementException success) { |
59 |
< |
} |
55 |
> |
} catch (NoSuchElementException success) {} |
56 |
|
} |
57 |
|
|
58 |
|
/** |
72 |
|
try { |
73 |
|
new LinkedTransferQueue(null); |
74 |
|
shouldThrow(); |
75 |
< |
} catch (NullPointerException success) { |
80 |
< |
} |
75 |
> |
} catch (NullPointerException success) {} |
76 |
|
} |
77 |
|
|
78 |
|
/** |
84 |
|
Integer[] ints = new Integer[SIZE]; |
85 |
|
new LinkedTransferQueue(Arrays.asList(ints)); |
86 |
|
shouldThrow(); |
87 |
< |
} catch (NullPointerException success) { |
93 |
< |
} |
87 |
> |
} catch (NullPointerException success) {} |
88 |
|
} |
89 |
|
|
90 |
|
/** |
99 |
|
} |
100 |
|
new LinkedTransferQueue(Arrays.asList(ints)); |
101 |
|
shouldThrow(); |
102 |
< |
} catch (NullPointerException success) { |
109 |
< |
} |
102 |
> |
} catch (NullPointerException success) {} |
103 |
|
} |
104 |
|
|
105 |
|
/** |
151 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
152 |
|
q.offer(null); |
153 |
|
shouldThrow(); |
154 |
< |
} catch (NullPointerException success) { |
162 |
< |
} |
154 |
> |
} catch (NullPointerException success) {} |
155 |
|
} |
156 |
|
|
157 |
|
/** |
162 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
163 |
|
q.add(null); |
164 |
|
shouldThrow(); |
165 |
< |
} catch (NullPointerException success) { |
174 |
< |
} |
165 |
> |
} catch (NullPointerException success) {} |
166 |
|
} |
167 |
|
|
168 |
|
/** |
173 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
174 |
|
q.addAll(null); |
175 |
|
shouldThrow(); |
176 |
< |
} catch (NullPointerException success) { |
186 |
< |
} |
176 |
> |
} catch (NullPointerException success) {} |
177 |
|
} |
178 |
|
|
179 |
|
/** |
184 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
185 |
|
q.addAll(q); |
186 |
|
shouldThrow(); |
187 |
< |
} catch (IllegalArgumentException success) { |
198 |
< |
} |
187 |
> |
} catch (IllegalArgumentException success) {} |
188 |
|
} |
189 |
|
|
190 |
|
/** |
196 |
|
Integer[] ints = new Integer[SIZE]; |
197 |
|
q.addAll(Arrays.asList(ints)); |
198 |
|
shouldThrow(); |
199 |
< |
} catch (NullPointerException success) { |
211 |
< |
} |
199 |
> |
} catch (NullPointerException success) {} |
200 |
|
} |
201 |
|
|
202 |
|
/** |
212 |
|
} |
213 |
|
q.addAll(Arrays.asList(ints)); |
214 |
|
shouldThrow(); |
215 |
< |
} catch (NullPointerException success) { |
228 |
< |
} |
215 |
> |
} catch (NullPointerException success) {} |
216 |
|
} |
217 |
|
|
218 |
|
/** |
342 |
|
*/ |
343 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
344 |
|
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
345 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
345 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
346 |
|
void realRun() throws InterruptedException { |
347 |
|
for (int i = 0; i < SIZE; ++i) { |
348 |
|
long t0 = System.nanoTime(); |
351 |
|
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
352 |
|
assertTrue(millisElapsed < SMALL_DELAY_MS); |
353 |
|
} |
354 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
354 |
> |
try { |
355 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
356 |
> |
shouldThrow(); |
357 |
> |
} catch (InterruptedException success) {} |
358 |
|
}}); |
359 |
+ |
|
360 |
|
Thread.sleep(SMALL_DELAY_MS); |
361 |
|
t.interrupt(); |
362 |
|
t.join(); |
369 |
|
*/ |
370 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
371 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
372 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
372 |
> |
Thread t = new Thread(new CheckedRunnable() { |
373 |
|
void realRun() throws InterruptedException { |
374 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
375 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
376 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
374 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
375 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
376 |
> |
try { |
377 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
378 |
> |
shouldThrow(); |
379 |
> |
} catch (InterruptedException success) {} |
380 |
|
}}); |
381 |
+ |
|
382 |
|
Thread.sleep(SMALL_DELAY_MS); |
383 |
|
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
384 |
|
t.interrupt(); |
412 |
|
try { |
413 |
|
q.element(); |
414 |
|
shouldThrow(); |
415 |
< |
} catch (NoSuchElementException success) { |
421 |
< |
} |
415 |
> |
} catch (NoSuchElementException success) {} |
416 |
|
checkEmpty(q); |
417 |
|
} |
418 |
|
|
427 |
|
try { |
428 |
|
q.remove(); |
429 |
|
shouldThrow(); |
430 |
< |
} catch (NoSuchElementException success) { |
437 |
< |
} |
430 |
> |
} catch (NoSuchElementException success) {} |
431 |
|
checkEmpty(q); |
432 |
|
} |
433 |
|
|
538 |
|
} |
539 |
|
|
540 |
|
/** |
541 |
< |
* toArray contains all elements |
541 |
> |
* toArray() contains all elements |
542 |
|
*/ |
543 |
|
public void testToArray() throws InterruptedException { |
544 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
568 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
569 |
|
Object o[] = q.toArray(null); |
570 |
|
shouldThrow(); |
571 |
< |
} catch (NullPointerException success) { |
579 |
< |
} |
571 |
> |
} catch (NullPointerException success) {} |
572 |
|
} |
573 |
|
|
574 |
|
/** |
575 |
< |
* toArray with incompatible array type throws CCE |
575 |
> |
* toArray(incompatible array type) throws CCE |
576 |
|
*/ |
577 |
|
public void testToArray1_BadArg() { |
578 |
|
try { |
579 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
580 |
|
Object o[] = q.toArray(new String[10]); |
581 |
|
shouldThrow(); |
582 |
< |
} catch (ArrayStoreException success) { |
591 |
< |
} |
582 |
> |
} catch (ArrayStoreException success) {} |
583 |
|
} |
584 |
|
|
585 |
|
/** |
683 |
|
} |
684 |
|
|
685 |
|
/** |
686 |
< |
* poll retrieves elements across Executor threads |
686 |
> |
* timed poll retrieves elements across Executor threads |
687 |
|
*/ |
688 |
|
public void testPollInExecutor() { |
689 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
738 |
|
try { |
739 |
|
q.drainTo(null); |
740 |
|
shouldThrow(); |
741 |
< |
} catch (NullPointerException success) { |
751 |
< |
} |
741 |
> |
} catch (NullPointerException success) {} |
742 |
|
} |
743 |
|
|
744 |
|
/** |
749 |
|
try { |
750 |
|
q.drainTo(q); |
751 |
|
shouldThrow(); |
752 |
< |
} catch (IllegalArgumentException success) { |
763 |
< |
} |
752 |
> |
} catch (IllegalArgumentException success) {} |
753 |
|
} |
754 |
|
|
755 |
|
/** |
779 |
|
} |
780 |
|
|
781 |
|
/** |
782 |
< |
* drainTo empties full queue, unblocking a waiting put. |
782 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
783 |
|
*/ |
784 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
785 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
805 |
|
try { |
806 |
|
q.drainTo(null, SIZE); |
807 |
|
shouldThrow(); |
808 |
< |
} catch (NullPointerException success) { |
820 |
< |
} |
808 |
> |
} catch (NullPointerException success) {} |
809 |
|
} |
810 |
|
|
811 |
|
/** |
816 |
|
try { |
817 |
|
q.drainTo(q, SIZE); |
818 |
|
shouldThrow(); |
819 |
< |
} catch (IllegalArgumentException success) { |
832 |
< |
} |
819 |
> |
} catch (IllegalArgumentException success) {} |
820 |
|
} |
821 |
|
|
822 |
|
/** |
842 |
|
} |
843 |
|
|
844 |
|
/** |
845 |
< |
* poll and take decrement the waiting consumer count |
845 |
> |
* timed poll() or take() increments the waiting consumer count; |
846 |
> |
* offer(e) decrements the waiting consumer count |
847 |
|
*/ |
848 |
|
public void testWaitingConsumer() throws InterruptedException { |
849 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
850 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
850 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
851 |
> |
assertFalse(q.hasWaitingConsumer()); |
852 |
|
|
853 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
854 |
|
void realRun() throws InterruptedException { |
855 |
|
Thread.sleep(SMALL_DELAY_MS); |
856 |
|
threadAssertTrue(q.hasWaitingConsumer()); |
857 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
857 |
> |
threadAssertEquals(q.getWaitingConsumerCount(), 1); |
858 |
|
threadAssertTrue(q.offer(new Object())); |
859 |
+ |
threadAssertFalse(q.hasWaitingConsumer()); |
860 |
+ |
threadAssertEquals(q.getWaitingConsumerCount(), 0); |
861 |
|
}}); |
862 |
|
|
863 |
|
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
864 |
< |
assertTrue(q.getWaitingConsumerCount() |
865 |
< |
< waiting.getWaitingConsumers()); |
864 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
865 |
> |
assertFalse(q.hasWaitingConsumer()); |
866 |
|
t.join(); |
867 |
|
} |
868 |
|
|
874 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
875 |
|
q.transfer(null); |
876 |
|
shouldThrow(); |
877 |
< |
} catch (NullPointerException ex) { |
887 |
< |
} |
877 |
> |
} catch (NullPointerException success) {} |
878 |
|
} |
879 |
|
|
880 |
|
/** |
981 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
982 |
|
q.tryTransfer(null); |
983 |
|
shouldThrow(); |
984 |
< |
} catch (NullPointerException ex) { |
995 |
< |
} |
984 |
> |
} catch (NullPointerException success) {} |
985 |
|
} |
986 |
|
|
987 |
|
/** |
1110 |
|
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1111 |
|
assertEquals(1, q.size()); |
1112 |
|
assertEquals(four, q.poll()); |
1124 |
– |
checkEmpty(q); |
1113 |
|
assertNull(q.poll()); |
1114 |
+ |
checkEmpty(q); |
1115 |
|
} |
1116 |
|
|
1117 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1125 |
|
assertFalse(q.isEmpty()); |
1126 |
|
return q; |
1127 |
|
} |
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 |
– |
} |
1128 |
|
} |