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; |
22 |
|
import junit.framework.Test; |
23 |
|
import junit.framework.TestSuite; |
24 |
|
|
25 |
+ |
@SuppressWarnings({"unchecked", "rawtypes"}) |
26 |
|
public class LinkedTransferQueueTest extends JSR166TestCase { |
27 |
|
|
28 |
|
public static void main(String[] args) { |
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 being true |
63 |
> |
* Constructor builds new queue with size being zero and empty |
64 |
> |
* being true |
65 |
|
*/ |
66 |
|
public void testConstructor1() { |
67 |
|
assertEquals(0, new LinkedTransferQueue().size()); |
69 |
|
} |
70 |
|
|
71 |
|
/** |
72 |
< |
* Initializing constructor with null collection throws NullPointerException |
72 |
> |
* Initializing constructor with null collection throws |
73 |
> |
* NullPointerException |
74 |
|
*/ |
75 |
|
public void testConstructor2() { |
76 |
|
try { |
81 |
|
} |
82 |
|
|
83 |
|
/** |
84 |
< |
* Initializing from Collection of null elements throws NullPointerException |
84 |
> |
* Initializing from Collection of null elements throws |
85 |
> |
* NullPointerException |
86 |
|
*/ |
87 |
|
public void testConstructor3() { |
88 |
|
try { |
89 |
|
Integer[] ints = new Integer[SIZE]; |
90 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
90 |
> |
new LinkedTransferQueue(Arrays.asList(ints)); |
91 |
|
shouldThrow(); |
92 |
|
} catch (NullPointerException success) { |
93 |
|
} |
103 |
|
for (int i = 0; i < SIZE - 1; ++i) { |
104 |
|
ints[i] = i; |
105 |
|
} |
106 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
106 |
> |
new LinkedTransferQueue(Arrays.asList(ints)); |
107 |
|
shouldThrow(); |
108 |
|
} catch (NullPointerException success) { |
109 |
|
} |
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 = new LinkedTransferQueue(Arrays.asList(ints)); |
122 |
< |
for (int i = 0; i < SIZE; ++i) { |
123 |
< |
assertEquals(ints[i], q.poll()); |
124 |
< |
} |
125 |
< |
} 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); |
212 |
|
} |
213 |
|
|
214 |
|
/** |
215 |
< |
* addAll of a collection with any null elements throws NullPointerException after |
216 |
< |
* possibly adding some elements |
215 |
> |
* addAll of a collection with any null elements throws |
216 |
> |
* NullPointerException after possibly adding some elements |
217 |
|
*/ |
218 |
|
public void testAddAll3() { |
219 |
|
try { |
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 |
|
} |
229 |
– |
assertEquals(q.size(), SIZE); |
269 |
|
} |
270 |
|
|
271 |
|
/** |
283 |
|
*/ |
284 |
|
public void testTakeFromEmpty() throws InterruptedException { |
285 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
286 |
< |
Thread t = new Thread(new Runnable() { |
287 |
< |
public void run() { |
288 |
< |
try { |
250 |
< |
q.take(); |
251 |
< |
threadShouldThrow(); |
252 |
< |
} catch (InterruptedException success) { |
253 |
< |
} catch (Throwable ex) { |
254 |
< |
threadUnexpectedException(ex); |
255 |
< |
} |
286 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
287 |
> |
void realRun() throws InterruptedException { |
288 |
> |
q.take(); |
289 |
|
}}); |
257 |
– |
t.start(); |
290 |
|
Thread.sleep(SHORT_DELAY_MS); |
291 |
|
t.interrupt(); |
292 |
|
t.join(); |
296 |
|
* Take removes existing elements until empty, then blocks interruptibly |
297 |
|
*/ |
298 |
|
public void testBlockingTake() throws InterruptedException { |
299 |
< |
Thread t = new Thread(new Runnable() { |
300 |
< |
public void run() { |
301 |
< |
try { |
302 |
< |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
303 |
< |
for (int i = 0; i < SIZE; ++i) { |
272 |
< |
threadAssertEquals(i, (int) q.take()); |
273 |
< |
} |
274 |
< |
q.take(); |
275 |
< |
threadShouldThrow(); |
276 |
< |
} catch (InterruptedException success) { |
277 |
< |
} catch (Throwable ex) { |
278 |
< |
threadUnexpectedException(ex); |
299 |
> |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
300 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
301 |
> |
void realRun() throws InterruptedException { |
302 |
> |
for (int i = 0; i < SIZE; ++i) { |
303 |
> |
threadAssertEquals(i, (int) q.take()); |
304 |
|
} |
305 |
+ |
q.take(); |
306 |
|
}}); |
307 |
< |
t.start(); |
282 |
< |
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 |
|
/** |
328 |
|
public void testTimedPoll0() throws InterruptedException { |
329 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
330 |
|
for (int i = 0; i < SIZE; ++i) { |
331 |
< |
assertEquals(i, (int) q.poll(0, TimeUnit.MILLISECONDS)); |
331 |
> |
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
332 |
|
} |
333 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
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, TimeUnit.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, TimeUnit.MILLISECONDS)); |
348 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
349 |
> |
checkEmpty(q); |
350 |
|
} |
351 |
|
|
352 |
|
/** |
354 |
|
* returning timeout status |
355 |
|
*/ |
356 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
357 |
< |
Thread t = new Thread(new Runnable() { |
358 |
< |
public void run() { |
359 |
< |
try { |
360 |
< |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
361 |
< |
for (int i = 0; i < SIZE; ++i) { |
362 |
< |
threadAssertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
363 |
< |
} |
364 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
365 |
< |
} catch (InterruptedException success) { |
334 |
< |
} catch (Throwable ex) { |
335 |
< |
threadUnexpectedException(ex); |
357 |
> |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
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 |
< |
t.start(); |
339 |
< |
Thread.sleep(SHORT_DELAY_MS); |
369 |
> |
Thread.sleep(SMALL_DELAY_MS); |
370 |
|
t.interrupt(); |
371 |
|
t.join(); |
372 |
+ |
checkEmpty(q); |
373 |
|
} |
374 |
|
|
375 |
|
/** |
378 |
|
*/ |
379 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
380 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
381 |
< |
Thread t = new Thread(new Runnable() { |
382 |
< |
public void run() { |
383 |
< |
try { |
384 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
385 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
355 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
356 |
< |
threadShouldThrow(); |
357 |
< |
} catch (InterruptedException success) { |
358 |
< |
} catch (Throwable ex) { |
359 |
< |
threadUnexpectedException(ex); |
360 |
< |
} |
381 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
382 |
> |
void realRun() throws InterruptedException { |
383 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
384 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
385 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
386 |
|
}}); |
362 |
– |
t.start(); |
387 |
|
Thread.sleep(SMALL_DELAY_MS); |
388 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
388 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
389 |
|
t.interrupt(); |
390 |
|
t.join(); |
391 |
|
} |
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 |
|
/** |
463 |
|
assertTrue(q.remove(one)); |
464 |
|
assertTrue(q.remove(two)); |
465 |
|
assertTrue(q.add(three)); |
466 |
< |
assertTrue(q.take() != null); |
466 |
> |
assertTrue(q.take() == three); |
467 |
|
} |
468 |
|
|
469 |
|
/** |
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()); |
461 |
< |
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 |
|
/** |
509 |
|
} |
510 |
|
|
511 |
|
/** |
512 |
< |
* retainAll(c) retains only those elements of c and reports true if changed |
512 |
> |
* retainAll(c) retains only those elements of c and reports true |
513 |
> |
* if changed |
514 |
|
*/ |
515 |
|
public void testRetainAll() { |
516 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
529 |
|
} |
530 |
|
|
531 |
|
/** |
532 |
< |
* removeAll(c) removes only those elements of c and reports true if changed |
532 |
> |
* removeAll(c) removes only those elements of c and reports true |
533 |
> |
* if changed |
534 |
|
*/ |
535 |
|
public void testRemoveAll() { |
536 |
|
for (int i = 1; i < SIZE; ++i) { |
545 |
|
} |
546 |
|
|
547 |
|
/** |
548 |
< |
* toArray contains all elements |
548 |
> |
* toArray() contains all elements |
549 |
|
*/ |
550 |
|
public void testToArray() throws InterruptedException { |
551 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
580 |
|
} |
581 |
|
|
582 |
|
/** |
583 |
< |
* toArray with incompatible array type throws CCE |
583 |
> |
* toArray(incompatible array type) throws CCE |
584 |
|
*/ |
585 |
|
public void testToArray1_BadArg() { |
586 |
|
try { |
597 |
|
public void testIterator() throws InterruptedException { |
598 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
599 |
|
Iterator it = q.iterator(); |
600 |
+ |
int i = 0; |
601 |
|
while (it.hasNext()) { |
602 |
< |
assertEquals(it.next(), q.take()); |
602 |
> |
assertEquals(it.next(), i++); |
603 |
|
} |
604 |
+ |
assertEquals(i, SIZE); |
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(); |
627 |
|
* iterator ordering is FIFO |
628 |
|
*/ |
629 |
|
public void testIteratorOrdering() { |
630 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
630 |
> |
final LinkedTransferQueue<Integer> q |
631 |
> |
= new LinkedTransferQueue<Integer>(); |
632 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
633 |
|
q.add(one); |
634 |
|
q.add(two); |
675 |
|
q.add(one); |
676 |
|
q.add(two); |
677 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
678 |
< |
executor.execute(new Runnable() { |
679 |
< |
public void run() { |
680 |
< |
try { |
681 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
682 |
< |
} catch (Throwable ex) { |
651 |
< |
threadUnexpectedException(ex); |
652 |
< |
} |
678 |
> |
|
679 |
> |
executor.execute(new CheckedRunnable() { |
680 |
> |
void realRun() { |
681 |
> |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, |
682 |
> |
MILLISECONDS)); |
683 |
|
}}); |
684 |
|
|
685 |
< |
executor.execute(new Runnable() { |
686 |
< |
public void run() { |
687 |
< |
try { |
688 |
< |
Thread.sleep(SMALL_DELAY_MS); |
659 |
< |
threadAssertEquals(one, q.take()); |
660 |
< |
} catch (Throwable ex) { |
661 |
< |
threadUnexpectedException(ex); |
662 |
< |
} |
685 |
> |
executor.execute(new CheckedRunnable() { |
686 |
> |
void realRun() throws InterruptedException { |
687 |
> |
Thread.sleep(SMALL_DELAY_MS); |
688 |
> |
threadAssertEquals(one, q.take()); |
689 |
|
}}); |
690 |
|
|
691 |
|
joinPool(executor); |
692 |
|
} |
693 |
|
|
694 |
|
/** |
695 |
< |
* poll retrieves elements across Executor threads |
695 |
> |
* timed poll retrieves elements across Executor threads |
696 |
|
*/ |
697 |
|
public void testPollInExecutor() { |
698 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
699 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
700 |
< |
executor.execute(new Runnable() { |
701 |
< |
public void run() { |
702 |
< |
try { |
703 |
< |
threadAssertNull(q.poll()); |
704 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
705 |
< |
threadAssertTrue(q.isEmpty()); |
706 |
< |
} catch (Throwable ex) { |
681 |
< |
threadUnexpectedException(ex); |
682 |
< |
} |
700 |
> |
|
701 |
> |
executor.execute(new CheckedRunnable() { |
702 |
> |
void realRun() throws InterruptedException { |
703 |
> |
threadAssertNull(q.poll()); |
704 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, |
705 |
> |
MILLISECONDS)); |
706 |
> |
threadAssertTrue(q.isEmpty()); |
707 |
|
}}); |
708 |
|
|
709 |
< |
executor.execute(new Runnable() { |
710 |
< |
public void run() { |
711 |
< |
try { |
712 |
< |
Thread.sleep(SMALL_DELAY_MS); |
689 |
< |
q.put(one); |
690 |
< |
} catch (Throwable ex) { |
691 |
< |
threadUnexpectedException(ex); |
692 |
< |
} |
709 |
> |
executor.execute(new CheckedRunnable() { |
710 |
> |
void realRun() throws InterruptedException { |
711 |
> |
Thread.sleep(SMALL_DELAY_MS); |
712 |
> |
q.put(one); |
713 |
|
}}); |
714 |
|
|
715 |
|
joinPool(executor); |
722 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
723 |
|
|
724 |
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
725 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
725 |
> |
ObjectOutputStream out |
726 |
> |
= new ObjectOutputStream(new BufferedOutputStream(bout)); |
727 |
|
out.writeObject(q); |
728 |
|
out.close(); |
729 |
|
|
730 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
731 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
730 |
> |
ByteArrayInputStream bin |
731 |
> |
= new ByteArrayInputStream(bout.toByteArray()); |
732 |
> |
ObjectInputStream in |
733 |
> |
= new ObjectInputStream(new BufferedInputStream(bin)); |
734 |
|
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
735 |
|
|
736 |
|
assertEquals(q.size(), r.size()); |
790 |
|
} |
791 |
|
|
792 |
|
/** |
793 |
< |
* drainTo empties full queue, unblocking a waiting put. |
793 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
794 |
|
*/ |
795 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
796 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
797 |
< |
Thread t = new Thread(new Runnable() { |
798 |
< |
public void run() { |
799 |
< |
try { |
777 |
< |
q.put(SIZE + 1); |
778 |
< |
} catch (Throwable ex) { |
779 |
< |
threadUnexpectedException(ex); |
780 |
< |
} |
797 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
798 |
> |
void realRun() { |
799 |
> |
q.put(SIZE + 1); |
800 |
|
}}); |
782 |
– |
t.start(); |
801 |
|
ArrayList l = new ArrayList(); |
802 |
|
q.drainTo(l); |
803 |
|
assertTrue(l.size() >= SIZE); |
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 |
|
} |
855 |
|
} |
856 |
|
|
857 |
|
/** |
858 |
< |
* poll and take should decrement the waiting consumer count |
858 |
> |
* timed poll() or take() increments the waiting consumer count; |
859 |
> |
* offer(e) decrements the waiting consumer count |
860 |
|
*/ |
861 |
|
public void testWaitingConsumer() throws InterruptedException { |
862 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
863 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
864 |
< |
new Thread(new Runnable() { |
865 |
< |
public void run() { |
866 |
< |
try { |
867 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
868 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
869 |
< |
threadAssertTrue(q.offer(new Object())); |
870 |
< |
} catch (Throwable ex) { |
871 |
< |
threadUnexpectedException(ex); |
872 |
< |
} |
873 |
< |
}}).start(); |
874 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
875 |
< |
assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers()); |
863 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
864 |
> |
assertFalse(q.hasWaitingConsumer()); |
865 |
> |
|
866 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
867 |
> |
void realRun() throws InterruptedException { |
868 |
> |
Thread.sleep(SMALL_DELAY_MS); |
869 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
870 |
> |
threadAssertEquals(q.getWaitingConsumerCount(), 1); |
871 |
> |
threadAssertTrue(q.offer(new Object())); |
872 |
> |
threadAssertFalse(q.hasWaitingConsumer()); |
873 |
> |
threadAssertEquals(q.getWaitingConsumerCount(), 0); |
874 |
> |
}}); |
875 |
> |
|
876 |
> |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
877 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
878 |
> |
assertFalse(q.hasWaitingConsumer()); |
879 |
> |
t.join(); |
880 |
|
} |
881 |
|
|
882 |
|
/** |
892 |
|
} |
893 |
|
|
894 |
|
/** |
895 |
< |
* transfer attempts to insert into the queue then wait until that |
896 |
< |
* object is removed via take or poll. |
895 |
> |
* transfer waits until a poll occurs. The transfered element |
896 |
> |
* is returned by this associated poll. |
897 |
|
*/ |
898 |
|
public void testTransfer2() throws InterruptedException { |
899 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
899 |
> |
final LinkedTransferQueue<Integer> q |
900 |
> |
= new LinkedTransferQueue<Integer>(); |
901 |
|
|
902 |
< |
new Thread(new Runnable() { |
903 |
< |
public void run() { |
904 |
< |
try { |
905 |
< |
q.transfer(SIZE); |
906 |
< |
threadAssertTrue(q.isEmpty()); |
883 |
< |
} catch (Throwable ex) { |
884 |
< |
threadUnexpectedException(ex); |
885 |
< |
} |
886 |
< |
}}).start(); |
902 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
903 |
> |
void realRun() throws InterruptedException { |
904 |
> |
q.transfer(SIZE); |
905 |
> |
threadAssertTrue(q.isEmpty()); |
906 |
> |
}}); |
907 |
|
|
908 |
|
Thread.sleep(SHORT_DELAY_MS); |
909 |
|
assertEquals(1, q.size()); |
910 |
|
assertEquals(SIZE, (int) q.poll()); |
911 |
|
assertTrue(q.isEmpty()); |
912 |
+ |
t.join(); |
913 |
|
} |
914 |
|
|
915 |
|
/** |
916 |
< |
* transfer will attempt to transfer in fifo order and continue |
896 |
< |
* waiting if the element being transfered is not polled or taken |
916 |
> |
* transfer waits until a poll occurs, and then transfers in fifo order |
917 |
|
*/ |
918 |
|
public void testTransfer3() throws InterruptedException { |
919 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
919 |
> |
final LinkedTransferQueue<Integer> q |
920 |
> |
= new LinkedTransferQueue<Integer>(); |
921 |
|
|
922 |
< |
Thread first = |
923 |
< |
new Thread(new Runnable() { |
924 |
< |
public void run() { |
925 |
< |
try { |
926 |
< |
Integer i = SIZE + 1; |
927 |
< |
q.transfer(i); |
928 |
< |
threadAssertTrue(!q.contains(i)); |
908 |
< |
threadAssertEquals(1, q.size()); |
909 |
< |
} catch (Throwable ex) { |
910 |
< |
threadUnexpectedException(ex); |
911 |
< |
} |
912 |
< |
}}); |
913 |
< |
first.start(); |
922 |
> |
Thread first = newStartedThread(new CheckedRunnable() { |
923 |
> |
void realRun() throws InterruptedException { |
924 |
> |
Integer i = SIZE + 1; |
925 |
> |
q.transfer(i); |
926 |
> |
threadAssertTrue(!q.contains(i)); |
927 |
> |
threadAssertEquals(1, q.size()); |
928 |
> |
}}); |
929 |
|
|
930 |
< |
Thread interruptedThread = |
931 |
< |
new Thread(new Runnable() { |
932 |
< |
public void run() { |
933 |
< |
try { |
934 |
< |
while (q.size() == 0) |
935 |
< |
Thread.yield(); |
921 |
< |
q.transfer(SIZE); |
922 |
< |
threadShouldThrow(); |
923 |
< |
} catch (InterruptedException success) { |
924 |
< |
} catch (Throwable ex) { |
925 |
< |
threadUnexpectedException(ex); |
926 |
< |
} |
930 |
> |
Thread interruptedThread = newStartedThread( |
931 |
> |
new CheckedInterruptedRunnable() { |
932 |
> |
void realRun() throws InterruptedException { |
933 |
> |
while (q.size() == 0) |
934 |
> |
Thread.yield(); |
935 |
> |
q.transfer(SIZE); |
936 |
|
}}); |
928 |
– |
interruptedThread.start(); |
937 |
|
|
938 |
|
while (q.size() < 2) |
939 |
|
Thread.yield(); |
948 |
|
} |
949 |
|
|
950 |
|
/** |
951 |
< |
* transfer will wait as long as a poll or take occurs if one does occur |
952 |
< |
* the waiting is finished and the thread that tries to poll/take |
945 |
< |
* wins in retrieving the element |
951 |
> |
* transfer waits until a poll occurs, at which point the polling |
952 |
> |
* thread returns the element |
953 |
|
*/ |
954 |
|
public void testTransfer4() throws InterruptedException { |
955 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
956 |
< |
new Thread(new Runnable() { |
957 |
< |
public void run() { |
958 |
< |
try { |
959 |
< |
q.transfer(four); |
960 |
< |
threadAssertFalse(q.contains(four)); |
961 |
< |
threadAssertEquals(three, q.poll()); |
962 |
< |
} catch (Throwable ex) { |
963 |
< |
threadUnexpectedException(ex); |
964 |
< |
} |
958 |
< |
}}).start(); |
959 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
956 |
> |
|
957 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
958 |
> |
void realRun() throws InterruptedException { |
959 |
> |
q.transfer(four); |
960 |
> |
threadAssertFalse(q.contains(four)); |
961 |
> |
threadAssertEquals(three, q.poll()); |
962 |
> |
}}); |
963 |
> |
|
964 |
> |
Thread.sleep(SHORT_DELAY_MS); |
965 |
|
assertTrue(q.offer(three)); |
966 |
|
assertEquals(four, q.poll()); |
967 |
+ |
t.join(); |
968 |
+ |
} |
969 |
+ |
|
970 |
+ |
/** |
971 |
+ |
* transfer waits until a take occurs. The transfered element |
972 |
+ |
* is returned by this associated take. |
973 |
+ |
*/ |
974 |
+ |
public void testTransfer5() throws InterruptedException { |
975 |
+ |
final LinkedTransferQueue<Integer> q |
976 |
+ |
= new LinkedTransferQueue<Integer>(); |
977 |
+ |
|
978 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
979 |
+ |
void realRun() throws InterruptedException { |
980 |
+ |
q.transfer(SIZE); |
981 |
+ |
checkEmpty(q); |
982 |
+ |
}}); |
983 |
+ |
|
984 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
985 |
+ |
assertEquals(SIZE, (int) q.take()); |
986 |
+ |
checkEmpty(q); |
987 |
+ |
t.join(); |
988 |
|
} |
989 |
|
|
990 |
|
/** |
994 |
|
try { |
995 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
996 |
|
q.tryTransfer(null); |
997 |
< |
this.shouldThrow(); |
997 |
> |
shouldThrow(); |
998 |
|
} catch (NullPointerException ex) { |
999 |
|
} |
1000 |
|
} |
1003 |
|
* tryTransfer returns false and does not enqueue if there are no |
1004 |
|
* consumers waiting to poll or take. |
1005 |
|
*/ |
1006 |
< |
public void testTryTransfer2() { |
1006 |
> |
public void testTryTransfer2() throws InterruptedException { |
1007 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1008 |
|
assertFalse(q.tryTransfer(new Object())); |
1009 |
|
assertFalse(q.hasWaitingConsumer()); |
1010 |
< |
assertTrue(q.isEmpty()); |
985 |
< |
assertEquals(0, q.size()); |
1010 |
> |
checkEmpty(q); |
1011 |
|
} |
1012 |
|
|
1013 |
|
/** |
1017 |
|
public void testTryTransfer3() throws InterruptedException { |
1018 |
|
final Object hotPotato = new Object(); |
1019 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1020 |
< |
new Thread(new Runnable() { |
1021 |
< |
public void run() { |
1022 |
< |
try { |
1023 |
< |
while (! q.hasWaitingConsumer()) |
1024 |
< |
Thread.yield(); |
1025 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1026 |
< |
threadAssertTrue(q.isEmpty()); |
1027 |
< |
threadAssertTrue(q.size() == 0); |
1028 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1029 |
< |
} catch (Throwable ex) { |
1030 |
< |
threadUnexpectedException(ex); |
1031 |
< |
} |
1032 |
< |
}}).start(); |
1033 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) == hotPotato); |
1009 |
< |
assertTrue(q.isEmpty()); |
1020 |
> |
|
1021 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1022 |
> |
void realRun() { |
1023 |
> |
while (! q.hasWaitingConsumer()) |
1024 |
> |
Thread.yield(); |
1025 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
1026 |
> |
threadAssertTrue(q.isEmpty()); |
1027 |
> |
threadAssertTrue(q.size() == 0); |
1028 |
> |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1029 |
> |
}}); |
1030 |
> |
|
1031 |
> |
assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); |
1032 |
> |
checkEmpty(q); |
1033 |
> |
t.join(); |
1034 |
|
} |
1035 |
|
|
1036 |
|
/** |
1040 |
|
public void testTryTransfer4() throws InterruptedException { |
1041 |
|
final Object hotPotato = new Object(); |
1042 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1043 |
< |
new Thread(new Runnable() { |
1044 |
< |
public void run() { |
1045 |
< |
try { |
1046 |
< |
while (! q.hasWaitingConsumer()) |
1047 |
< |
Thread.yield(); |
1048 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1049 |
< |
threadAssertTrue(q.isEmpty()); |
1050 |
< |
threadAssertTrue(q.size() == 0); |
1051 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1052 |
< |
} catch (Throwable ex) { |
1053 |
< |
threadUnexpectedException(ex); |
1030 |
< |
} |
1031 |
< |
}}).start(); |
1043 |
> |
|
1044 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1045 |
> |
void realRun() { |
1046 |
> |
while (! q.hasWaitingConsumer()) |
1047 |
> |
Thread.yield(); |
1048 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
1049 |
> |
threadAssertTrue(q.isEmpty()); |
1050 |
> |
threadAssertTrue(q.size() == 0); |
1051 |
> |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1052 |
> |
}}); |
1053 |
> |
|
1054 |
|
assertTrue(q.take() == hotPotato); |
1055 |
< |
assertTrue(q.isEmpty()); |
1055 |
> |
checkEmpty(q); |
1056 |
> |
t.join(); |
1057 |
|
} |
1058 |
|
|
1059 |
|
/** |
1060 |
< |
* tryTransfer waits the amount given if interrupted, show an |
1061 |
< |
* interrupted exception |
1060 |
> |
* tryTransfer waits the amount given if interrupted, and |
1061 |
> |
* throws interrupted exception |
1062 |
|
*/ |
1063 |
|
public void testTryTransfer5() throws InterruptedException { |
1064 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1065 |
< |
Thread toInterrupt = new Thread(new Runnable() { |
1066 |
< |
public void run() { |
1067 |
< |
try { |
1068 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1046 |
< |
threadShouldThrow(); |
1047 |
< |
} catch (InterruptedException success) { |
1048 |
< |
} catch (Throwable ex) { |
1049 |
< |
threadUnexpectedException(ex); |
1050 |
< |
} |
1065 |
> |
|
1066 |
> |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1067 |
> |
void realRun() throws InterruptedException { |
1068 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1069 |
|
}}); |
1070 |
< |
toInterrupt.start(); |
1070 |
> |
|
1071 |
|
Thread.sleep(SMALL_DELAY_MS); |
1072 |
|
toInterrupt.interrupt(); |
1073 |
+ |
toInterrupt.join(); |
1074 |
|
} |
1075 |
|
|
1076 |
|
/** |
1078 |
|
*/ |
1079 |
|
public void testTryTransfer6() throws InterruptedException { |
1080 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1081 |
< |
new Thread(new Runnable() { |
1082 |
< |
public void run() { |
1083 |
< |
try { |
1084 |
< |
threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1085 |
< |
} catch (Throwable ex) { |
1086 |
< |
threadUnexpectedException(ex); |
1087 |
< |
} |
1088 |
< |
}}).start(); |
1089 |
< |
Thread.sleep(LONG_DELAY_MS); |
1090 |
< |
assertTrue(q.isEmpty()); |
1081 |
> |
|
1082 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1083 |
> |
void realRun() throws InterruptedException { |
1084 |
> |
threadAssertFalse |
1085 |
> |
(q.tryTransfer(new Object(), |
1086 |
> |
SHORT_DELAY_MS, MILLISECONDS)); |
1087 |
> |
}}); |
1088 |
> |
|
1089 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1090 |
> |
checkEmpty(q); |
1091 |
> |
t.join(); |
1092 |
|
} |
1093 |
|
|
1094 |
|
/** |
1098 |
|
public void testTryTransfer7() throws InterruptedException { |
1099 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1100 |
|
assertTrue(q.offer(four)); |
1101 |
< |
new Thread(new Runnable() { |
1102 |
< |
public void run() { |
1103 |
< |
try { |
1104 |
< |
threadAssertTrue(q.tryTransfer(five, LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
1105 |
< |
threadAssertTrue(q.isEmpty()); |
1106 |
< |
} catch (Throwable ex) { |
1107 |
< |
threadUnexpectedException(ex); |
1108 |
< |
} |
1089 |
< |
}}).start(); |
1101 |
> |
|
1102 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1103 |
> |
void realRun() throws InterruptedException { |
1104 |
> |
threadAssertTrue(q.tryTransfer(five, |
1105 |
> |
MEDIUM_DELAY_MS, MILLISECONDS)); |
1106 |
> |
threadAssertTrue(q.isEmpty()); |
1107 |
> |
}}); |
1108 |
> |
|
1109 |
|
Thread.sleep(SHORT_DELAY_MS); |
1110 |
|
assertEquals(2, q.size()); |
1111 |
|
assertEquals(four, q.poll()); |
1112 |
|
assertEquals(five, q.poll()); |
1113 |
< |
assertTrue(q.isEmpty()); |
1113 |
> |
checkEmpty(q); |
1114 |
> |
t.join(); |
1115 |
|
} |
1116 |
|
|
1117 |
|
/** |
1122 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1123 |
|
assertTrue(q.offer(four)); |
1124 |
|
assertEquals(1, q.size()); |
1125 |
< |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1125 |
> |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1126 |
|
assertEquals(1, q.size()); |
1127 |
|
assertEquals(four, q.poll()); |
1108 |
– |
threadAssertTrue(q.isEmpty()); |
1128 |
|
assertNull(q.poll()); |
1129 |
+ |
checkEmpty(q); |
1130 |
|
} |
1131 |
|
|
1132 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1140 |
|
assertFalse(q.isEmpty()); |
1141 |
|
return q; |
1142 |
|
} |
1123 |
– |
|
1124 |
– |
private static class ConsumerObserver { |
1125 |
– |
|
1126 |
– |
private int waitingConsumers; |
1127 |
– |
|
1128 |
– |
private ConsumerObserver() { |
1129 |
– |
} |
1130 |
– |
|
1131 |
– |
private void setWaitingConsumer(int i) { |
1132 |
– |
this.waitingConsumers = i; |
1133 |
– |
} |
1134 |
– |
|
1135 |
– |
private int getWaitingConsumers() { |
1136 |
– |
return waitingConsumers; |
1137 |
– |
} |
1138 |
– |
} |
1143 |
|
} |