15 |
|
import java.util.Arrays; |
16 |
|
import java.util.ConcurrentModificationException; |
17 |
|
import java.util.Iterator; |
18 |
+ |
import java.util.List; |
19 |
|
import java.util.NoSuchElementException; |
20 |
|
import java.util.concurrent.*; |
21 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
33 |
|
return new TestSuite(LinkedTransferQueueTest.class); |
34 |
|
} |
35 |
|
|
36 |
+ |
void checkEmpty(LinkedTransferQueue q) throws InterruptedException { |
37 |
+ |
assertTrue(q.isEmpty()); |
38 |
+ |
assertEquals(0, q.size()); |
39 |
+ |
assertNull(q.peek()); |
40 |
+ |
assertNull(q.poll()); |
41 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
42 |
+ |
assertEquals(q.toString(), "[]"); |
43 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
44 |
+ |
assertFalse(q.iterator().hasNext()); |
45 |
+ |
try { |
46 |
+ |
q.element(); |
47 |
+ |
shouldThrow(); |
48 |
+ |
} catch (NoSuchElementException success) { |
49 |
+ |
} |
50 |
+ |
try { |
51 |
+ |
q.iterator().next(); |
52 |
+ |
shouldThrow(); |
53 |
+ |
} catch (NoSuchElementException success) { |
54 |
+ |
} |
55 |
+ |
try { |
56 |
+ |
q.remove(); |
57 |
+ |
shouldThrow(); |
58 |
+ |
} catch (NoSuchElementException success) { |
59 |
+ |
} |
60 |
+ |
} |
61 |
+ |
|
62 |
|
/** |
63 |
|
* Constructor builds new queue with size being zero and empty |
64 |
|
* being true |
113 |
|
* Queue contains all elements of the collection it is initialized by |
114 |
|
*/ |
115 |
|
public void testConstructor5() { |
116 |
< |
try { |
117 |
< |
Integer[] ints = new Integer[SIZE]; |
118 |
< |
for (int i = 0; i < SIZE; ++i) { |
119 |
< |
ints[i] = i; |
120 |
< |
} |
121 |
< |
LinkedTransferQueue q |
122 |
< |
= new LinkedTransferQueue(Arrays.asList(ints)); |
123 |
< |
for (int i = 0; i < SIZE; ++i) { |
124 |
< |
assertEquals(ints[i], q.poll()); |
125 |
< |
} |
126 |
< |
} finally { |
116 |
> |
Integer[] ints = new Integer[SIZE]; |
117 |
> |
for (int i = 0; i < SIZE; ++i) { |
118 |
> |
ints[i] = i; |
119 |
> |
} |
120 |
> |
List intList = Arrays.asList(ints); |
121 |
> |
LinkedTransferQueue q |
122 |
> |
= new LinkedTransferQueue(intList); |
123 |
> |
assertEquals(q.size(), intList.size()); |
124 |
> |
assertEquals(q.toString(), intList.toString()); |
125 |
> |
assertTrue(Arrays.equals(q.toArray(), |
126 |
> |
intList.toArray())); |
127 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[0]), |
128 |
> |
intList.toArray(new Object[0]))); |
129 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[SIZE]), |
130 |
> |
intList.toArray(new Object[SIZE]))); |
131 |
> |
for (int i = 0; i < SIZE; ++i) { |
132 |
> |
assertEquals(ints[i], q.poll()); |
133 |
|
} |
134 |
|
} |
135 |
|
|
136 |
|
/** |
137 |
< |
* Remaining capacity never decrease nor increase on add or remove |
137 |
> |
* remainingCapacity() always returns Integer.MAX_VALUE |
138 |
|
*/ |
139 |
|
public void testRemainingCapacity() { |
140 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
262 |
|
public void testPut() { |
263 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
264 |
|
for (int i = 0; i < SIZE; ++i) { |
265 |
+ |
assertEquals(q.size(), i); |
266 |
|
q.put(i); |
267 |
|
assertTrue(q.contains(i)); |
268 |
|
} |
235 |
– |
assertEquals(q.size(), SIZE); |
269 |
|
} |
270 |
|
|
271 |
|
/** |
296 |
|
* Take removes existing elements until empty, then blocks interruptibly |
297 |
|
*/ |
298 |
|
public void testBlockingTake() throws InterruptedException { |
299 |
+ |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
300 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
301 |
|
void realRun() throws InterruptedException { |
268 |
– |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
302 |
|
for (int i = 0; i < SIZE; ++i) { |
303 |
|
threadAssertEquals(i, (int) q.take()); |
304 |
|
} |
305 |
|
q.take(); |
306 |
|
}}); |
307 |
< |
Thread.sleep(SHORT_DELAY_MS); |
307 |
> |
Thread.sleep(SMALL_DELAY_MS); |
308 |
|
t.interrupt(); |
309 |
|
t.join(); |
310 |
+ |
checkEmpty(q); |
311 |
|
} |
312 |
|
|
313 |
|
/** |
314 |
|
* poll succeeds unless empty |
315 |
|
*/ |
316 |
< |
public void testPoll() { |
316 |
> |
public void testPoll() throws InterruptedException { |
317 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
318 |
|
for (int i = 0; i < SIZE; ++i) { |
319 |
|
assertEquals(i, (int) q.poll()); |
320 |
|
} |
321 |
|
assertNull(q.poll()); |
322 |
+ |
checkEmpty(q); |
323 |
|
} |
324 |
|
|
325 |
|
/** |
331 |
|
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
332 |
|
} |
333 |
|
assertNull(q.poll(0, MILLISECONDS)); |
334 |
+ |
checkEmpty(q); |
335 |
|
} |
336 |
|
|
337 |
|
/** |
340 |
|
public void testTimedPoll() throws InterruptedException { |
341 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
342 |
|
for (int i = 0; i < SIZE; ++i) { |
343 |
< |
assertEquals(i, (int) q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
343 |
> |
long t0 = System.nanoTime(); |
344 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
345 |
> |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
346 |
> |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
347 |
|
} |
348 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
349 |
+ |
checkEmpty(q); |
350 |
|
} |
351 |
|
|
352 |
|
/** |
358 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
359 |
|
void realRun() throws InterruptedException { |
360 |
|
for (int i = 0; i < SIZE; ++i) { |
361 |
+ |
long t0 = System.nanoTime(); |
362 |
|
threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS, |
363 |
|
MILLISECONDS)); |
364 |
+ |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
365 |
+ |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
366 |
|
} |
367 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
368 |
|
}}); |
369 |
|
Thread.sleep(SMALL_DELAY_MS); |
370 |
|
t.interrupt(); |
371 |
|
t.join(); |
372 |
< |
assertEquals(q.size(), 0); |
330 |
< |
assertNull(q.poll()); |
372 |
> |
checkEmpty(q); |
373 |
|
} |
374 |
|
|
375 |
|
/** |
393 |
|
/** |
394 |
|
* peek returns next element, or null if empty |
395 |
|
*/ |
396 |
< |
public void testPeek() { |
396 |
> |
public void testPeek() throws InterruptedException { |
397 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
398 |
|
for (int i = 0; i < SIZE; ++i) { |
399 |
|
assertEquals(i, (int) q.peek()); |
402 |
|
i != (int) q.peek()); |
403 |
|
} |
404 |
|
assertNull(q.peek()); |
405 |
+ |
checkEmpty(q); |
406 |
|
} |
407 |
|
|
408 |
|
/** |
409 |
|
* element returns next element, or throws NoSuchElementException if empty |
410 |
|
*/ |
411 |
< |
public void testElement() { |
411 |
> |
public void testElement() throws InterruptedException { |
412 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
413 |
|
for (int i = 0; i < SIZE; ++i) { |
414 |
|
assertEquals(i, (int) q.element()); |
419 |
|
shouldThrow(); |
420 |
|
} catch (NoSuchElementException success) { |
421 |
|
} |
422 |
+ |
checkEmpty(q); |
423 |
|
} |
424 |
|
|
425 |
|
/** |
426 |
|
* remove removes next element, or throws NoSuchElementException if empty |
427 |
|
*/ |
428 |
< |
public void testRemove() { |
428 |
> |
public void testRemove() throws InterruptedException { |
429 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
430 |
|
for (int i = 0; i < SIZE; ++i) { |
431 |
|
assertEquals(i, (int) q.remove()); |
435 |
|
shouldThrow(); |
436 |
|
} catch (NoSuchElementException success) { |
437 |
|
} |
438 |
+ |
checkEmpty(q); |
439 |
|
} |
440 |
|
|
441 |
|
/** |
442 |
|
* remove(x) removes x and returns true if present |
443 |
|
*/ |
444 |
< |
public void testRemoveElement() { |
444 |
> |
public void testRemoveElement() throws InterruptedException { |
445 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
446 |
|
for (int i = 1; i < SIZE; i += 2) { |
447 |
|
assertTrue(q.remove(i)); |
450 |
|
assertTrue(q.remove(i)); |
451 |
|
assertFalse(q.remove(i + 1)); |
452 |
|
} |
453 |
< |
assertTrue(q.isEmpty()); |
453 |
> |
checkEmpty(q); |
454 |
|
} |
455 |
|
|
456 |
|
/** |
481 |
|
/** |
482 |
|
* clear removes all elements |
483 |
|
*/ |
484 |
< |
public void testClear() { |
484 |
> |
public void testClear() throws InterruptedException { |
485 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
486 |
|
q.clear(); |
487 |
< |
assertTrue(q.isEmpty()); |
443 |
< |
assertEquals(0, q.size()); |
487 |
> |
checkEmpty(q); |
488 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
489 |
|
q.add(one); |
490 |
|
assertFalse(q.isEmpty()); |
491 |
+ |
assertEquals(1, q.size()); |
492 |
|
assertTrue(q.contains(one)); |
493 |
|
q.clear(); |
494 |
< |
assertTrue(q.isEmpty()); |
494 |
> |
checkEmpty(q); |
495 |
|
} |
496 |
|
|
497 |
|
/** |
605 |
|
} |
606 |
|
|
607 |
|
/** |
608 |
< |
* iterator.remove removes current element |
608 |
> |
* iterator.remove() removes current element |
609 |
|
*/ |
610 |
|
public void testIteratorRemove() { |
611 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
814 |
|
public void testDrainToNullN() { |
815 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
816 |
|
try { |
817 |
< |
q.drainTo(null, 0); |
817 |
> |
q.drainTo(null, SIZE); |
818 |
|
shouldThrow(); |
819 |
|
} catch (NullPointerException success) { |
820 |
|
} |
826 |
|
public void testDrainToSelfN() { |
827 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
828 |
|
try { |
829 |
< |
q.drainTo(q, 0); |
829 |
> |
q.drainTo(q, SIZE); |
830 |
|
shouldThrow(); |
831 |
|
} catch (IllegalArgumentException success) { |
832 |
|
} |
974 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
975 |
|
void realRun() throws InterruptedException { |
976 |
|
q.transfer(SIZE); |
977 |
< |
threadAssertTrue(q.isEmpty()); |
977 |
> |
checkEmpty(q); |
978 |
|
}}); |
979 |
|
|
980 |
|
Thread.sleep(SHORT_DELAY_MS); |
981 |
|
assertEquals(SIZE, (int) q.take()); |
982 |
< |
assertTrue(q.isEmpty()); |
982 |
> |
checkEmpty(q); |
983 |
|
t.join(); |
984 |
|
} |
985 |
|
|
999 |
|
* tryTransfer returns false and does not enqueue if there are no |
1000 |
|
* consumers waiting to poll or take. |
1001 |
|
*/ |
1002 |
< |
public void testTryTransfer2() { |
1002 |
> |
public void testTryTransfer2() throws InterruptedException { |
1003 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1004 |
|
assertFalse(q.tryTransfer(new Object())); |
1005 |
|
assertFalse(q.hasWaitingConsumer()); |
1006 |
< |
assertTrue(q.isEmpty()); |
962 |
< |
assertEquals(0, q.size()); |
1006 |
> |
checkEmpty(q); |
1007 |
|
} |
1008 |
|
|
1009 |
|
/** |
1025 |
|
}}); |
1026 |
|
|
1027 |
|
assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); |
1028 |
< |
assertTrue(q.isEmpty()); |
1028 |
> |
checkEmpty(q); |
1029 |
|
t.join(); |
1030 |
|
} |
1031 |
|
|
1048 |
|
}}); |
1049 |
|
|
1050 |
|
assertTrue(q.take() == hotPotato); |
1051 |
< |
assertTrue(q.isEmpty()); |
1051 |
> |
checkEmpty(q); |
1052 |
|
t.join(); |
1053 |
|
} |
1054 |
|
|
1083 |
|
}}); |
1084 |
|
|
1085 |
|
Thread.sleep(SMALL_DELAY_MS); |
1086 |
< |
assertTrue(q.isEmpty()); |
1086 |
> |
checkEmpty(q); |
1087 |
|
t.join(); |
1088 |
|
} |
1089 |
|
|
1106 |
|
assertEquals(2, q.size()); |
1107 |
|
assertEquals(four, q.poll()); |
1108 |
|
assertEquals(five, q.poll()); |
1109 |
< |
assertTrue(q.isEmpty()); |
1109 |
> |
checkEmpty(q); |
1110 |
|
t.join(); |
1111 |
|
} |
1112 |
|
|
1121 |
|
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1122 |
|
assertEquals(1, q.size()); |
1123 |
|
assertEquals(four, q.poll()); |
1124 |
< |
threadAssertTrue(q.isEmpty()); |
1124 |
> |
checkEmpty(q); |
1125 |
|
assertNull(q.poll()); |
1126 |
|
} |
1127 |
|
|