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 |
|
/** |
284 |
|
*/ |
285 |
|
public void testBlockingTake() throws InterruptedException { |
286 |
|
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
287 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
288 |
< |
void realRun() throws InterruptedException { |
287 |
> |
Thread t = new Thread(new CheckedRunnable() { |
288 |
> |
public void realRun() throws InterruptedException { |
289 |
|
for (int i = 0; i < SIZE; ++i) { |
290 |
< |
threadAssertEquals(i, (int) q.take()); |
290 |
> |
assertEquals(i, (int) q.take()); |
291 |
|
} |
292 |
< |
q.take(); |
292 |
> |
try { |
293 |
> |
q.take(); |
294 |
> |
shouldThrow(); |
295 |
> |
} catch (InterruptedException success) {} |
296 |
|
}}); |
297 |
< |
Thread.sleep(SMALL_DELAY_MS); |
297 |
> |
|
298 |
> |
t.start(); |
299 |
> |
Thread.sleep(SHORT_DELAY_MS); |
300 |
|
t.interrupt(); |
301 |
|
t.join(); |
302 |
|
checkEmpty(q); |
347 |
|
*/ |
348 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
349 |
|
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
350 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
350 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
351 |
|
void realRun() throws InterruptedException { |
352 |
|
for (int i = 0; i < SIZE; ++i) { |
353 |
|
long t0 = System.nanoTime(); |
356 |
|
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
357 |
|
assertTrue(millisElapsed < SMALL_DELAY_MS); |
358 |
|
} |
359 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
359 |
> |
try { |
360 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
361 |
> |
shouldThrow(); |
362 |
> |
} catch (InterruptedException success) {} |
363 |
|
}}); |
364 |
+ |
|
365 |
|
Thread.sleep(SMALL_DELAY_MS); |
366 |
|
t.interrupt(); |
367 |
|
t.join(); |
374 |
|
*/ |
375 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
376 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
377 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
377 |
> |
Thread t = new Thread(new CheckedRunnable() { |
378 |
|
void realRun() throws InterruptedException { |
379 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
380 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
381 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
379 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
380 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
381 |
> |
try { |
382 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
383 |
> |
shouldThrow(); |
384 |
> |
} catch (InterruptedException success) {} |
385 |
|
}}); |
386 |
+ |
|
387 |
+ |
t.start(); |
388 |
|
Thread.sleep(SMALL_DELAY_MS); |
389 |
|
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
390 |
|
t.interrupt(); |
418 |
|
try { |
419 |
|
q.element(); |
420 |
|
shouldThrow(); |
421 |
< |
} catch (NoSuchElementException success) { |
421 |
< |
} |
421 |
> |
} catch (NoSuchElementException success) {} |
422 |
|
checkEmpty(q); |
423 |
|
} |
424 |
|
|
433 |
|
try { |
434 |
|
q.remove(); |
435 |
|
shouldThrow(); |
436 |
< |
} catch (NoSuchElementException success) { |
437 |
< |
} |
436 |
> |
} catch (NoSuchElementException success) {} |
437 |
|
checkEmpty(q); |
438 |
|
} |
439 |
|
|
544 |
|
} |
545 |
|
|
546 |
|
/** |
547 |
< |
* toArray contains all elements |
547 |
> |
* toArray() contains all elements |
548 |
|
*/ |
549 |
|
public void testToArray() throws InterruptedException { |
550 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
574 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
575 |
|
Object o[] = q.toArray(null); |
576 |
|
shouldThrow(); |
577 |
< |
} catch (NullPointerException success) { |
579 |
< |
} |
577 |
> |
} catch (NullPointerException success) {} |
578 |
|
} |
579 |
|
|
580 |
|
/** |
581 |
< |
* toArray with incompatible array type throws CCE |
581 |
> |
* toArray(incompatible array type) throws CCE |
582 |
|
*/ |
583 |
|
public void testToArray1_BadArg() { |
584 |
|
try { |
585 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
586 |
|
Object o[] = q.toArray(new String[10]); |
587 |
|
shouldThrow(); |
588 |
< |
} catch (ArrayStoreException success) { |
591 |
< |
} |
588 |
> |
} catch (ArrayStoreException success) {} |
589 |
|
} |
590 |
|
|
591 |
|
/** |
689 |
|
} |
690 |
|
|
691 |
|
/** |
692 |
< |
* poll retrieves elements across Executor threads |
692 |
> |
* timed poll retrieves elements across Executor threads |
693 |
|
*/ |
694 |
|
public void testPollInExecutor() { |
695 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
744 |
|
try { |
745 |
|
q.drainTo(null); |
746 |
|
shouldThrow(); |
747 |
< |
} catch (NullPointerException success) { |
751 |
< |
} |
747 |
> |
} catch (NullPointerException success) {} |
748 |
|
} |
749 |
|
|
750 |
|
/** |
755 |
|
try { |
756 |
|
q.drainTo(q); |
757 |
|
shouldThrow(); |
758 |
< |
} catch (IllegalArgumentException success) { |
763 |
< |
} |
758 |
> |
} catch (IllegalArgumentException success) {} |
759 |
|
} |
760 |
|
|
761 |
|
/** |
785 |
|
} |
786 |
|
|
787 |
|
/** |
788 |
< |
* drainTo empties full queue, unblocking a waiting put. |
788 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
789 |
|
*/ |
790 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
791 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
811 |
|
try { |
812 |
|
q.drainTo(null, SIZE); |
813 |
|
shouldThrow(); |
814 |
< |
} catch (NullPointerException success) { |
820 |
< |
} |
814 |
> |
} catch (NullPointerException success) {} |
815 |
|
} |
816 |
|
|
817 |
|
/** |
822 |
|
try { |
823 |
|
q.drainTo(q, SIZE); |
824 |
|
shouldThrow(); |
825 |
< |
} catch (IllegalArgumentException success) { |
832 |
< |
} |
825 |
> |
} catch (IllegalArgumentException success) {} |
826 |
|
} |
827 |
|
|
828 |
|
/** |
848 |
|
} |
849 |
|
|
850 |
|
/** |
851 |
< |
* poll and take decrement the waiting consumer count |
851 |
> |
* timed poll() or take() increments the waiting consumer count; |
852 |
> |
* offer(e) decrements the waiting consumer count |
853 |
|
*/ |
854 |
|
public void testWaitingConsumer() throws InterruptedException { |
855 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
856 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
856 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
857 |
> |
assertFalse(q.hasWaitingConsumer()); |
858 |
|
|
859 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
860 |
|
void realRun() throws InterruptedException { |
861 |
|
Thread.sleep(SMALL_DELAY_MS); |
862 |
|
threadAssertTrue(q.hasWaitingConsumer()); |
863 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
863 |
> |
threadAssertEquals(q.getWaitingConsumerCount(), 1); |
864 |
|
threadAssertTrue(q.offer(new Object())); |
865 |
+ |
threadAssertFalse(q.hasWaitingConsumer()); |
866 |
+ |
threadAssertEquals(q.getWaitingConsumerCount(), 0); |
867 |
|
}}); |
868 |
|
|
869 |
|
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
870 |
< |
assertTrue(q.getWaitingConsumerCount() |
871 |
< |
< waiting.getWaitingConsumers()); |
870 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
871 |
> |
assertFalse(q.hasWaitingConsumer()); |
872 |
|
t.join(); |
873 |
|
} |
874 |
|
|
880 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
881 |
|
q.transfer(null); |
882 |
|
shouldThrow(); |
883 |
< |
} catch (NullPointerException ex) { |
887 |
< |
} |
883 |
> |
} catch (NullPointerException success) {} |
884 |
|
} |
885 |
|
|
886 |
|
/** |
987 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
988 |
|
q.tryTransfer(null); |
989 |
|
shouldThrow(); |
990 |
< |
} catch (NullPointerException ex) { |
995 |
< |
} |
990 |
> |
} catch (NullPointerException success) {} |
991 |
|
} |
992 |
|
|
993 |
|
/** |
1116 |
|
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1117 |
|
assertEquals(1, q.size()); |
1118 |
|
assertEquals(four, q.poll()); |
1124 |
– |
checkEmpty(q); |
1119 |
|
assertNull(q.poll()); |
1120 |
+ |
checkEmpty(q); |
1121 |
|
} |
1122 |
|
|
1123 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1131 |
|
assertFalse(q.isEmpty()); |
1132 |
|
return q; |
1133 |
|
} |
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 |
– |
} |
1134 |
|
} |