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) { |
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); |
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()); |
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 |
> |
* poll and take decrement the waiting consumer count |
859 |
|
*/ |
860 |
|
public void testWaitingConsumer() throws InterruptedException { |
861 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
862 |
|
final ConsumerObserver waiting = new ConsumerObserver(); |
863 |
< |
new Thread(new Runnable() { |
864 |
< |
public void run() { |
865 |
< |
try { |
866 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
867 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
868 |
< |
threadAssertTrue(q.offer(new Object())); |
869 |
< |
} catch (Throwable ex) { |
870 |
< |
threadUnexpectedException(ex); |
871 |
< |
} |
872 |
< |
}}).start(); |
873 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
874 |
< |
assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers()); |
863 |
> |
|
864 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
865 |
> |
void realRun() throws InterruptedException { |
866 |
> |
Thread.sleep(SMALL_DELAY_MS); |
867 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
868 |
> |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
869 |
> |
threadAssertTrue(q.offer(new Object())); |
870 |
> |
}}); |
871 |
> |
|
872 |
> |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
873 |
> |
assertTrue(q.getWaitingConsumerCount() |
874 |
> |
< waiting.getWaitingConsumers()); |
875 |
> |
t.join(); |
876 |
|
} |
877 |
|
|
878 |
|
/** |
888 |
|
} |
889 |
|
|
890 |
|
/** |
891 |
< |
* transfer attempts to insert into the queue then wait until that |
892 |
< |
* object is removed via take or poll. |
891 |
> |
* transfer waits until a poll occurs. The transfered element |
892 |
> |
* is returned by this associated poll. |
893 |
|
*/ |
894 |
|
public void testTransfer2() throws InterruptedException { |
895 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
895 |
> |
final LinkedTransferQueue<Integer> q |
896 |
> |
= new LinkedTransferQueue<Integer>(); |
897 |
|
|
898 |
< |
new Thread(new Runnable() { |
899 |
< |
public void run() { |
900 |
< |
try { |
901 |
< |
q.transfer(SIZE); |
902 |
< |
threadAssertTrue(q.isEmpty()); |
883 |
< |
} catch (Throwable ex) { |
884 |
< |
threadUnexpectedException(ex); |
885 |
< |
} |
886 |
< |
}}).start(); |
898 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
899 |
> |
void realRun() throws InterruptedException { |
900 |
> |
q.transfer(SIZE); |
901 |
> |
threadAssertTrue(q.isEmpty()); |
902 |
> |
}}); |
903 |
|
|
904 |
|
Thread.sleep(SHORT_DELAY_MS); |
905 |
|
assertEquals(1, q.size()); |
906 |
|
assertEquals(SIZE, (int) q.poll()); |
907 |
|
assertTrue(q.isEmpty()); |
908 |
+ |
t.join(); |
909 |
|
} |
910 |
|
|
911 |
|
/** |
912 |
< |
* transfer will attempt to transfer in fifo order and continue |
896 |
< |
* waiting if the element being transfered is not polled or taken |
912 |
> |
* transfer waits until a poll occurs, and then transfers in fifo order |
913 |
|
*/ |
914 |
|
public void testTransfer3() throws InterruptedException { |
915 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
915 |
> |
final LinkedTransferQueue<Integer> q |
916 |
> |
= new LinkedTransferQueue<Integer>(); |
917 |
|
|
918 |
< |
Thread first = |
919 |
< |
new Thread(new Runnable() { |
920 |
< |
public void run() { |
921 |
< |
try { |
922 |
< |
Integer i = SIZE + 1; |
923 |
< |
q.transfer(i); |
924 |
< |
threadAssertTrue(!q.contains(i)); |
908 |
< |
threadAssertEquals(1, q.size()); |
909 |
< |
} catch (Throwable ex) { |
910 |
< |
threadUnexpectedException(ex); |
911 |
< |
} |
912 |
< |
}}); |
913 |
< |
first.start(); |
918 |
> |
Thread first = newStartedThread(new CheckedRunnable() { |
919 |
> |
void realRun() throws InterruptedException { |
920 |
> |
Integer i = SIZE + 1; |
921 |
> |
q.transfer(i); |
922 |
> |
threadAssertTrue(!q.contains(i)); |
923 |
> |
threadAssertEquals(1, q.size()); |
924 |
> |
}}); |
925 |
|
|
926 |
< |
Thread interruptedThread = |
927 |
< |
new Thread(new Runnable() { |
928 |
< |
public void run() { |
929 |
< |
try { |
930 |
< |
while (q.size() == 0) |
931 |
< |
Thread.yield(); |
921 |
< |
q.transfer(SIZE); |
922 |
< |
threadShouldThrow(); |
923 |
< |
} catch (InterruptedException success) { |
924 |
< |
} catch (Throwable ex) { |
925 |
< |
threadUnexpectedException(ex); |
926 |
< |
} |
926 |
> |
Thread interruptedThread = newStartedThread( |
927 |
> |
new CheckedInterruptedRunnable() { |
928 |
> |
void realRun() throws InterruptedException { |
929 |
> |
while (q.size() == 0) |
930 |
> |
Thread.yield(); |
931 |
> |
q.transfer(SIZE); |
932 |
|
}}); |
928 |
– |
interruptedThread.start(); |
933 |
|
|
934 |
|
while (q.size() < 2) |
935 |
|
Thread.yield(); |
944 |
|
} |
945 |
|
|
946 |
|
/** |
947 |
< |
* transfer will wait as long as a poll or take occurs if one does occur |
948 |
< |
* the waiting is finished and the thread that tries to poll/take |
945 |
< |
* wins in retrieving the element |
947 |
> |
* transfer waits until a poll occurs, at which point the polling |
948 |
> |
* thread returns the element |
949 |
|
*/ |
950 |
|
public void testTransfer4() throws InterruptedException { |
951 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
952 |
< |
new Thread(new Runnable() { |
953 |
< |
public void run() { |
954 |
< |
try { |
955 |
< |
q.transfer(four); |
956 |
< |
threadAssertFalse(q.contains(four)); |
957 |
< |
threadAssertEquals(three, q.poll()); |
958 |
< |
} catch (Throwable ex) { |
959 |
< |
threadUnexpectedException(ex); |
960 |
< |
} |
958 |
< |
}}).start(); |
959 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
952 |
> |
|
953 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
954 |
> |
void realRun() throws InterruptedException { |
955 |
> |
q.transfer(four); |
956 |
> |
threadAssertFalse(q.contains(four)); |
957 |
> |
threadAssertEquals(three, q.poll()); |
958 |
> |
}}); |
959 |
> |
|
960 |
> |
Thread.sleep(SHORT_DELAY_MS); |
961 |
|
assertTrue(q.offer(three)); |
962 |
|
assertEquals(four, q.poll()); |
963 |
+ |
t.join(); |
964 |
+ |
} |
965 |
+ |
|
966 |
+ |
/** |
967 |
+ |
* transfer waits until a take occurs. The transfered element |
968 |
+ |
* is returned by this associated take. |
969 |
+ |
*/ |
970 |
+ |
public void testTransfer5() throws InterruptedException { |
971 |
+ |
final LinkedTransferQueue<Integer> q |
972 |
+ |
= new LinkedTransferQueue<Integer>(); |
973 |
+ |
|
974 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
975 |
+ |
void realRun() throws InterruptedException { |
976 |
+ |
q.transfer(SIZE); |
977 |
+ |
checkEmpty(q); |
978 |
+ |
}}); |
979 |
+ |
|
980 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
981 |
+ |
assertEquals(SIZE, (int) q.take()); |
982 |
+ |
checkEmpty(q); |
983 |
+ |
t.join(); |
984 |
|
} |
985 |
|
|
986 |
|
/** |
990 |
|
try { |
991 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
992 |
|
q.tryTransfer(null); |
993 |
< |
this.shouldThrow(); |
993 |
> |
shouldThrow(); |
994 |
|
} catch (NullPointerException ex) { |
995 |
|
} |
996 |
|
} |
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()); |
985 |
< |
assertEquals(0, q.size()); |
1006 |
> |
checkEmpty(q); |
1007 |
|
} |
1008 |
|
|
1009 |
|
/** |
1013 |
|
public void testTryTransfer3() throws InterruptedException { |
1014 |
|
final Object hotPotato = new Object(); |
1015 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1016 |
< |
new Thread(new Runnable() { |
1017 |
< |
public void run() { |
1018 |
< |
try { |
1019 |
< |
while (! q.hasWaitingConsumer()) |
1020 |
< |
Thread.yield(); |
1021 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1022 |
< |
threadAssertTrue(q.isEmpty()); |
1023 |
< |
threadAssertTrue(q.size() == 0); |
1024 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1025 |
< |
} catch (Throwable ex) { |
1026 |
< |
threadUnexpectedException(ex); |
1027 |
< |
} |
1028 |
< |
}}).start(); |
1029 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) == hotPotato); |
1009 |
< |
assertTrue(q.isEmpty()); |
1016 |
> |
|
1017 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1018 |
> |
void realRun() { |
1019 |
> |
while (! q.hasWaitingConsumer()) |
1020 |
> |
Thread.yield(); |
1021 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
1022 |
> |
threadAssertTrue(q.isEmpty()); |
1023 |
> |
threadAssertTrue(q.size() == 0); |
1024 |
> |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1025 |
> |
}}); |
1026 |
> |
|
1027 |
> |
assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); |
1028 |
> |
checkEmpty(q); |
1029 |
> |
t.join(); |
1030 |
|
} |
1031 |
|
|
1032 |
|
/** |
1036 |
|
public void testTryTransfer4() throws InterruptedException { |
1037 |
|
final Object hotPotato = new Object(); |
1038 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1039 |
< |
new Thread(new Runnable() { |
1040 |
< |
public void run() { |
1041 |
< |
try { |
1042 |
< |
while (! q.hasWaitingConsumer()) |
1043 |
< |
Thread.yield(); |
1044 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1045 |
< |
threadAssertTrue(q.isEmpty()); |
1046 |
< |
threadAssertTrue(q.size() == 0); |
1047 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1048 |
< |
} catch (Throwable ex) { |
1049 |
< |
threadUnexpectedException(ex); |
1030 |
< |
} |
1031 |
< |
}}).start(); |
1039 |
> |
|
1040 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1041 |
> |
void realRun() { |
1042 |
> |
while (! q.hasWaitingConsumer()) |
1043 |
> |
Thread.yield(); |
1044 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
1045 |
> |
threadAssertTrue(q.isEmpty()); |
1046 |
> |
threadAssertTrue(q.size() == 0); |
1047 |
> |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1048 |
> |
}}); |
1049 |
> |
|
1050 |
|
assertTrue(q.take() == hotPotato); |
1051 |
< |
assertTrue(q.isEmpty()); |
1051 |
> |
checkEmpty(q); |
1052 |
> |
t.join(); |
1053 |
|
} |
1054 |
|
|
1055 |
|
/** |
1056 |
< |
* tryTransfer waits the amount given if interrupted, show an |
1057 |
< |
* interrupted exception |
1056 |
> |
* tryTransfer waits the amount given if interrupted, and |
1057 |
> |
* throws interrupted exception |
1058 |
|
*/ |
1059 |
|
public void testTryTransfer5() throws InterruptedException { |
1060 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1061 |
< |
Thread toInterrupt = new Thread(new Runnable() { |
1062 |
< |
public void run() { |
1063 |
< |
try { |
1064 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1046 |
< |
threadShouldThrow(); |
1047 |
< |
} catch (InterruptedException success) { |
1048 |
< |
} catch (Throwable ex) { |
1049 |
< |
threadUnexpectedException(ex); |
1050 |
< |
} |
1061 |
> |
|
1062 |
> |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1063 |
> |
void realRun() throws InterruptedException { |
1064 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1065 |
|
}}); |
1066 |
< |
toInterrupt.start(); |
1066 |
> |
|
1067 |
|
Thread.sleep(SMALL_DELAY_MS); |
1068 |
|
toInterrupt.interrupt(); |
1069 |
+ |
toInterrupt.join(); |
1070 |
|
} |
1071 |
|
|
1072 |
|
/** |
1074 |
|
*/ |
1075 |
|
public void testTryTransfer6() throws InterruptedException { |
1076 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1077 |
< |
new Thread(new Runnable() { |
1078 |
< |
public void run() { |
1079 |
< |
try { |
1080 |
< |
threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1081 |
< |
} catch (Throwable ex) { |
1082 |
< |
threadUnexpectedException(ex); |
1083 |
< |
} |
1084 |
< |
}}).start(); |
1085 |
< |
Thread.sleep(LONG_DELAY_MS); |
1086 |
< |
assertTrue(q.isEmpty()); |
1077 |
> |
|
1078 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1079 |
> |
void realRun() throws InterruptedException { |
1080 |
> |
threadAssertFalse |
1081 |
> |
(q.tryTransfer(new Object(), |
1082 |
> |
SHORT_DELAY_MS, MILLISECONDS)); |
1083 |
> |
}}); |
1084 |
> |
|
1085 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1086 |
> |
checkEmpty(q); |
1087 |
> |
t.join(); |
1088 |
|
} |
1089 |
|
|
1090 |
|
/** |
1094 |
|
public void testTryTransfer7() throws InterruptedException { |
1095 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1096 |
|
assertTrue(q.offer(four)); |
1097 |
< |
new Thread(new Runnable() { |
1098 |
< |
public void run() { |
1099 |
< |
try { |
1100 |
< |
threadAssertTrue(q.tryTransfer(five, LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
1101 |
< |
threadAssertTrue(q.isEmpty()); |
1102 |
< |
} catch (Throwable ex) { |
1103 |
< |
threadUnexpectedException(ex); |
1104 |
< |
} |
1089 |
< |
}}).start(); |
1097 |
> |
|
1098 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1099 |
> |
void realRun() throws InterruptedException { |
1100 |
> |
threadAssertTrue(q.tryTransfer(five, |
1101 |
> |
MEDIUM_DELAY_MS, MILLISECONDS)); |
1102 |
> |
threadAssertTrue(q.isEmpty()); |
1103 |
> |
}}); |
1104 |
> |
|
1105 |
|
Thread.sleep(SHORT_DELAY_MS); |
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 |
|
|
1113 |
|
/** |
1118 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1119 |
|
assertTrue(q.offer(four)); |
1120 |
|
assertEquals(1, q.size()); |
1121 |
< |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
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 |
|
|