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(LinkedTransferQueue q) throws InterruptedException { |
44 |
> |
assertTrue(q.isEmpty()); |
45 |
> |
assertEquals(0, q.size()); |
46 |
> |
assertNull(q.peek()); |
47 |
> |
assertNull(q.poll()); |
48 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
49 |
> |
assertEquals(q.toString(), "[]"); |
50 |
> |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
51 |
> |
assertFalse(q.iterator().hasNext()); |
52 |
> |
try { |
53 |
> |
q.element(); |
54 |
> |
shouldThrow(); |
55 |
> |
} catch (NoSuchElementException success) {} |
56 |
> |
try { |
57 |
> |
q.iterator().next(); |
58 |
> |
shouldThrow(); |
59 |
> |
} catch (NoSuchElementException success) {} |
60 |
> |
try { |
61 |
> |
q.remove(); |
62 |
> |
shouldThrow(); |
63 |
> |
} catch (NoSuchElementException success) {} |
64 |
|
} |
65 |
|
|
66 |
|
/** |
67 |
< |
* Constructor builds new queue with size being zero and empty being true |
67 |
> |
* Constructor builds new queue with size being zero and empty |
68 |
> |
* being true |
69 |
|
*/ |
70 |
|
public void testConstructor1() { |
71 |
|
assertEquals(0, new LinkedTransferQueue().size()); |
73 |
|
} |
74 |
|
|
75 |
|
/** |
76 |
< |
* Initializing constructor with null collection throws NullPointerException |
76 |
> |
* Initializing constructor with null collection throws |
77 |
> |
* NullPointerException |
78 |
|
*/ |
79 |
|
public void testConstructor2() { |
80 |
|
try { |
81 |
|
new LinkedTransferQueue(null); |
82 |
|
shouldThrow(); |
83 |
< |
} catch (NullPointerException success) { |
49 |
< |
} |
83 |
> |
} catch (NullPointerException success) {} |
84 |
|
} |
85 |
|
|
86 |
|
/** |
87 |
< |
* Initializing from Collection of null elements throws NullPointerException |
87 |
> |
* Initializing from Collection of null elements throws |
88 |
> |
* NullPointerException |
89 |
|
*/ |
90 |
|
public void testConstructor3() { |
91 |
|
try { |
92 |
|
Integer[] ints = new Integer[SIZE]; |
93 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
93 |
> |
new LinkedTransferQueue(Arrays.asList(ints)); |
94 |
|
shouldThrow(); |
95 |
< |
} catch (NullPointerException success) { |
61 |
< |
} |
95 |
> |
} catch (NullPointerException success) {} |
96 |
|
} |
97 |
|
|
98 |
|
/** |
105 |
|
for (int i = 0; i < SIZE - 1; ++i) { |
106 |
|
ints[i] = i; |
107 |
|
} |
108 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
108 |
> |
new LinkedTransferQueue(Arrays.asList(ints)); |
109 |
|
shouldThrow(); |
110 |
< |
} catch (NullPointerException success) { |
77 |
< |
} |
110 |
> |
} catch (NullPointerException success) {} |
111 |
|
} |
112 |
|
|
113 |
|
/** |
114 |
|
* Queue contains all elements of the collection it is initialized by |
115 |
|
*/ |
116 |
|
public void testConstructor5() { |
117 |
< |
try { |
118 |
< |
Integer[] ints = new Integer[SIZE]; |
119 |
< |
for (int i = 0; i < SIZE; ++i) { |
120 |
< |
ints[i] = i; |
121 |
< |
} |
122 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
123 |
< |
for (int i = 0; i < SIZE; ++i) { |
124 |
< |
assertEquals(ints[i], q.poll()); |
125 |
< |
} |
126 |
< |
} finally { |
117 |
> |
Integer[] ints = new Integer[SIZE]; |
118 |
> |
for (int i = 0; i < SIZE; ++i) { |
119 |
> |
ints[i] = i; |
120 |
> |
} |
121 |
> |
List intList = Arrays.asList(ints); |
122 |
> |
LinkedTransferQueue q |
123 |
> |
= new LinkedTransferQueue(intList); |
124 |
> |
assertEquals(q.size(), intList.size()); |
125 |
> |
assertEquals(q.toString(), intList.toString()); |
126 |
> |
assertTrue(Arrays.equals(q.toArray(), |
127 |
> |
intList.toArray())); |
128 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[0]), |
129 |
> |
intList.toArray(new Object[0]))); |
130 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[SIZE]), |
131 |
> |
intList.toArray(new Object[SIZE]))); |
132 |
> |
for (int i = 0; i < SIZE; ++i) { |
133 |
> |
assertEquals(ints[i], q.poll()); |
134 |
|
} |
135 |
|
} |
136 |
|
|
137 |
|
/** |
138 |
< |
* Remaining capacity never decrease nor increase on add or remove |
138 |
> |
* remainingCapacity() always returns Integer.MAX_VALUE |
139 |
|
*/ |
140 |
|
public void testRemainingCapacity() { |
141 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
159 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
160 |
|
q.offer(null); |
161 |
|
shouldThrow(); |
162 |
< |
} catch (NullPointerException success) { |
123 |
< |
} |
162 |
> |
} catch (NullPointerException success) {} |
163 |
|
} |
164 |
|
|
165 |
|
/** |
170 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
171 |
|
q.add(null); |
172 |
|
shouldThrow(); |
173 |
< |
} catch (NullPointerException success) { |
135 |
< |
} |
173 |
> |
} catch (NullPointerException success) {} |
174 |
|
} |
175 |
|
|
176 |
|
/** |
181 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
182 |
|
q.addAll(null); |
183 |
|
shouldThrow(); |
184 |
< |
} catch (NullPointerException success) { |
147 |
< |
} |
184 |
> |
} catch (NullPointerException success) {} |
185 |
|
} |
186 |
|
|
187 |
|
/** |
192 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
193 |
|
q.addAll(q); |
194 |
|
shouldThrow(); |
195 |
< |
} catch (IllegalArgumentException success) { |
159 |
< |
} |
195 |
> |
} catch (IllegalArgumentException success) {} |
196 |
|
} |
197 |
|
|
198 |
|
/** |
204 |
|
Integer[] ints = new Integer[SIZE]; |
205 |
|
q.addAll(Arrays.asList(ints)); |
206 |
|
shouldThrow(); |
207 |
< |
} catch (NullPointerException success) { |
172 |
< |
} |
207 |
> |
} catch (NullPointerException success) {} |
208 |
|
} |
209 |
|
|
210 |
|
/** |
211 |
< |
* addAll of a collection with any null elements throws NullPointerException after |
212 |
< |
* possibly adding some elements |
211 |
> |
* addAll of a collection with any null elements throws |
212 |
> |
* NullPointerException after possibly adding some elements |
213 |
|
*/ |
214 |
|
public void testAddAll3() { |
215 |
|
try { |
220 |
|
} |
221 |
|
q.addAll(Arrays.asList(ints)); |
222 |
|
shouldThrow(); |
223 |
< |
} catch (NullPointerException success) { |
189 |
< |
} |
223 |
> |
} catch (NullPointerException success) {} |
224 |
|
} |
225 |
|
|
226 |
|
/** |
257 |
|
public void testPut() { |
258 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
259 |
|
for (int i = 0; i < SIZE; ++i) { |
260 |
+ |
assertEquals(q.size(), i); |
261 |
|
q.put(i); |
262 |
|
assertTrue(q.contains(i)); |
263 |
|
} |
229 |
– |
assertEquals(q.size(), SIZE); |
264 |
|
} |
265 |
|
|
266 |
|
/** |
274 |
|
} |
275 |
|
|
276 |
|
/** |
277 |
< |
* take blocks interruptibly when empty |
244 |
< |
*/ |
245 |
< |
public void testTakeFromEmpty() throws InterruptedException { |
246 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
247 |
< |
Thread t = new Thread(new Runnable() { |
248 |
< |
public void run() { |
249 |
< |
try { |
250 |
< |
q.take(); |
251 |
< |
threadShouldThrow(); |
252 |
< |
} catch (InterruptedException success) { |
253 |
< |
} catch (Throwable ex) { |
254 |
< |
threadUnexpectedException(ex); |
255 |
< |
} |
256 |
< |
}}); |
257 |
< |
t.start(); |
258 |
< |
Thread.sleep(SHORT_DELAY_MS); |
259 |
< |
t.interrupt(); |
260 |
< |
t.join(); |
261 |
< |
} |
262 |
< |
|
263 |
< |
/** |
264 |
< |
* Take removes existing elements until empty, then blocks interruptibly |
277 |
> |
* take removes existing elements until empty, then blocks interruptibly |
278 |
|
*/ |
279 |
|
public void testBlockingTake() throws InterruptedException { |
280 |
< |
Thread t = new Thread(new Runnable() { |
281 |
< |
public void run() { |
280 |
> |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
281 |
> |
Thread t = new Thread(new CheckedRunnable() { |
282 |
> |
public void realRun() throws InterruptedException { |
283 |
> |
for (int i = 0; i < SIZE; ++i) { |
284 |
> |
assertEquals(i, (int) q.take()); |
285 |
> |
} |
286 |
|
try { |
270 |
– |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
271 |
– |
for (int i = 0; i < SIZE; ++i) { |
272 |
– |
threadAssertEquals(i, (int) q.take()); |
273 |
– |
} |
287 |
|
q.take(); |
288 |
< |
threadShouldThrow(); |
289 |
< |
} catch (InterruptedException success) { |
277 |
< |
} catch (Throwable ex) { |
278 |
< |
threadUnexpectedException(ex); |
279 |
< |
} |
288 |
> |
shouldThrow(); |
289 |
> |
} catch (InterruptedException success) {} |
290 |
|
}}); |
291 |
+ |
|
292 |
|
t.start(); |
293 |
|
Thread.sleep(SHORT_DELAY_MS); |
294 |
|
t.interrupt(); |
295 |
< |
t.join(); |
295 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
296 |
> |
checkEmpty(q); |
297 |
|
} |
298 |
|
|
299 |
|
/** |
300 |
|
* poll succeeds unless empty |
301 |
|
*/ |
302 |
< |
public void testPoll() { |
302 |
> |
public void testPoll() throws InterruptedException { |
303 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
304 |
|
for (int i = 0; i < SIZE; ++i) { |
305 |
|
assertEquals(i, (int) q.poll()); |
306 |
|
} |
307 |
|
assertNull(q.poll()); |
308 |
+ |
checkEmpty(q); |
309 |
|
} |
310 |
|
|
311 |
|
/** |
312 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
312 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
313 |
|
*/ |
314 |
|
public void testTimedPoll0() throws InterruptedException { |
315 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
316 |
|
for (int i = 0; i < SIZE; ++i) { |
317 |
< |
assertEquals(i, (int) q.poll(0, TimeUnit.MILLISECONDS)); |
317 |
> |
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
318 |
|
} |
319 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
319 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
320 |
> |
checkEmpty(q); |
321 |
|
} |
322 |
|
|
323 |
|
/** |
324 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
324 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
325 |
|
*/ |
326 |
|
public void testTimedPoll() throws InterruptedException { |
327 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
328 |
|
for (int i = 0; i < SIZE; ++i) { |
329 |
< |
assertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
329 |
> |
long t0 = System.nanoTime(); |
330 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
331 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
332 |
|
} |
333 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
333 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
334 |
> |
checkEmpty(q); |
335 |
|
} |
336 |
|
|
337 |
|
/** |
339 |
|
* returning timeout status |
340 |
|
*/ |
341 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
342 |
< |
Thread t = new Thread(new Runnable() { |
343 |
< |
public void run() { |
344 |
< |
try { |
345 |
< |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
346 |
< |
for (int i = 0; i < SIZE; ++i) { |
347 |
< |
threadAssertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
348 |
< |
} |
332 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
333 |
< |
} catch (InterruptedException success) { |
334 |
< |
} catch (Throwable ex) { |
335 |
< |
threadUnexpectedException(ex); |
342 |
> |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
343 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
344 |
> |
public void realRun() throws InterruptedException { |
345 |
> |
for (int i = 0; i < SIZE; ++i) { |
346 |
> |
long t0 = System.nanoTime(); |
347 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
348 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
349 |
|
} |
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() { |
350 |
|
try { |
351 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
352 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
353 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
356 |
< |
threadShouldThrow(); |
357 |
< |
} catch (InterruptedException success) { |
358 |
< |
} catch (Throwable ex) { |
359 |
< |
threadUnexpectedException(ex); |
360 |
< |
} |
351 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
352 |
> |
shouldThrow(); |
353 |
> |
} catch (InterruptedException success) {} |
354 |
|
}}); |
355 |
< |
t.start(); |
355 |
> |
|
356 |
|
Thread.sleep(SMALL_DELAY_MS); |
364 |
– |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
357 |
|
t.interrupt(); |
358 |
< |
t.join(); |
358 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
359 |
> |
checkEmpty(q); |
360 |
|
} |
361 |
|
|
362 |
|
/** |
363 |
|
* peek returns next element, or null if empty |
364 |
|
*/ |
365 |
< |
public void testPeek() { |
365 |
> |
public void testPeek() throws InterruptedException { |
366 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
367 |
|
for (int i = 0; i < SIZE; ++i) { |
368 |
|
assertEquals(i, (int) q.peek()); |
371 |
|
i != (int) q.peek()); |
372 |
|
} |
373 |
|
assertNull(q.peek()); |
374 |
+ |
checkEmpty(q); |
375 |
|
} |
376 |
|
|
377 |
|
/** |
378 |
|
* element returns next element, or throws NoSuchElementException if empty |
379 |
|
*/ |
380 |
< |
public void testElement() { |
380 |
> |
public void testElement() throws InterruptedException { |
381 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
382 |
|
for (int i = 0; i < SIZE; ++i) { |
383 |
|
assertEquals(i, (int) q.element()); |
386 |
|
try { |
387 |
|
q.element(); |
388 |
|
shouldThrow(); |
389 |
< |
} catch (NoSuchElementException success) { |
390 |
< |
} |
389 |
> |
} catch (NoSuchElementException success) {} |
390 |
> |
checkEmpty(q); |
391 |
|
} |
392 |
|
|
393 |
|
/** |
394 |
|
* remove removes next element, or throws NoSuchElementException if empty |
395 |
|
*/ |
396 |
< |
public void testRemove() { |
396 |
> |
public void testRemove() throws InterruptedException { |
397 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
398 |
|
for (int i = 0; i < SIZE; ++i) { |
399 |
|
assertEquals(i, (int) q.remove()); |
401 |
|
try { |
402 |
|
q.remove(); |
403 |
|
shouldThrow(); |
404 |
< |
} catch (NoSuchElementException success) { |
405 |
< |
} |
404 |
> |
} catch (NoSuchElementException success) {} |
405 |
> |
checkEmpty(q); |
406 |
|
} |
407 |
|
|
408 |
|
/** |
409 |
|
* remove(x) removes x and returns true if present |
410 |
|
*/ |
411 |
< |
public void testRemoveElement() { |
411 |
> |
public void testRemoveElement() throws InterruptedException { |
412 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
413 |
|
for (int i = 1; i < SIZE; i += 2) { |
414 |
|
assertTrue(q.remove(i)); |
417 |
|
assertTrue(q.remove(i)); |
418 |
|
assertFalse(q.remove(i + 1)); |
419 |
|
} |
420 |
< |
assertTrue(q.isEmpty()); |
420 |
> |
checkEmpty(q); |
421 |
|
} |
422 |
|
|
423 |
|
/** |
430 |
|
assertTrue(q.remove(one)); |
431 |
|
assertTrue(q.remove(two)); |
432 |
|
assertTrue(q.add(three)); |
433 |
< |
assertTrue(q.take() != null); |
433 |
> |
assertSame(q.take(), three); |
434 |
|
} |
435 |
|
|
436 |
|
/** |
448 |
|
/** |
449 |
|
* clear removes all elements |
450 |
|
*/ |
451 |
< |
public void testClear() { |
451 |
> |
public void testClear() throws InterruptedException { |
452 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
453 |
|
q.clear(); |
454 |
< |
assertTrue(q.isEmpty()); |
461 |
< |
assertEquals(0, q.size()); |
454 |
> |
checkEmpty(q); |
455 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
456 |
|
q.add(one); |
457 |
|
assertFalse(q.isEmpty()); |
458 |
+ |
assertEquals(1, q.size()); |
459 |
|
assertTrue(q.contains(one)); |
460 |
|
q.clear(); |
461 |
< |
assertTrue(q.isEmpty()); |
461 |
> |
checkEmpty(q); |
462 |
|
} |
463 |
|
|
464 |
|
/** |
476 |
|
} |
477 |
|
|
478 |
|
/** |
479 |
< |
* retainAll(c) retains only those elements of c and reports true if changed |
479 |
> |
* retainAll(c) retains only those elements of c and reports true |
480 |
> |
* if changed |
481 |
|
*/ |
482 |
|
public void testRetainAll() { |
483 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
496 |
|
} |
497 |
|
|
498 |
|
/** |
499 |
< |
* removeAll(c) removes only those elements of c and reports true if changed |
499 |
> |
* removeAll(c) removes only those elements of c and reports true |
500 |
> |
* if changed |
501 |
|
*/ |
502 |
|
public void testRemoveAll() { |
503 |
|
for (int i = 1; i < SIZE; ++i) { |
512 |
|
} |
513 |
|
|
514 |
|
/** |
515 |
< |
* toArray contains all elements |
515 |
> |
* toArray() contains all elements |
516 |
|
*/ |
517 |
|
public void testToArray() throws InterruptedException { |
518 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
538 |
|
* toArray(null) throws NullPointerException |
539 |
|
*/ |
540 |
|
public void testToArray_BadArg() { |
541 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
542 |
|
try { |
546 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
543 |
|
Object o[] = q.toArray(null); |
544 |
|
shouldThrow(); |
545 |
< |
} catch (NullPointerException success) { |
550 |
< |
} |
545 |
> |
} catch (NullPointerException success) {} |
546 |
|
} |
547 |
|
|
548 |
|
/** |
549 |
< |
* toArray with incompatible array type throws CCE |
549 |
> |
* toArray(incompatible array type) throws CCE |
550 |
|
*/ |
551 |
|
public void testToArray1_BadArg() { |
552 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
553 |
|
try { |
558 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
554 |
|
Object o[] = q.toArray(new String[10]); |
555 |
|
shouldThrow(); |
556 |
< |
} catch (ArrayStoreException success) { |
562 |
< |
} |
556 |
> |
} catch (ArrayStoreException success) {} |
557 |
|
} |
558 |
|
|
559 |
|
/** |
562 |
|
public void testIterator() throws InterruptedException { |
563 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
564 |
|
Iterator it = q.iterator(); |
565 |
+ |
int i = 0; |
566 |
|
while (it.hasNext()) { |
567 |
< |
assertEquals(it.next(), q.take()); |
567 |
> |
assertEquals(it.next(), i++); |
568 |
|
} |
569 |
+ |
assertEquals(i, SIZE); |
570 |
|
} |
571 |
|
|
572 |
|
/** |
573 |
< |
* iterator.remove removes current element |
573 |
> |
* iterator.remove() removes current element |
574 |
|
*/ |
575 |
|
public void testIteratorRemove() { |
576 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
583 |
|
it.remove(); |
584 |
|
|
585 |
|
it = q.iterator(); |
586 |
< |
assertEquals(it.next(), one); |
587 |
< |
assertEquals(it.next(), three); |
586 |
> |
assertSame(it.next(), one); |
587 |
> |
assertSame(it.next(), three); |
588 |
|
assertFalse(it.hasNext()); |
589 |
|
} |
590 |
|
|
592 |
|
* iterator ordering is FIFO |
593 |
|
*/ |
594 |
|
public void testIteratorOrdering() { |
595 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
595 |
> |
final LinkedTransferQueue<Integer> q |
596 |
> |
= new LinkedTransferQueue<Integer>(); |
597 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
598 |
|
q.add(one); |
599 |
|
q.add(two); |
640 |
|
q.add(one); |
641 |
|
q.add(two); |
642 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
643 |
< |
executor.execute(new Runnable() { |
644 |
< |
public void run() { |
645 |
< |
try { |
646 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
650 |
< |
} catch (Throwable ex) { |
651 |
< |
threadUnexpectedException(ex); |
652 |
< |
} |
643 |
> |
|
644 |
> |
executor.execute(new CheckedRunnable() { |
645 |
> |
public void realRun() { |
646 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
647 |
|
}}); |
648 |
|
|
649 |
< |
executor.execute(new Runnable() { |
650 |
< |
public void run() { |
651 |
< |
try { |
652 |
< |
Thread.sleep(SMALL_DELAY_MS); |
659 |
< |
threadAssertEquals(one, q.take()); |
660 |
< |
} catch (Throwable ex) { |
661 |
< |
threadUnexpectedException(ex); |
662 |
< |
} |
649 |
> |
executor.execute(new CheckedRunnable() { |
650 |
> |
public void realRun() throws InterruptedException { |
651 |
> |
Thread.sleep(SMALL_DELAY_MS); |
652 |
> |
assertSame(one, q.take()); |
653 |
|
}}); |
654 |
|
|
655 |
|
joinPool(executor); |
656 |
|
} |
657 |
|
|
658 |
|
/** |
659 |
< |
* poll retrieves elements across Executor threads |
659 |
> |
* timed poll retrieves elements across Executor threads |
660 |
|
*/ |
661 |
|
public void testPollInExecutor() { |
662 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
663 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
664 |
< |
executor.execute(new Runnable() { |
665 |
< |
public void run() { |
666 |
< |
try { |
667 |
< |
threadAssertNull(q.poll()); |
668 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
669 |
< |
threadAssertTrue(q.isEmpty()); |
680 |
< |
} catch (Throwable ex) { |
681 |
< |
threadUnexpectedException(ex); |
682 |
< |
} |
664 |
> |
|
665 |
> |
executor.execute(new CheckedRunnable() { |
666 |
> |
public void realRun() throws InterruptedException { |
667 |
> |
assertNull(q.poll()); |
668 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
669 |
> |
assertTrue(q.isEmpty()); |
670 |
|
}}); |
671 |
|
|
672 |
< |
executor.execute(new Runnable() { |
673 |
< |
public void run() { |
674 |
< |
try { |
675 |
< |
Thread.sleep(SMALL_DELAY_MS); |
689 |
< |
q.put(one); |
690 |
< |
} catch (Throwable ex) { |
691 |
< |
threadUnexpectedException(ex); |
692 |
< |
} |
672 |
> |
executor.execute(new CheckedRunnable() { |
673 |
> |
public void realRun() throws InterruptedException { |
674 |
> |
Thread.sleep(SMALL_DELAY_MS); |
675 |
> |
q.put(one); |
676 |
|
}}); |
677 |
|
|
678 |
|
joinPool(executor); |
685 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
686 |
|
|
687 |
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
688 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
688 |
> |
ObjectOutputStream out |
689 |
> |
= new ObjectOutputStream(new BufferedOutputStream(bout)); |
690 |
|
out.writeObject(q); |
691 |
|
out.close(); |
692 |
|
|
693 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
694 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
693 |
> |
ByteArrayInputStream bin |
694 |
> |
= new ByteArrayInputStream(bout.toByteArray()); |
695 |
> |
ObjectInputStream in |
696 |
> |
= new ObjectInputStream(new BufferedInputStream(bin)); |
697 |
|
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
698 |
|
|
699 |
|
assertEquals(q.size(), r.size()); |
710 |
|
try { |
711 |
|
q.drainTo(null); |
712 |
|
shouldThrow(); |
713 |
< |
} catch (NullPointerException success) { |
728 |
< |
} |
713 |
> |
} catch (NullPointerException success) {} |
714 |
|
} |
715 |
|
|
716 |
|
/** |
721 |
|
try { |
722 |
|
q.drainTo(q); |
723 |
|
shouldThrow(); |
724 |
< |
} catch (IllegalArgumentException success) { |
740 |
< |
} |
724 |
> |
} catch (IllegalArgumentException success) {} |
725 |
|
} |
726 |
|
|
727 |
|
/** |
751 |
|
} |
752 |
|
|
753 |
|
/** |
754 |
< |
* drainTo empties full queue, unblocking a waiting put. |
754 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
755 |
|
*/ |
756 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
757 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
758 |
< |
Thread t = new Thread(new Runnable() { |
759 |
< |
public void run() { |
760 |
< |
try { |
777 |
< |
q.put(SIZE + 1); |
778 |
< |
} catch (Throwable ex) { |
779 |
< |
threadUnexpectedException(ex); |
780 |
< |
} |
758 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
759 |
> |
public void realRun() { |
760 |
> |
q.put(SIZE + 1); |
761 |
|
}}); |
782 |
– |
t.start(); |
762 |
|
ArrayList l = new ArrayList(); |
763 |
|
q.drainTo(l); |
764 |
|
assertTrue(l.size() >= SIZE); |
765 |
|
for (int i = 0; i < SIZE; ++i) { |
766 |
|
assertEquals(l.get(i), i); |
767 |
|
} |
768 |
< |
t.join(); |
768 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
769 |
|
assertTrue(q.size() + l.size() >= SIZE); |
770 |
|
} |
771 |
|
|
775 |
|
public void testDrainToNullN() { |
776 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
777 |
|
try { |
778 |
< |
q.drainTo(null, 0); |
778 |
> |
q.drainTo(null, SIZE); |
779 |
|
shouldThrow(); |
780 |
< |
} catch (NullPointerException success) { |
802 |
< |
} |
780 |
> |
} catch (NullPointerException success) {} |
781 |
|
} |
782 |
|
|
783 |
|
/** |
786 |
|
public void testDrainToSelfN() { |
787 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
788 |
|
try { |
789 |
< |
q.drainTo(q, 0); |
789 |
> |
q.drainTo(q, SIZE); |
790 |
|
shouldThrow(); |
791 |
< |
} catch (IllegalArgumentException success) { |
814 |
< |
} |
791 |
> |
} catch (IllegalArgumentException success) {} |
792 |
|
} |
793 |
|
|
794 |
|
/** |
795 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
795 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
796 |
|
*/ |
797 |
|
public void testDrainToN() { |
798 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
814 |
|
} |
815 |
|
|
816 |
|
/** |
817 |
< |
* poll and take should decrement the waiting consumer count |
817 |
> |
* timed poll() or take() increments the waiting consumer count; |
818 |
> |
* offer(e) decrements the waiting consumer count |
819 |
|
*/ |
820 |
|
public void testWaitingConsumer() throws InterruptedException { |
821 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
822 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
823 |
< |
new Thread(new Runnable() { |
824 |
< |
public void run() { |
825 |
< |
try { |
826 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
827 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
828 |
< |
threadAssertTrue(q.offer(new Object())); |
829 |
< |
} catch (Throwable ex) { |
830 |
< |
threadUnexpectedException(ex); |
831 |
< |
} |
832 |
< |
}}).start(); |
833 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
834 |
< |
assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers()); |
822 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
823 |
> |
assertFalse(q.hasWaitingConsumer()); |
824 |
> |
|
825 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
826 |
> |
public void realRun() throws InterruptedException { |
827 |
> |
Thread.sleep(SMALL_DELAY_MS); |
828 |
> |
assertTrue(q.hasWaitingConsumer()); |
829 |
> |
assertEquals(q.getWaitingConsumerCount(), 1); |
830 |
> |
assertTrue(q.offer(one)); |
831 |
> |
assertFalse(q.hasWaitingConsumer()); |
832 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
833 |
> |
}}); |
834 |
> |
|
835 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
836 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
837 |
> |
assertFalse(q.hasWaitingConsumer()); |
838 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
839 |
|
} |
840 |
|
|
841 |
|
/** |
846 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
847 |
|
q.transfer(null); |
848 |
|
shouldThrow(); |
849 |
< |
} catch (NullPointerException ex) { |
868 |
< |
} |
849 |
> |
} catch (NullPointerException success) {} |
850 |
|
} |
851 |
|
|
852 |
|
/** |
853 |
< |
* transfer attempts to insert into the queue then wait until that |
854 |
< |
* object is removed via take or poll. |
853 |
> |
* transfer waits until a poll occurs. The transfered element |
854 |
> |
* is returned by this associated poll. |
855 |
|
*/ |
856 |
|
public void testTransfer2() throws InterruptedException { |
857 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
857 |
> |
final LinkedTransferQueue<Integer> q |
858 |
> |
= new LinkedTransferQueue<Integer>(); |
859 |
|
|
860 |
< |
new Thread(new Runnable() { |
861 |
< |
public void run() { |
862 |
< |
try { |
863 |
< |
q.transfer(SIZE); |
864 |
< |
threadAssertTrue(q.isEmpty()); |
883 |
< |
} catch (Throwable ex) { |
884 |
< |
threadUnexpectedException(ex); |
885 |
< |
} |
886 |
< |
}}).start(); |
860 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
861 |
> |
public void realRun() throws InterruptedException { |
862 |
> |
q.transfer(SIZE); |
863 |
> |
assertTrue(q.isEmpty()); |
864 |
> |
}}); |
865 |
|
|
866 |
|
Thread.sleep(SHORT_DELAY_MS); |
867 |
|
assertEquals(1, q.size()); |
868 |
|
assertEquals(SIZE, (int) q.poll()); |
869 |
|
assertTrue(q.isEmpty()); |
870 |
+ |
awaitTermination(t, MEDIUM_DELAY_MS); |
871 |
|
} |
872 |
|
|
873 |
|
/** |
874 |
< |
* transfer will attempt to transfer in fifo order and continue |
896 |
< |
* waiting if the element being transfered is not polled or taken |
874 |
> |
* transfer waits until a poll occurs, and then transfers in fifo order |
875 |
|
*/ |
876 |
|
public void testTransfer3() throws InterruptedException { |
877 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
877 |
> |
final LinkedTransferQueue<Integer> q |
878 |
> |
= new LinkedTransferQueue<Integer>(); |
879 |
|
|
880 |
< |
Thread first = |
881 |
< |
new Thread(new Runnable() { |
882 |
< |
public void run() { |
883 |
< |
try { |
884 |
< |
Integer i = SIZE + 1; |
885 |
< |
q.transfer(i); |
886 |
< |
threadAssertTrue(!q.contains(i)); |
908 |
< |
threadAssertEquals(1, q.size()); |
909 |
< |
} catch (Throwable ex) { |
910 |
< |
threadUnexpectedException(ex); |
911 |
< |
} |
912 |
< |
}}); |
913 |
< |
first.start(); |
880 |
> |
Thread first = newStartedThread(new CheckedRunnable() { |
881 |
> |
public void realRun() throws InterruptedException { |
882 |
> |
Integer i = SIZE + 1; |
883 |
> |
q.transfer(i); |
884 |
> |
assertTrue(!q.contains(i)); |
885 |
> |
assertEquals(1, q.size()); |
886 |
> |
}}); |
887 |
|
|
888 |
< |
Thread interruptedThread = |
889 |
< |
new Thread(new Runnable() { |
890 |
< |
public void run() { |
891 |
< |
try { |
892 |
< |
while (q.size() == 0) |
893 |
< |
Thread.yield(); |
921 |
< |
q.transfer(SIZE); |
922 |
< |
threadShouldThrow(); |
923 |
< |
} catch (InterruptedException success) { |
924 |
< |
} catch (Throwable ex) { |
925 |
< |
threadUnexpectedException(ex); |
926 |
< |
} |
888 |
> |
Thread interruptedThread = newStartedThread( |
889 |
> |
new CheckedInterruptedRunnable() { |
890 |
> |
public void realRun() throws InterruptedException { |
891 |
> |
while (q.size() == 0) |
892 |
> |
Thread.yield(); |
893 |
> |
q.transfer(SIZE); |
894 |
|
}}); |
928 |
– |
interruptedThread.start(); |
895 |
|
|
896 |
|
while (q.size() < 2) |
897 |
|
Thread.yield(); |
906 |
|
} |
907 |
|
|
908 |
|
/** |
909 |
< |
* transfer will wait as long as a poll or take occurs if one does occur |
910 |
< |
* the waiting is finished and the thread that tries to poll/take |
945 |
< |
* wins in retrieving the element |
909 |
> |
* transfer waits until a poll occurs, at which point the polling |
910 |
> |
* thread returns the element |
911 |
|
*/ |
912 |
|
public void testTransfer4() throws InterruptedException { |
913 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
914 |
< |
new Thread(new Runnable() { |
915 |
< |
public void run() { |
916 |
< |
try { |
917 |
< |
q.transfer(four); |
918 |
< |
threadAssertFalse(q.contains(four)); |
919 |
< |
threadAssertEquals(three, q.poll()); |
920 |
< |
} catch (Throwable ex) { |
921 |
< |
threadUnexpectedException(ex); |
922 |
< |
} |
958 |
< |
}}).start(); |
959 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
914 |
> |
|
915 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
916 |
> |
public void realRun() throws InterruptedException { |
917 |
> |
q.transfer(four); |
918 |
> |
assertFalse(q.contains(four)); |
919 |
> |
assertSame(three, q.poll()); |
920 |
> |
}}); |
921 |
> |
|
922 |
> |
Thread.sleep(SHORT_DELAY_MS); |
923 |
|
assertTrue(q.offer(three)); |
924 |
< |
assertEquals(four, q.poll()); |
924 |
> |
assertSame(four, q.poll()); |
925 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
926 |
> |
} |
927 |
> |
|
928 |
> |
/** |
929 |
> |
* transfer waits until a take occurs. The transfered element |
930 |
> |
* is returned by this associated take. |
931 |
> |
*/ |
932 |
> |
public void testTransfer5() throws InterruptedException { |
933 |
> |
final LinkedTransferQueue<Integer> q |
934 |
> |
= new LinkedTransferQueue<Integer>(); |
935 |
> |
|
936 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
937 |
> |
public void realRun() throws InterruptedException { |
938 |
> |
q.transfer(SIZE); |
939 |
> |
checkEmpty(q); |
940 |
> |
}}); |
941 |
> |
|
942 |
> |
Thread.sleep(SHORT_DELAY_MS); |
943 |
> |
assertEquals(SIZE, (int) q.take()); |
944 |
> |
checkEmpty(q); |
945 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
946 |
|
} |
947 |
|
|
948 |
|
/** |
952 |
|
try { |
953 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
954 |
|
q.tryTransfer(null); |
955 |
< |
this.shouldThrow(); |
956 |
< |
} catch (NullPointerException ex) { |
973 |
< |
} |
955 |
> |
shouldThrow(); |
956 |
> |
} catch (NullPointerException success) {} |
957 |
|
} |
958 |
|
|
959 |
|
/** |
960 |
|
* tryTransfer returns false and does not enqueue if there are no |
961 |
|
* consumers waiting to poll or take. |
962 |
|
*/ |
963 |
< |
public void testTryTransfer2() { |
963 |
> |
public void testTryTransfer2() throws InterruptedException { |
964 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
965 |
|
assertFalse(q.tryTransfer(new Object())); |
966 |
|
assertFalse(q.hasWaitingConsumer()); |
967 |
< |
assertTrue(q.isEmpty()); |
985 |
< |
assertEquals(0, q.size()); |
967 |
> |
checkEmpty(q); |
968 |
|
} |
969 |
|
|
970 |
|
/** |
974 |
|
public void testTryTransfer3() throws InterruptedException { |
975 |
|
final Object hotPotato = new Object(); |
976 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
977 |
< |
new Thread(new Runnable() { |
978 |
< |
public void run() { |
979 |
< |
try { |
980 |
< |
while (! q.hasWaitingConsumer()) |
981 |
< |
Thread.yield(); |
982 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
983 |
< |
threadAssertTrue(q.isEmpty()); |
984 |
< |
threadAssertTrue(q.size() == 0); |
985 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
986 |
< |
} catch (Throwable ex) { |
987 |
< |
threadUnexpectedException(ex); |
988 |
< |
} |
989 |
< |
}}).start(); |
990 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) == hotPotato); |
1009 |
< |
assertTrue(q.isEmpty()); |
977 |
> |
|
978 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
979 |
> |
public void realRun() { |
980 |
> |
while (! q.hasWaitingConsumer()) |
981 |
> |
Thread.yield(); |
982 |
> |
assertTrue(q.hasWaitingConsumer()); |
983 |
> |
assertTrue(q.isEmpty()); |
984 |
> |
assertEquals(q.size(), 0); |
985 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
986 |
> |
}}); |
987 |
> |
|
988 |
> |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
989 |
> |
checkEmpty(q); |
990 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
991 |
|
} |
992 |
|
|
993 |
|
/** |
997 |
|
public void testTryTransfer4() throws InterruptedException { |
998 |
|
final Object hotPotato = new Object(); |
999 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1000 |
< |
new Thread(new Runnable() { |
1001 |
< |
public void run() { |
1002 |
< |
try { |
1003 |
< |
while (! q.hasWaitingConsumer()) |
1004 |
< |
Thread.yield(); |
1005 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1006 |
< |
threadAssertTrue(q.isEmpty()); |
1007 |
< |
threadAssertTrue(q.size() == 0); |
1008 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1009 |
< |
} catch (Throwable ex) { |
1010 |
< |
threadUnexpectedException(ex); |
1011 |
< |
} |
1012 |
< |
}}).start(); |
1013 |
< |
assertTrue(q.take() == hotPotato); |
1033 |
< |
assertTrue(q.isEmpty()); |
1000 |
> |
|
1001 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1002 |
> |
public void realRun() { |
1003 |
> |
while (! q.hasWaitingConsumer()) |
1004 |
> |
Thread.yield(); |
1005 |
> |
assertTrue(q.hasWaitingConsumer()); |
1006 |
> |
assertTrue(q.isEmpty()); |
1007 |
> |
assertEquals(q.size(), 0); |
1008 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
1009 |
> |
}}); |
1010 |
> |
|
1011 |
> |
assertSame(q.take(), hotPotato); |
1012 |
> |
checkEmpty(q); |
1013 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1014 |
|
} |
1015 |
|
|
1016 |
|
/** |
1017 |
< |
* tryTransfer waits the amount given if interrupted, show an |
1018 |
< |
* interrupted exception |
1017 |
> |
* tryTransfer waits the amount given if interrupted, and |
1018 |
> |
* throws interrupted exception |
1019 |
|
*/ |
1020 |
|
public void testTryTransfer5() throws InterruptedException { |
1021 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1022 |
< |
Thread toInterrupt = new Thread(new Runnable() { |
1023 |
< |
public void run() { |
1024 |
< |
try { |
1025 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1046 |
< |
threadShouldThrow(); |
1047 |
< |
} catch (InterruptedException success) { |
1048 |
< |
} catch (Throwable ex) { |
1049 |
< |
threadUnexpectedException(ex); |
1050 |
< |
} |
1022 |
> |
|
1023 |
> |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1024 |
> |
public void realRun() throws InterruptedException { |
1025 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1026 |
|
}}); |
1027 |
< |
toInterrupt.start(); |
1027 |
> |
|
1028 |
|
Thread.sleep(SMALL_DELAY_MS); |
1029 |
|
toInterrupt.interrupt(); |
1030 |
+ |
toInterrupt.join(); |
1031 |
|
} |
1032 |
|
|
1033 |
|
/** |
1035 |
|
*/ |
1036 |
|
public void testTryTransfer6() throws InterruptedException { |
1037 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1038 |
< |
new Thread(new Runnable() { |
1039 |
< |
public void run() { |
1040 |
< |
try { |
1041 |
< |
threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1042 |
< |
} catch (Throwable ex) { |
1043 |
< |
threadUnexpectedException(ex); |
1044 |
< |
} |
1045 |
< |
}}).start(); |
1046 |
< |
Thread.sleep(LONG_DELAY_MS); |
1047 |
< |
assertTrue(q.isEmpty()); |
1038 |
> |
|
1039 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1040 |
> |
public void realRun() throws InterruptedException { |
1041 |
> |
long t0 = System.nanoTime(); |
1042 |
> |
assertFalse(q.tryTransfer(new Object(), |
1043 |
> |
SHORT_DELAY_MS, MILLISECONDS)); |
1044 |
> |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1045 |
> |
}}); |
1046 |
> |
|
1047 |
> |
checkEmpty(q); |
1048 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1049 |
> |
checkEmpty(q); |
1050 |
|
} |
1051 |
|
|
1052 |
|
/** |
1056 |
|
public void testTryTransfer7() throws InterruptedException { |
1057 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1058 |
|
assertTrue(q.offer(four)); |
1059 |
< |
new Thread(new Runnable() { |
1060 |
< |
public void run() { |
1061 |
< |
try { |
1062 |
< |
threadAssertTrue(q.tryTransfer(five, LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
1063 |
< |
threadAssertTrue(q.isEmpty()); |
1064 |
< |
} catch (Throwable ex) { |
1065 |
< |
threadUnexpectedException(ex); |
1088 |
< |
} |
1089 |
< |
}}).start(); |
1059 |
> |
|
1060 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1061 |
> |
public void realRun() throws InterruptedException { |
1062 |
> |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1063 |
> |
assertTrue(q.isEmpty()); |
1064 |
> |
}}); |
1065 |
> |
|
1066 |
|
Thread.sleep(SHORT_DELAY_MS); |
1067 |
|
assertEquals(2, q.size()); |
1068 |
< |
assertEquals(four, q.poll()); |
1069 |
< |
assertEquals(five, q.poll()); |
1070 |
< |
assertTrue(q.isEmpty()); |
1068 |
> |
assertSame(four, q.poll()); |
1069 |
> |
assertSame(five, q.poll()); |
1070 |
> |
checkEmpty(q); |
1071 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1072 |
|
} |
1073 |
|
|
1074 |
|
/** |
1079 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1080 |
|
assertTrue(q.offer(four)); |
1081 |
|
assertEquals(1, q.size()); |
1082 |
< |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1082 |
> |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1083 |
|
assertEquals(1, q.size()); |
1084 |
< |
assertEquals(four, q.poll()); |
1108 |
< |
threadAssertTrue(q.isEmpty()); |
1084 |
> |
assertSame(four, q.poll()); |
1085 |
|
assertNull(q.poll()); |
1086 |
+ |
checkEmpty(q); |
1087 |
|
} |
1088 |
|
|
1089 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1097 |
|
assertFalse(q.isEmpty()); |
1098 |
|
return q; |
1099 |
|
} |
1123 |
– |
|
1124 |
– |
private static class ConsumerObserver { |
1125 |
– |
|
1126 |
– |
private int waitingConsumers; |
1127 |
– |
|
1128 |
– |
private ConsumerObserver() { |
1129 |
– |
} |
1130 |
– |
|
1131 |
– |
private void setWaitingConsumer(int i) { |
1132 |
– |
this.waitingConsumers = i; |
1133 |
– |
} |
1134 |
– |
|
1135 |
– |
private int getWaitingConsumers() { |
1136 |
– |
return waitingConsumers; |
1137 |
– |
} |
1138 |
– |
} |
1100 |
|
} |