1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include John Vint |
6 |
|
*/ |
7 |
|
|
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 static java.util.concurrent.TimeUnit.NANOSECONDS; |
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 class Generic extends BlockingQueueTest { |
29 |
+ |
protected BlockingQueue emptyCollection() { |
30 |
+ |
return new LinkedTransferQueue(); |
31 |
+ |
} |
32 |
+ |
} |
33 |
+ |
|
34 |
|
public static void main(String[] args) { |
35 |
|
junit.textui.TestRunner.run(suite()); |
36 |
|
} |
37 |
|
|
38 |
|
public static Test suite() { |
39 |
< |
return new TestSuite(LinkedTransferQueueTest.class); |
40 |
< |
} |
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 |
< |
} |
39 |
> |
return newTestSuite(LinkedTransferQueueTest.class, |
40 |
> |
new Generic().testSuite()); |
41 |
|
} |
42 |
|
|
43 |
|
/** |
57 |
|
try { |
58 |
|
new LinkedTransferQueue(null); |
59 |
|
shouldThrow(); |
60 |
< |
} catch (NullPointerException success) { |
80 |
< |
} |
60 |
> |
} catch (NullPointerException success) {} |
61 |
|
} |
62 |
|
|
63 |
|
/** |
69 |
|
Integer[] ints = new Integer[SIZE]; |
70 |
|
new LinkedTransferQueue(Arrays.asList(ints)); |
71 |
|
shouldThrow(); |
72 |
< |
} catch (NullPointerException success) { |
93 |
< |
} |
72 |
> |
} catch (NullPointerException success) {} |
73 |
|
} |
74 |
|
|
75 |
|
/** |
84 |
|
} |
85 |
|
new LinkedTransferQueue(Arrays.asList(ints)); |
86 |
|
shouldThrow(); |
87 |
< |
} catch (NullPointerException success) { |
109 |
< |
} |
87 |
> |
} catch (NullPointerException success) {} |
88 |
|
} |
89 |
|
|
90 |
|
/** |
136 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
137 |
|
q.offer(null); |
138 |
|
shouldThrow(); |
139 |
< |
} catch (NullPointerException success) { |
162 |
< |
} |
139 |
> |
} catch (NullPointerException success) {} |
140 |
|
} |
141 |
|
|
142 |
|
/** |
147 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
148 |
|
q.add(null); |
149 |
|
shouldThrow(); |
150 |
< |
} catch (NullPointerException success) { |
174 |
< |
} |
150 |
> |
} catch (NullPointerException success) {} |
151 |
|
} |
152 |
|
|
153 |
|
/** |
158 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
159 |
|
q.addAll(null); |
160 |
|
shouldThrow(); |
161 |
< |
} catch (NullPointerException success) { |
186 |
< |
} |
161 |
> |
} catch (NullPointerException success) {} |
162 |
|
} |
163 |
|
|
164 |
|
/** |
169 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
170 |
|
q.addAll(q); |
171 |
|
shouldThrow(); |
172 |
< |
} catch (IllegalArgumentException success) { |
198 |
< |
} |
172 |
> |
} catch (IllegalArgumentException success) {} |
173 |
|
} |
174 |
|
|
175 |
|
/** |
181 |
|
Integer[] ints = new Integer[SIZE]; |
182 |
|
q.addAll(Arrays.asList(ints)); |
183 |
|
shouldThrow(); |
184 |
< |
} catch (NullPointerException success) { |
211 |
< |
} |
184 |
> |
} catch (NullPointerException success) {} |
185 |
|
} |
186 |
|
|
187 |
|
/** |
197 |
|
} |
198 |
|
q.addAll(Arrays.asList(ints)); |
199 |
|
shouldThrow(); |
200 |
< |
} catch (NullPointerException success) { |
228 |
< |
} |
200 |
> |
} catch (NullPointerException success) {} |
201 |
|
} |
202 |
|
|
203 |
|
/** |
251 |
|
} |
252 |
|
|
253 |
|
/** |
254 |
< |
* take blocks interruptibly when empty |
283 |
< |
*/ |
284 |
< |
public void testTakeFromEmpty() throws InterruptedException { |
285 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
286 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
287 |
< |
void realRun() throws InterruptedException { |
288 |
< |
q.take(); |
289 |
< |
}}); |
290 |
< |
Thread.sleep(SHORT_DELAY_MS); |
291 |
< |
t.interrupt(); |
292 |
< |
t.join(); |
293 |
< |
} |
294 |
< |
|
295 |
< |
/** |
296 |
< |
* Take removes existing elements until empty, then blocks interruptibly |
254 |
> |
* take removes existing elements until empty, then blocks interruptibly |
255 |
|
*/ |
256 |
|
public void testBlockingTake() throws InterruptedException { |
257 |
< |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
258 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
259 |
< |
void realRun() throws InterruptedException { |
257 |
> |
final BlockingQueue q = populatedQueue(SIZE); |
258 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
259 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
260 |
> |
public void realRun() throws InterruptedException { |
261 |
|
for (int i = 0; i < SIZE; ++i) { |
262 |
< |
threadAssertEquals(i, (int) q.take()); |
262 |
> |
assertEquals(i, q.take()); |
263 |
|
} |
264 |
< |
q.take(); |
264 |
> |
|
265 |
> |
Thread.currentThread().interrupt(); |
266 |
> |
try { |
267 |
> |
q.take(); |
268 |
> |
shouldThrow(); |
269 |
> |
} catch (InterruptedException success) {} |
270 |
> |
assertFalse(Thread.interrupted()); |
271 |
> |
|
272 |
> |
pleaseInterrupt.countDown(); |
273 |
> |
try { |
274 |
> |
q.take(); |
275 |
> |
shouldThrow(); |
276 |
> |
} catch (InterruptedException success) {} |
277 |
> |
assertFalse(Thread.interrupted()); |
278 |
|
}}); |
279 |
< |
Thread.sleep(SMALL_DELAY_MS); |
279 |
> |
|
280 |
> |
await(pleaseInterrupt); |
281 |
> |
assertThreadStaysAlive(t); |
282 |
|
t.interrupt(); |
283 |
< |
t.join(); |
310 |
< |
checkEmpty(q); |
283 |
> |
awaitTermination(t); |
284 |
|
} |
285 |
|
|
286 |
|
/** |
296 |
|
} |
297 |
|
|
298 |
|
/** |
299 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
299 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
300 |
|
*/ |
301 |
|
public void testTimedPoll0() throws InterruptedException { |
302 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
308 |
|
} |
309 |
|
|
310 |
|
/** |
311 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
311 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
312 |
|
*/ |
313 |
|
public void testTimedPoll() throws InterruptedException { |
314 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
315 |
|
for (int i = 0; i < SIZE; ++i) { |
316 |
< |
long t0 = System.nanoTime(); |
316 |
> |
long startTime = System.nanoTime(); |
317 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
318 |
< |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
346 |
< |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
318 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
319 |
|
} |
320 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
320 |
> |
long startTime = System.nanoTime(); |
321 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
322 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
323 |
|
checkEmpty(q); |
324 |
|
} |
325 |
|
|
328 |
|
* returning timeout status |
329 |
|
*/ |
330 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
331 |
< |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
332 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
333 |
< |
void realRun() throws InterruptedException { |
331 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
332 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
333 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
334 |
> |
public void realRun() throws InterruptedException { |
335 |
|
for (int i = 0; i < SIZE; ++i) { |
336 |
|
long t0 = System.nanoTime(); |
337 |
< |
threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS, |
338 |
< |
MILLISECONDS)); |
339 |
< |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
340 |
< |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
337 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
338 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
339 |
> |
} |
340 |
> |
long t0 = System.nanoTime(); |
341 |
> |
aboutToWait.countDown(); |
342 |
> |
try { |
343 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
344 |
> |
shouldThrow(); |
345 |
> |
} catch (InterruptedException success) { |
346 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
347 |
|
} |
367 |
– |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
348 |
|
}}); |
349 |
< |
Thread.sleep(SMALL_DELAY_MS); |
349 |
> |
|
350 |
> |
aboutToWait.await(); |
351 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
352 |
|
t.interrupt(); |
353 |
< |
t.join(); |
353 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
354 |
|
checkEmpty(q); |
355 |
|
} |
356 |
|
|
357 |
|
/** |
358 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
359 |
< |
* on interruption throws |
358 |
> |
* timed poll after thread interrupted throws InterruptedException |
359 |
> |
* instead of returning timeout status |
360 |
|
*/ |
361 |
< |
public void testTimedPollWithOffer() throws InterruptedException { |
362 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
363 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
364 |
< |
void realRun() throws InterruptedException { |
365 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
366 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
367 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
361 |
> |
public void testTimedPollAfterInterrupt() throws InterruptedException { |
362 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
363 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
364 |
> |
public void realRun() throws InterruptedException { |
365 |
> |
Thread.currentThread().interrupt(); |
366 |
> |
for (int i = 0; i < SIZE; ++i) { |
367 |
> |
long t0 = System.nanoTime(); |
368 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
369 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
370 |
> |
} |
371 |
> |
try { |
372 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
373 |
> |
shouldThrow(); |
374 |
> |
} catch (InterruptedException success) {} |
375 |
|
}}); |
376 |
< |
Thread.sleep(SMALL_DELAY_MS); |
377 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
378 |
< |
t.interrupt(); |
390 |
< |
t.join(); |
376 |
> |
|
377 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
378 |
> |
checkEmpty(q); |
379 |
|
} |
380 |
|
|
381 |
|
/** |
405 |
|
try { |
406 |
|
q.element(); |
407 |
|
shouldThrow(); |
408 |
< |
} catch (NoSuchElementException success) { |
421 |
< |
} |
408 |
> |
} catch (NoSuchElementException success) {} |
409 |
|
checkEmpty(q); |
410 |
|
} |
411 |
|
|
420 |
|
try { |
421 |
|
q.remove(); |
422 |
|
shouldThrow(); |
423 |
< |
} catch (NoSuchElementException success) { |
437 |
< |
} |
423 |
> |
} catch (NoSuchElementException success) {} |
424 |
|
checkEmpty(q); |
425 |
|
} |
426 |
|
|
429 |
|
*/ |
430 |
|
public void testRemoveElement() throws InterruptedException { |
431 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
432 |
< |
for (int i = 1; i < SIZE; i += 2) { |
432 |
> |
for (int i = 1; i < SIZE; i+=2) { |
433 |
> |
assertTrue(q.contains(i)); |
434 |
|
assertTrue(q.remove(i)); |
435 |
+ |
assertFalse(q.contains(i)); |
436 |
+ |
assertTrue(q.contains(i-1)); |
437 |
|
} |
438 |
< |
for (int i = 0; i < SIZE; i += 2) { |
438 |
> |
for (int i = 0; i < SIZE; i+=2) { |
439 |
> |
assertTrue(q.contains(i)); |
440 |
|
assertTrue(q.remove(i)); |
441 |
< |
assertFalse(q.remove(i + 1)); |
441 |
> |
assertFalse(q.contains(i)); |
442 |
> |
assertFalse(q.remove(i+1)); |
443 |
> |
assertFalse(q.contains(i+1)); |
444 |
|
} |
445 |
|
checkEmpty(q); |
446 |
|
} |
455 |
|
assertTrue(q.remove(one)); |
456 |
|
assertTrue(q.remove(two)); |
457 |
|
assertTrue(q.add(three)); |
458 |
< |
assertTrue(q.take() == three); |
458 |
> |
assertSame(q.take(), three); |
459 |
|
} |
460 |
|
|
461 |
|
/** |
537 |
|
} |
538 |
|
|
539 |
|
/** |
540 |
< |
* toArray contains all elements |
540 |
> |
* toArray() contains all elements in FIFO order |
541 |
|
*/ |
542 |
< |
public void testToArray() throws InterruptedException { |
542 |
> |
public void testToArray() { |
543 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
544 |
|
Object[] o = q.toArray(); |
545 |
|
for (int i = 0; i < o.length; i++) { |
546 |
< |
assertEquals(o[i], q.take()); |
546 |
> |
assertSame(o[i], q.poll()); |
547 |
|
} |
548 |
|
} |
549 |
|
|
550 |
|
/** |
551 |
< |
* toArray(a) contains all elements |
551 |
> |
* toArray(a) contains all elements in FIFO order |
552 |
|
*/ |
553 |
< |
public void testToArray2() throws InterruptedException { |
553 |
> |
public void testToArray2() { |
554 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
555 |
|
Integer[] ints = new Integer[SIZE]; |
556 |
< |
ints = q.toArray(ints); |
556 |
> |
Integer[] array = q.toArray(ints); |
557 |
> |
assertSame(ints, array); |
558 |
|
for (int i = 0; i < ints.length; i++) { |
559 |
< |
assertEquals(ints[i], q.take()); |
559 |
> |
assertSame(ints[i], q.poll()); |
560 |
|
} |
561 |
|
} |
562 |
|
|
563 |
|
/** |
564 |
|
* toArray(null) throws NullPointerException |
565 |
|
*/ |
566 |
< |
public void testToArray_BadArg() { |
566 |
> |
public void testToArray_NullArg() { |
567 |
> |
LinkedTransferQueue q = populatedQueue(SIZE); |
568 |
|
try { |
569 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
576 |
< |
Object o[] = q.toArray(null); |
569 |
> |
q.toArray(null); |
570 |
|
shouldThrow(); |
571 |
< |
} catch (NullPointerException success) { |
579 |
< |
} |
571 |
> |
} catch (NullPointerException success) {} |
572 |
|
} |
573 |
|
|
574 |
|
/** |
575 |
< |
* toArray with incompatible array type throws CCE |
575 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
576 |
|
*/ |
577 |
|
public void testToArray1_BadArg() { |
578 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
579 |
|
try { |
580 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
588 |
< |
Object o[] = q.toArray(new String[10]); |
580 |
> |
q.toArray(new String[10]); |
581 |
|
shouldThrow(); |
582 |
< |
} catch (ArrayStoreException success) { |
591 |
< |
} |
582 |
> |
} catch (ArrayStoreException success) {} |
583 |
|
} |
584 |
|
|
585 |
|
/** |
609 |
|
it.remove(); |
610 |
|
|
611 |
|
it = q.iterator(); |
612 |
< |
assertEquals(it.next(), one); |
613 |
< |
assertEquals(it.next(), three); |
612 |
> |
assertSame(it.next(), one); |
613 |
> |
assertSame(it.next(), three); |
614 |
|
assertFalse(it.hasNext()); |
615 |
|
} |
616 |
|
|
654 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
655 |
|
String s = q.toString(); |
656 |
|
for (int i = 0; i < SIZE; ++i) { |
657 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
657 |
> |
assertTrue(s.contains(String.valueOf(i))); |
658 |
|
} |
659 |
|
} |
660 |
|
|
663 |
|
*/ |
664 |
|
public void testOfferInExecutor() { |
665 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
666 |
< |
q.add(one); |
676 |
< |
q.add(two); |
666 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
667 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
668 |
|
|
669 |
|
executor.execute(new CheckedRunnable() { |
670 |
< |
void realRun() { |
671 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, |
672 |
< |
MILLISECONDS)); |
670 |
> |
public void realRun() throws InterruptedException { |
671 |
> |
threadsStarted.await(); |
672 |
> |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
673 |
|
}}); |
674 |
|
|
675 |
|
executor.execute(new CheckedRunnable() { |
676 |
< |
void realRun() throws InterruptedException { |
677 |
< |
Thread.sleep(SMALL_DELAY_MS); |
678 |
< |
threadAssertEquals(one, q.take()); |
676 |
> |
public void realRun() throws InterruptedException { |
677 |
> |
threadsStarted.await(); |
678 |
> |
assertSame(one, q.take()); |
679 |
> |
checkEmpty(q); |
680 |
|
}}); |
681 |
|
|
682 |
|
joinPool(executor); |
683 |
|
} |
684 |
|
|
685 |
|
/** |
686 |
< |
* poll retrieves elements across Executor threads |
686 |
> |
* timed poll retrieves elements across Executor threads |
687 |
|
*/ |
688 |
|
public void testPollInExecutor() { |
689 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
690 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
691 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
692 |
|
|
693 |
|
executor.execute(new CheckedRunnable() { |
694 |
< |
void realRun() throws InterruptedException { |
695 |
< |
threadAssertNull(q.poll()); |
696 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, |
697 |
< |
MILLISECONDS)); |
698 |
< |
threadAssertTrue(q.isEmpty()); |
694 |
> |
public void realRun() throws InterruptedException { |
695 |
> |
assertNull(q.poll()); |
696 |
> |
threadsStarted.await(); |
697 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
698 |
> |
checkEmpty(q); |
699 |
|
}}); |
700 |
|
|
701 |
|
executor.execute(new CheckedRunnable() { |
702 |
< |
void realRun() throws InterruptedException { |
703 |
< |
Thread.sleep(SMALL_DELAY_MS); |
702 |
> |
public void realRun() throws InterruptedException { |
703 |
> |
threadsStarted.await(); |
704 |
|
q.put(one); |
705 |
|
}}); |
706 |
|
|
726 |
|
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
727 |
|
|
728 |
|
assertEquals(q.size(), r.size()); |
729 |
+ |
assertEquals(q.toString(), r.toString()); |
730 |
+ |
assertTrue(Arrays.equals(q.toArray(), r.toArray())); |
731 |
|
while (!q.isEmpty()) { |
732 |
|
assertEquals(q.remove(), r.remove()); |
733 |
|
} |
741 |
|
try { |
742 |
|
q.drainTo(null); |
743 |
|
shouldThrow(); |
744 |
< |
} catch (NullPointerException success) { |
751 |
< |
} |
744 |
> |
} catch (NullPointerException success) {} |
745 |
|
} |
746 |
|
|
747 |
|
/** |
752 |
|
try { |
753 |
|
q.drainTo(q); |
754 |
|
shouldThrow(); |
755 |
< |
} catch (IllegalArgumentException success) { |
763 |
< |
} |
755 |
> |
} catch (IllegalArgumentException success) {} |
756 |
|
} |
757 |
|
|
758 |
|
/** |
782 |
|
} |
783 |
|
|
784 |
|
/** |
785 |
< |
* drainTo empties full queue, unblocking a waiting put. |
785 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
786 |
|
*/ |
787 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
788 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
789 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
790 |
< |
void realRun() { |
790 |
> |
public void realRun() { |
791 |
|
q.put(SIZE + 1); |
792 |
|
}}); |
793 |
|
ArrayList l = new ArrayList(); |
796 |
|
for (int i = 0; i < SIZE; ++i) { |
797 |
|
assertEquals(l.get(i), i); |
798 |
|
} |
799 |
< |
t.join(); |
799 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
800 |
|
assertTrue(q.size() + l.size() >= SIZE); |
801 |
|
} |
802 |
|
|
808 |
|
try { |
809 |
|
q.drainTo(null, SIZE); |
810 |
|
shouldThrow(); |
811 |
< |
} catch (NullPointerException success) { |
820 |
< |
} |
811 |
> |
} catch (NullPointerException success) {} |
812 |
|
} |
813 |
|
|
814 |
|
/** |
819 |
|
try { |
820 |
|
q.drainTo(q, SIZE); |
821 |
|
shouldThrow(); |
822 |
< |
} catch (IllegalArgumentException success) { |
832 |
< |
} |
822 |
> |
} catch (IllegalArgumentException success) {} |
823 |
|
} |
824 |
|
|
825 |
|
/** |
826 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
826 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
827 |
|
*/ |
828 |
|
public void testDrainToN() { |
829 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
845 |
|
} |
846 |
|
|
847 |
|
/** |
848 |
< |
* poll and take decrement the waiting consumer count |
848 |
> |
* timed poll() or take() increments the waiting consumer count; |
849 |
> |
* offer(e) decrements the waiting consumer count |
850 |
|
*/ |
851 |
|
public void testWaitingConsumer() throws InterruptedException { |
852 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
853 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
853 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
854 |
> |
assertFalse(q.hasWaitingConsumer()); |
855 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
856 |
|
|
857 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
858 |
< |
void realRun() throws InterruptedException { |
859 |
< |
Thread.sleep(SMALL_DELAY_MS); |
860 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
861 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
862 |
< |
threadAssertTrue(q.offer(new Object())); |
858 |
> |
public void realRun() throws InterruptedException { |
859 |
> |
threadStarted.countDown(); |
860 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
861 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
862 |
> |
assertFalse(q.hasWaitingConsumer()); |
863 |
|
}}); |
864 |
|
|
865 |
< |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
866 |
< |
assertTrue(q.getWaitingConsumerCount() |
867 |
< |
< waiting.getWaitingConsumers()); |
868 |
< |
t.join(); |
865 |
> |
threadStarted.await(); |
866 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
867 |
> |
assertEquals(q.getWaitingConsumerCount(), 1); |
868 |
> |
assertTrue(q.hasWaitingConsumer()); |
869 |
> |
|
870 |
> |
assertTrue(q.offer(one)); |
871 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
872 |
> |
assertFalse(q.hasWaitingConsumer()); |
873 |
> |
|
874 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
875 |
|
} |
876 |
|
|
877 |
|
/** |
882 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
883 |
|
q.transfer(null); |
884 |
|
shouldThrow(); |
885 |
< |
} catch (NullPointerException ex) { |
887 |
< |
} |
885 |
> |
} catch (NullPointerException success) {} |
886 |
|
} |
887 |
|
|
888 |
|
/** |
892 |
|
public void testTransfer2() throws InterruptedException { |
893 |
|
final LinkedTransferQueue<Integer> q |
894 |
|
= new LinkedTransferQueue<Integer>(); |
895 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
896 |
|
|
897 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
898 |
< |
void realRun() throws InterruptedException { |
899 |
< |
q.transfer(SIZE); |
900 |
< |
threadAssertTrue(q.isEmpty()); |
898 |
> |
public void realRun() throws InterruptedException { |
899 |
> |
threadStarted.countDown(); |
900 |
> |
q.transfer(five); |
901 |
> |
checkEmpty(q); |
902 |
|
}}); |
903 |
|
|
904 |
< |
Thread.sleep(SHORT_DELAY_MS); |
904 |
> |
threadStarted.await(); |
905 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
906 |
|
assertEquals(1, q.size()); |
907 |
< |
assertEquals(SIZE, (int) q.poll()); |
908 |
< |
assertTrue(q.isEmpty()); |
909 |
< |
t.join(); |
907 |
> |
assertSame(five, q.poll()); |
908 |
> |
checkEmpty(q); |
909 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
910 |
|
} |
911 |
|
|
912 |
|
/** |
917 |
|
= new LinkedTransferQueue<Integer>(); |
918 |
|
|
919 |
|
Thread first = newStartedThread(new CheckedRunnable() { |
920 |
< |
void realRun() throws InterruptedException { |
921 |
< |
Integer i = SIZE + 1; |
922 |
< |
q.transfer(i); |
923 |
< |
threadAssertTrue(!q.contains(i)); |
923 |
< |
threadAssertEquals(1, q.size()); |
920 |
> |
public void realRun() throws InterruptedException { |
921 |
> |
q.transfer(four); |
922 |
> |
assertTrue(!q.contains(four)); |
923 |
> |
assertEquals(1, q.size()); |
924 |
|
}}); |
925 |
|
|
926 |
|
Thread interruptedThread = newStartedThread( |
927 |
|
new CheckedInterruptedRunnable() { |
928 |
< |
void realRun() throws InterruptedException { |
929 |
< |
while (q.size() == 0) |
928 |
> |
public void realRun() throws InterruptedException { |
929 |
> |
while (q.isEmpty()) |
930 |
|
Thread.yield(); |
931 |
< |
q.transfer(SIZE); |
931 |
> |
q.transfer(five); |
932 |
|
}}); |
933 |
|
|
934 |
|
while (q.size() < 2) |
935 |
|
Thread.yield(); |
936 |
|
assertEquals(2, q.size()); |
937 |
< |
assertEquals(SIZE + 1, (int) q.poll()); |
937 |
> |
assertSame(four, q.poll()); |
938 |
|
first.join(); |
939 |
|
assertEquals(1, q.size()); |
940 |
|
interruptedThread.interrupt(); |
941 |
|
interruptedThread.join(); |
942 |
< |
assertEquals(0, q.size()); |
943 |
< |
assertTrue(q.isEmpty()); |
942 |
> |
checkEmpty(q); |
943 |
|
} |
944 |
|
|
945 |
|
/** |
950 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
951 |
|
|
952 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
953 |
< |
void realRun() throws InterruptedException { |
953 |
> |
public void realRun() throws InterruptedException { |
954 |
|
q.transfer(four); |
955 |
< |
threadAssertFalse(q.contains(four)); |
956 |
< |
threadAssertEquals(three, q.poll()); |
955 |
> |
assertFalse(q.contains(four)); |
956 |
> |
assertSame(three, q.poll()); |
957 |
|
}}); |
958 |
|
|
959 |
< |
Thread.sleep(SHORT_DELAY_MS); |
959 |
> |
while (q.isEmpty()) |
960 |
> |
Thread.yield(); |
961 |
> |
assertFalse(q.isEmpty()); |
962 |
> |
assertEquals(1, q.size()); |
963 |
|
assertTrue(q.offer(three)); |
964 |
< |
assertEquals(four, q.poll()); |
965 |
< |
t.join(); |
964 |
> |
assertSame(four, q.poll()); |
965 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
966 |
|
} |
967 |
|
|
968 |
|
/** |
974 |
|
= new LinkedTransferQueue<Integer>(); |
975 |
|
|
976 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
977 |
< |
void realRun() throws InterruptedException { |
978 |
< |
q.transfer(SIZE); |
977 |
> |
public void realRun() throws InterruptedException { |
978 |
> |
q.transfer(four); |
979 |
|
checkEmpty(q); |
980 |
|
}}); |
981 |
|
|
982 |
< |
Thread.sleep(SHORT_DELAY_MS); |
983 |
< |
assertEquals(SIZE, (int) q.take()); |
982 |
> |
while (q.isEmpty()) |
983 |
> |
Thread.yield(); |
984 |
> |
assertFalse(q.isEmpty()); |
985 |
> |
assertEquals(1, q.size()); |
986 |
> |
assertSame(four, q.take()); |
987 |
|
checkEmpty(q); |
988 |
< |
t.join(); |
988 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
989 |
|
} |
990 |
|
|
991 |
|
/** |
996 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
997 |
|
q.tryTransfer(null); |
998 |
|
shouldThrow(); |
999 |
< |
} catch (NullPointerException ex) { |
995 |
< |
} |
999 |
> |
} catch (NullPointerException success) {} |
1000 |
|
} |
1001 |
|
|
1002 |
|
/** |
1019 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1020 |
|
|
1021 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1022 |
< |
void realRun() { |
1022 |
> |
public void realRun() { |
1023 |
|
while (! q.hasWaitingConsumer()) |
1024 |
|
Thread.yield(); |
1025 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1026 |
< |
threadAssertTrue(q.isEmpty()); |
1027 |
< |
threadAssertTrue(q.size() == 0); |
1024 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1025 |
> |
assertTrue(q.hasWaitingConsumer()); |
1026 |
> |
checkEmpty(q); |
1027 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
1028 |
|
}}); |
1029 |
|
|
1030 |
< |
assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); |
1030 |
> |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1031 |
|
checkEmpty(q); |
1032 |
< |
t.join(); |
1032 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1033 |
|
} |
1034 |
|
|
1035 |
|
/** |
1041 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1042 |
|
|
1043 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1044 |
< |
void realRun() { |
1044 |
> |
public void realRun() { |
1045 |
|
while (! q.hasWaitingConsumer()) |
1046 |
|
Thread.yield(); |
1047 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1048 |
< |
threadAssertTrue(q.isEmpty()); |
1049 |
< |
threadAssertTrue(q.size() == 0); |
1047 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1047 |
> |
assertTrue(q.hasWaitingConsumer()); |
1048 |
> |
checkEmpty(q); |
1049 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
1050 |
|
}}); |
1051 |
|
|
1052 |
< |
assertTrue(q.take() == hotPotato); |
1052 |
> |
assertSame(q.take(), hotPotato); |
1053 |
|
checkEmpty(q); |
1054 |
< |
t.join(); |
1054 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1055 |
|
} |
1056 |
|
|
1057 |
|
/** |
1058 |
< |
* tryTransfer waits the amount given if interrupted, and |
1057 |
< |
* throws interrupted exception |
1058 |
> |
* tryTransfer blocks interruptibly if no takers |
1059 |
|
*/ |
1060 |
|
public void testTryTransfer5() throws InterruptedException { |
1061 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1062 |
+ |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1063 |
+ |
assertTrue(q.isEmpty()); |
1064 |
|
|
1065 |
< |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1066 |
< |
void realRun() throws InterruptedException { |
1067 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1065 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1066 |
> |
public void realRun() throws InterruptedException { |
1067 |
> |
Thread.currentThread().interrupt(); |
1068 |
> |
try { |
1069 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1070 |
> |
shouldThrow(); |
1071 |
> |
} catch (InterruptedException success) {} |
1072 |
> |
assertFalse(Thread.interrupted()); |
1073 |
> |
|
1074 |
> |
pleaseInterrupt.countDown(); |
1075 |
> |
try { |
1076 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1077 |
> |
shouldThrow(); |
1078 |
> |
} catch (InterruptedException success) {} |
1079 |
> |
assertFalse(Thread.interrupted()); |
1080 |
|
}}); |
1081 |
|
|
1082 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1083 |
< |
toInterrupt.interrupt(); |
1084 |
< |
toInterrupt.join(); |
1082 |
> |
await(pleaseInterrupt); |
1083 |
> |
assertThreadStaysAlive(t); |
1084 |
> |
t.interrupt(); |
1085 |
> |
awaitTermination(t); |
1086 |
> |
checkEmpty(q); |
1087 |
|
} |
1088 |
|
|
1089 |
|
/** |
1090 |
< |
* tryTransfer gives up after the timeout and return false |
1090 |
> |
* tryTransfer gives up after the timeout and returns false |
1091 |
|
*/ |
1092 |
|
public void testTryTransfer6() throws InterruptedException { |
1093 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1094 |
|
|
1095 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1096 |
< |
void realRun() throws InterruptedException { |
1097 |
< |
threadAssertFalse |
1098 |
< |
(q.tryTransfer(new Object(), |
1099 |
< |
SHORT_DELAY_MS, MILLISECONDS)); |
1096 |
> |
public void realRun() throws InterruptedException { |
1097 |
> |
long t0 = System.nanoTime(); |
1098 |
> |
assertFalse(q.tryTransfer(new Object(), |
1099 |
> |
timeoutMillis(), MILLISECONDS)); |
1100 |
> |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
1101 |
> |
checkEmpty(q); |
1102 |
|
}}); |
1103 |
|
|
1104 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1104 |
> |
awaitTermination(t); |
1105 |
|
checkEmpty(q); |
1087 |
– |
t.join(); |
1106 |
|
} |
1107 |
|
|
1108 |
|
/** |
1114 |
|
assertTrue(q.offer(four)); |
1115 |
|
|
1116 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1117 |
< |
void realRun() throws InterruptedException { |
1118 |
< |
threadAssertTrue(q.tryTransfer(five, |
1119 |
< |
MEDIUM_DELAY_MS, MILLISECONDS)); |
1102 |
< |
threadAssertTrue(q.isEmpty()); |
1117 |
> |
public void realRun() throws InterruptedException { |
1118 |
> |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1119 |
> |
checkEmpty(q); |
1120 |
|
}}); |
1121 |
|
|
1122 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1122 |
> |
while (q.size() != 2) |
1123 |
> |
Thread.yield(); |
1124 |
|
assertEquals(2, q.size()); |
1125 |
< |
assertEquals(four, q.poll()); |
1126 |
< |
assertEquals(five, q.poll()); |
1125 |
> |
assertSame(four, q.poll()); |
1126 |
> |
assertSame(five, q.poll()); |
1127 |
|
checkEmpty(q); |
1128 |
< |
t.join(); |
1128 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1129 |
|
} |
1130 |
|
|
1131 |
|
/** |
1132 |
< |
* tryTransfer attempts to enqueue into the q and fails returning |
1133 |
< |
* false not enqueueing and the successive poll is null |
1132 |
> |
* tryTransfer attempts to enqueue into the queue and fails |
1133 |
> |
* returning false not enqueueing and the successive poll is null |
1134 |
|
*/ |
1135 |
|
public void testTryTransfer8() throws InterruptedException { |
1136 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1137 |
|
assertTrue(q.offer(four)); |
1138 |
|
assertEquals(1, q.size()); |
1139 |
< |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1139 |
> |
long t0 = System.nanoTime(); |
1140 |
> |
assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); |
1141 |
> |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
1142 |
|
assertEquals(1, q.size()); |
1143 |
< |
assertEquals(four, q.poll()); |
1124 |
< |
checkEmpty(q); |
1143 |
> |
assertSame(four, q.poll()); |
1144 |
|
assertNull(q.poll()); |
1145 |
+ |
checkEmpty(q); |
1146 |
|
} |
1147 |
|
|
1148 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1149 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1150 |
< |
assertTrue(q.isEmpty()); |
1150 |
> |
checkEmpty(q); |
1151 |
|
for (int i = 0; i < n; i++) { |
1152 |
|
assertEquals(i, q.size()); |
1153 |
|
assertTrue(q.offer(i)); |
1156 |
|
assertFalse(q.isEmpty()); |
1157 |
|
return q; |
1158 |
|
} |
1139 |
– |
|
1140 |
– |
private static class ConsumerObserver { |
1141 |
– |
|
1142 |
– |
private int waitingConsumers; |
1143 |
– |
|
1144 |
– |
private ConsumerObserver() { |
1145 |
– |
} |
1146 |
– |
|
1147 |
– |
private void setWaitingConsumer(int i) { |
1148 |
– |
this.waitingConsumers = i; |
1149 |
– |
} |
1150 |
– |
|
1151 |
– |
private int getWaitingConsumers() { |
1152 |
– |
return waitingConsumers; |
1153 |
– |
} |
1154 |
– |
} |
1159 |
|
} |