13 |
|
import java.io.ObjectOutputStream; |
14 |
|
import java.util.ArrayList; |
15 |
|
import java.util.Arrays; |
16 |
– |
import java.util.ConcurrentModificationException; |
16 |
|
import java.util.Iterator; |
17 |
+ |
import java.util.List; |
18 |
|
import java.util.NoSuchElementException; |
19 |
|
import java.util.concurrent.*; |
20 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
21 |
|
import junit.framework.Test; |
22 |
|
import junit.framework.TestSuite; |
23 |
|
|
24 |
+ |
@SuppressWarnings({"unchecked", "rawtypes"}) |
25 |
|
public class LinkedTransferQueueTest extends JSR166TestCase { |
26 |
|
|
27 |
+ |
public static class Generic extends BlockingQueueTest { |
28 |
+ |
protected BlockingQueue emptyCollection() { |
29 |
+ |
return new LinkedTransferQueue(); |
30 |
+ |
} |
31 |
+ |
} |
32 |
+ |
|
33 |
|
public static void main(String[] args) { |
34 |
|
junit.textui.TestRunner.run(suite()); |
35 |
|
} |
36 |
|
|
37 |
|
public static Test suite() { |
38 |
< |
return new TestSuite(LinkedTransferQueueTest.class); |
38 |
> |
return newTestSuite(LinkedTransferQueueTest.class, |
39 |
> |
new Generic().testSuite()); |
40 |
> |
} |
41 |
> |
|
42 |
> |
void checkEmpty(LinkedTransferQueue q) throws InterruptedException { |
43 |
> |
assertTrue(q.isEmpty()); |
44 |
> |
assertEquals(0, q.size()); |
45 |
> |
assertNull(q.peek()); |
46 |
> |
assertNull(q.poll()); |
47 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
48 |
> |
assertEquals(q.toString(), "[]"); |
49 |
> |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
50 |
> |
assertFalse(q.iterator().hasNext()); |
51 |
> |
try { |
52 |
> |
q.element(); |
53 |
> |
shouldThrow(); |
54 |
> |
} catch (NoSuchElementException success) {} |
55 |
> |
try { |
56 |
> |
q.iterator().next(); |
57 |
> |
shouldThrow(); |
58 |
> |
} catch (NoSuchElementException success) {} |
59 |
> |
try { |
60 |
> |
q.remove(); |
61 |
> |
shouldThrow(); |
62 |
> |
} catch (NoSuchElementException success) {} |
63 |
|
} |
64 |
|
|
65 |
|
/** |
66 |
< |
* Constructor builds new queue with size being zero and empty being true |
66 |
> |
* Constructor builds new queue with size being zero and empty |
67 |
> |
* being true |
68 |
|
*/ |
69 |
|
public void testConstructor1() { |
70 |
|
assertEquals(0, new LinkedTransferQueue().size()); |
72 |
|
} |
73 |
|
|
74 |
|
/** |
75 |
< |
* Initializing constructor with null collection throws NullPointerException |
75 |
> |
* Initializing constructor with null collection throws |
76 |
> |
* NullPointerException |
77 |
|
*/ |
78 |
|
public void testConstructor2() { |
79 |
|
try { |
80 |
|
new LinkedTransferQueue(null); |
81 |
|
shouldThrow(); |
82 |
< |
} catch (NullPointerException success) { |
49 |
< |
} |
82 |
> |
} catch (NullPointerException success) {} |
83 |
|
} |
84 |
|
|
85 |
|
/** |
86 |
< |
* Initializing from Collection of null elements throws NullPointerException |
86 |
> |
* Initializing from Collection of null elements throws |
87 |
> |
* NullPointerException |
88 |
|
*/ |
89 |
|
public void testConstructor3() { |
90 |
|
try { |
91 |
|
Integer[] ints = new Integer[SIZE]; |
92 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
92 |
> |
new LinkedTransferQueue(Arrays.asList(ints)); |
93 |
|
shouldThrow(); |
94 |
< |
} catch (NullPointerException success) { |
61 |
< |
} |
94 |
> |
} catch (NullPointerException success) {} |
95 |
|
} |
96 |
|
|
97 |
|
/** |
104 |
|
for (int i = 0; i < SIZE - 1; ++i) { |
105 |
|
ints[i] = i; |
106 |
|
} |
107 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
107 |
> |
new LinkedTransferQueue(Arrays.asList(ints)); |
108 |
|
shouldThrow(); |
109 |
< |
} catch (NullPointerException success) { |
77 |
< |
} |
109 |
> |
} catch (NullPointerException success) {} |
110 |
|
} |
111 |
|
|
112 |
|
/** |
113 |
|
* Queue contains all elements of the collection it is initialized by |
114 |
|
*/ |
115 |
|
public void testConstructor5() { |
116 |
< |
try { |
117 |
< |
Integer[] ints = new Integer[SIZE]; |
118 |
< |
for (int i = 0; i < SIZE; ++i) { |
119 |
< |
ints[i] = i; |
120 |
< |
} |
121 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
122 |
< |
for (int i = 0; i < SIZE; ++i) { |
123 |
< |
assertEquals(ints[i], q.poll()); |
124 |
< |
} |
125 |
< |
} finally { |
116 |
> |
Integer[] ints = new Integer[SIZE]; |
117 |
> |
for (int i = 0; i < SIZE; ++i) { |
118 |
> |
ints[i] = i; |
119 |
> |
} |
120 |
> |
List intList = Arrays.asList(ints); |
121 |
> |
LinkedTransferQueue q |
122 |
> |
= new LinkedTransferQueue(intList); |
123 |
> |
assertEquals(q.size(), intList.size()); |
124 |
> |
assertEquals(q.toString(), intList.toString()); |
125 |
> |
assertTrue(Arrays.equals(q.toArray(), |
126 |
> |
intList.toArray())); |
127 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[0]), |
128 |
> |
intList.toArray(new Object[0]))); |
129 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[SIZE]), |
130 |
> |
intList.toArray(new Object[SIZE]))); |
131 |
> |
for (int i = 0; i < SIZE; ++i) { |
132 |
> |
assertEquals(ints[i], q.poll()); |
133 |
|
} |
134 |
|
} |
135 |
|
|
136 |
|
/** |
137 |
< |
* Remaining capacity never decrease nor increase on add or remove |
137 |
> |
* remainingCapacity() always returns Integer.MAX_VALUE |
138 |
|
*/ |
139 |
|
public void testRemainingCapacity() { |
140 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
158 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
159 |
|
q.offer(null); |
160 |
|
shouldThrow(); |
161 |
< |
} catch (NullPointerException success) { |
123 |
< |
} |
161 |
> |
} catch (NullPointerException success) {} |
162 |
|
} |
163 |
|
|
164 |
|
/** |
169 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
170 |
|
q.add(null); |
171 |
|
shouldThrow(); |
172 |
< |
} catch (NullPointerException success) { |
135 |
< |
} |
172 |
> |
} catch (NullPointerException success) {} |
173 |
|
} |
174 |
|
|
175 |
|
/** |
180 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
181 |
|
q.addAll(null); |
182 |
|
shouldThrow(); |
183 |
< |
} catch (NullPointerException success) { |
147 |
< |
} |
183 |
> |
} catch (NullPointerException success) {} |
184 |
|
} |
185 |
|
|
186 |
|
/** |
191 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
192 |
|
q.addAll(q); |
193 |
|
shouldThrow(); |
194 |
< |
} catch (IllegalArgumentException success) { |
159 |
< |
} |
194 |
> |
} catch (IllegalArgumentException success) {} |
195 |
|
} |
196 |
|
|
197 |
|
/** |
203 |
|
Integer[] ints = new Integer[SIZE]; |
204 |
|
q.addAll(Arrays.asList(ints)); |
205 |
|
shouldThrow(); |
206 |
< |
} catch (NullPointerException success) { |
172 |
< |
} |
206 |
> |
} catch (NullPointerException success) {} |
207 |
|
} |
208 |
|
|
209 |
|
/** |
210 |
< |
* addAll of a collection with any null elements throws NullPointerException after |
211 |
< |
* possibly adding some elements |
210 |
> |
* addAll of a collection with any null elements throws |
211 |
> |
* NullPointerException after possibly adding some elements |
212 |
|
*/ |
213 |
|
public void testAddAll3() { |
214 |
|
try { |
219 |
|
} |
220 |
|
q.addAll(Arrays.asList(ints)); |
221 |
|
shouldThrow(); |
222 |
< |
} catch (NullPointerException success) { |
189 |
< |
} |
222 |
> |
} catch (NullPointerException success) {} |
223 |
|
} |
224 |
|
|
225 |
|
/** |
256 |
|
public void testPut() { |
257 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
258 |
|
for (int i = 0; i < SIZE; ++i) { |
259 |
+ |
assertEquals(q.size(), i); |
260 |
|
q.put(i); |
261 |
|
assertTrue(q.contains(i)); |
262 |
|
} |
229 |
– |
assertEquals(q.size(), SIZE); |
263 |
|
} |
264 |
|
|
265 |
|
/** |
277 |
|
*/ |
278 |
|
public void testTakeFromEmpty() throws InterruptedException { |
279 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
280 |
< |
Thread t = new Thread(new Runnable() { |
281 |
< |
public void run() { |
282 |
< |
try { |
250 |
< |
q.take(); |
251 |
< |
threadShouldThrow(); |
252 |
< |
} catch (InterruptedException success) { |
253 |
< |
} catch (Throwable ex) { |
254 |
< |
threadUnexpectedException(ex); |
255 |
< |
} |
280 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
281 |
> |
public void realRun() throws InterruptedException { |
282 |
> |
q.take(); |
283 |
|
}}); |
257 |
– |
t.start(); |
284 |
|
Thread.sleep(SHORT_DELAY_MS); |
285 |
|
t.interrupt(); |
286 |
|
t.join(); |
290 |
|
* Take removes existing elements until empty, then blocks interruptibly |
291 |
|
*/ |
292 |
|
public void testBlockingTake() throws InterruptedException { |
293 |
< |
Thread t = new Thread(new Runnable() { |
294 |
< |
public void run() { |
293 |
> |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
294 |
> |
Thread t = new Thread(new CheckedRunnable() { |
295 |
> |
public void realRun() throws InterruptedException { |
296 |
> |
for (int i = 0; i < SIZE; ++i) { |
297 |
> |
assertEquals(i, (int) q.take()); |
298 |
> |
} |
299 |
|
try { |
270 |
– |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
271 |
– |
for (int i = 0; i < SIZE; ++i) { |
272 |
– |
threadAssertEquals(i, (int) q.take()); |
273 |
– |
} |
300 |
|
q.take(); |
301 |
< |
threadShouldThrow(); |
302 |
< |
} catch (InterruptedException success) { |
277 |
< |
} catch (Throwable ex) { |
278 |
< |
threadUnexpectedException(ex); |
279 |
< |
} |
301 |
> |
shouldThrow(); |
302 |
> |
} catch (InterruptedException success) {} |
303 |
|
}}); |
304 |
+ |
|
305 |
|
t.start(); |
306 |
|
Thread.sleep(SHORT_DELAY_MS); |
307 |
|
t.interrupt(); |
308 |
|
t.join(); |
309 |
+ |
checkEmpty(q); |
310 |
|
} |
311 |
|
|
312 |
|
/** |
313 |
|
* poll succeeds unless empty |
314 |
|
*/ |
315 |
< |
public void testPoll() { |
315 |
> |
public void testPoll() throws InterruptedException { |
316 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
317 |
|
for (int i = 0; i < SIZE; ++i) { |
318 |
|
assertEquals(i, (int) q.poll()); |
319 |
|
} |
320 |
|
assertNull(q.poll()); |
321 |
+ |
checkEmpty(q); |
322 |
|
} |
323 |
|
|
324 |
|
/** |
327 |
|
public void testTimedPoll0() throws InterruptedException { |
328 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
329 |
|
for (int i = 0; i < SIZE; ++i) { |
330 |
< |
assertEquals(i, (int) q.poll(0, TimeUnit.MILLISECONDS)); |
330 |
> |
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
331 |
|
} |
332 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
332 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
333 |
> |
checkEmpty(q); |
334 |
|
} |
335 |
|
|
336 |
|
/** |
339 |
|
public void testTimedPoll() throws InterruptedException { |
340 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
341 |
|
for (int i = 0; i < SIZE; ++i) { |
342 |
< |
assertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
342 |
> |
long t0 = System.nanoTime(); |
343 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
344 |
> |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
345 |
> |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
346 |
|
} |
347 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
347 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
348 |
> |
checkEmpty(q); |
349 |
|
} |
350 |
|
|
351 |
|
/** |
353 |
|
* returning timeout status |
354 |
|
*/ |
355 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
356 |
< |
Thread t = new Thread(new Runnable() { |
357 |
< |
public void run() { |
358 |
< |
try { |
359 |
< |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
360 |
< |
for (int i = 0; i < SIZE; ++i) { |
361 |
< |
threadAssertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
362 |
< |
} |
363 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
333 |
< |
} catch (InterruptedException success) { |
334 |
< |
} catch (Throwable ex) { |
335 |
< |
threadUnexpectedException(ex); |
356 |
> |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
357 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
358 |
> |
public void realRun() throws InterruptedException { |
359 |
> |
for (int i = 0; i < SIZE; ++i) { |
360 |
> |
long t0 = System.nanoTime(); |
361 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
362 |
> |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
363 |
> |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
364 |
|
} |
337 |
– |
}}); |
338 |
– |
t.start(); |
339 |
– |
Thread.sleep(SHORT_DELAY_MS); |
340 |
– |
t.interrupt(); |
341 |
– |
t.join(); |
342 |
– |
} |
343 |
– |
|
344 |
– |
/** |
345 |
– |
* timed poll before a delayed offer fails; after offer succeeds; |
346 |
– |
* on interruption throws |
347 |
– |
*/ |
348 |
– |
public void testTimedPollWithOffer() throws InterruptedException { |
349 |
– |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
350 |
– |
Thread t = new Thread(new Runnable() { |
351 |
– |
public void run() { |
365 |
|
try { |
366 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
367 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
368 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
356 |
< |
threadShouldThrow(); |
357 |
< |
} catch (InterruptedException success) { |
358 |
< |
} catch (Throwable ex) { |
359 |
< |
threadUnexpectedException(ex); |
360 |
< |
} |
366 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
367 |
> |
shouldThrow(); |
368 |
> |
} catch (InterruptedException success) {} |
369 |
|
}}); |
370 |
< |
t.start(); |
370 |
> |
|
371 |
|
Thread.sleep(SMALL_DELAY_MS); |
364 |
– |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
372 |
|
t.interrupt(); |
373 |
|
t.join(); |
374 |
+ |
checkEmpty(q); |
375 |
|
} |
376 |
|
|
377 |
|
/** |
378 |
|
* peek returns next element, or null if empty |
379 |
|
*/ |
380 |
< |
public void testPeek() { |
380 |
> |
public void testPeek() throws InterruptedException { |
381 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
382 |
|
for (int i = 0; i < SIZE; ++i) { |
383 |
|
assertEquals(i, (int) q.peek()); |
386 |
|
i != (int) q.peek()); |
387 |
|
} |
388 |
|
assertNull(q.peek()); |
389 |
+ |
checkEmpty(q); |
390 |
|
} |
391 |
|
|
392 |
|
/** |
393 |
|
* element returns next element, or throws NoSuchElementException if empty |
394 |
|
*/ |
395 |
< |
public void testElement() { |
395 |
> |
public void testElement() throws InterruptedException { |
396 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
397 |
|
for (int i = 0; i < SIZE; ++i) { |
398 |
|
assertEquals(i, (int) q.element()); |
401 |
|
try { |
402 |
|
q.element(); |
403 |
|
shouldThrow(); |
404 |
< |
} catch (NoSuchElementException success) { |
405 |
< |
} |
404 |
> |
} catch (NoSuchElementException success) {} |
405 |
> |
checkEmpty(q); |
406 |
|
} |
407 |
|
|
408 |
|
/** |
409 |
|
* remove removes next element, or throws NoSuchElementException if empty |
410 |
|
*/ |
411 |
< |
public void testRemove() { |
411 |
> |
public void testRemove() throws InterruptedException { |
412 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
413 |
|
for (int i = 0; i < SIZE; ++i) { |
414 |
|
assertEquals(i, (int) q.remove()); |
416 |
|
try { |
417 |
|
q.remove(); |
418 |
|
shouldThrow(); |
419 |
< |
} catch (NoSuchElementException success) { |
420 |
< |
} |
419 |
> |
} catch (NoSuchElementException success) {} |
420 |
> |
checkEmpty(q); |
421 |
|
} |
422 |
|
|
423 |
|
/** |
424 |
|
* remove(x) removes x and returns true if present |
425 |
|
*/ |
426 |
< |
public void testRemoveElement() { |
426 |
> |
public void testRemoveElement() throws InterruptedException { |
427 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
428 |
|
for (int i = 1; i < SIZE; i += 2) { |
429 |
|
assertTrue(q.remove(i)); |
432 |
|
assertTrue(q.remove(i)); |
433 |
|
assertFalse(q.remove(i + 1)); |
434 |
|
} |
435 |
< |
assertTrue(q.isEmpty()); |
435 |
> |
checkEmpty(q); |
436 |
|
} |
437 |
|
|
438 |
|
/** |
445 |
|
assertTrue(q.remove(one)); |
446 |
|
assertTrue(q.remove(two)); |
447 |
|
assertTrue(q.add(three)); |
448 |
< |
assertTrue(q.take() != null); |
448 |
> |
assertSame(q.take(), three); |
449 |
|
} |
450 |
|
|
451 |
|
/** |
463 |
|
/** |
464 |
|
* clear removes all elements |
465 |
|
*/ |
466 |
< |
public void testClear() { |
466 |
> |
public void testClear() throws InterruptedException { |
467 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
468 |
|
q.clear(); |
469 |
< |
assertTrue(q.isEmpty()); |
461 |
< |
assertEquals(0, q.size()); |
469 |
> |
checkEmpty(q); |
470 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
471 |
|
q.add(one); |
472 |
|
assertFalse(q.isEmpty()); |
473 |
+ |
assertEquals(1, q.size()); |
474 |
|
assertTrue(q.contains(one)); |
475 |
|
q.clear(); |
476 |
< |
assertTrue(q.isEmpty()); |
476 |
> |
checkEmpty(q); |
477 |
|
} |
478 |
|
|
479 |
|
/** |
491 |
|
} |
492 |
|
|
493 |
|
/** |
494 |
< |
* retainAll(c) retains only those elements of c and reports true if changed |
494 |
> |
* retainAll(c) retains only those elements of c and reports true |
495 |
> |
* if changed |
496 |
|
*/ |
497 |
|
public void testRetainAll() { |
498 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
511 |
|
} |
512 |
|
|
513 |
|
/** |
514 |
< |
* removeAll(c) removes only those elements of c and reports true if changed |
514 |
> |
* removeAll(c) removes only those elements of c and reports true |
515 |
> |
* if changed |
516 |
|
*/ |
517 |
|
public void testRemoveAll() { |
518 |
|
for (int i = 1; i < SIZE; ++i) { |
527 |
|
} |
528 |
|
|
529 |
|
/** |
530 |
< |
* toArray contains all elements |
530 |
> |
* toArray() contains all elements |
531 |
|
*/ |
532 |
|
public void testToArray() throws InterruptedException { |
533 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
553 |
|
* toArray(null) throws NullPointerException |
554 |
|
*/ |
555 |
|
public void testToArray_BadArg() { |
556 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
557 |
|
try { |
546 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
558 |
|
Object o[] = q.toArray(null); |
559 |
|
shouldThrow(); |
560 |
< |
} catch (NullPointerException success) { |
550 |
< |
} |
560 |
> |
} catch (NullPointerException success) {} |
561 |
|
} |
562 |
|
|
563 |
|
/** |
564 |
< |
* toArray with incompatible array type throws CCE |
564 |
> |
* toArray(incompatible array type) throws CCE |
565 |
|
*/ |
566 |
|
public void testToArray1_BadArg() { |
567 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
568 |
|
try { |
558 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
569 |
|
Object o[] = q.toArray(new String[10]); |
570 |
|
shouldThrow(); |
571 |
< |
} catch (ArrayStoreException success) { |
562 |
< |
} |
571 |
> |
} catch (ArrayStoreException success) {} |
572 |
|
} |
573 |
|
|
574 |
|
/** |
577 |
|
public void testIterator() throws InterruptedException { |
578 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
579 |
|
Iterator it = q.iterator(); |
580 |
+ |
int i = 0; |
581 |
|
while (it.hasNext()) { |
582 |
< |
assertEquals(it.next(), q.take()); |
582 |
> |
assertEquals(it.next(), i++); |
583 |
|
} |
584 |
+ |
assertEquals(i, SIZE); |
585 |
|
} |
586 |
|
|
587 |
|
/** |
588 |
< |
* iterator.remove removes current element |
588 |
> |
* iterator.remove() removes current element |
589 |
|
*/ |
590 |
|
public void testIteratorRemove() { |
591 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
598 |
|
it.remove(); |
599 |
|
|
600 |
|
it = q.iterator(); |
601 |
< |
assertEquals(it.next(), one); |
602 |
< |
assertEquals(it.next(), three); |
601 |
> |
assertSame(it.next(), one); |
602 |
> |
assertSame(it.next(), three); |
603 |
|
assertFalse(it.hasNext()); |
604 |
|
} |
605 |
|
|
607 |
|
* iterator ordering is FIFO |
608 |
|
*/ |
609 |
|
public void testIteratorOrdering() { |
610 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
610 |
> |
final LinkedTransferQueue<Integer> q |
611 |
> |
= new LinkedTransferQueue<Integer>(); |
612 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
613 |
|
q.add(one); |
614 |
|
q.add(two); |
655 |
|
q.add(one); |
656 |
|
q.add(two); |
657 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
658 |
< |
executor.execute(new Runnable() { |
659 |
< |
public void run() { |
660 |
< |
try { |
661 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
650 |
< |
} catch (Throwable ex) { |
651 |
< |
threadUnexpectedException(ex); |
652 |
< |
} |
658 |
> |
|
659 |
> |
executor.execute(new CheckedRunnable() { |
660 |
> |
public void realRun() { |
661 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
662 |
|
}}); |
663 |
|
|
664 |
< |
executor.execute(new Runnable() { |
665 |
< |
public void run() { |
666 |
< |
try { |
667 |
< |
Thread.sleep(SMALL_DELAY_MS); |
659 |
< |
threadAssertEquals(one, q.take()); |
660 |
< |
} catch (Throwable ex) { |
661 |
< |
threadUnexpectedException(ex); |
662 |
< |
} |
664 |
> |
executor.execute(new CheckedRunnable() { |
665 |
> |
public void realRun() throws InterruptedException { |
666 |
> |
Thread.sleep(SMALL_DELAY_MS); |
667 |
> |
assertSame(one, q.take()); |
668 |
|
}}); |
669 |
|
|
670 |
|
joinPool(executor); |
671 |
|
} |
672 |
|
|
673 |
|
/** |
674 |
< |
* poll retrieves elements across Executor threads |
674 |
> |
* timed poll retrieves elements across Executor threads |
675 |
|
*/ |
676 |
|
public void testPollInExecutor() { |
677 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
678 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
679 |
< |
executor.execute(new Runnable() { |
680 |
< |
public void run() { |
681 |
< |
try { |
682 |
< |
threadAssertNull(q.poll()); |
683 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
684 |
< |
threadAssertTrue(q.isEmpty()); |
680 |
< |
} catch (Throwable ex) { |
681 |
< |
threadUnexpectedException(ex); |
682 |
< |
} |
679 |
> |
|
680 |
> |
executor.execute(new CheckedRunnable() { |
681 |
> |
public void realRun() throws InterruptedException { |
682 |
> |
assertNull(q.poll()); |
683 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
684 |
> |
assertTrue(q.isEmpty()); |
685 |
|
}}); |
686 |
|
|
687 |
< |
executor.execute(new Runnable() { |
688 |
< |
public void run() { |
689 |
< |
try { |
690 |
< |
Thread.sleep(SMALL_DELAY_MS); |
689 |
< |
q.put(one); |
690 |
< |
} catch (Throwable ex) { |
691 |
< |
threadUnexpectedException(ex); |
692 |
< |
} |
687 |
> |
executor.execute(new CheckedRunnable() { |
688 |
> |
public void realRun() throws InterruptedException { |
689 |
> |
Thread.sleep(SMALL_DELAY_MS); |
690 |
> |
q.put(one); |
691 |
|
}}); |
692 |
|
|
693 |
|
joinPool(executor); |
700 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
701 |
|
|
702 |
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
703 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
703 |
> |
ObjectOutputStream out |
704 |
> |
= new ObjectOutputStream(new BufferedOutputStream(bout)); |
705 |
|
out.writeObject(q); |
706 |
|
out.close(); |
707 |
|
|
708 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
709 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
708 |
> |
ByteArrayInputStream bin |
709 |
> |
= new ByteArrayInputStream(bout.toByteArray()); |
710 |
> |
ObjectInputStream in |
711 |
> |
= new ObjectInputStream(new BufferedInputStream(bin)); |
712 |
|
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
713 |
|
|
714 |
|
assertEquals(q.size(), r.size()); |
725 |
|
try { |
726 |
|
q.drainTo(null); |
727 |
|
shouldThrow(); |
728 |
< |
} catch (NullPointerException success) { |
728 |
< |
} |
728 |
> |
} catch (NullPointerException success) {} |
729 |
|
} |
730 |
|
|
731 |
|
/** |
736 |
|
try { |
737 |
|
q.drainTo(q); |
738 |
|
shouldThrow(); |
739 |
< |
} catch (IllegalArgumentException success) { |
740 |
< |
} |
739 |
> |
} catch (IllegalArgumentException success) {} |
740 |
|
} |
741 |
|
|
742 |
|
/** |
766 |
|
} |
767 |
|
|
768 |
|
/** |
769 |
< |
* drainTo empties full queue, unblocking a waiting put. |
769 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
770 |
|
*/ |
771 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
772 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
773 |
< |
Thread t = new Thread(new Runnable() { |
774 |
< |
public void run() { |
775 |
< |
try { |
777 |
< |
q.put(SIZE + 1); |
778 |
< |
} catch (Throwable ex) { |
779 |
< |
threadUnexpectedException(ex); |
780 |
< |
} |
773 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
774 |
> |
public void realRun() { |
775 |
> |
q.put(SIZE + 1); |
776 |
|
}}); |
782 |
– |
t.start(); |
777 |
|
ArrayList l = new ArrayList(); |
778 |
|
q.drainTo(l); |
779 |
|
assertTrue(l.size() >= SIZE); |
790 |
|
public void testDrainToNullN() { |
791 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
792 |
|
try { |
793 |
< |
q.drainTo(null, 0); |
793 |
> |
q.drainTo(null, SIZE); |
794 |
|
shouldThrow(); |
795 |
< |
} catch (NullPointerException success) { |
802 |
< |
} |
795 |
> |
} catch (NullPointerException success) {} |
796 |
|
} |
797 |
|
|
798 |
|
/** |
801 |
|
public void testDrainToSelfN() { |
802 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
803 |
|
try { |
804 |
< |
q.drainTo(q, 0); |
804 |
> |
q.drainTo(q, SIZE); |
805 |
|
shouldThrow(); |
806 |
< |
} catch (IllegalArgumentException success) { |
814 |
< |
} |
806 |
> |
} catch (IllegalArgumentException success) {} |
807 |
|
} |
808 |
|
|
809 |
|
/** |
829 |
|
} |
830 |
|
|
831 |
|
/** |
832 |
< |
* poll and take decrement the waiting consumer count |
832 |
> |
* timed poll() or take() increments the waiting consumer count; |
833 |
> |
* offer(e) decrements the waiting consumer count |
834 |
|
*/ |
835 |
|
public void testWaitingConsumer() throws InterruptedException { |
836 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
837 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
838 |
< |
new Thread(new Runnable() { |
839 |
< |
public void run() { |
840 |
< |
try { |
841 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
842 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
843 |
< |
threadAssertTrue(q.offer(new Object())); |
844 |
< |
} catch (Throwable ex) { |
845 |
< |
threadUnexpectedException(ex); |
846 |
< |
} |
847 |
< |
}}).start(); |
848 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
849 |
< |
assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers()); |
837 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
838 |
> |
assertFalse(q.hasWaitingConsumer()); |
839 |
> |
|
840 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
841 |
> |
public void realRun() throws InterruptedException { |
842 |
> |
Thread.sleep(SMALL_DELAY_MS); |
843 |
> |
assertTrue(q.hasWaitingConsumer()); |
844 |
> |
assertEquals(q.getWaitingConsumerCount(), 1); |
845 |
> |
assertTrue(q.offer(one)); |
846 |
> |
assertFalse(q.hasWaitingConsumer()); |
847 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
848 |
> |
}}); |
849 |
> |
|
850 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
851 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
852 |
> |
assertFalse(q.hasWaitingConsumer()); |
853 |
> |
t.join(); |
854 |
|
} |
855 |
|
|
856 |
|
/** |
861 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
862 |
|
q.transfer(null); |
863 |
|
shouldThrow(); |
864 |
< |
} catch (NullPointerException ex) { |
868 |
< |
} |
864 |
> |
} catch (NullPointerException success) {} |
865 |
|
} |
866 |
|
|
867 |
|
/** |
869 |
|
* is returned by this associated poll. |
870 |
|
*/ |
871 |
|
public void testTransfer2() throws InterruptedException { |
872 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
872 |
> |
final LinkedTransferQueue<Integer> q |
873 |
> |
= new LinkedTransferQueue<Integer>(); |
874 |
|
|
875 |
< |
new Thread(new Runnable() { |
876 |
< |
public void run() { |
877 |
< |
try { |
878 |
< |
q.transfer(SIZE); |
879 |
< |
threadAssertTrue(q.isEmpty()); |
883 |
< |
} catch (Throwable ex) { |
884 |
< |
threadUnexpectedException(ex); |
885 |
< |
} |
886 |
< |
}}).start(); |
875 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
876 |
> |
public void realRun() throws InterruptedException { |
877 |
> |
q.transfer(SIZE); |
878 |
> |
assertTrue(q.isEmpty()); |
879 |
> |
}}); |
880 |
|
|
881 |
|
Thread.sleep(SHORT_DELAY_MS); |
882 |
|
assertEquals(1, q.size()); |
883 |
|
assertEquals(SIZE, (int) q.poll()); |
884 |
|
assertTrue(q.isEmpty()); |
885 |
+ |
t.join(); |
886 |
|
} |
887 |
|
|
888 |
|
/** |
889 |
|
* transfer waits until a poll occurs, and then transfers in fifo order |
890 |
|
*/ |
891 |
|
public void testTransfer3() throws InterruptedException { |
892 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
892 |
> |
final LinkedTransferQueue<Integer> q |
893 |
> |
= new LinkedTransferQueue<Integer>(); |
894 |
|
|
895 |
< |
Thread first = |
896 |
< |
new Thread(new Runnable() { |
897 |
< |
public void run() { |
898 |
< |
try { |
899 |
< |
Integer i = SIZE + 1; |
900 |
< |
q.transfer(i); |
901 |
< |
threadAssertTrue(!q.contains(i)); |
907 |
< |
threadAssertEquals(1, q.size()); |
908 |
< |
} catch (Throwable ex) { |
909 |
< |
threadUnexpectedException(ex); |
910 |
< |
} |
911 |
< |
}}); |
912 |
< |
first.start(); |
895 |
> |
Thread first = newStartedThread(new CheckedRunnable() { |
896 |
> |
public void realRun() throws InterruptedException { |
897 |
> |
Integer i = SIZE + 1; |
898 |
> |
q.transfer(i); |
899 |
> |
assertTrue(!q.contains(i)); |
900 |
> |
assertEquals(1, q.size()); |
901 |
> |
}}); |
902 |
|
|
903 |
< |
Thread interruptedThread = |
904 |
< |
new Thread(new Runnable() { |
905 |
< |
public void run() { |
906 |
< |
try { |
907 |
< |
while (q.size() == 0) |
908 |
< |
Thread.yield(); |
920 |
< |
q.transfer(SIZE); |
921 |
< |
threadShouldThrow(); |
922 |
< |
} catch (InterruptedException success) { |
923 |
< |
} catch (Throwable ex) { |
924 |
< |
threadUnexpectedException(ex); |
925 |
< |
} |
903 |
> |
Thread interruptedThread = newStartedThread( |
904 |
> |
new CheckedInterruptedRunnable() { |
905 |
> |
public void realRun() throws InterruptedException { |
906 |
> |
while (q.size() == 0) |
907 |
> |
Thread.yield(); |
908 |
> |
q.transfer(SIZE); |
909 |
|
}}); |
927 |
– |
interruptedThread.start(); |
910 |
|
|
911 |
|
while (q.size() < 2) |
912 |
|
Thread.yield(); |
926 |
|
*/ |
927 |
|
public void testTransfer4() throws InterruptedException { |
928 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
929 |
< |
new Thread(new Runnable() { |
930 |
< |
public void run() { |
931 |
< |
try { |
932 |
< |
q.transfer(four); |
933 |
< |
threadAssertFalse(q.contains(four)); |
934 |
< |
threadAssertEquals(three, q.poll()); |
935 |
< |
} catch (Throwable ex) { |
936 |
< |
threadUnexpectedException(ex); |
937 |
< |
} |
956 |
< |
}}).start(); |
957 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
929 |
> |
|
930 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
931 |
> |
public void realRun() throws InterruptedException { |
932 |
> |
q.transfer(four); |
933 |
> |
assertFalse(q.contains(four)); |
934 |
> |
assertSame(three, q.poll()); |
935 |
> |
}}); |
936 |
> |
|
937 |
> |
Thread.sleep(SHORT_DELAY_MS); |
938 |
|
assertTrue(q.offer(three)); |
939 |
< |
assertEquals(four, q.poll()); |
939 |
> |
assertSame(four, q.poll()); |
940 |
> |
t.join(); |
941 |
|
} |
942 |
|
|
943 |
|
/** |
945 |
|
* is returned by this associated take. |
946 |
|
*/ |
947 |
|
public void testTransfer5() throws InterruptedException { |
948 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
948 |
> |
final LinkedTransferQueue<Integer> q |
949 |
> |
= new LinkedTransferQueue<Integer>(); |
950 |
|
|
951 |
< |
new Thread(new Runnable() { |
952 |
< |
public void run() { |
953 |
< |
try { |
954 |
< |
q.transfer(SIZE); |
955 |
< |
threadAssertTrue(q.isEmpty()); |
974 |
< |
} catch (Throwable ex) { |
975 |
< |
threadUnexpectedException(ex); |
976 |
< |
} |
977 |
< |
}}).start(); |
951 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
952 |
> |
public void realRun() throws InterruptedException { |
953 |
> |
q.transfer(SIZE); |
954 |
> |
checkEmpty(q); |
955 |
> |
}}); |
956 |
|
|
957 |
|
Thread.sleep(SHORT_DELAY_MS); |
958 |
|
assertEquals(SIZE, (int) q.take()); |
959 |
< |
assertTrue(q.isEmpty()); |
959 |
> |
checkEmpty(q); |
960 |
> |
t.join(); |
961 |
|
} |
962 |
|
|
963 |
|
/** |
967 |
|
try { |
968 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
969 |
|
q.tryTransfer(null); |
970 |
< |
this.shouldThrow(); |
971 |
< |
} catch (NullPointerException ex) { |
993 |
< |
} |
970 |
> |
shouldThrow(); |
971 |
> |
} catch (NullPointerException success) {} |
972 |
|
} |
973 |
|
|
974 |
|
/** |
975 |
|
* tryTransfer returns false and does not enqueue if there are no |
976 |
|
* consumers waiting to poll or take. |
977 |
|
*/ |
978 |
< |
public void testTryTransfer2() { |
978 |
> |
public void testTryTransfer2() throws InterruptedException { |
979 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
980 |
|
assertFalse(q.tryTransfer(new Object())); |
981 |
|
assertFalse(q.hasWaitingConsumer()); |
982 |
< |
assertTrue(q.isEmpty()); |
1005 |
< |
assertEquals(0, q.size()); |
982 |
> |
checkEmpty(q); |
983 |
|
} |
984 |
|
|
985 |
|
/** |
989 |
|
public void testTryTransfer3() throws InterruptedException { |
990 |
|
final Object hotPotato = new Object(); |
991 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
992 |
< |
new Thread(new Runnable() { |
993 |
< |
public void run() { |
994 |
< |
try { |
995 |
< |
while (! q.hasWaitingConsumer()) |
996 |
< |
Thread.yield(); |
997 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
998 |
< |
threadAssertTrue(q.isEmpty()); |
999 |
< |
threadAssertTrue(q.size() == 0); |
1000 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1001 |
< |
} catch (Throwable ex) { |
1002 |
< |
threadUnexpectedException(ex); |
1003 |
< |
} |
1004 |
< |
}}).start(); |
1005 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) == hotPotato); |
1029 |
< |
assertTrue(q.isEmpty()); |
992 |
> |
|
993 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
994 |
> |
public void realRun() { |
995 |
> |
while (! q.hasWaitingConsumer()) |
996 |
> |
Thread.yield(); |
997 |
> |
assertTrue(q.hasWaitingConsumer()); |
998 |
> |
assertTrue(q.isEmpty()); |
999 |
> |
assertEquals(q.size(), 0); |
1000 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
1001 |
> |
}}); |
1002 |
> |
|
1003 |
> |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1004 |
> |
checkEmpty(q); |
1005 |
> |
t.join(); |
1006 |
|
} |
1007 |
|
|
1008 |
|
/** |
1012 |
|
public void testTryTransfer4() throws InterruptedException { |
1013 |
|
final Object hotPotato = new Object(); |
1014 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1015 |
< |
new Thread(new Runnable() { |
1016 |
< |
public void run() { |
1017 |
< |
try { |
1018 |
< |
while (! q.hasWaitingConsumer()) |
1019 |
< |
Thread.yield(); |
1020 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1021 |
< |
threadAssertTrue(q.isEmpty()); |
1022 |
< |
threadAssertTrue(q.size() == 0); |
1023 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1024 |
< |
} catch (Throwable ex) { |
1025 |
< |
threadUnexpectedException(ex); |
1026 |
< |
} |
1027 |
< |
}}).start(); |
1028 |
< |
assertTrue(q.take() == hotPotato); |
1053 |
< |
assertTrue(q.isEmpty()); |
1015 |
> |
|
1016 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1017 |
> |
public void realRun() { |
1018 |
> |
while (! q.hasWaitingConsumer()) |
1019 |
> |
Thread.yield(); |
1020 |
> |
assertTrue(q.hasWaitingConsumer()); |
1021 |
> |
assertTrue(q.isEmpty()); |
1022 |
> |
assertEquals(q.size(), 0); |
1023 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
1024 |
> |
}}); |
1025 |
> |
|
1026 |
> |
assertSame(q.take(), hotPotato); |
1027 |
> |
checkEmpty(q); |
1028 |
> |
t.join(); |
1029 |
|
} |
1030 |
|
|
1031 |
|
/** |
1034 |
|
*/ |
1035 |
|
public void testTryTransfer5() throws InterruptedException { |
1036 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1037 |
< |
Thread toInterrupt = new Thread(new Runnable() { |
1038 |
< |
public void run() { |
1039 |
< |
try { |
1040 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1066 |
< |
threadShouldThrow(); |
1067 |
< |
} catch (InterruptedException success) { |
1068 |
< |
} catch (Throwable ex) { |
1069 |
< |
threadUnexpectedException(ex); |
1070 |
< |
} |
1037 |
> |
|
1038 |
> |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1039 |
> |
public void realRun() throws InterruptedException { |
1040 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1041 |
|
}}); |
1042 |
< |
toInterrupt.start(); |
1042 |
> |
|
1043 |
|
Thread.sleep(SMALL_DELAY_MS); |
1044 |
|
toInterrupt.interrupt(); |
1045 |
+ |
toInterrupt.join(); |
1046 |
|
} |
1047 |
|
|
1048 |
|
/** |
1050 |
|
*/ |
1051 |
|
public void testTryTransfer6() throws InterruptedException { |
1052 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1053 |
< |
new Thread(new Runnable() { |
1054 |
< |
public void run() { |
1055 |
< |
try { |
1056 |
< |
threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1057 |
< |
} catch (Throwable ex) { |
1058 |
< |
threadUnexpectedException(ex); |
1059 |
< |
} |
1060 |
< |
}}).start(); |
1061 |
< |
Thread.sleep(LONG_DELAY_MS); |
1062 |
< |
assertTrue(q.isEmpty()); |
1053 |
> |
|
1054 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1055 |
> |
public void realRun() throws InterruptedException { |
1056 |
> |
assertFalse(q.tryTransfer(new Object(), |
1057 |
> |
SHORT_DELAY_MS, MILLISECONDS)); |
1058 |
> |
}}); |
1059 |
> |
|
1060 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1061 |
> |
checkEmpty(q); |
1062 |
> |
t.join(); |
1063 |
|
} |
1064 |
|
|
1065 |
|
/** |
1069 |
|
public void testTryTransfer7() throws InterruptedException { |
1070 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1071 |
|
assertTrue(q.offer(four)); |
1072 |
< |
new Thread(new Runnable() { |
1073 |
< |
public void run() { |
1074 |
< |
try { |
1075 |
< |
threadAssertTrue(q.tryTransfer(five, LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
1076 |
< |
threadAssertTrue(q.isEmpty()); |
1077 |
< |
} catch (Throwable ex) { |
1078 |
< |
threadUnexpectedException(ex); |
1108 |
< |
} |
1109 |
< |
}}).start(); |
1072 |
> |
|
1073 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1074 |
> |
public void realRun() throws InterruptedException { |
1075 |
> |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1076 |
> |
assertTrue(q.isEmpty()); |
1077 |
> |
}}); |
1078 |
> |
|
1079 |
|
Thread.sleep(SHORT_DELAY_MS); |
1080 |
|
assertEquals(2, q.size()); |
1081 |
< |
assertEquals(four, q.poll()); |
1082 |
< |
assertEquals(five, q.poll()); |
1083 |
< |
assertTrue(q.isEmpty()); |
1081 |
> |
assertSame(four, q.poll()); |
1082 |
> |
assertSame(five, q.poll()); |
1083 |
> |
checkEmpty(q); |
1084 |
> |
t.join(); |
1085 |
|
} |
1086 |
|
|
1087 |
|
/** |
1092 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1093 |
|
assertTrue(q.offer(four)); |
1094 |
|
assertEquals(1, q.size()); |
1095 |
< |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1095 |
> |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1096 |
|
assertEquals(1, q.size()); |
1097 |
< |
assertEquals(four, q.poll()); |
1128 |
< |
threadAssertTrue(q.isEmpty()); |
1097 |
> |
assertSame(four, q.poll()); |
1098 |
|
assertNull(q.poll()); |
1099 |
+ |
checkEmpty(q); |
1100 |
|
} |
1101 |
|
|
1102 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1110 |
|
assertFalse(q.isEmpty()); |
1111 |
|
return q; |
1112 |
|
} |
1143 |
– |
|
1144 |
– |
private static class ConsumerObserver { |
1145 |
– |
|
1146 |
– |
private int waitingConsumers; |
1147 |
– |
|
1148 |
– |
private ConsumerObserver() { |
1149 |
– |
} |
1150 |
– |
|
1151 |
– |
private void setWaitingConsumer(int i) { |
1152 |
– |
this.waitingConsumers = i; |
1153 |
– |
} |
1154 |
– |
|
1155 |
– |
private int getWaitingConsumers() { |
1156 |
– |
return waitingConsumers; |
1157 |
– |
} |
1158 |
– |
} |
1113 |
|
} |