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; |
21 |
|
import junit.framework.Test; |
22 |
|
import junit.framework.TestSuite; |
23 |
|
|
24 |
+ |
@SuppressWarnings({"unchecked", "rawtypes"}) |
25 |
|
public class LinkedTransferQueueTest extends JSR166TestCase { |
26 |
|
|
27 |
|
public static void main(String[] args) { |
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 being true |
62 |
> |
* Constructor builds new queue with size being zero and empty |
63 |
> |
* being true |
64 |
|
*/ |
65 |
|
public void testConstructor1() { |
66 |
|
assertEquals(0, new LinkedTransferQueue().size()); |
68 |
|
} |
69 |
|
|
70 |
|
/** |
71 |
< |
* Initializing constructor with null collection throws NullPointerException |
71 |
> |
* Initializing constructor with null collection throws |
72 |
> |
* NullPointerException |
73 |
|
*/ |
74 |
|
public void testConstructor2() { |
75 |
|
try { |
80 |
|
} |
81 |
|
|
82 |
|
/** |
83 |
< |
* Initializing from Collection of null elements throws NullPointerException |
83 |
> |
* Initializing from Collection of null elements throws |
84 |
> |
* NullPointerException |
85 |
|
*/ |
86 |
|
public void testConstructor3() { |
87 |
|
try { |
88 |
|
Integer[] ints = new Integer[SIZE]; |
89 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
89 |
> |
new LinkedTransferQueue(Arrays.asList(ints)); |
90 |
|
shouldThrow(); |
91 |
|
} catch (NullPointerException success) { |
92 |
|
} |
102 |
|
for (int i = 0; i < SIZE - 1; ++i) { |
103 |
|
ints[i] = i; |
104 |
|
} |
105 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
105 |
> |
new LinkedTransferQueue(Arrays.asList(ints)); |
106 |
|
shouldThrow(); |
107 |
|
} catch (NullPointerException success) { |
108 |
|
} |
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 = new LinkedTransferQueue(Arrays.asList(ints)); |
121 |
< |
for (int i = 0; i < SIZE; ++i) { |
122 |
< |
assertEquals(ints[i], q.poll()); |
123 |
< |
} |
124 |
< |
} 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); |
211 |
|
} |
212 |
|
|
213 |
|
/** |
214 |
< |
* addAll of a collection with any null elements throws NullPointerException after |
215 |
< |
* possibly adding some elements |
214 |
> |
* addAll of a collection with any null elements throws |
215 |
> |
* NullPointerException after possibly adding some elements |
216 |
|
*/ |
217 |
|
public void testAddAll3() { |
218 |
|
try { |
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 |
|
} |
229 |
– |
assertEquals(q.size(), SIZE); |
268 |
|
} |
269 |
|
|
270 |
|
/** |
282 |
|
*/ |
283 |
|
public void testTakeFromEmpty() throws InterruptedException { |
284 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
285 |
< |
Thread t = new Thread(new Runnable() { |
286 |
< |
public void run() { |
287 |
< |
try { |
250 |
< |
q.take(); |
251 |
< |
threadShouldThrow(); |
252 |
< |
} catch (InterruptedException success) { |
253 |
< |
} catch (Throwable ex) { |
254 |
< |
threadUnexpectedException(ex); |
255 |
< |
} |
285 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
286 |
> |
void realRun() throws InterruptedException { |
287 |
> |
q.take(); |
288 |
|
}}); |
257 |
– |
t.start(); |
289 |
|
Thread.sleep(SHORT_DELAY_MS); |
290 |
|
t.interrupt(); |
291 |
|
t.join(); |
295 |
|
* Take removes existing elements until empty, then blocks interruptibly |
296 |
|
*/ |
297 |
|
public void testBlockingTake() throws InterruptedException { |
298 |
< |
Thread t = new Thread(new Runnable() { |
299 |
< |
public void run() { |
300 |
< |
try { |
301 |
< |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
302 |
< |
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); |
298 |
> |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
299 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
300 |
> |
void realRun() throws InterruptedException { |
301 |
> |
for (int i = 0; i < SIZE; ++i) { |
302 |
> |
threadAssertEquals(i, (int) q.take()); |
303 |
|
} |
304 |
+ |
q.take(); |
305 |
|
}}); |
306 |
< |
t.start(); |
282 |
< |
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 |
|
/** |
327 |
|
public void testTimedPoll0() throws InterruptedException { |
328 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
329 |
|
for (int i = 0; i < SIZE; ++i) { |
330 |
< |
assertEquals(i, (int) q.poll(0, TimeUnit.MILLISECONDS)); |
330 |
> |
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
331 |
|
} |
332 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
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, TimeUnit.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, TimeUnit.MILLISECONDS)); |
347 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
348 |
> |
checkEmpty(q); |
349 |
|
} |
350 |
|
|
351 |
|
/** |
353 |
|
* returning timeout status |
354 |
|
*/ |
355 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
356 |
< |
Thread t = new Thread(new Runnable() { |
357 |
< |
public void run() { |
358 |
< |
try { |
359 |
< |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
360 |
< |
for (int i = 0; i < SIZE; ++i) { |
361 |
< |
threadAssertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
362 |
< |
} |
363 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
364 |
< |
} catch (InterruptedException success) { |
334 |
< |
} catch (Throwable ex) { |
335 |
< |
threadUnexpectedException(ex); |
356 |
> |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
357 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
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 |
+ |
try { |
367 |
+ |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
368 |
+ |
} catch (InterruptedException success) {} |
369 |
|
}}); |
370 |
< |
t.start(); |
339 |
< |
Thread.sleep(SHORT_DELAY_MS); |
370 |
> |
Thread.sleep(SMALL_DELAY_MS); |
371 |
|
t.interrupt(); |
372 |
|
t.join(); |
373 |
+ |
checkEmpty(q); |
374 |
|
} |
375 |
|
|
376 |
|
/** |
379 |
|
*/ |
380 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
381 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
382 |
< |
Thread t = new Thread(new Runnable() { |
383 |
< |
public void run() { |
384 |
< |
try { |
385 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
386 |
< |
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 |
< |
} |
382 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
383 |
> |
void realRun() throws InterruptedException { |
384 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
385 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
386 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
387 |
|
}}); |
362 |
– |
t.start(); |
388 |
|
Thread.sleep(SMALL_DELAY_MS); |
389 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
389 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
390 |
|
t.interrupt(); |
391 |
|
t.join(); |
392 |
|
} |
394 |
|
/** |
395 |
|
* peek returns next element, or null if empty |
396 |
|
*/ |
397 |
< |
public void testPeek() { |
397 |
> |
public void testPeek() throws InterruptedException { |
398 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
399 |
|
for (int i = 0; i < SIZE; ++i) { |
400 |
|
assertEquals(i, (int) q.peek()); |
403 |
|
i != (int) q.peek()); |
404 |
|
} |
405 |
|
assertNull(q.peek()); |
406 |
+ |
checkEmpty(q); |
407 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
|
* element returns next element, or throws NoSuchElementException if empty |
411 |
|
*/ |
412 |
< |
public void testElement() { |
412 |
> |
public void testElement() throws InterruptedException { |
413 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
414 |
|
for (int i = 0; i < SIZE; ++i) { |
415 |
|
assertEquals(i, (int) q.element()); |
420 |
|
shouldThrow(); |
421 |
|
} catch (NoSuchElementException success) { |
422 |
|
} |
423 |
+ |
checkEmpty(q); |
424 |
|
} |
425 |
|
|
426 |
|
/** |
427 |
|
* remove removes next element, or throws NoSuchElementException if empty |
428 |
|
*/ |
429 |
< |
public void testRemove() { |
429 |
> |
public void testRemove() throws InterruptedException { |
430 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
431 |
|
for (int i = 0; i < SIZE; ++i) { |
432 |
|
assertEquals(i, (int) q.remove()); |
436 |
|
shouldThrow(); |
437 |
|
} catch (NoSuchElementException success) { |
438 |
|
} |
439 |
+ |
checkEmpty(q); |
440 |
|
} |
441 |
|
|
442 |
|
/** |
443 |
|
* remove(x) removes x and returns true if present |
444 |
|
*/ |
445 |
< |
public void testRemoveElement() { |
445 |
> |
public void testRemoveElement() throws InterruptedException { |
446 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
447 |
|
for (int i = 1; i < SIZE; i += 2) { |
448 |
|
assertTrue(q.remove(i)); |
451 |
|
assertTrue(q.remove(i)); |
452 |
|
assertFalse(q.remove(i + 1)); |
453 |
|
} |
454 |
< |
assertTrue(q.isEmpty()); |
454 |
> |
checkEmpty(q); |
455 |
|
} |
456 |
|
|
457 |
|
/** |
464 |
|
assertTrue(q.remove(one)); |
465 |
|
assertTrue(q.remove(two)); |
466 |
|
assertTrue(q.add(three)); |
467 |
< |
assertTrue(q.take() != null); |
467 |
> |
assertTrue(q.take() == three); |
468 |
|
} |
469 |
|
|
470 |
|
/** |
482 |
|
/** |
483 |
|
* clear removes all elements |
484 |
|
*/ |
485 |
< |
public void testClear() { |
485 |
> |
public void testClear() throws InterruptedException { |
486 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
487 |
|
q.clear(); |
488 |
< |
assertTrue(q.isEmpty()); |
461 |
< |
assertEquals(0, q.size()); |
488 |
> |
checkEmpty(q); |
489 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
490 |
|
q.add(one); |
491 |
|
assertFalse(q.isEmpty()); |
492 |
+ |
assertEquals(1, q.size()); |
493 |
|
assertTrue(q.contains(one)); |
494 |
|
q.clear(); |
495 |
< |
assertTrue(q.isEmpty()); |
495 |
> |
checkEmpty(q); |
496 |
|
} |
497 |
|
|
498 |
|
/** |
510 |
|
} |
511 |
|
|
512 |
|
/** |
513 |
< |
* retainAll(c) retains only those elements of c and reports true if changed |
513 |
> |
* retainAll(c) retains only those elements of c and reports true |
514 |
> |
* if changed |
515 |
|
*/ |
516 |
|
public void testRetainAll() { |
517 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
530 |
|
} |
531 |
|
|
532 |
|
/** |
533 |
< |
* removeAll(c) removes only those elements of c and reports true if changed |
533 |
> |
* removeAll(c) removes only those elements of c and reports true |
534 |
> |
* if changed |
535 |
|
*/ |
536 |
|
public void testRemoveAll() { |
537 |
|
for (int i = 1; i < SIZE; ++i) { |
546 |
|
} |
547 |
|
|
548 |
|
/** |
549 |
< |
* toArray contains all elements |
549 |
> |
* toArray() contains all elements |
550 |
|
*/ |
551 |
|
public void testToArray() throws InterruptedException { |
552 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
581 |
|
} |
582 |
|
|
583 |
|
/** |
584 |
< |
* toArray with incompatible array type throws CCE |
584 |
> |
* toArray(incompatible array type) throws CCE |
585 |
|
*/ |
586 |
|
public void testToArray1_BadArg() { |
587 |
|
try { |
598 |
|
public void testIterator() throws InterruptedException { |
599 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
600 |
|
Iterator it = q.iterator(); |
601 |
+ |
int i = 0; |
602 |
|
while (it.hasNext()) { |
603 |
< |
assertEquals(it.next(), q.take()); |
603 |
> |
assertEquals(it.next(), i++); |
604 |
|
} |
605 |
+ |
assertEquals(i, SIZE); |
606 |
|
} |
607 |
|
|
608 |
|
/** |
609 |
< |
* iterator.remove removes current element |
609 |
> |
* iterator.remove() removes current element |
610 |
|
*/ |
611 |
|
public void testIteratorRemove() { |
612 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
628 |
|
* iterator ordering is FIFO |
629 |
|
*/ |
630 |
|
public void testIteratorOrdering() { |
631 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
631 |
> |
final LinkedTransferQueue<Integer> q |
632 |
> |
= new LinkedTransferQueue<Integer>(); |
633 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
634 |
|
q.add(one); |
635 |
|
q.add(two); |
676 |
|
q.add(one); |
677 |
|
q.add(two); |
678 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
679 |
< |
executor.execute(new Runnable() { |
680 |
< |
public void run() { |
681 |
< |
try { |
682 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
683 |
< |
} catch (Throwable ex) { |
651 |
< |
threadUnexpectedException(ex); |
652 |
< |
} |
679 |
> |
|
680 |
> |
executor.execute(new CheckedRunnable() { |
681 |
> |
void realRun() { |
682 |
> |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, |
683 |
> |
MILLISECONDS)); |
684 |
|
}}); |
685 |
|
|
686 |
< |
executor.execute(new Runnable() { |
687 |
< |
public void run() { |
688 |
< |
try { |
689 |
< |
Thread.sleep(SMALL_DELAY_MS); |
659 |
< |
threadAssertEquals(one, q.take()); |
660 |
< |
} catch (Throwable ex) { |
661 |
< |
threadUnexpectedException(ex); |
662 |
< |
} |
686 |
> |
executor.execute(new CheckedRunnable() { |
687 |
> |
void realRun() throws InterruptedException { |
688 |
> |
Thread.sleep(SMALL_DELAY_MS); |
689 |
> |
threadAssertEquals(one, q.take()); |
690 |
|
}}); |
691 |
|
|
692 |
|
joinPool(executor); |
693 |
|
} |
694 |
|
|
695 |
|
/** |
696 |
< |
* poll retrieves elements across Executor threads |
696 |
> |
* timed poll retrieves elements across Executor threads |
697 |
|
*/ |
698 |
|
public void testPollInExecutor() { |
699 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
700 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
701 |
< |
executor.execute(new Runnable() { |
702 |
< |
public void run() { |
703 |
< |
try { |
704 |
< |
threadAssertNull(q.poll()); |
705 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
706 |
< |
threadAssertTrue(q.isEmpty()); |
707 |
< |
} catch (Throwable ex) { |
681 |
< |
threadUnexpectedException(ex); |
682 |
< |
} |
701 |
> |
|
702 |
> |
executor.execute(new CheckedRunnable() { |
703 |
> |
void realRun() throws InterruptedException { |
704 |
> |
threadAssertNull(q.poll()); |
705 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, |
706 |
> |
MILLISECONDS)); |
707 |
> |
threadAssertTrue(q.isEmpty()); |
708 |
|
}}); |
709 |
|
|
710 |
< |
executor.execute(new Runnable() { |
711 |
< |
public void run() { |
712 |
< |
try { |
713 |
< |
Thread.sleep(SMALL_DELAY_MS); |
689 |
< |
q.put(one); |
690 |
< |
} catch (Throwable ex) { |
691 |
< |
threadUnexpectedException(ex); |
692 |
< |
} |
710 |
> |
executor.execute(new CheckedRunnable() { |
711 |
> |
void realRun() throws InterruptedException { |
712 |
> |
Thread.sleep(SMALL_DELAY_MS); |
713 |
> |
q.put(one); |
714 |
|
}}); |
715 |
|
|
716 |
|
joinPool(executor); |
723 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
724 |
|
|
725 |
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
726 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
726 |
> |
ObjectOutputStream out |
727 |
> |
= new ObjectOutputStream(new BufferedOutputStream(bout)); |
728 |
|
out.writeObject(q); |
729 |
|
out.close(); |
730 |
|
|
731 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
732 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
731 |
> |
ByteArrayInputStream bin |
732 |
> |
= new ByteArrayInputStream(bout.toByteArray()); |
733 |
> |
ObjectInputStream in |
734 |
> |
= new ObjectInputStream(new BufferedInputStream(bin)); |
735 |
|
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
736 |
|
|
737 |
|
assertEquals(q.size(), r.size()); |
791 |
|
} |
792 |
|
|
793 |
|
/** |
794 |
< |
* drainTo empties full queue, unblocking a waiting put. |
794 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
795 |
|
*/ |
796 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
797 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
798 |
< |
Thread t = new Thread(new Runnable() { |
799 |
< |
public void run() { |
800 |
< |
try { |
777 |
< |
q.put(SIZE + 1); |
778 |
< |
} catch (Throwable ex) { |
779 |
< |
threadUnexpectedException(ex); |
780 |
< |
} |
798 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
799 |
> |
void realRun() { |
800 |
> |
q.put(SIZE + 1); |
801 |
|
}}); |
782 |
– |
t.start(); |
802 |
|
ArrayList l = new ArrayList(); |
803 |
|
q.drainTo(l); |
804 |
|
assertTrue(l.size() >= SIZE); |
815 |
|
public void testDrainToNullN() { |
816 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
817 |
|
try { |
818 |
< |
q.drainTo(null, 0); |
818 |
> |
q.drainTo(null, SIZE); |
819 |
|
shouldThrow(); |
820 |
|
} catch (NullPointerException success) { |
821 |
|
} |
827 |
|
public void testDrainToSelfN() { |
828 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
829 |
|
try { |
830 |
< |
q.drainTo(q, 0); |
830 |
> |
q.drainTo(q, SIZE); |
831 |
|
shouldThrow(); |
832 |
|
} catch (IllegalArgumentException success) { |
833 |
|
} |
856 |
|
} |
857 |
|
|
858 |
|
/** |
859 |
< |
* poll and take decrement the waiting consumer count |
859 |
> |
* timed poll() or take() increments the waiting consumer count; |
860 |
> |
* offer(e) decrements the waiting consumer count |
861 |
|
*/ |
862 |
|
public void testWaitingConsumer() throws InterruptedException { |
863 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
864 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
865 |
< |
new Thread(new Runnable() { |
866 |
< |
public void run() { |
867 |
< |
try { |
868 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
869 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
870 |
< |
threadAssertTrue(q.offer(new Object())); |
871 |
< |
} catch (Throwable ex) { |
872 |
< |
threadUnexpectedException(ex); |
873 |
< |
} |
874 |
< |
}}).start(); |
875 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
876 |
< |
assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers()); |
864 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
865 |
> |
assertFalse(q.hasWaitingConsumer()); |
866 |
> |
|
867 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
868 |
> |
void realRun() throws InterruptedException { |
869 |
> |
Thread.sleep(SMALL_DELAY_MS); |
870 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
871 |
> |
threadAssertEquals(q.getWaitingConsumerCount(), 1); |
872 |
> |
threadAssertTrue(q.offer(new Object())); |
873 |
> |
threadAssertFalse(q.hasWaitingConsumer()); |
874 |
> |
threadAssertEquals(q.getWaitingConsumerCount(), 0); |
875 |
> |
}}); |
876 |
> |
|
877 |
> |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
878 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
879 |
> |
assertFalse(q.hasWaitingConsumer()); |
880 |
> |
t.join(); |
881 |
|
} |
882 |
|
|
883 |
|
/** |
888 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
889 |
|
q.transfer(null); |
890 |
|
shouldThrow(); |
891 |
< |
} catch (NullPointerException ex) { |
868 |
< |
} |
891 |
> |
} catch (NullPointerException success) {} |
892 |
|
} |
893 |
|
|
894 |
|
/** |
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 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)); |
907 |
< |
threadAssertEquals(1, q.size()); |
908 |
< |
} catch (Throwable ex) { |
909 |
< |
threadUnexpectedException(ex); |
910 |
< |
} |
911 |
< |
}}); |
912 |
< |
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(); |
920 |
< |
q.transfer(SIZE); |
921 |
< |
threadShouldThrow(); |
922 |
< |
} catch (InterruptedException success) { |
923 |
< |
} catch (Throwable ex) { |
924 |
< |
threadUnexpectedException(ex); |
925 |
< |
} |
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 |
|
}}); |
927 |
– |
interruptedThread.start(); |
937 |
|
|
938 |
|
while (q.size() < 2) |
939 |
|
Thread.yield(); |
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 |
< |
} |
956 |
< |
}}).start(); |
957 |
< |
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 |
|
/** |
972 |
|
* is returned by this associated take. |
973 |
|
*/ |
974 |
|
public void testTransfer5() throws InterruptedException { |
975 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
975 |
> |
final LinkedTransferQueue<Integer> q |
976 |
> |
= new LinkedTransferQueue<Integer>(); |
977 |
|
|
978 |
< |
new Thread(new Runnable() { |
979 |
< |
public void run() { |
980 |
< |
try { |
981 |
< |
q.transfer(SIZE); |
982 |
< |
threadAssertTrue(q.isEmpty()); |
974 |
< |
} catch (Throwable ex) { |
975 |
< |
threadUnexpectedException(ex); |
976 |
< |
} |
977 |
< |
}}).start(); |
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 |
< |
assertTrue(q.isEmpty()); |
986 |
> |
checkEmpty(q); |
987 |
> |
t.join(); |
988 |
|
} |
989 |
|
|
990 |
|
/** |
994 |
|
try { |
995 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
996 |
|
q.tryTransfer(null); |
997 |
< |
this.shouldThrow(); |
998 |
< |
} catch (NullPointerException ex) { |
993 |
< |
} |
997 |
> |
shouldThrow(); |
998 |
> |
} catch (NullPointerException success) {} |
999 |
|
} |
1000 |
|
|
1001 |
|
/** |
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()); |
1005 |
< |
assertEquals(0, q.size()); |
1009 |
> |
checkEmpty(q); |
1010 |
|
} |
1011 |
|
|
1012 |
|
/** |
1016 |
|
public void testTryTransfer3() throws InterruptedException { |
1017 |
|
final Object hotPotato = new Object(); |
1018 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1019 |
< |
new Thread(new Runnable() { |
1020 |
< |
public void run() { |
1021 |
< |
try { |
1022 |
< |
while (! q.hasWaitingConsumer()) |
1023 |
< |
Thread.yield(); |
1024 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1025 |
< |
threadAssertTrue(q.isEmpty()); |
1026 |
< |
threadAssertTrue(q.size() == 0); |
1027 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1028 |
< |
} catch (Throwable ex) { |
1029 |
< |
threadUnexpectedException(ex); |
1030 |
< |
} |
1031 |
< |
}}).start(); |
1032 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) == hotPotato); |
1029 |
< |
assertTrue(q.isEmpty()); |
1019 |
> |
|
1020 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1021 |
> |
void realRun() { |
1022 |
> |
while (! q.hasWaitingConsumer()) |
1023 |
> |
Thread.yield(); |
1024 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
1025 |
> |
threadAssertTrue(q.isEmpty()); |
1026 |
> |
threadAssertTrue(q.size() == 0); |
1027 |
> |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1028 |
> |
}}); |
1029 |
> |
|
1030 |
> |
assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); |
1031 |
> |
checkEmpty(q); |
1032 |
> |
t.join(); |
1033 |
|
} |
1034 |
|
|
1035 |
|
/** |
1039 |
|
public void testTryTransfer4() throws InterruptedException { |
1040 |
|
final Object hotPotato = new Object(); |
1041 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1042 |
< |
new Thread(new Runnable() { |
1043 |
< |
public void run() { |
1044 |
< |
try { |
1045 |
< |
while (! q.hasWaitingConsumer()) |
1046 |
< |
Thread.yield(); |
1047 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1048 |
< |
threadAssertTrue(q.isEmpty()); |
1049 |
< |
threadAssertTrue(q.size() == 0); |
1050 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1051 |
< |
} catch (Throwable ex) { |
1052 |
< |
threadUnexpectedException(ex); |
1050 |
< |
} |
1051 |
< |
}}).start(); |
1042 |
> |
|
1043 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1044 |
> |
void realRun() { |
1045 |
> |
while (! q.hasWaitingConsumer()) |
1046 |
> |
Thread.yield(); |
1047 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
1048 |
> |
threadAssertTrue(q.isEmpty()); |
1049 |
> |
threadAssertTrue(q.size() == 0); |
1050 |
> |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1051 |
> |
}}); |
1052 |
> |
|
1053 |
|
assertTrue(q.take() == hotPotato); |
1054 |
< |
assertTrue(q.isEmpty()); |
1054 |
> |
checkEmpty(q); |
1055 |
> |
t.join(); |
1056 |
|
} |
1057 |
|
|
1058 |
|
/** |
1061 |
|
*/ |
1062 |
|
public void testTryTransfer5() throws InterruptedException { |
1063 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1064 |
< |
Thread toInterrupt = new Thread(new Runnable() { |
1065 |
< |
public void run() { |
1066 |
< |
try { |
1067 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1066 |
< |
threadShouldThrow(); |
1067 |
< |
} catch (InterruptedException success) { |
1068 |
< |
} catch (Throwable ex) { |
1069 |
< |
threadUnexpectedException(ex); |
1070 |
< |
} |
1064 |
> |
|
1065 |
> |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1066 |
> |
void realRun() throws InterruptedException { |
1067 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1068 |
|
}}); |
1069 |
< |
toInterrupt.start(); |
1069 |
> |
|
1070 |
|
Thread.sleep(SMALL_DELAY_MS); |
1071 |
|
toInterrupt.interrupt(); |
1072 |
+ |
toInterrupt.join(); |
1073 |
|
} |
1074 |
|
|
1075 |
|
/** |
1077 |
|
*/ |
1078 |
|
public void testTryTransfer6() throws InterruptedException { |
1079 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1080 |
< |
new Thread(new Runnable() { |
1081 |
< |
public void run() { |
1082 |
< |
try { |
1083 |
< |
threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1084 |
< |
} catch (Throwable ex) { |
1085 |
< |
threadUnexpectedException(ex); |
1086 |
< |
} |
1087 |
< |
}}).start(); |
1088 |
< |
Thread.sleep(LONG_DELAY_MS); |
1089 |
< |
assertTrue(q.isEmpty()); |
1080 |
> |
|
1081 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1082 |
> |
void realRun() throws InterruptedException { |
1083 |
> |
threadAssertFalse |
1084 |
> |
(q.tryTransfer(new Object(), |
1085 |
> |
SHORT_DELAY_MS, MILLISECONDS)); |
1086 |
> |
}}); |
1087 |
> |
|
1088 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1089 |
> |
checkEmpty(q); |
1090 |
> |
t.join(); |
1091 |
|
} |
1092 |
|
|
1093 |
|
/** |
1097 |
|
public void testTryTransfer7() throws InterruptedException { |
1098 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1099 |
|
assertTrue(q.offer(four)); |
1100 |
< |
new Thread(new Runnable() { |
1101 |
< |
public void run() { |
1102 |
< |
try { |
1103 |
< |
threadAssertTrue(q.tryTransfer(five, LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
1104 |
< |
threadAssertTrue(q.isEmpty()); |
1105 |
< |
} catch (Throwable ex) { |
1106 |
< |
threadUnexpectedException(ex); |
1107 |
< |
} |
1109 |
< |
}}).start(); |
1100 |
> |
|
1101 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1102 |
> |
void realRun() throws InterruptedException { |
1103 |
> |
threadAssertTrue(q.tryTransfer(five, |
1104 |
> |
MEDIUM_DELAY_MS, MILLISECONDS)); |
1105 |
> |
threadAssertTrue(q.isEmpty()); |
1106 |
> |
}}); |
1107 |
> |
|
1108 |
|
Thread.sleep(SHORT_DELAY_MS); |
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 |
|
|
1116 |
|
/** |
1121 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1122 |
|
assertTrue(q.offer(four)); |
1123 |
|
assertEquals(1, q.size()); |
1124 |
< |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1124 |
> |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1125 |
|
assertEquals(1, q.size()); |
1126 |
|
assertEquals(four, q.poll()); |
1128 |
– |
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 |
|
} |
1143 |
– |
|
1144 |
– |
private static class ConsumerObserver { |
1145 |
– |
|
1146 |
– |
private int waitingConsumers; |
1147 |
– |
|
1148 |
– |
private ConsumerObserver() { |
1149 |
– |
} |
1150 |
– |
|
1151 |
– |
private void setWaitingConsumer(int i) { |
1152 |
– |
this.waitingConsumers = i; |
1153 |
– |
} |
1154 |
– |
|
1155 |
– |
private int getWaitingConsumers() { |
1156 |
– |
return waitingConsumers; |
1157 |
– |
} |
1158 |
– |
} |
1142 |
|
} |