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; |
19 |
|
import java.util.concurrent.*; |
20 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
32 |
|
return new TestSuite(LinkedTransferQueueTest.class); |
33 |
|
} |
34 |
|
|
35 |
+ |
void checkEmpty(LinkedTransferQueue q) throws InterruptedException { |
36 |
+ |
assertTrue(q.isEmpty()); |
37 |
+ |
assertEquals(0, q.size()); |
38 |
+ |
assertNull(q.peek()); |
39 |
+ |
assertNull(q.poll()); |
40 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
41 |
+ |
assertEquals(q.toString(), "[]"); |
42 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
43 |
+ |
assertFalse(q.iterator().hasNext()); |
44 |
+ |
try { |
45 |
+ |
q.element(); |
46 |
+ |
shouldThrow(); |
47 |
+ |
} catch (NoSuchElementException success) { |
48 |
+ |
} |
49 |
+ |
try { |
50 |
+ |
q.iterator().next(); |
51 |
+ |
shouldThrow(); |
52 |
+ |
} catch (NoSuchElementException success) { |
53 |
+ |
} |
54 |
+ |
try { |
55 |
+ |
q.remove(); |
56 |
+ |
shouldThrow(); |
57 |
+ |
} catch (NoSuchElementException success) { |
58 |
+ |
} |
59 |
+ |
} |
60 |
+ |
|
61 |
|
/** |
62 |
|
* Constructor builds new queue with size being zero and empty |
63 |
|
* being true |
112 |
|
* Queue contains all elements of the collection it is initialized by |
113 |
|
*/ |
114 |
|
public void testConstructor5() { |
115 |
< |
try { |
116 |
< |
Integer[] ints = new Integer[SIZE]; |
117 |
< |
for (int i = 0; i < SIZE; ++i) { |
118 |
< |
ints[i] = i; |
119 |
< |
} |
120 |
< |
LinkedTransferQueue q |
121 |
< |
= new LinkedTransferQueue(Arrays.asList(ints)); |
122 |
< |
for (int i = 0; i < SIZE; ++i) { |
123 |
< |
assertEquals(ints[i], q.poll()); |
124 |
< |
} |
125 |
< |
} finally { |
115 |
> |
Integer[] ints = new Integer[SIZE]; |
116 |
> |
for (int i = 0; i < SIZE; ++i) { |
117 |
> |
ints[i] = i; |
118 |
> |
} |
119 |
> |
List intList = Arrays.asList(ints); |
120 |
> |
LinkedTransferQueue q |
121 |
> |
= new LinkedTransferQueue(intList); |
122 |
> |
assertEquals(q.size(), intList.size()); |
123 |
> |
assertEquals(q.toString(), intList.toString()); |
124 |
> |
assertTrue(Arrays.equals(q.toArray(), |
125 |
> |
intList.toArray())); |
126 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[0]), |
127 |
> |
intList.toArray(new Object[0]))); |
128 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[SIZE]), |
129 |
> |
intList.toArray(new Object[SIZE]))); |
130 |
> |
for (int i = 0; i < SIZE; ++i) { |
131 |
> |
assertEquals(ints[i], q.poll()); |
132 |
|
} |
133 |
|
} |
134 |
|
|
135 |
|
/** |
136 |
< |
* Remaining capacity never decrease nor increase on add or remove |
136 |
> |
* remainingCapacity() always returns Integer.MAX_VALUE |
137 |
|
*/ |
138 |
|
public void testRemainingCapacity() { |
139 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
261 |
|
public void testPut() { |
262 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
263 |
|
for (int i = 0; i < SIZE; ++i) { |
264 |
+ |
assertEquals(q.size(), i); |
265 |
|
q.put(i); |
266 |
|
assertTrue(q.contains(i)); |
267 |
|
} |
235 |
– |
assertEquals(q.size(), SIZE); |
268 |
|
} |
269 |
|
|
270 |
|
/** |
295 |
|
* Take removes existing elements until empty, then blocks interruptibly |
296 |
|
*/ |
297 |
|
public void testBlockingTake() throws InterruptedException { |
298 |
+ |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
299 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
300 |
|
void realRun() throws InterruptedException { |
268 |
– |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
301 |
|
for (int i = 0; i < SIZE; ++i) { |
302 |
|
threadAssertEquals(i, (int) q.take()); |
303 |
|
} |
304 |
|
q.take(); |
305 |
|
}}); |
306 |
< |
Thread.sleep(SHORT_DELAY_MS); |
306 |
> |
Thread.sleep(SMALL_DELAY_MS); |
307 |
|
t.interrupt(); |
308 |
|
t.join(); |
309 |
+ |
checkEmpty(q); |
310 |
|
} |
311 |
|
|
312 |
|
/** |
313 |
|
* poll succeeds unless empty |
314 |
|
*/ |
315 |
< |
public void testPoll() { |
315 |
> |
public void testPoll() throws InterruptedException { |
316 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
317 |
|
for (int i = 0; i < SIZE; ++i) { |
318 |
|
assertEquals(i, (int) q.poll()); |
319 |
|
} |
320 |
|
assertNull(q.poll()); |
321 |
+ |
checkEmpty(q); |
322 |
|
} |
323 |
|
|
324 |
|
/** |
330 |
|
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
331 |
|
} |
332 |
|
assertNull(q.poll(0, MILLISECONDS)); |
333 |
+ |
checkEmpty(q); |
334 |
|
} |
335 |
|
|
336 |
|
/** |
339 |
|
public void testTimedPoll() throws InterruptedException { |
340 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
341 |
|
for (int i = 0; i < SIZE; ++i) { |
342 |
< |
assertEquals(i, (int) q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
342 |
> |
long t0 = System.nanoTime(); |
343 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
344 |
> |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
345 |
> |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
346 |
|
} |
347 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
348 |
+ |
checkEmpty(q); |
349 |
|
} |
350 |
|
|
351 |
|
/** |
357 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
358 |
|
void realRun() throws InterruptedException { |
359 |
|
for (int i = 0; i < SIZE; ++i) { |
360 |
+ |
long t0 = System.nanoTime(); |
361 |
|
threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS, |
362 |
|
MILLISECONDS)); |
363 |
+ |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
364 |
+ |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
365 |
|
} |
366 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
367 |
|
}}); |
368 |
|
Thread.sleep(SMALL_DELAY_MS); |
369 |
|
t.interrupt(); |
370 |
|
t.join(); |
371 |
< |
assertEquals(q.size(), 0); |
330 |
< |
assertNull(q.poll()); |
371 |
> |
checkEmpty(q); |
372 |
|
} |
373 |
|
|
374 |
|
/** |
392 |
|
/** |
393 |
|
* peek returns next element, or null if empty |
394 |
|
*/ |
395 |
< |
public void testPeek() { |
395 |
> |
public void testPeek() throws InterruptedException { |
396 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
397 |
|
for (int i = 0; i < SIZE; ++i) { |
398 |
|
assertEquals(i, (int) q.peek()); |
401 |
|
i != (int) q.peek()); |
402 |
|
} |
403 |
|
assertNull(q.peek()); |
404 |
+ |
checkEmpty(q); |
405 |
|
} |
406 |
|
|
407 |
|
/** |
408 |
|
* element returns next element, or throws NoSuchElementException if empty |
409 |
|
*/ |
410 |
< |
public void testElement() { |
410 |
> |
public void testElement() throws InterruptedException { |
411 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
412 |
|
for (int i = 0; i < SIZE; ++i) { |
413 |
|
assertEquals(i, (int) q.element()); |
418 |
|
shouldThrow(); |
419 |
|
} catch (NoSuchElementException success) { |
420 |
|
} |
421 |
+ |
checkEmpty(q); |
422 |
|
} |
423 |
|
|
424 |
|
/** |
425 |
|
* remove removes next element, or throws NoSuchElementException if empty |
426 |
|
*/ |
427 |
< |
public void testRemove() { |
427 |
> |
public void testRemove() throws InterruptedException { |
428 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
429 |
|
for (int i = 0; i < SIZE; ++i) { |
430 |
|
assertEquals(i, (int) q.remove()); |
434 |
|
shouldThrow(); |
435 |
|
} catch (NoSuchElementException success) { |
436 |
|
} |
437 |
+ |
checkEmpty(q); |
438 |
|
} |
439 |
|
|
440 |
|
/** |
441 |
|
* remove(x) removes x and returns true if present |
442 |
|
*/ |
443 |
< |
public void testRemoveElement() { |
443 |
> |
public void testRemoveElement() throws InterruptedException { |
444 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
445 |
|
for (int i = 1; i < SIZE; i += 2) { |
446 |
|
assertTrue(q.remove(i)); |
449 |
|
assertTrue(q.remove(i)); |
450 |
|
assertFalse(q.remove(i + 1)); |
451 |
|
} |
452 |
< |
assertTrue(q.isEmpty()); |
452 |
> |
checkEmpty(q); |
453 |
|
} |
454 |
|
|
455 |
|
/** |
462 |
|
assertTrue(q.remove(one)); |
463 |
|
assertTrue(q.remove(two)); |
464 |
|
assertTrue(q.add(three)); |
465 |
< |
assertTrue(q.take() != null); |
465 |
> |
assertTrue(q.take() == three); |
466 |
|
} |
467 |
|
|
468 |
|
/** |
480 |
|
/** |
481 |
|
* clear removes all elements |
482 |
|
*/ |
483 |
< |
public void testClear() { |
483 |
> |
public void testClear() throws InterruptedException { |
484 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
485 |
|
q.clear(); |
486 |
< |
assertTrue(q.isEmpty()); |
443 |
< |
assertEquals(0, q.size()); |
486 |
> |
checkEmpty(q); |
487 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
488 |
|
q.add(one); |
489 |
|
assertFalse(q.isEmpty()); |
490 |
+ |
assertEquals(1, q.size()); |
491 |
|
assertTrue(q.contains(one)); |
492 |
|
q.clear(); |
493 |
< |
assertTrue(q.isEmpty()); |
493 |
> |
checkEmpty(q); |
494 |
|
} |
495 |
|
|
496 |
|
/** |
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); |
579 |
|
} |
580 |
|
|
581 |
|
/** |
582 |
< |
* toArray with incompatible array type throws CCE |
582 |
> |
* toArray(incompatible array type) throws CCE |
583 |
|
*/ |
584 |
|
public void testToArray1_BadArg() { |
585 |
|
try { |
604 |
|
} |
605 |
|
|
606 |
|
/** |
607 |
< |
* iterator.remove removes current element |
607 |
> |
* iterator.remove() removes current element |
608 |
|
*/ |
609 |
|
public void testIteratorRemove() { |
610 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
691 |
|
} |
692 |
|
|
693 |
|
/** |
694 |
< |
* poll retrieves elements across Executor threads |
694 |
> |
* timed poll retrieves elements across Executor threads |
695 |
|
*/ |
696 |
|
public void testPollInExecutor() { |
697 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
789 |
|
} |
790 |
|
|
791 |
|
/** |
792 |
< |
* drainTo empties full queue, unblocking a waiting put. |
792 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
793 |
|
*/ |
794 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
795 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
813 |
|
public void testDrainToNullN() { |
814 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
815 |
|
try { |
816 |
< |
q.drainTo(null, 0); |
816 |
> |
q.drainTo(null, SIZE); |
817 |
|
shouldThrow(); |
818 |
|
} catch (NullPointerException success) { |
819 |
|
} |
825 |
|
public void testDrainToSelfN() { |
826 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
827 |
|
try { |
828 |
< |
q.drainTo(q, 0); |
828 |
> |
q.drainTo(q, SIZE); |
829 |
|
shouldThrow(); |
830 |
|
} catch (IllegalArgumentException success) { |
831 |
|
} |
854 |
|
} |
855 |
|
|
856 |
|
/** |
857 |
< |
* poll and take decrement the waiting consumer count |
857 |
> |
* timed poll() or take() increments the waiting consumer count; |
858 |
> |
* offer(e) decrements the waiting consumer count |
859 |
|
*/ |
860 |
|
public void testWaitingConsumer() throws InterruptedException { |
861 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
862 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
862 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
863 |
> |
assertFalse(q.hasWaitingConsumer()); |
864 |
|
|
865 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
866 |
|
void realRun() throws InterruptedException { |
867 |
|
Thread.sleep(SMALL_DELAY_MS); |
868 |
|
threadAssertTrue(q.hasWaitingConsumer()); |
869 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
869 |
> |
threadAssertEquals(q.getWaitingConsumerCount(), 1); |
870 |
|
threadAssertTrue(q.offer(new Object())); |
871 |
+ |
threadAssertFalse(q.hasWaitingConsumer()); |
872 |
+ |
threadAssertEquals(q.getWaitingConsumerCount(), 0); |
873 |
|
}}); |
874 |
|
|
875 |
|
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
876 |
< |
assertTrue(q.getWaitingConsumerCount() |
877 |
< |
< waiting.getWaitingConsumers()); |
876 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
877 |
> |
assertFalse(q.hasWaitingConsumer()); |
878 |
|
t.join(); |
879 |
|
} |
880 |
|
|
977 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
978 |
|
void realRun() throws InterruptedException { |
979 |
|
q.transfer(SIZE); |
980 |
< |
threadAssertTrue(q.isEmpty()); |
980 |
> |
checkEmpty(q); |
981 |
|
}}); |
982 |
|
|
983 |
|
Thread.sleep(SHORT_DELAY_MS); |
984 |
|
assertEquals(SIZE, (int) q.take()); |
985 |
< |
assertTrue(q.isEmpty()); |
985 |
> |
checkEmpty(q); |
986 |
|
t.join(); |
987 |
|
} |
988 |
|
|
1002 |
|
* tryTransfer returns false and does not enqueue if there are no |
1003 |
|
* consumers waiting to poll or take. |
1004 |
|
*/ |
1005 |
< |
public void testTryTransfer2() { |
1005 |
> |
public void testTryTransfer2() throws InterruptedException { |
1006 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1007 |
|
assertFalse(q.tryTransfer(new Object())); |
1008 |
|
assertFalse(q.hasWaitingConsumer()); |
1009 |
< |
assertTrue(q.isEmpty()); |
962 |
< |
assertEquals(0, q.size()); |
1009 |
> |
checkEmpty(q); |
1010 |
|
} |
1011 |
|
|
1012 |
|
/** |
1028 |
|
}}); |
1029 |
|
|
1030 |
|
assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); |
1031 |
< |
assertTrue(q.isEmpty()); |
1031 |
> |
checkEmpty(q); |
1032 |
|
t.join(); |
1033 |
|
} |
1034 |
|
|
1051 |
|
}}); |
1052 |
|
|
1053 |
|
assertTrue(q.take() == hotPotato); |
1054 |
< |
assertTrue(q.isEmpty()); |
1054 |
> |
checkEmpty(q); |
1055 |
|
t.join(); |
1056 |
|
} |
1057 |
|
|
1086 |
|
}}); |
1087 |
|
|
1088 |
|
Thread.sleep(SMALL_DELAY_MS); |
1089 |
< |
assertTrue(q.isEmpty()); |
1089 |
> |
checkEmpty(q); |
1090 |
|
t.join(); |
1091 |
|
} |
1092 |
|
|
1109 |
|
assertEquals(2, q.size()); |
1110 |
|
assertEquals(four, q.poll()); |
1111 |
|
assertEquals(five, q.poll()); |
1112 |
< |
assertTrue(q.isEmpty()); |
1112 |
> |
checkEmpty(q); |
1113 |
|
t.join(); |
1114 |
|
} |
1115 |
|
|
1124 |
|
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1125 |
|
assertEquals(1, q.size()); |
1126 |
|
assertEquals(four, q.poll()); |
1080 |
– |
threadAssertTrue(q.isEmpty()); |
1127 |
|
assertNull(q.poll()); |
1128 |
+ |
checkEmpty(q); |
1129 |
|
} |
1130 |
|
|
1131 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1139 |
|
assertFalse(q.isEmpty()); |
1140 |
|
return q; |
1141 |
|
} |
1095 |
– |
|
1096 |
– |
private static class ConsumerObserver { |
1097 |
– |
|
1098 |
– |
private int waitingConsumers; |
1099 |
– |
|
1100 |
– |
private ConsumerObserver() { |
1101 |
– |
} |
1102 |
– |
|
1103 |
– |
private void setWaitingConsumer(int i) { |
1104 |
– |
this.waitingConsumers = i; |
1105 |
– |
} |
1106 |
– |
|
1107 |
– |
private int getWaitingConsumers() { |
1108 |
– |
return waitingConsumers; |
1109 |
– |
} |
1110 |
– |
} |
1142 |
|
} |