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); |
39 |
> |
return newTestSuite(LinkedTransferQueueTest.class, |
40 |
> |
new Generic().testSuite()); |
41 |
|
} |
42 |
|
|
43 |
|
/** |
57 |
|
try { |
58 |
|
new LinkedTransferQueue(null); |
59 |
|
shouldThrow(); |
60 |
< |
} catch (NullPointerException success) { |
52 |
< |
} |
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) { |
65 |
< |
} |
72 |
> |
} catch (NullPointerException success) {} |
73 |
|
} |
74 |
|
|
75 |
|
/** |
84 |
|
} |
85 |
|
new LinkedTransferQueue(Arrays.asList(ints)); |
86 |
|
shouldThrow(); |
87 |
< |
} catch (NullPointerException success) { |
81 |
< |
} |
87 |
> |
} catch (NullPointerException success) {} |
88 |
|
} |
89 |
|
|
90 |
|
/** |
91 |
|
* Queue contains all elements of the collection it is initialized by |
92 |
|
*/ |
93 |
|
public void testConstructor5() { |
94 |
< |
try { |
95 |
< |
Integer[] ints = new Integer[SIZE]; |
96 |
< |
for (int i = 0; i < SIZE; ++i) { |
97 |
< |
ints[i] = i; |
98 |
< |
} |
99 |
< |
LinkedTransferQueue q |
100 |
< |
= new LinkedTransferQueue(Arrays.asList(ints)); |
101 |
< |
for (int i = 0; i < SIZE; ++i) { |
102 |
< |
assertEquals(ints[i], q.poll()); |
103 |
< |
} |
104 |
< |
} finally { |
94 |
> |
Integer[] ints = new Integer[SIZE]; |
95 |
> |
for (int i = 0; i < SIZE; ++i) { |
96 |
> |
ints[i] = i; |
97 |
> |
} |
98 |
> |
List intList = Arrays.asList(ints); |
99 |
> |
LinkedTransferQueue q |
100 |
> |
= new LinkedTransferQueue(intList); |
101 |
> |
assertEquals(q.size(), intList.size()); |
102 |
> |
assertEquals(q.toString(), intList.toString()); |
103 |
> |
assertTrue(Arrays.equals(q.toArray(), |
104 |
> |
intList.toArray())); |
105 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[0]), |
106 |
> |
intList.toArray(new Object[0]))); |
107 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[SIZE]), |
108 |
> |
intList.toArray(new Object[SIZE]))); |
109 |
> |
for (int i = 0; i < SIZE; ++i) { |
110 |
> |
assertEquals(ints[i], q.poll()); |
111 |
|
} |
112 |
|
} |
113 |
|
|
114 |
|
/** |
115 |
< |
* Remaining capacity never decrease nor increase on add or remove |
115 |
> |
* remainingCapacity() always returns Integer.MAX_VALUE |
116 |
|
*/ |
117 |
|
public void testRemainingCapacity() { |
118 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
136 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
137 |
|
q.offer(null); |
138 |
|
shouldThrow(); |
139 |
< |
} catch (NullPointerException success) { |
128 |
< |
} |
139 |
> |
} catch (NullPointerException success) {} |
140 |
|
} |
141 |
|
|
142 |
|
/** |
147 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
148 |
|
q.add(null); |
149 |
|
shouldThrow(); |
150 |
< |
} catch (NullPointerException success) { |
140 |
< |
} |
150 |
> |
} catch (NullPointerException success) {} |
151 |
|
} |
152 |
|
|
153 |
|
/** |
158 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
159 |
|
q.addAll(null); |
160 |
|
shouldThrow(); |
161 |
< |
} catch (NullPointerException success) { |
152 |
< |
} |
161 |
> |
} catch (NullPointerException success) {} |
162 |
|
} |
163 |
|
|
164 |
|
/** |
169 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
170 |
|
q.addAll(q); |
171 |
|
shouldThrow(); |
172 |
< |
} catch (IllegalArgumentException success) { |
164 |
< |
} |
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) { |
177 |
< |
} |
184 |
> |
} catch (NullPointerException success) {} |
185 |
|
} |
186 |
|
|
187 |
|
/** |
197 |
|
} |
198 |
|
q.addAll(Arrays.asList(ints)); |
199 |
|
shouldThrow(); |
200 |
< |
} catch (NullPointerException success) { |
194 |
< |
} |
200 |
> |
} catch (NullPointerException success) {} |
201 |
|
} |
202 |
|
|
203 |
|
/** |
234 |
|
public void testPut() { |
235 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
236 |
|
for (int i = 0; i < SIZE; ++i) { |
237 |
+ |
assertEquals(q.size(), i); |
238 |
|
q.put(i); |
239 |
|
assertTrue(q.contains(i)); |
240 |
|
} |
234 |
– |
assertEquals(q.size(), SIZE); |
241 |
|
} |
242 |
|
|
243 |
|
/** |
251 |
|
} |
252 |
|
|
253 |
|
/** |
254 |
< |
* take blocks interruptibly when empty |
249 |
< |
*/ |
250 |
< |
public void testTakeFromEmpty() throws InterruptedException { |
251 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
252 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
253 |
< |
void realRun() throws InterruptedException { |
254 |
< |
q.take(); |
255 |
< |
}}); |
256 |
< |
Thread.sleep(SHORT_DELAY_MS); |
257 |
< |
t.interrupt(); |
258 |
< |
t.join(); |
259 |
< |
} |
260 |
< |
|
261 |
< |
/** |
262 |
< |
* 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 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
258 |
< |
void realRun() throws InterruptedException { |
259 |
< |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
257 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
258 |
> |
final CountDownLatch aboutToWait = 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, (int) q.take()); |
263 |
|
} |
264 |
< |
q.take(); |
264 |
> |
aboutToWait.countDown(); |
265 |
> |
try { |
266 |
> |
q.take(); |
267 |
> |
shouldThrow(); |
268 |
> |
} catch (InterruptedException success) {} |
269 |
|
}}); |
270 |
< |
Thread.sleep(SHORT_DELAY_MS); |
270 |
> |
|
271 |
> |
aboutToWait.await(); |
272 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
273 |
|
t.interrupt(); |
274 |
< |
t.join(); |
274 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
275 |
> |
checkEmpty(q); |
276 |
|
} |
277 |
|
|
278 |
|
/** |
279 |
|
* poll succeeds unless empty |
280 |
|
*/ |
281 |
< |
public void testPoll() { |
281 |
> |
public void testPoll() throws InterruptedException { |
282 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
283 |
|
for (int i = 0; i < SIZE; ++i) { |
284 |
|
assertEquals(i, (int) q.poll()); |
285 |
|
} |
286 |
|
assertNull(q.poll()); |
287 |
+ |
checkEmpty(q); |
288 |
|
} |
289 |
|
|
290 |
|
/** |
291 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
291 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
292 |
|
*/ |
293 |
|
public void testTimedPoll0() throws InterruptedException { |
294 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
296 |
|
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
297 |
|
} |
298 |
|
assertNull(q.poll(0, MILLISECONDS)); |
299 |
+ |
checkEmpty(q); |
300 |
|
} |
301 |
|
|
302 |
|
/** |
303 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
303 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
304 |
|
*/ |
305 |
|
public void testTimedPoll() throws InterruptedException { |
306 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
307 |
|
for (int i = 0; i < SIZE; ++i) { |
308 |
< |
assertEquals(i, (int) q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
308 |
> |
long t0 = System.nanoTime(); |
309 |
> |
assertEquals(i, (int) q.poll(SMALL_DELAY_MS, MILLISECONDS)); |
310 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
311 |
|
} |
312 |
+ |
long t0 = System.nanoTime(); |
313 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
314 |
+ |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
315 |
+ |
checkEmpty(q); |
316 |
|
} |
317 |
|
|
318 |
|
/** |
320 |
|
* returning timeout status |
321 |
|
*/ |
322 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
323 |
< |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
324 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
325 |
< |
void realRun() throws InterruptedException { |
323 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
324 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
325 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
326 |
> |
public void realRun() throws InterruptedException { |
327 |
|
for (int i = 0; i < SIZE; ++i) { |
328 |
< |
threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS, |
329 |
< |
MILLISECONDS)); |
328 |
> |
long t0 = System.nanoTime(); |
329 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
330 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
331 |
> |
} |
332 |
> |
long t0 = System.nanoTime(); |
333 |
> |
aboutToWait.countDown(); |
334 |
> |
try { |
335 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
336 |
> |
shouldThrow(); |
337 |
> |
} catch (InterruptedException success) { |
338 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
339 |
|
} |
323 |
– |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
340 |
|
}}); |
341 |
< |
Thread.sleep(SMALL_DELAY_MS); |
341 |
> |
|
342 |
> |
aboutToWait.await(); |
343 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
344 |
|
t.interrupt(); |
345 |
< |
t.join(); |
346 |
< |
assertEquals(q.size(), 0); |
329 |
< |
assertNull(q.poll()); |
345 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
346 |
> |
checkEmpty(q); |
347 |
|
} |
348 |
|
|
349 |
|
/** |
350 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
351 |
< |
* on interruption throws |
350 |
> |
* timed poll after thread interrupted throws InterruptedException |
351 |
> |
* instead of returning timeout status |
352 |
|
*/ |
353 |
< |
public void testTimedPollWithOffer() throws InterruptedException { |
354 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
355 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
356 |
< |
void realRun() throws InterruptedException { |
357 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
358 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
359 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
353 |
> |
public void testTimedPollAfterInterrupt() throws InterruptedException { |
354 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
355 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
356 |
> |
public void realRun() throws InterruptedException { |
357 |
> |
Thread.currentThread().interrupt(); |
358 |
> |
for (int i = 0; i < SIZE; ++i) { |
359 |
> |
long t0 = System.nanoTime(); |
360 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
361 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
362 |
> |
} |
363 |
> |
try { |
364 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
365 |
> |
shouldThrow(); |
366 |
> |
} catch (InterruptedException success) {} |
367 |
|
}}); |
368 |
< |
Thread.sleep(SMALL_DELAY_MS); |
369 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
370 |
< |
t.interrupt(); |
347 |
< |
t.join(); |
368 |
> |
|
369 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
370 |
> |
checkEmpty(q); |
371 |
|
} |
372 |
|
|
373 |
|
/** |
374 |
|
* peek returns next element, or null if empty |
375 |
|
*/ |
376 |
< |
public void testPeek() { |
376 |
> |
public void testPeek() throws InterruptedException { |
377 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
378 |
|
for (int i = 0; i < SIZE; ++i) { |
379 |
|
assertEquals(i, (int) q.peek()); |
382 |
|
i != (int) q.peek()); |
383 |
|
} |
384 |
|
assertNull(q.peek()); |
385 |
+ |
checkEmpty(q); |
386 |
|
} |
387 |
|
|
388 |
|
/** |
389 |
|
* element returns next element, or throws NoSuchElementException if empty |
390 |
|
*/ |
391 |
< |
public void testElement() { |
391 |
> |
public void testElement() throws InterruptedException { |
392 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
393 |
|
for (int i = 0; i < SIZE; ++i) { |
394 |
|
assertEquals(i, (int) q.element()); |
397 |
|
try { |
398 |
|
q.element(); |
399 |
|
shouldThrow(); |
400 |
< |
} catch (NoSuchElementException success) { |
401 |
< |
} |
400 |
> |
} catch (NoSuchElementException success) {} |
401 |
> |
checkEmpty(q); |
402 |
|
} |
403 |
|
|
404 |
|
/** |
405 |
|
* remove removes next element, or throws NoSuchElementException if empty |
406 |
|
*/ |
407 |
< |
public void testRemove() { |
407 |
> |
public void testRemove() throws InterruptedException { |
408 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
409 |
|
for (int i = 0; i < SIZE; ++i) { |
410 |
|
assertEquals(i, (int) q.remove()); |
412 |
|
try { |
413 |
|
q.remove(); |
414 |
|
shouldThrow(); |
415 |
< |
} catch (NoSuchElementException success) { |
416 |
< |
} |
415 |
> |
} catch (NoSuchElementException success) {} |
416 |
> |
checkEmpty(q); |
417 |
|
} |
418 |
|
|
419 |
|
/** |
420 |
|
* remove(x) removes x and returns true if present |
421 |
|
*/ |
422 |
< |
public void testRemoveElement() { |
422 |
> |
public void testRemoveElement() throws InterruptedException { |
423 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
424 |
< |
for (int i = 1; i < SIZE; i += 2) { |
424 |
> |
for (int i = 1; i < SIZE; i+=2) { |
425 |
> |
assertTrue(q.contains(i)); |
426 |
|
assertTrue(q.remove(i)); |
427 |
+ |
assertFalse(q.contains(i)); |
428 |
+ |
assertTrue(q.contains(i-1)); |
429 |
|
} |
430 |
< |
for (int i = 0; i < SIZE; i += 2) { |
430 |
> |
for (int i = 0; i < SIZE; i+=2) { |
431 |
> |
assertTrue(q.contains(i)); |
432 |
|
assertTrue(q.remove(i)); |
433 |
< |
assertFalse(q.remove(i + 1)); |
433 |
> |
assertFalse(q.contains(i)); |
434 |
> |
assertFalse(q.remove(i+1)); |
435 |
> |
assertFalse(q.contains(i+1)); |
436 |
|
} |
437 |
< |
assertTrue(q.isEmpty()); |
437 |
> |
checkEmpty(q); |
438 |
|
} |
439 |
|
|
440 |
|
/** |
447 |
|
assertTrue(q.remove(one)); |
448 |
|
assertTrue(q.remove(two)); |
449 |
|
assertTrue(q.add(three)); |
450 |
< |
assertTrue(q.take() != null); |
450 |
> |
assertSame(q.take(), three); |
451 |
|
} |
452 |
|
|
453 |
|
/** |
465 |
|
/** |
466 |
|
* clear removes all elements |
467 |
|
*/ |
468 |
< |
public void testClear() { |
468 |
> |
public void testClear() throws InterruptedException { |
469 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
470 |
|
q.clear(); |
471 |
< |
assertTrue(q.isEmpty()); |
442 |
< |
assertEquals(0, q.size()); |
471 |
> |
checkEmpty(q); |
472 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
473 |
|
q.add(one); |
474 |
|
assertFalse(q.isEmpty()); |
475 |
+ |
assertEquals(1, q.size()); |
476 |
|
assertTrue(q.contains(one)); |
477 |
|
q.clear(); |
478 |
< |
assertTrue(q.isEmpty()); |
478 |
> |
checkEmpty(q); |
479 |
|
} |
480 |
|
|
481 |
|
/** |
529 |
|
} |
530 |
|
|
531 |
|
/** |
532 |
< |
* toArray contains all elements |
532 |
> |
* toArray() contains all elements in FIFO order |
533 |
|
*/ |
534 |
< |
public void testToArray() throws InterruptedException { |
534 |
> |
public void testToArray() { |
535 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
536 |
|
Object[] o = q.toArray(); |
537 |
|
for (int i = 0; i < o.length; i++) { |
538 |
< |
assertEquals(o[i], q.take()); |
538 |
> |
assertSame(o[i], q.poll()); |
539 |
|
} |
540 |
|
} |
541 |
|
|
542 |
|
/** |
543 |
< |
* toArray(a) contains all elements |
543 |
> |
* toArray(a) contains all elements in FIFO order |
544 |
|
*/ |
545 |
< |
public void testToArray2() throws InterruptedException { |
545 |
> |
public void testToArray2() { |
546 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
547 |
|
Integer[] ints = new Integer[SIZE]; |
548 |
< |
ints = q.toArray(ints); |
548 |
> |
Integer[] array = q.toArray(ints); |
549 |
> |
assertSame(ints, array); |
550 |
|
for (int i = 0; i < ints.length; i++) { |
551 |
< |
assertEquals(ints[i], q.take()); |
551 |
> |
assertSame(ints[i], q.poll()); |
552 |
|
} |
553 |
|
} |
554 |
|
|
555 |
|
/** |
556 |
|
* toArray(null) throws NullPointerException |
557 |
|
*/ |
558 |
< |
public void testToArray_BadArg() { |
558 |
> |
public void testToArray_NullArg() { |
559 |
> |
LinkedTransferQueue q = populatedQueue(SIZE); |
560 |
|
try { |
561 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
530 |
< |
Object o[] = q.toArray(null); |
561 |
> |
q.toArray(null); |
562 |
|
shouldThrow(); |
563 |
< |
} catch (NullPointerException success) { |
533 |
< |
} |
563 |
> |
} catch (NullPointerException success) {} |
564 |
|
} |
565 |
|
|
566 |
|
/** |
567 |
< |
* toArray with incompatible array type throws CCE |
567 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
568 |
|
*/ |
569 |
|
public void testToArray1_BadArg() { |
570 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
571 |
|
try { |
572 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
542 |
< |
Object o[] = q.toArray(new String[10]); |
572 |
> |
q.toArray(new String[10]); |
573 |
|
shouldThrow(); |
574 |
< |
} catch (ArrayStoreException success) { |
545 |
< |
} |
574 |
> |
} catch (ArrayStoreException success) {} |
575 |
|
} |
576 |
|
|
577 |
|
/** |
580 |
|
public void testIterator() throws InterruptedException { |
581 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
582 |
|
Iterator it = q.iterator(); |
583 |
+ |
int i = 0; |
584 |
|
while (it.hasNext()) { |
585 |
< |
assertEquals(it.next(), q.take()); |
585 |
> |
assertEquals(it.next(), i++); |
586 |
|
} |
587 |
+ |
assertEquals(i, SIZE); |
588 |
|
} |
589 |
|
|
590 |
|
/** |
591 |
< |
* iterator.remove removes current element |
591 |
> |
* iterator.remove() removes current element |
592 |
|
*/ |
593 |
|
public void testIteratorRemove() { |
594 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
601 |
|
it.remove(); |
602 |
|
|
603 |
|
it = q.iterator(); |
604 |
< |
assertEquals(it.next(), one); |
605 |
< |
assertEquals(it.next(), three); |
604 |
> |
assertSame(it.next(), one); |
605 |
> |
assertSame(it.next(), three); |
606 |
|
assertFalse(it.hasNext()); |
607 |
|
} |
608 |
|
|
655 |
|
*/ |
656 |
|
public void testOfferInExecutor() { |
657 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
658 |
< |
q.add(one); |
628 |
< |
q.add(two); |
658 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
659 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
660 |
|
|
661 |
|
executor.execute(new CheckedRunnable() { |
662 |
< |
void realRun() { |
663 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, |
664 |
< |
MILLISECONDS)); |
662 |
> |
public void realRun() throws InterruptedException { |
663 |
> |
threadsStarted.countDown(); |
664 |
> |
threadsStarted.await(); |
665 |
> |
assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
666 |
|
}}); |
667 |
|
|
668 |
|
executor.execute(new CheckedRunnable() { |
669 |
< |
void realRun() throws InterruptedException { |
670 |
< |
Thread.sleep(SMALL_DELAY_MS); |
671 |
< |
threadAssertEquals(one, q.take()); |
669 |
> |
public void realRun() throws InterruptedException { |
670 |
> |
threadsStarted.countDown(); |
671 |
> |
threadsStarted.await(); |
672 |
> |
assertSame(one, q.take()); |
673 |
> |
checkEmpty(q); |
674 |
|
}}); |
675 |
|
|
676 |
|
joinPool(executor); |
677 |
|
} |
678 |
|
|
679 |
|
/** |
680 |
< |
* poll retrieves elements across Executor threads |
680 |
> |
* timed poll retrieves elements across Executor threads |
681 |
|
*/ |
682 |
|
public void testPollInExecutor() { |
683 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
684 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
685 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
686 |
|
|
687 |
|
executor.execute(new CheckedRunnable() { |
688 |
< |
void realRun() throws InterruptedException { |
689 |
< |
threadAssertNull(q.poll()); |
690 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, |
691 |
< |
MILLISECONDS)); |
692 |
< |
threadAssertTrue(q.isEmpty()); |
688 |
> |
public void realRun() throws InterruptedException { |
689 |
> |
assertNull(q.poll()); |
690 |
> |
threadsStarted.countDown(); |
691 |
> |
threadsStarted.await(); |
692 |
> |
assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS)); |
693 |
> |
checkEmpty(q); |
694 |
|
}}); |
695 |
|
|
696 |
|
executor.execute(new CheckedRunnable() { |
697 |
< |
void realRun() throws InterruptedException { |
698 |
< |
Thread.sleep(SMALL_DELAY_MS); |
697 |
> |
public void realRun() throws InterruptedException { |
698 |
> |
threadsStarted.countDown(); |
699 |
> |
threadsStarted.await(); |
700 |
|
q.put(one); |
701 |
|
}}); |
702 |
|
|
722 |
|
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
723 |
|
|
724 |
|
assertEquals(q.size(), r.size()); |
725 |
+ |
assertEquals(q.toString(), r.toString()); |
726 |
+ |
assertTrue(Arrays.equals(q.toArray(), r.toArray())); |
727 |
|
while (!q.isEmpty()) { |
728 |
|
assertEquals(q.remove(), r.remove()); |
729 |
|
} |
737 |
|
try { |
738 |
|
q.drainTo(null); |
739 |
|
shouldThrow(); |
740 |
< |
} catch (NullPointerException success) { |
703 |
< |
} |
740 |
> |
} catch (NullPointerException success) {} |
741 |
|
} |
742 |
|
|
743 |
|
/** |
748 |
|
try { |
749 |
|
q.drainTo(q); |
750 |
|
shouldThrow(); |
751 |
< |
} catch (IllegalArgumentException success) { |
715 |
< |
} |
751 |
> |
} catch (IllegalArgumentException success) {} |
752 |
|
} |
753 |
|
|
754 |
|
/** |
778 |
|
} |
779 |
|
|
780 |
|
/** |
781 |
< |
* drainTo empties full queue, unblocking a waiting put. |
781 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
782 |
|
*/ |
783 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
784 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
785 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
786 |
< |
void realRun() { |
786 |
> |
public void realRun() { |
787 |
|
q.put(SIZE + 1); |
788 |
|
}}); |
789 |
|
ArrayList l = new ArrayList(); |
792 |
|
for (int i = 0; i < SIZE; ++i) { |
793 |
|
assertEquals(l.get(i), i); |
794 |
|
} |
795 |
< |
t.join(); |
795 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
796 |
|
assertTrue(q.size() + l.size() >= SIZE); |
797 |
|
} |
798 |
|
|
802 |
|
public void testDrainToNullN() { |
803 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
804 |
|
try { |
805 |
< |
q.drainTo(null, 0); |
805 |
> |
q.drainTo(null, SIZE); |
806 |
|
shouldThrow(); |
807 |
< |
} catch (NullPointerException success) { |
772 |
< |
} |
807 |
> |
} catch (NullPointerException success) {} |
808 |
|
} |
809 |
|
|
810 |
|
/** |
813 |
|
public void testDrainToSelfN() { |
814 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
815 |
|
try { |
816 |
< |
q.drainTo(q, 0); |
816 |
> |
q.drainTo(q, SIZE); |
817 |
|
shouldThrow(); |
818 |
< |
} catch (IllegalArgumentException success) { |
784 |
< |
} |
818 |
> |
} catch (IllegalArgumentException success) {} |
819 |
|
} |
820 |
|
|
821 |
|
/** |
822 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
822 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
823 |
|
*/ |
824 |
|
public void testDrainToN() { |
825 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
841 |
|
} |
842 |
|
|
843 |
|
/** |
844 |
< |
* poll and take decrement the waiting consumer count |
844 |
> |
* timed poll() or take() increments the waiting consumer count; |
845 |
> |
* offer(e) decrements the waiting consumer count |
846 |
|
*/ |
847 |
|
public void testWaitingConsumer() throws InterruptedException { |
848 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
849 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
849 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
850 |
> |
assertFalse(q.hasWaitingConsumer()); |
851 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
852 |
|
|
853 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
854 |
< |
void realRun() throws InterruptedException { |
855 |
< |
Thread.sleep(SMALL_DELAY_MS); |
856 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
857 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
858 |
< |
threadAssertTrue(q.offer(new Object())); |
854 |
> |
public void realRun() throws InterruptedException { |
855 |
> |
threadStarted.countDown(); |
856 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
857 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
858 |
> |
assertFalse(q.hasWaitingConsumer()); |
859 |
|
}}); |
860 |
|
|
861 |
< |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
862 |
< |
assertTrue(q.getWaitingConsumerCount() |
863 |
< |
< waiting.getWaitingConsumers()); |
864 |
< |
t.join(); |
861 |
> |
threadStarted.await(); |
862 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
863 |
> |
assertEquals(q.getWaitingConsumerCount(), 1); |
864 |
> |
assertTrue(q.hasWaitingConsumer()); |
865 |
> |
|
866 |
> |
assertTrue(q.offer(one)); |
867 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
868 |
> |
assertFalse(q.hasWaitingConsumer()); |
869 |
> |
|
870 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
871 |
|
} |
872 |
|
|
873 |
|
/** |
878 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
879 |
|
q.transfer(null); |
880 |
|
shouldThrow(); |
881 |
< |
} catch (NullPointerException ex) { |
839 |
< |
} |
881 |
> |
} catch (NullPointerException success) {} |
882 |
|
} |
883 |
|
|
884 |
|
/** |
888 |
|
public void testTransfer2() throws InterruptedException { |
889 |
|
final LinkedTransferQueue<Integer> q |
890 |
|
= new LinkedTransferQueue<Integer>(); |
891 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
892 |
|
|
893 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
894 |
< |
void realRun() throws InterruptedException { |
895 |
< |
q.transfer(SIZE); |
896 |
< |
threadAssertTrue(q.isEmpty()); |
894 |
> |
public void realRun() throws InterruptedException { |
895 |
> |
threadStarted.countDown(); |
896 |
> |
q.transfer(five); |
897 |
> |
checkEmpty(q); |
898 |
|
}}); |
899 |
|
|
900 |
< |
Thread.sleep(SHORT_DELAY_MS); |
900 |
> |
threadStarted.await(); |
901 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
902 |
|
assertEquals(1, q.size()); |
903 |
< |
assertEquals(SIZE, (int) q.poll()); |
904 |
< |
assertTrue(q.isEmpty()); |
905 |
< |
t.join(); |
903 |
> |
assertSame(five, q.poll()); |
904 |
> |
checkEmpty(q); |
905 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
906 |
|
} |
907 |
|
|
908 |
|
/** |
913 |
|
= new LinkedTransferQueue<Integer>(); |
914 |
|
|
915 |
|
Thread first = newStartedThread(new CheckedRunnable() { |
916 |
< |
void realRun() throws InterruptedException { |
917 |
< |
Integer i = SIZE + 1; |
918 |
< |
q.transfer(i); |
919 |
< |
threadAssertTrue(!q.contains(i)); |
875 |
< |
threadAssertEquals(1, q.size()); |
916 |
> |
public void realRun() throws InterruptedException { |
917 |
> |
q.transfer(four); |
918 |
> |
assertTrue(!q.contains(four)); |
919 |
> |
assertEquals(1, q.size()); |
920 |
|
}}); |
921 |
|
|
922 |
|
Thread interruptedThread = newStartedThread( |
923 |
|
new CheckedInterruptedRunnable() { |
924 |
< |
void realRun() throws InterruptedException { |
925 |
< |
while (q.size() == 0) |
924 |
> |
public void realRun() throws InterruptedException { |
925 |
> |
while (q.isEmpty()) |
926 |
|
Thread.yield(); |
927 |
< |
q.transfer(SIZE); |
927 |
> |
q.transfer(five); |
928 |
|
}}); |
929 |
|
|
930 |
|
while (q.size() < 2) |
931 |
|
Thread.yield(); |
932 |
|
assertEquals(2, q.size()); |
933 |
< |
assertEquals(SIZE + 1, (int) q.poll()); |
933 |
> |
assertSame(four, q.poll()); |
934 |
|
first.join(); |
935 |
|
assertEquals(1, q.size()); |
936 |
|
interruptedThread.interrupt(); |
937 |
|
interruptedThread.join(); |
938 |
< |
assertEquals(0, q.size()); |
895 |
< |
assertTrue(q.isEmpty()); |
938 |
> |
checkEmpty(q); |
939 |
|
} |
940 |
|
|
941 |
|
/** |
946 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
947 |
|
|
948 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
949 |
< |
void realRun() throws InterruptedException { |
949 |
> |
public void realRun() throws InterruptedException { |
950 |
|
q.transfer(four); |
951 |
< |
threadAssertFalse(q.contains(four)); |
952 |
< |
threadAssertEquals(three, q.poll()); |
951 |
> |
assertFalse(q.contains(four)); |
952 |
> |
assertSame(three, q.poll()); |
953 |
|
}}); |
954 |
|
|
955 |
< |
Thread.sleep(SHORT_DELAY_MS); |
955 |
> |
while (q.isEmpty()) |
956 |
> |
Thread.yield(); |
957 |
> |
assertFalse(q.isEmpty()); |
958 |
> |
assertEquals(1, q.size()); |
959 |
|
assertTrue(q.offer(three)); |
960 |
< |
assertEquals(four, q.poll()); |
961 |
< |
t.join(); |
960 |
> |
assertSame(four, q.poll()); |
961 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
962 |
|
} |
963 |
|
|
964 |
|
/** |
970 |
|
= new LinkedTransferQueue<Integer>(); |
971 |
|
|
972 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
973 |
< |
void realRun() throws InterruptedException { |
974 |
< |
q.transfer(SIZE); |
975 |
< |
threadAssertTrue(q.isEmpty()); |
973 |
> |
public void realRun() throws InterruptedException { |
974 |
> |
q.transfer(four); |
975 |
> |
checkEmpty(q); |
976 |
|
}}); |
977 |
|
|
978 |
< |
Thread.sleep(SHORT_DELAY_MS); |
979 |
< |
assertEquals(SIZE, (int) q.take()); |
980 |
< |
assertTrue(q.isEmpty()); |
981 |
< |
t.join(); |
978 |
> |
while (q.isEmpty()) |
979 |
> |
Thread.yield(); |
980 |
> |
assertFalse(q.isEmpty()); |
981 |
> |
assertEquals(1, q.size()); |
982 |
> |
assertSame(four, q.take()); |
983 |
> |
checkEmpty(q); |
984 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
985 |
|
} |
986 |
|
|
987 |
|
/** |
992 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
993 |
|
q.tryTransfer(null); |
994 |
|
shouldThrow(); |
995 |
< |
} catch (NullPointerException ex) { |
947 |
< |
} |
995 |
> |
} catch (NullPointerException success) {} |
996 |
|
} |
997 |
|
|
998 |
|
/** |
999 |
|
* tryTransfer returns false and does not enqueue if there are no |
1000 |
|
* consumers waiting to poll or take. |
1001 |
|
*/ |
1002 |
< |
public void testTryTransfer2() { |
1002 |
> |
public void testTryTransfer2() throws InterruptedException { |
1003 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1004 |
|
assertFalse(q.tryTransfer(new Object())); |
1005 |
|
assertFalse(q.hasWaitingConsumer()); |
1006 |
< |
assertTrue(q.isEmpty()); |
959 |
< |
assertEquals(0, q.size()); |
1006 |
> |
checkEmpty(q); |
1007 |
|
} |
1008 |
|
|
1009 |
|
/** |
1015 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1016 |
|
|
1017 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1018 |
< |
void realRun() { |
1018 |
> |
public void realRun() { |
1019 |
|
while (! q.hasWaitingConsumer()) |
1020 |
|
Thread.yield(); |
1021 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1022 |
< |
threadAssertTrue(q.isEmpty()); |
1023 |
< |
threadAssertTrue(q.size() == 0); |
977 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1021 |
> |
assertTrue(q.hasWaitingConsumer()); |
1022 |
> |
checkEmpty(q); |
1023 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
1024 |
|
}}); |
1025 |
|
|
1026 |
< |
assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); |
1027 |
< |
assertTrue(q.isEmpty()); |
1028 |
< |
t.join(); |
1026 |
> |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1027 |
> |
checkEmpty(q); |
1028 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1029 |
|
} |
1030 |
|
|
1031 |
|
/** |
1037 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1038 |
|
|
1039 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1040 |
< |
void realRun() { |
1040 |
> |
public void realRun() { |
1041 |
|
while (! q.hasWaitingConsumer()) |
1042 |
|
Thread.yield(); |
1043 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1044 |
< |
threadAssertTrue(q.isEmpty()); |
1045 |
< |
threadAssertTrue(q.size() == 0); |
1000 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1043 |
> |
assertTrue(q.hasWaitingConsumer()); |
1044 |
> |
checkEmpty(q); |
1045 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
1046 |
|
}}); |
1047 |
|
|
1048 |
< |
assertTrue(q.take() == hotPotato); |
1049 |
< |
assertTrue(q.isEmpty()); |
1050 |
< |
t.join(); |
1048 |
> |
assertSame(q.take(), hotPotato); |
1049 |
> |
checkEmpty(q); |
1050 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1051 |
|
} |
1052 |
|
|
1053 |
|
/** |
1054 |
< |
* tryTransfer waits the amount given if interrupted, and |
1055 |
< |
* throws interrupted exception |
1054 |
> |
* tryTransfer waits the amount given, and throws |
1055 |
> |
* InterruptedException when interrupted. |
1056 |
|
*/ |
1057 |
|
public void testTryTransfer5() throws InterruptedException { |
1058 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1059 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1060 |
+ |
assertTrue(q.isEmpty()); |
1061 |
|
|
1062 |
< |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1063 |
< |
void realRun() throws InterruptedException { |
1064 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1062 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1063 |
> |
public void realRun() throws InterruptedException { |
1064 |
> |
long t0 = System.nanoTime(); |
1065 |
> |
threadStarted.countDown(); |
1066 |
> |
try { |
1067 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1068 |
> |
shouldThrow(); |
1069 |
> |
} catch (InterruptedException success) {} |
1070 |
> |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1071 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
1072 |
|
}}); |
1073 |
|
|
1074 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1075 |
< |
toInterrupt.interrupt(); |
1076 |
< |
toInterrupt.join(); |
1074 |
> |
threadStarted.await(); |
1075 |
> |
while (q.isEmpty()) |
1076 |
> |
Thread.yield(); |
1077 |
> |
delay(SHORT_DELAY_MS); |
1078 |
> |
t.interrupt(); |
1079 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1080 |
> |
checkEmpty(q); |
1081 |
|
} |
1082 |
|
|
1083 |
|
/** |
1084 |
< |
* tryTransfer gives up after the timeout and return false |
1084 |
> |
* tryTransfer gives up after the timeout and returns false |
1085 |
|
*/ |
1086 |
|
public void testTryTransfer6() throws InterruptedException { |
1087 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1088 |
|
|
1089 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1090 |
< |
void realRun() throws InterruptedException { |
1091 |
< |
threadAssertFalse |
1092 |
< |
(q.tryTransfer(new Object(), |
1093 |
< |
SHORT_DELAY_MS, MILLISECONDS)); |
1090 |
> |
public void realRun() throws InterruptedException { |
1091 |
> |
long t0 = System.nanoTime(); |
1092 |
> |
assertFalse(q.tryTransfer(new Object(), |
1093 |
> |
SHORT_DELAY_MS, MILLISECONDS)); |
1094 |
> |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1095 |
> |
checkEmpty(q); |
1096 |
|
}}); |
1097 |
|
|
1098 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1099 |
< |
assertTrue(q.isEmpty()); |
1040 |
< |
t.join(); |
1098 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1099 |
> |
checkEmpty(q); |
1100 |
|
} |
1101 |
|
|
1102 |
|
/** |
1108 |
|
assertTrue(q.offer(four)); |
1109 |
|
|
1110 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1111 |
< |
void realRun() throws InterruptedException { |
1112 |
< |
threadAssertTrue(q.tryTransfer(five, |
1113 |
< |
MEDIUM_DELAY_MS, MILLISECONDS)); |
1055 |
< |
threadAssertTrue(q.isEmpty()); |
1111 |
> |
public void realRun() throws InterruptedException { |
1112 |
> |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1113 |
> |
checkEmpty(q); |
1114 |
|
}}); |
1115 |
|
|
1116 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1116 |
> |
while (q.size() != 2) |
1117 |
> |
Thread.yield(); |
1118 |
|
assertEquals(2, q.size()); |
1119 |
< |
assertEquals(four, q.poll()); |
1120 |
< |
assertEquals(five, q.poll()); |
1121 |
< |
assertTrue(q.isEmpty()); |
1122 |
< |
t.join(); |
1119 |
> |
assertSame(four, q.poll()); |
1120 |
> |
assertSame(five, q.poll()); |
1121 |
> |
checkEmpty(q); |
1122 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1123 |
|
} |
1124 |
|
|
1125 |
|
/** |
1126 |
< |
* tryTransfer attempts to enqueue into the q and fails returning |
1127 |
< |
* false not enqueueing and the successive poll is null |
1126 |
> |
* tryTransfer attempts to enqueue into the queue and fails |
1127 |
> |
* returning false not enqueueing and the successive poll is null |
1128 |
|
*/ |
1129 |
|
public void testTryTransfer8() throws InterruptedException { |
1130 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1131 |
|
assertTrue(q.offer(four)); |
1132 |
|
assertEquals(1, q.size()); |
1133 |
+ |
long t0 = System.nanoTime(); |
1134 |
|
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1135 |
+ |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1136 |
|
assertEquals(1, q.size()); |
1137 |
< |
assertEquals(four, q.poll()); |
1077 |
< |
threadAssertTrue(q.isEmpty()); |
1137 |
> |
assertSame(four, q.poll()); |
1138 |
|
assertNull(q.poll()); |
1139 |
+ |
checkEmpty(q); |
1140 |
|
} |
1141 |
|
|
1142 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1143 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1144 |
< |
assertTrue(q.isEmpty()); |
1144 |
> |
checkEmpty(q); |
1145 |
|
for (int i = 0; i < n; i++) { |
1146 |
|
assertEquals(i, q.size()); |
1147 |
|
assertTrue(q.offer(i)); |
1150 |
|
assertFalse(q.isEmpty()); |
1151 |
|
return q; |
1152 |
|
} |
1092 |
– |
|
1093 |
– |
private static class ConsumerObserver { |
1094 |
– |
|
1095 |
– |
private int waitingConsumers; |
1096 |
– |
|
1097 |
– |
private ConsumerObserver() { |
1098 |
– |
} |
1099 |
– |
|
1100 |
– |
private void setWaitingConsumer(int i) { |
1101 |
– |
this.waitingConsumers = i; |
1102 |
– |
} |
1103 |
– |
|
1104 |
– |
private int getWaitingConsumers() { |
1105 |
– |
return waitingConsumers; |
1106 |
– |
} |
1107 |
– |
} |
1153 |
|
} |