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); |
39 |
> |
return newTestSuite(LinkedTransferQueueTest.class, |
40 |
> |
new Generic().testSuite()); |
41 |
> |
} |
42 |
> |
|
43 |
> |
void checkEmpty(BlockingQueue q) { |
44 |
> |
try { |
45 |
> |
assertTrue(q.isEmpty()); |
46 |
> |
assertEquals(0, q.size()); |
47 |
> |
assertNull(q.peek()); |
48 |
> |
assertNull(q.poll()); |
49 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
50 |
> |
assertEquals(q.toString(), "[]"); |
51 |
> |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
52 |
> |
assertFalse(q.iterator().hasNext()); |
53 |
> |
try { |
54 |
> |
q.element(); |
55 |
> |
shouldThrow(); |
56 |
> |
} catch (NoSuchElementException success) {} |
57 |
> |
try { |
58 |
> |
q.iterator().next(); |
59 |
> |
shouldThrow(); |
60 |
> |
} catch (NoSuchElementException success) {} |
61 |
> |
try { |
62 |
> |
q.remove(); |
63 |
> |
shouldThrow(); |
64 |
> |
} catch (NoSuchElementException success) {} |
65 |
> |
} catch (InterruptedException ie) { |
66 |
> |
threadUnexpectedException(ie); |
67 |
> |
} |
68 |
|
} |
69 |
|
|
70 |
|
/** |
84 |
|
try { |
85 |
|
new LinkedTransferQueue(null); |
86 |
|
shouldThrow(); |
87 |
< |
} catch (NullPointerException success) { |
52 |
< |
} |
87 |
> |
} catch (NullPointerException success) {} |
88 |
|
} |
89 |
|
|
90 |
|
/** |
96 |
|
Integer[] ints = new Integer[SIZE]; |
97 |
|
new LinkedTransferQueue(Arrays.asList(ints)); |
98 |
|
shouldThrow(); |
99 |
< |
} catch (NullPointerException success) { |
65 |
< |
} |
99 |
> |
} catch (NullPointerException success) {} |
100 |
|
} |
101 |
|
|
102 |
|
/** |
111 |
|
} |
112 |
|
new LinkedTransferQueue(Arrays.asList(ints)); |
113 |
|
shouldThrow(); |
114 |
< |
} catch (NullPointerException success) { |
81 |
< |
} |
114 |
> |
} catch (NullPointerException success) {} |
115 |
|
} |
116 |
|
|
117 |
|
/** |
118 |
|
* Queue contains all elements of the collection it is initialized by |
119 |
|
*/ |
120 |
|
public void testConstructor5() { |
121 |
< |
try { |
122 |
< |
Integer[] ints = new Integer[SIZE]; |
123 |
< |
for (int i = 0; i < SIZE; ++i) { |
124 |
< |
ints[i] = i; |
125 |
< |
} |
126 |
< |
LinkedTransferQueue q |
127 |
< |
= new LinkedTransferQueue(Arrays.asList(ints)); |
128 |
< |
for (int i = 0; i < SIZE; ++i) { |
129 |
< |
assertEquals(ints[i], q.poll()); |
130 |
< |
} |
131 |
< |
} finally { |
121 |
> |
Integer[] ints = new Integer[SIZE]; |
122 |
> |
for (int i = 0; i < SIZE; ++i) { |
123 |
> |
ints[i] = i; |
124 |
> |
} |
125 |
> |
List intList = Arrays.asList(ints); |
126 |
> |
LinkedTransferQueue q |
127 |
> |
= new LinkedTransferQueue(intList); |
128 |
> |
assertEquals(q.size(), intList.size()); |
129 |
> |
assertEquals(q.toString(), intList.toString()); |
130 |
> |
assertTrue(Arrays.equals(q.toArray(), |
131 |
> |
intList.toArray())); |
132 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[0]), |
133 |
> |
intList.toArray(new Object[0]))); |
134 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[SIZE]), |
135 |
> |
intList.toArray(new Object[SIZE]))); |
136 |
> |
for (int i = 0; i < SIZE; ++i) { |
137 |
> |
assertEquals(ints[i], q.poll()); |
138 |
|
} |
139 |
|
} |
140 |
|
|
141 |
|
/** |
142 |
< |
* Remaining capacity never decrease nor increase on add or remove |
142 |
> |
* remainingCapacity() always returns Integer.MAX_VALUE |
143 |
|
*/ |
144 |
|
public void testRemainingCapacity() { |
145 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
163 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
164 |
|
q.offer(null); |
165 |
|
shouldThrow(); |
166 |
< |
} catch (NullPointerException success) { |
128 |
< |
} |
166 |
> |
} catch (NullPointerException success) {} |
167 |
|
} |
168 |
|
|
169 |
|
/** |
174 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
175 |
|
q.add(null); |
176 |
|
shouldThrow(); |
177 |
< |
} catch (NullPointerException success) { |
140 |
< |
} |
177 |
> |
} catch (NullPointerException success) {} |
178 |
|
} |
179 |
|
|
180 |
|
/** |
185 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
186 |
|
q.addAll(null); |
187 |
|
shouldThrow(); |
188 |
< |
} catch (NullPointerException success) { |
152 |
< |
} |
188 |
> |
} catch (NullPointerException success) {} |
189 |
|
} |
190 |
|
|
191 |
|
/** |
196 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
197 |
|
q.addAll(q); |
198 |
|
shouldThrow(); |
199 |
< |
} catch (IllegalArgumentException success) { |
164 |
< |
} |
199 |
> |
} catch (IllegalArgumentException success) {} |
200 |
|
} |
201 |
|
|
202 |
|
/** |
208 |
|
Integer[] ints = new Integer[SIZE]; |
209 |
|
q.addAll(Arrays.asList(ints)); |
210 |
|
shouldThrow(); |
211 |
< |
} catch (NullPointerException success) { |
177 |
< |
} |
211 |
> |
} catch (NullPointerException success) {} |
212 |
|
} |
213 |
|
|
214 |
|
/** |
224 |
|
} |
225 |
|
q.addAll(Arrays.asList(ints)); |
226 |
|
shouldThrow(); |
227 |
< |
} catch (NullPointerException success) { |
194 |
< |
} |
227 |
> |
} catch (NullPointerException success) {} |
228 |
|
} |
229 |
|
|
230 |
|
/** |
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 |
|
} |
234 |
– |
assertEquals(q.size(), SIZE); |
268 |
|
} |
269 |
|
|
270 |
|
/** |
278 |
|
} |
279 |
|
|
280 |
|
/** |
281 |
< |
* take blocks interruptibly when empty |
249 |
< |
*/ |
250 |
< |
public void testTakeFromEmpty() throws InterruptedException { |
251 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
252 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
253 |
< |
void realRun() throws InterruptedException { |
254 |
< |
q.take(); |
255 |
< |
}}); |
256 |
< |
t.start(); |
257 |
< |
Thread.sleep(SHORT_DELAY_MS); |
258 |
< |
t.interrupt(); |
259 |
< |
t.join(); |
260 |
< |
} |
261 |
< |
|
262 |
< |
/** |
263 |
< |
* Take removes existing elements until empty, then blocks interruptibly |
281 |
> |
* take removes existing elements until empty, then blocks interruptibly |
282 |
|
*/ |
283 |
|
public void testBlockingTake() throws InterruptedException { |
284 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
285 |
< |
void realRun() throws InterruptedException { |
286 |
< |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
284 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
285 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
286 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
287 |
> |
public void realRun() throws InterruptedException { |
288 |
|
for (int i = 0; i < SIZE; ++i) { |
289 |
< |
threadAssertEquals(i, (int) q.take()); |
289 |
> |
assertEquals(i, (int) q.take()); |
290 |
|
} |
291 |
< |
q.take(); |
291 |
> |
aboutToWait.countDown(); |
292 |
> |
try { |
293 |
> |
q.take(); |
294 |
> |
shouldThrow(); |
295 |
> |
} catch (InterruptedException success) {} |
296 |
|
}}); |
297 |
< |
t.start(); |
298 |
< |
Thread.sleep(SHORT_DELAY_MS); |
297 |
> |
|
298 |
> |
aboutToWait.await(); |
299 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
300 |
|
t.interrupt(); |
301 |
< |
t.join(); |
301 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
302 |
> |
checkEmpty(q); |
303 |
|
} |
304 |
|
|
305 |
|
/** |
306 |
|
* poll succeeds unless empty |
307 |
|
*/ |
308 |
< |
public void testPoll() { |
308 |
> |
public void testPoll() throws InterruptedException { |
309 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
310 |
|
for (int i = 0; i < SIZE; ++i) { |
311 |
|
assertEquals(i, (int) q.poll()); |
312 |
|
} |
313 |
|
assertNull(q.poll()); |
314 |
+ |
checkEmpty(q); |
315 |
|
} |
316 |
|
|
317 |
|
/** |
318 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
318 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
319 |
|
*/ |
320 |
|
public void testTimedPoll0() throws InterruptedException { |
321 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
323 |
|
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
324 |
|
} |
325 |
|
assertNull(q.poll(0, MILLISECONDS)); |
326 |
+ |
checkEmpty(q); |
327 |
|
} |
328 |
|
|
329 |
|
/** |
330 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
330 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
331 |
|
*/ |
332 |
|
public void testTimedPoll() throws InterruptedException { |
333 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
334 |
|
for (int i = 0; i < SIZE; ++i) { |
335 |
< |
assertEquals(i, (int) q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
335 |
> |
long t0 = System.nanoTime(); |
336 |
> |
assertEquals(i, (int) q.poll(SMALL_DELAY_MS, MILLISECONDS)); |
337 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
338 |
|
} |
339 |
+ |
long t0 = System.nanoTime(); |
340 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
341 |
+ |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
342 |
+ |
checkEmpty(q); |
343 |
|
} |
344 |
|
|
345 |
|
/** |
347 |
|
* returning timeout status |
348 |
|
*/ |
349 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
350 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
351 |
< |
void realRun() throws InterruptedException { |
352 |
< |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
350 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
351 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
352 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
353 |
> |
public void realRun() throws InterruptedException { |
354 |
|
for (int i = 0; i < SIZE; ++i) { |
355 |
< |
threadAssertEquals(i, (int) q.poll(SHORT_DELAY_MS, |
356 |
< |
MILLISECONDS)); |
355 |
> |
long t0 = System.nanoTime(); |
356 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
357 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
358 |
> |
} |
359 |
> |
long t0 = System.nanoTime(); |
360 |
> |
aboutToWait.countDown(); |
361 |
> |
try { |
362 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
363 |
> |
shouldThrow(); |
364 |
> |
} catch (InterruptedException success) { |
365 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
366 |
|
} |
325 |
– |
q.poll(SHORT_DELAY_MS, MILLISECONDS); |
367 |
|
}}); |
368 |
< |
t.start(); |
369 |
< |
Thread.sleep(SHORT_DELAY_MS); |
368 |
> |
|
369 |
> |
aboutToWait.await(); |
370 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
371 |
|
t.interrupt(); |
372 |
< |
t.join(); |
372 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
373 |
> |
checkEmpty(q); |
374 |
|
} |
375 |
|
|
376 |
|
/** |
377 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
378 |
< |
* on interruption throws |
377 |
> |
* timed poll after thread interrupted throws InterruptedException |
378 |
> |
* instead of returning timeout status |
379 |
|
*/ |
380 |
< |
public void testTimedPollWithOffer() throws InterruptedException { |
381 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
382 |
< |
Thread t = new Thread(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); |
380 |
> |
public void testTimedPollAfterInterrupt() throws InterruptedException { |
381 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
382 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
383 |
> |
public void realRun() throws InterruptedException { |
384 |
> |
Thread.currentThread().interrupt(); |
385 |
> |
for (int i = 0; i < SIZE; ++i) { |
386 |
> |
long t0 = System.nanoTime(); |
387 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
388 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
389 |
> |
} |
390 |
> |
try { |
391 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
392 |
> |
shouldThrow(); |
393 |
> |
} catch (InterruptedException success) {} |
394 |
|
}}); |
395 |
< |
t.start(); |
396 |
< |
Thread.sleep(SMALL_DELAY_MS); |
397 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
348 |
< |
t.interrupt(); |
349 |
< |
t.join(); |
395 |
> |
|
396 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
397 |
> |
checkEmpty(q); |
398 |
|
} |
399 |
|
|
400 |
|
/** |
401 |
|
* peek returns next element, or null if empty |
402 |
|
*/ |
403 |
< |
public void testPeek() { |
403 |
> |
public void testPeek() throws InterruptedException { |
404 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
405 |
|
for (int i = 0; i < SIZE; ++i) { |
406 |
|
assertEquals(i, (int) q.peek()); |
409 |
|
i != (int) q.peek()); |
410 |
|
} |
411 |
|
assertNull(q.peek()); |
412 |
+ |
checkEmpty(q); |
413 |
|
} |
414 |
|
|
415 |
|
/** |
416 |
|
* element returns next element, or throws NoSuchElementException if empty |
417 |
|
*/ |
418 |
< |
public void testElement() { |
418 |
> |
public void testElement() throws InterruptedException { |
419 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
420 |
|
for (int i = 0; i < SIZE; ++i) { |
421 |
|
assertEquals(i, (int) q.element()); |
424 |
|
try { |
425 |
|
q.element(); |
426 |
|
shouldThrow(); |
427 |
< |
} catch (NoSuchElementException success) { |
428 |
< |
} |
427 |
> |
} catch (NoSuchElementException success) {} |
428 |
> |
checkEmpty(q); |
429 |
|
} |
430 |
|
|
431 |
|
/** |
432 |
|
* remove removes next element, or throws NoSuchElementException if empty |
433 |
|
*/ |
434 |
< |
public void testRemove() { |
434 |
> |
public void testRemove() throws InterruptedException { |
435 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
436 |
|
for (int i = 0; i < SIZE; ++i) { |
437 |
|
assertEquals(i, (int) q.remove()); |
439 |
|
try { |
440 |
|
q.remove(); |
441 |
|
shouldThrow(); |
442 |
< |
} catch (NoSuchElementException success) { |
443 |
< |
} |
442 |
> |
} catch (NoSuchElementException success) {} |
443 |
> |
checkEmpty(q); |
444 |
|
} |
445 |
|
|
446 |
|
/** |
447 |
|
* remove(x) removes x and returns true if present |
448 |
|
*/ |
449 |
< |
public void testRemoveElement() { |
449 |
> |
public void testRemoveElement() throws InterruptedException { |
450 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
451 |
|
for (int i = 1; i < SIZE; i += 2) { |
452 |
|
assertTrue(q.remove(i)); |
455 |
|
assertTrue(q.remove(i)); |
456 |
|
assertFalse(q.remove(i + 1)); |
457 |
|
} |
458 |
< |
assertTrue(q.isEmpty()); |
458 |
> |
checkEmpty(q); |
459 |
|
} |
460 |
|
|
461 |
|
/** |
468 |
|
assertTrue(q.remove(one)); |
469 |
|
assertTrue(q.remove(two)); |
470 |
|
assertTrue(q.add(three)); |
471 |
< |
assertTrue(q.take() != null); |
471 |
> |
assertSame(q.take(), three); |
472 |
|
} |
473 |
|
|
474 |
|
/** |
486 |
|
/** |
487 |
|
* clear removes all elements |
488 |
|
*/ |
489 |
< |
public void testClear() { |
489 |
> |
public void testClear() throws InterruptedException { |
490 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
491 |
|
q.clear(); |
492 |
< |
assertTrue(q.isEmpty()); |
444 |
< |
assertEquals(0, q.size()); |
492 |
> |
checkEmpty(q); |
493 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
494 |
|
q.add(one); |
495 |
|
assertFalse(q.isEmpty()); |
496 |
+ |
assertEquals(1, q.size()); |
497 |
|
assertTrue(q.contains(one)); |
498 |
|
q.clear(); |
499 |
< |
assertTrue(q.isEmpty()); |
499 |
> |
checkEmpty(q); |
500 |
|
} |
501 |
|
|
502 |
|
/** |
550 |
|
} |
551 |
|
|
552 |
|
/** |
553 |
< |
* toArray contains all elements |
553 |
> |
* toArray() contains all elements |
554 |
|
*/ |
555 |
|
public void testToArray() throws InterruptedException { |
556 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
576 |
|
* toArray(null) throws NullPointerException |
577 |
|
*/ |
578 |
|
public void testToArray_BadArg() { |
579 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
580 |
|
try { |
531 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
581 |
|
Object o[] = q.toArray(null); |
582 |
|
shouldThrow(); |
583 |
< |
} catch (NullPointerException success) { |
535 |
< |
} |
583 |
> |
} catch (NullPointerException success) {} |
584 |
|
} |
585 |
|
|
586 |
|
/** |
587 |
< |
* toArray with incompatible array type throws CCE |
587 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
588 |
|
*/ |
589 |
|
public void testToArray1_BadArg() { |
590 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
591 |
|
try { |
592 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
544 |
< |
Object o[] = q.toArray(new String[10]); |
592 |
> |
q.toArray(new String[10]); |
593 |
|
shouldThrow(); |
594 |
< |
} catch (ArrayStoreException success) { |
547 |
< |
} |
594 |
> |
} catch (ArrayStoreException success) {} |
595 |
|
} |
596 |
|
|
597 |
|
/** |
600 |
|
public void testIterator() throws InterruptedException { |
601 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
602 |
|
Iterator it = q.iterator(); |
603 |
+ |
int i = 0; |
604 |
|
while (it.hasNext()) { |
605 |
< |
assertEquals(it.next(), q.take()); |
605 |
> |
assertEquals(it.next(), i++); |
606 |
|
} |
607 |
+ |
assertEquals(i, SIZE); |
608 |
|
} |
609 |
|
|
610 |
|
/** |
611 |
< |
* iterator.remove removes current element |
611 |
> |
* iterator.remove() removes current element |
612 |
|
*/ |
613 |
|
public void testIteratorRemove() { |
614 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
621 |
|
it.remove(); |
622 |
|
|
623 |
|
it = q.iterator(); |
624 |
< |
assertEquals(it.next(), one); |
625 |
< |
assertEquals(it.next(), three); |
624 |
> |
assertSame(it.next(), one); |
625 |
> |
assertSame(it.next(), three); |
626 |
|
assertFalse(it.hasNext()); |
627 |
|
} |
628 |
|
|
675 |
|
*/ |
676 |
|
public void testOfferInExecutor() { |
677 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
678 |
< |
q.add(one); |
630 |
< |
q.add(two); |
678 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
679 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
680 |
|
|
681 |
|
executor.execute(new CheckedRunnable() { |
682 |
< |
void realRun() { |
683 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, |
684 |
< |
MILLISECONDS)); |
682 |
> |
public void realRun() throws InterruptedException { |
683 |
> |
threadsStarted.countDown(); |
684 |
> |
threadsStarted.await(); |
685 |
> |
assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
686 |
|
}}); |
687 |
|
|
688 |
|
executor.execute(new CheckedRunnable() { |
689 |
< |
void realRun() throws InterruptedException { |
690 |
< |
Thread.sleep(SMALL_DELAY_MS); |
691 |
< |
threadAssertEquals(one, q.take()); |
689 |
> |
public void realRun() throws InterruptedException { |
690 |
> |
threadsStarted.countDown(); |
691 |
> |
threadsStarted.await(); |
692 |
> |
assertSame(one, q.take()); |
693 |
> |
checkEmpty(q); |
694 |
|
}}); |
695 |
|
|
696 |
|
joinPool(executor); |
697 |
|
} |
698 |
|
|
699 |
|
/** |
700 |
< |
* poll retrieves elements across Executor threads |
700 |
> |
* timed poll retrieves elements across Executor threads |
701 |
|
*/ |
702 |
|
public void testPollInExecutor() { |
703 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
704 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
705 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
706 |
|
|
707 |
|
executor.execute(new CheckedRunnable() { |
708 |
< |
void realRun() throws InterruptedException { |
709 |
< |
threadAssertNull(q.poll()); |
710 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, |
711 |
< |
MILLISECONDS)); |
712 |
< |
threadAssertTrue(q.isEmpty()); |
708 |
> |
public void realRun() throws InterruptedException { |
709 |
> |
assertNull(q.poll()); |
710 |
> |
threadsStarted.countDown(); |
711 |
> |
threadsStarted.await(); |
712 |
> |
assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS)); |
713 |
> |
checkEmpty(q); |
714 |
|
}}); |
715 |
|
|
716 |
|
executor.execute(new CheckedRunnable() { |
717 |
< |
void realRun() throws InterruptedException { |
718 |
< |
Thread.sleep(SMALL_DELAY_MS); |
717 |
> |
public void realRun() throws InterruptedException { |
718 |
> |
threadsStarted.countDown(); |
719 |
> |
threadsStarted.await(); |
720 |
|
q.put(one); |
721 |
|
}}); |
722 |
|
|
742 |
|
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
743 |
|
|
744 |
|
assertEquals(q.size(), r.size()); |
745 |
+ |
assertEquals(q.toString(), r.toString()); |
746 |
+ |
assertTrue(Arrays.equals(q.toArray(), r.toArray())); |
747 |
|
while (!q.isEmpty()) { |
748 |
|
assertEquals(q.remove(), r.remove()); |
749 |
|
} |
757 |
|
try { |
758 |
|
q.drainTo(null); |
759 |
|
shouldThrow(); |
760 |
< |
} catch (NullPointerException success) { |
705 |
< |
} |
760 |
> |
} catch (NullPointerException success) {} |
761 |
|
} |
762 |
|
|
763 |
|
/** |
768 |
|
try { |
769 |
|
q.drainTo(q); |
770 |
|
shouldThrow(); |
771 |
< |
} catch (IllegalArgumentException success) { |
717 |
< |
} |
771 |
> |
} catch (IllegalArgumentException success) {} |
772 |
|
} |
773 |
|
|
774 |
|
/** |
798 |
|
} |
799 |
|
|
800 |
|
/** |
801 |
< |
* drainTo empties full queue, unblocking a waiting put. |
801 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
802 |
|
*/ |
803 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
804 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
805 |
< |
Thread t = new Thread(new CheckedRunnable() { |
806 |
< |
void realRun() { |
805 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
806 |
> |
public void realRun() { |
807 |
|
q.put(SIZE + 1); |
808 |
|
}}); |
755 |
– |
t.start(); |
809 |
|
ArrayList l = new ArrayList(); |
810 |
|
q.drainTo(l); |
811 |
|
assertTrue(l.size() >= SIZE); |
812 |
|
for (int i = 0; i < SIZE; ++i) { |
813 |
|
assertEquals(l.get(i), i); |
814 |
|
} |
815 |
< |
t.join(); |
815 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
816 |
|
assertTrue(q.size() + l.size() >= SIZE); |
817 |
|
} |
818 |
|
|
822 |
|
public void testDrainToNullN() { |
823 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
824 |
|
try { |
825 |
< |
q.drainTo(null, 0); |
825 |
> |
q.drainTo(null, SIZE); |
826 |
|
shouldThrow(); |
827 |
< |
} catch (NullPointerException success) { |
775 |
< |
} |
827 |
> |
} catch (NullPointerException success) {} |
828 |
|
} |
829 |
|
|
830 |
|
/** |
833 |
|
public void testDrainToSelfN() { |
834 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
835 |
|
try { |
836 |
< |
q.drainTo(q, 0); |
836 |
> |
q.drainTo(q, SIZE); |
837 |
|
shouldThrow(); |
838 |
< |
} catch (IllegalArgumentException success) { |
787 |
< |
} |
838 |
> |
} catch (IllegalArgumentException success) {} |
839 |
|
} |
840 |
|
|
841 |
|
/** |
842 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
842 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
843 |
|
*/ |
844 |
|
public void testDrainToN() { |
845 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
861 |
|
} |
862 |
|
|
863 |
|
/** |
864 |
< |
* poll and take decrement the waiting consumer count |
864 |
> |
* timed poll() or take() increments the waiting consumer count; |
865 |
> |
* offer(e) decrements the waiting consumer count |
866 |
|
*/ |
867 |
|
public void testWaitingConsumer() throws InterruptedException { |
868 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
869 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
870 |
< |
new Thread(new CheckedRunnable() { |
871 |
< |
void realRun() { |
872 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
873 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
874 |
< |
threadAssertTrue(q.offer(new Object())); |
875 |
< |
}}).start(); |
876 |
< |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
877 |
< |
assertTrue(q.getWaitingConsumerCount() |
878 |
< |
< waiting.getWaitingConsumers()); |
869 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
870 |
> |
assertFalse(q.hasWaitingConsumer()); |
871 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
872 |
> |
|
873 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
874 |
> |
public void realRun() throws InterruptedException { |
875 |
> |
threadStarted.countDown(); |
876 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
877 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
878 |
> |
assertFalse(q.hasWaitingConsumer()); |
879 |
> |
}}); |
880 |
> |
|
881 |
> |
threadStarted.await(); |
882 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
883 |
> |
assertEquals(q.getWaitingConsumerCount(), 1); |
884 |
> |
assertTrue(q.hasWaitingConsumer()); |
885 |
> |
|
886 |
> |
assertTrue(q.offer(one)); |
887 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
888 |
> |
assertFalse(q.hasWaitingConsumer()); |
889 |
> |
|
890 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
891 |
|
} |
892 |
|
|
893 |
|
/** |
898 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
899 |
|
q.transfer(null); |
900 |
|
shouldThrow(); |
901 |
< |
} catch (NullPointerException ex) { |
838 |
< |
} |
901 |
> |
} catch (NullPointerException success) {} |
902 |
|
} |
903 |
|
|
904 |
|
/** |
908 |
|
public void testTransfer2() throws InterruptedException { |
909 |
|
final LinkedTransferQueue<Integer> q |
910 |
|
= new LinkedTransferQueue<Integer>(); |
911 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
912 |
|
|
913 |
< |
new Thread(new CheckedRunnable() { |
914 |
< |
void realRun() throws InterruptedException { |
915 |
< |
q.transfer(SIZE); |
916 |
< |
threadAssertTrue(q.isEmpty()); |
917 |
< |
}}).start(); |
913 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
914 |
> |
public void realRun() throws InterruptedException { |
915 |
> |
threadStarted.countDown(); |
916 |
> |
q.transfer(five); |
917 |
> |
checkEmpty(q); |
918 |
> |
}}); |
919 |
|
|
920 |
< |
Thread.sleep(SHORT_DELAY_MS); |
920 |
> |
threadStarted.await(); |
921 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
922 |
|
assertEquals(1, q.size()); |
923 |
< |
assertEquals(SIZE, (int) q.poll()); |
924 |
< |
assertTrue(q.isEmpty()); |
923 |
> |
assertSame(five, q.poll()); |
924 |
> |
checkEmpty(q); |
925 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
926 |
|
} |
927 |
|
|
928 |
|
/** |
932 |
|
final LinkedTransferQueue<Integer> q |
933 |
|
= new LinkedTransferQueue<Integer>(); |
934 |
|
|
935 |
< |
Thread first = new Thread(new CheckedRunnable() { |
936 |
< |
void realRun() throws InterruptedException { |
937 |
< |
Integer i = SIZE + 1; |
938 |
< |
q.transfer(i); |
939 |
< |
threadAssertTrue(!q.contains(i)); |
873 |
< |
threadAssertEquals(1, q.size()); |
935 |
> |
Thread first = newStartedThread(new CheckedRunnable() { |
936 |
> |
public void realRun() throws InterruptedException { |
937 |
> |
q.transfer(four); |
938 |
> |
assertTrue(!q.contains(four)); |
939 |
> |
assertEquals(1, q.size()); |
940 |
|
}}); |
875 |
– |
first.start(); |
941 |
|
|
942 |
< |
Thread interruptedThread = new Thread(new CheckedInterruptedRunnable() { |
943 |
< |
void realRun() throws InterruptedException { |
944 |
< |
while (q.size() == 0) |
945 |
< |
Thread.yield(); |
946 |
< |
q.transfer(SIZE); |
947 |
< |
}}); |
948 |
< |
interruptedThread.start(); |
942 |
> |
Thread interruptedThread = newStartedThread( |
943 |
> |
new CheckedInterruptedRunnable() { |
944 |
> |
public void realRun() throws InterruptedException { |
945 |
> |
while (q.isEmpty()) |
946 |
> |
Thread.yield(); |
947 |
> |
q.transfer(five); |
948 |
> |
}}); |
949 |
|
|
950 |
|
while (q.size() < 2) |
951 |
|
Thread.yield(); |
952 |
|
assertEquals(2, q.size()); |
953 |
< |
assertEquals(SIZE + 1, (int) q.poll()); |
953 |
> |
assertSame(four, q.poll()); |
954 |
|
first.join(); |
955 |
|
assertEquals(1, q.size()); |
956 |
|
interruptedThread.interrupt(); |
957 |
|
interruptedThread.join(); |
958 |
< |
assertEquals(0, q.size()); |
894 |
< |
assertTrue(q.isEmpty()); |
958 |
> |
checkEmpty(q); |
959 |
|
} |
960 |
|
|
961 |
|
/** |
964 |
|
*/ |
965 |
|
public void testTransfer4() throws InterruptedException { |
966 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
967 |
< |
new Thread(new CheckedRunnable() { |
968 |
< |
void realRun() throws InterruptedException { |
967 |
> |
|
968 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
969 |
> |
public void realRun() throws InterruptedException { |
970 |
|
q.transfer(four); |
971 |
< |
threadAssertFalse(q.contains(four)); |
972 |
< |
threadAssertEquals(three, q.poll()); |
973 |
< |
}}).start(); |
974 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
971 |
> |
assertFalse(q.contains(four)); |
972 |
> |
assertSame(three, q.poll()); |
973 |
> |
}}); |
974 |
> |
|
975 |
> |
while (q.isEmpty()) |
976 |
> |
Thread.yield(); |
977 |
> |
assertFalse(q.isEmpty()); |
978 |
> |
assertEquals(1, q.size()); |
979 |
|
assertTrue(q.offer(three)); |
980 |
< |
assertEquals(four, q.poll()); |
980 |
> |
assertSame(four, q.poll()); |
981 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
982 |
|
} |
983 |
|
|
984 |
|
/** |
989 |
|
final LinkedTransferQueue<Integer> q |
990 |
|
= new LinkedTransferQueue<Integer>(); |
991 |
|
|
992 |
< |
new Thread(new CheckedRunnable() { |
993 |
< |
void realRun() throws InterruptedException { |
994 |
< |
q.transfer(SIZE); |
995 |
< |
threadAssertTrue(q.isEmpty()); |
996 |
< |
}}).start(); |
992 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
993 |
> |
public void realRun() throws InterruptedException { |
994 |
> |
q.transfer(four); |
995 |
> |
checkEmpty(q); |
996 |
> |
}}); |
997 |
|
|
998 |
< |
Thread.sleep(SHORT_DELAY_MS); |
999 |
< |
assertEquals(SIZE, (int) q.take()); |
1000 |
< |
assertTrue(q.isEmpty()); |
998 |
> |
while (q.isEmpty()) |
999 |
> |
Thread.yield(); |
1000 |
> |
assertFalse(q.isEmpty()); |
1001 |
> |
assertEquals(1, q.size()); |
1002 |
> |
assertSame(four, q.take()); |
1003 |
> |
checkEmpty(q); |
1004 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1005 |
|
} |
1006 |
|
|
1007 |
|
/** |
1012 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1013 |
|
q.tryTransfer(null); |
1014 |
|
shouldThrow(); |
1015 |
< |
} catch (NullPointerException ex) { |
942 |
< |
} |
1015 |
> |
} catch (NullPointerException success) {} |
1016 |
|
} |
1017 |
|
|
1018 |
|
/** |
1019 |
|
* tryTransfer returns false and does not enqueue if there are no |
1020 |
|
* consumers waiting to poll or take. |
1021 |
|
*/ |
1022 |
< |
public void testTryTransfer2() { |
1022 |
> |
public void testTryTransfer2() throws InterruptedException { |
1023 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1024 |
|
assertFalse(q.tryTransfer(new Object())); |
1025 |
|
assertFalse(q.hasWaitingConsumer()); |
1026 |
< |
assertTrue(q.isEmpty()); |
954 |
< |
assertEquals(0, q.size()); |
1026 |
> |
checkEmpty(q); |
1027 |
|
} |
1028 |
|
|
1029 |
|
/** |
1033 |
|
public void testTryTransfer3() throws InterruptedException { |
1034 |
|
final Object hotPotato = new Object(); |
1035 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1036 |
< |
new Thread(new CheckedRunnable() { |
1037 |
< |
void realRun() { |
1036 |
> |
|
1037 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1038 |
> |
public void realRun() { |
1039 |
|
while (! q.hasWaitingConsumer()) |
1040 |
|
Thread.yield(); |
1041 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1042 |
< |
threadAssertTrue(q.isEmpty()); |
1043 |
< |
threadAssertTrue(q.size() == 0); |
1044 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1045 |
< |
}}).start(); |
1046 |
< |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) == hotPotato); |
1047 |
< |
assertTrue(q.isEmpty()); |
1041 |
> |
assertTrue(q.hasWaitingConsumer()); |
1042 |
> |
checkEmpty(q); |
1043 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
1044 |
> |
}}); |
1045 |
> |
|
1046 |
> |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1047 |
> |
checkEmpty(q); |
1048 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1049 |
|
} |
1050 |
|
|
1051 |
|
/** |
1055 |
|
public void testTryTransfer4() throws InterruptedException { |
1056 |
|
final Object hotPotato = new Object(); |
1057 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1058 |
< |
new Thread(new CheckedRunnable() { |
1059 |
< |
void realRun() { |
1058 |
> |
|
1059 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1060 |
> |
public void realRun() { |
1061 |
|
while (! q.hasWaitingConsumer()) |
1062 |
|
Thread.yield(); |
1063 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1064 |
< |
threadAssertTrue(q.isEmpty()); |
1065 |
< |
threadAssertTrue(q.size() == 0); |
1066 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1067 |
< |
}}).start(); |
1068 |
< |
assertTrue(q.take() == hotPotato); |
1069 |
< |
assertTrue(q.isEmpty()); |
1063 |
> |
assertTrue(q.hasWaitingConsumer()); |
1064 |
> |
checkEmpty(q); |
1065 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
1066 |
> |
}}); |
1067 |
> |
|
1068 |
> |
assertSame(q.take(), hotPotato); |
1069 |
> |
checkEmpty(q); |
1070 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1071 |
|
} |
1072 |
|
|
1073 |
|
/** |
1076 |
|
*/ |
1077 |
|
public void testTryTransfer5() throws InterruptedException { |
1078 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1079 |
< |
Thread toInterrupt = new Thread(new CheckedInterruptedRunnable() { |
1080 |
< |
void realRun() throws InterruptedException { |
1081 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1079 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1080 |
> |
|
1081 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1082 |
> |
public void realRun() throws InterruptedException { |
1083 |
> |
long t0 = System.nanoTime(); |
1084 |
> |
threadStarted.countDown(); |
1085 |
> |
try { |
1086 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1087 |
> |
shouldThrow(); |
1088 |
> |
} catch (InterruptedException success) {} |
1089 |
> |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1090 |
|
}}); |
1091 |
< |
toInterrupt.start(); |
1092 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1093 |
< |
toInterrupt.interrupt(); |
1091 |
> |
|
1092 |
> |
threadStarted.await(); |
1093 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1094 |
> |
t.interrupt(); |
1095 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1096 |
> |
checkEmpty(q); |
1097 |
|
} |
1098 |
|
|
1099 |
|
/** |
1100 |
< |
* tryTransfer gives up after the timeout and return false |
1100 |
> |
* tryTransfer gives up after the timeout and returns false |
1101 |
|
*/ |
1102 |
|
public void testTryTransfer6() throws InterruptedException { |
1103 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1104 |
< |
new Thread(new CheckedRunnable() { |
1105 |
< |
void realRun() throws InterruptedException { |
1106 |
< |
threadAssertFalse |
1107 |
< |
(q.tryTransfer(new Object(), |
1108 |
< |
SHORT_DELAY_MS, MILLISECONDS)); |
1109 |
< |
}}).start(); |
1110 |
< |
Thread.sleep(LONG_DELAY_MS); |
1111 |
< |
assertTrue(q.isEmpty()); |
1104 |
> |
|
1105 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1106 |
> |
public void realRun() throws InterruptedException { |
1107 |
> |
long t0 = System.nanoTime(); |
1108 |
> |
assertFalse(q.tryTransfer(new Object(), |
1109 |
> |
SHORT_DELAY_MS, MILLISECONDS)); |
1110 |
> |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1111 |
> |
checkEmpty(q); |
1112 |
> |
}}); |
1113 |
> |
|
1114 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1115 |
> |
checkEmpty(q); |
1116 |
|
} |
1117 |
|
|
1118 |
|
/** |
1122 |
|
public void testTryTransfer7() throws InterruptedException { |
1123 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1124 |
|
assertTrue(q.offer(four)); |
1125 |
< |
new Thread(new CheckedRunnable() { |
1126 |
< |
void realRun() throws InterruptedException { |
1127 |
< |
threadAssertTrue(q.tryTransfer(five, |
1128 |
< |
LONG_DELAY_MS, MILLISECONDS)); |
1129 |
< |
threadAssertTrue(q.isEmpty()); |
1130 |
< |
}}).start(); |
1131 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1125 |
> |
|
1126 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1127 |
> |
public void realRun() throws InterruptedException { |
1128 |
> |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1129 |
> |
checkEmpty(q); |
1130 |
> |
}}); |
1131 |
> |
|
1132 |
> |
while (q.size() != 2) |
1133 |
> |
Thread.yield(); |
1134 |
|
assertEquals(2, q.size()); |
1135 |
< |
assertEquals(four, q.poll()); |
1136 |
< |
assertEquals(five, q.poll()); |
1137 |
< |
assertTrue(q.isEmpty()); |
1135 |
> |
assertSame(four, q.poll()); |
1136 |
> |
assertSame(five, q.poll()); |
1137 |
> |
checkEmpty(q); |
1138 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1139 |
|
} |
1140 |
|
|
1141 |
|
/** |
1146 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1147 |
|
assertTrue(q.offer(four)); |
1148 |
|
assertEquals(1, q.size()); |
1149 |
+ |
long t0 = System.nanoTime(); |
1150 |
|
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1151 |
+ |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1152 |
|
assertEquals(1, q.size()); |
1153 |
< |
assertEquals(four, q.poll()); |
1058 |
< |
threadAssertTrue(q.isEmpty()); |
1153 |
> |
assertSame(four, q.poll()); |
1154 |
|
assertNull(q.poll()); |
1155 |
+ |
checkEmpty(q); |
1156 |
|
} |
1157 |
|
|
1158 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1159 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1160 |
< |
assertTrue(q.isEmpty()); |
1160 |
> |
checkEmpty(q); |
1161 |
|
for (int i = 0; i < n; i++) { |
1162 |
|
assertEquals(i, q.size()); |
1163 |
|
assertTrue(q.offer(i)); |
1166 |
|
assertFalse(q.isEmpty()); |
1167 |
|
return q; |
1168 |
|
} |
1073 |
– |
|
1074 |
– |
private static class ConsumerObserver { |
1075 |
– |
|
1076 |
– |
private int waitingConsumers; |
1077 |
– |
|
1078 |
– |
private ConsumerObserver() { |
1079 |
– |
} |
1080 |
– |
|
1081 |
– |
private void setWaitingConsumer(int i) { |
1082 |
– |
this.waitingConsumers = i; |
1083 |
– |
} |
1084 |
– |
|
1085 |
– |
private int getWaitingConsumers() { |
1086 |
– |
return waitingConsumers; |
1087 |
– |
} |
1088 |
– |
} |
1169 |
|
} |