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 Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
14 |
|
|
15 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
16 |
|
|
17 |
+ |
public static class Unbounded extends BlockingQueueTest { |
18 |
+ |
protected BlockingQueue emptyCollection() { |
19 |
+ |
return new LinkedBlockingQueue(); |
20 |
+ |
} |
21 |
+ |
} |
22 |
+ |
|
23 |
+ |
public static class Bounded extends BlockingQueueTest { |
24 |
+ |
protected BlockingQueue emptyCollection() { |
25 |
+ |
return new LinkedBlockingQueue(20); |
26 |
+ |
} |
27 |
+ |
} |
28 |
+ |
|
29 |
|
public static void main(String[] args) { |
30 |
< |
junit.textui.TestRunner.run (suite()); |
30 |
> |
junit.textui.TestRunner.run(suite()); |
31 |
|
} |
32 |
|
|
33 |
|
public static Test suite() { |
34 |
< |
return new TestSuite(LinkedBlockingQueueTest.class); |
34 |
> |
return newTestSuite(LinkedBlockingQueueTest.class, |
35 |
> |
new Unbounded().testSuite(), |
36 |
> |
new Bounded().testSuite()); |
37 |
|
} |
38 |
|
|
25 |
– |
|
39 |
|
/** |
40 |
|
* Create a queue of given size containing consecutive |
41 |
|
* Integers 0 ... n. |
42 |
|
*/ |
43 |
< |
private LinkedBlockingQueue populatedQueue(int n) { |
44 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(n); |
43 |
> |
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
44 |
> |
LinkedBlockingQueue<Integer> q = |
45 |
> |
new LinkedBlockingQueue<Integer>(n); |
46 |
|
assertTrue(q.isEmpty()); |
47 |
|
for (int i = 0; i < n; i++) |
48 |
|
assertTrue(q.offer(new Integer(i))); |
62 |
|
} |
63 |
|
|
64 |
|
/** |
65 |
< |
* Constructor throws IAE if capacity argument nonpositive |
65 |
> |
* Constructor throws IAE if capacity argument nonpositive |
66 |
|
*/ |
67 |
|
public void testConstructor2() { |
68 |
|
try { |
69 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(0); |
70 |
|
shouldThrow(); |
71 |
< |
} |
58 |
< |
catch (IllegalArgumentException success) {} |
71 |
> |
} catch (IllegalArgumentException success) {} |
72 |
|
} |
73 |
|
|
74 |
|
/** |
78 |
|
try { |
79 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(null); |
80 |
|
shouldThrow(); |
81 |
< |
} |
69 |
< |
catch (NullPointerException success) {} |
81 |
> |
} catch (NullPointerException success) {} |
82 |
|
} |
83 |
|
|
84 |
|
/** |
89 |
|
Integer[] ints = new Integer[SIZE]; |
90 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
91 |
|
shouldThrow(); |
92 |
< |
} |
81 |
< |
catch (NullPointerException success) {} |
92 |
> |
} catch (NullPointerException success) {} |
93 |
|
} |
94 |
|
|
95 |
|
/** |
102 |
|
ints[i] = new Integer(i); |
103 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
104 |
|
shouldThrow(); |
105 |
< |
} |
95 |
< |
catch (NullPointerException success) {} |
105 |
> |
} catch (NullPointerException success) {} |
106 |
|
} |
107 |
|
|
108 |
|
/** |
109 |
|
* Queue contains all elements of collection used to initialize |
110 |
|
*/ |
111 |
|
public void testConstructor6() { |
112 |
< |
try { |
113 |
< |
Integer[] ints = new Integer[SIZE]; |
114 |
< |
for (int i = 0; i < SIZE; ++i) |
115 |
< |
ints[i] = new Integer(i); |
116 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
117 |
< |
for (int i = 0; i < SIZE; ++i) |
108 |
< |
assertEquals(ints[i], q.poll()); |
109 |
< |
} |
110 |
< |
finally {} |
112 |
> |
Integer[] ints = new Integer[SIZE]; |
113 |
> |
for (int i = 0; i < SIZE; ++i) |
114 |
> |
ints[i] = new Integer(i); |
115 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
116 |
> |
for (int i = 0; i < SIZE; ++i) |
117 |
> |
assertEquals(ints[i], q.poll()); |
118 |
|
} |
119 |
|
|
120 |
|
/** |
157 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
158 |
|
q.offer(null); |
159 |
|
shouldThrow(); |
160 |
< |
} catch (NullPointerException success) { } |
160 |
> |
} catch (NullPointerException success) {} |
161 |
|
} |
162 |
|
|
163 |
|
/** |
168 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
169 |
|
q.add(null); |
170 |
|
shouldThrow(); |
171 |
< |
} catch (NullPointerException success) { } |
171 |
> |
} catch (NullPointerException success) {} |
172 |
|
} |
173 |
|
|
174 |
|
/** |
191 |
|
} |
192 |
|
assertEquals(0, q.remainingCapacity()); |
193 |
|
q.add(new Integer(SIZE)); |
194 |
< |
} catch (IllegalStateException success) { |
195 |
< |
} |
194 |
> |
shouldThrow(); |
195 |
> |
} catch (IllegalStateException success) {} |
196 |
|
} |
197 |
|
|
198 |
|
/** |
203 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
204 |
|
q.addAll(null); |
205 |
|
shouldThrow(); |
206 |
< |
} |
200 |
< |
catch (NullPointerException success) {} |
206 |
> |
} catch (NullPointerException success) {} |
207 |
|
} |
208 |
|
|
209 |
|
/** |
214 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
215 |
|
q.addAll(q); |
216 |
|
shouldThrow(); |
217 |
< |
} |
212 |
< |
catch (IllegalArgumentException success) {} |
217 |
> |
} catch (IllegalArgumentException success) {} |
218 |
|
} |
219 |
|
|
220 |
|
/** |
226 |
|
Integer[] ints = new Integer[SIZE]; |
227 |
|
q.addAll(Arrays.asList(ints)); |
228 |
|
shouldThrow(); |
229 |
< |
} |
225 |
< |
catch (NullPointerException success) {} |
229 |
> |
} catch (NullPointerException success) {} |
230 |
|
} |
231 |
+ |
|
232 |
|
/** |
233 |
|
* addAll of a collection with any null elements throws NPE after |
234 |
|
* possibly adding some elements |
241 |
|
ints[i] = new Integer(i); |
242 |
|
q.addAll(Arrays.asList(ints)); |
243 |
|
shouldThrow(); |
244 |
< |
} |
240 |
< |
catch (NullPointerException success) {} |
244 |
> |
} catch (NullPointerException success) {} |
245 |
|
} |
246 |
+ |
|
247 |
|
/** |
248 |
|
* addAll throws ISE if not enough room |
249 |
|
*/ |
255 |
|
ints[i] = new Integer(i); |
256 |
|
q.addAll(Arrays.asList(ints)); |
257 |
|
shouldThrow(); |
258 |
< |
} |
254 |
< |
catch (IllegalStateException success) {} |
258 |
> |
} catch (IllegalStateException success) {} |
259 |
|
} |
260 |
+ |
|
261 |
|
/** |
262 |
|
* Queue contains all elements, in traversal order, of successful addAll |
263 |
|
*/ |
264 |
|
public void testAddAll5() { |
265 |
< |
try { |
266 |
< |
Integer[] empty = new Integer[0]; |
267 |
< |
Integer[] ints = new Integer[SIZE]; |
268 |
< |
for (int i = 0; i < SIZE; ++i) |
269 |
< |
ints[i] = new Integer(i); |
270 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
271 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
272 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
273 |
< |
for (int i = 0; i < SIZE; ++i) |
269 |
< |
assertEquals(ints[i], q.poll()); |
270 |
< |
} |
271 |
< |
finally {} |
265 |
> |
Integer[] empty = new Integer[0]; |
266 |
> |
Integer[] ints = new Integer[SIZE]; |
267 |
> |
for (int i = 0; i < SIZE; ++i) |
268 |
> |
ints[i] = new Integer(i); |
269 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
270 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
271 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
272 |
> |
for (int i = 0; i < SIZE; ++i) |
273 |
> |
assertEquals(ints[i], q.poll()); |
274 |
|
} |
275 |
|
|
276 |
|
/** |
277 |
|
* put(null) throws NPE |
278 |
|
*/ |
279 |
< |
public void testPutNull() { |
279 |
> |
public void testPutNull() throws InterruptedException { |
280 |
|
try { |
281 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
282 |
|
q.put(null); |
283 |
|
shouldThrow(); |
284 |
< |
} |
285 |
< |
catch (NullPointerException success) { |
284 |
< |
} |
285 |
< |
catch (InterruptedException ie) { |
286 |
< |
unexpectedException(); |
287 |
< |
} |
288 |
< |
} |
284 |
> |
} catch (NullPointerException success) {} |
285 |
> |
} |
286 |
|
|
287 |
|
/** |
288 |
|
* all elements successfully put are contained |
289 |
|
*/ |
290 |
< |
public void testPut() { |
291 |
< |
try { |
292 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
293 |
< |
for (int i = 0; i < SIZE; ++i) { |
294 |
< |
Integer I = new Integer(i); |
295 |
< |
q.put(I); |
299 |
< |
assertTrue(q.contains(I)); |
300 |
< |
} |
301 |
< |
assertEquals(0, q.remainingCapacity()); |
302 |
< |
} |
303 |
< |
catch (InterruptedException ie) { |
304 |
< |
unexpectedException(); |
290 |
> |
public void testPut() throws InterruptedException { |
291 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
292 |
> |
for (int i = 0; i < SIZE; ++i) { |
293 |
> |
Integer I = new Integer(i); |
294 |
> |
q.put(I); |
295 |
> |
assertTrue(q.contains(I)); |
296 |
|
} |
297 |
+ |
assertEquals(0, q.remainingCapacity()); |
298 |
|
} |
299 |
|
|
300 |
|
/** |
301 |
|
* put blocks interruptibly if full |
302 |
|
*/ |
303 |
< |
public void testBlockingPut() { |
304 |
< |
Thread t = new Thread(new Runnable() { |
305 |
< |
public void run() { |
306 |
< |
int added = 0; |
307 |
< |
try { |
308 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
309 |
< |
for (int i = 0; i < SIZE; ++i) { |
310 |
< |
q.put(new Integer(i)); |
311 |
< |
++added; |
312 |
< |
} |
313 |
< |
q.put(new Integer(SIZE)); |
314 |
< |
threadShouldThrow(); |
315 |
< |
} catch (InterruptedException ie) { |
316 |
< |
threadAssertEquals(added, SIZE); |
317 |
< |
} |
318 |
< |
}}); |
319 |
< |
t.start(); |
320 |
< |
try { |
321 |
< |
Thread.sleep(SHORT_DELAY_MS); |
322 |
< |
t.interrupt(); |
323 |
< |
t.join(); |
324 |
< |
} |
325 |
< |
catch (InterruptedException ie) { |
326 |
< |
unexpectedException(); |
327 |
< |
} |
303 |
> |
public void testBlockingPut() throws InterruptedException { |
304 |
> |
final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
305 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
306 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
307 |
> |
public void realRun() throws InterruptedException { |
308 |
> |
for (int i = 0; i < SIZE; ++i) |
309 |
> |
q.put(i); |
310 |
> |
assertEquals(SIZE, q.size()); |
311 |
> |
assertEquals(0, q.remainingCapacity()); |
312 |
> |
|
313 |
> |
Thread.currentThread().interrupt(); |
314 |
> |
try { |
315 |
> |
q.put(99); |
316 |
> |
shouldThrow(); |
317 |
> |
} catch (InterruptedException success) {} |
318 |
> |
assertFalse(Thread.interrupted()); |
319 |
> |
|
320 |
> |
pleaseInterrupt.countDown(); |
321 |
> |
try { |
322 |
> |
q.put(99); |
323 |
> |
shouldThrow(); |
324 |
> |
} catch (InterruptedException success) {} |
325 |
> |
assertFalse(Thread.interrupted()); |
326 |
> |
}}); |
327 |
> |
|
328 |
> |
await(pleaseInterrupt); |
329 |
> |
assertThreadStaysAlive(t); |
330 |
> |
t.interrupt(); |
331 |
> |
awaitTermination(t); |
332 |
> |
assertEquals(SIZE, q.size()); |
333 |
> |
assertEquals(0, q.remainingCapacity()); |
334 |
|
} |
335 |
|
|
336 |
|
/** |
337 |
< |
* put blocks waiting for take when full |
337 |
> |
* put blocks interruptibly waiting for take when full |
338 |
|
*/ |
339 |
< |
public void testPutWithTake() { |
339 |
> |
public void testPutWithTake() throws InterruptedException { |
340 |
> |
final int capacity = 2; |
341 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
342 |
< |
Thread t = new Thread(new Runnable() { |
343 |
< |
public void run() { |
344 |
< |
int added = 0; |
345 |
< |
try { |
346 |
< |
q.put(new Object()); |
347 |
< |
++added; |
348 |
< |
q.put(new Object()); |
349 |
< |
++added; |
350 |
< |
q.put(new Object()); |
351 |
< |
++added; |
352 |
< |
q.put(new Object()); |
353 |
< |
++added; |
354 |
< |
threadShouldThrow(); |
355 |
< |
} catch (InterruptedException e) { |
356 |
< |
threadAssertTrue(added >= 2); |
357 |
< |
} |
358 |
< |
} |
359 |
< |
}); |
360 |
< |
try { |
361 |
< |
t.start(); |
362 |
< |
Thread.sleep(SHORT_DELAY_MS); |
363 |
< |
q.take(); |
364 |
< |
t.interrupt(); |
365 |
< |
t.join(); |
366 |
< |
} catch (Exception e) { |
367 |
< |
unexpectedException(); |
369 |
< |
} |
342 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
343 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
344 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
345 |
> |
public void realRun() throws InterruptedException { |
346 |
> |
for (int i = 0; i < capacity; i++) |
347 |
> |
q.put(i); |
348 |
> |
pleaseTake.countDown(); |
349 |
> |
q.put(86); |
350 |
> |
|
351 |
> |
pleaseInterrupt.countDown(); |
352 |
> |
try { |
353 |
> |
q.put(99); |
354 |
> |
shouldThrow(); |
355 |
> |
} catch (InterruptedException success) {} |
356 |
> |
assertFalse(Thread.interrupted()); |
357 |
> |
}}); |
358 |
> |
|
359 |
> |
await(pleaseTake); |
360 |
> |
assertEquals(q.remainingCapacity(), 0); |
361 |
> |
assertEquals(0, q.take()); |
362 |
> |
|
363 |
> |
await(pleaseInterrupt); |
364 |
> |
assertThreadStaysAlive(t); |
365 |
> |
t.interrupt(); |
366 |
> |
awaitTermination(t); |
367 |
> |
assertEquals(q.remainingCapacity(), 0); |
368 |
|
} |
369 |
|
|
370 |
|
/** |
372 |
|
*/ |
373 |
|
public void testTimedOffer() { |
374 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
375 |
< |
Thread t = new Thread(new Runnable() { |
376 |
< |
public void run() { |
377 |
< |
try { |
378 |
< |
q.put(new Object()); |
379 |
< |
q.put(new Object()); |
380 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
381 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
382 |
< |
threadShouldThrow(); |
383 |
< |
} catch (InterruptedException success) {} |
384 |
< |
} |
385 |
< |
}); |
386 |
< |
|
387 |
< |
try { |
388 |
< |
t.start(); |
389 |
< |
Thread.sleep(SMALL_DELAY_MS); |
390 |
< |
t.interrupt(); |
391 |
< |
t.join(); |
392 |
< |
} catch (Exception e) { |
393 |
< |
unexpectedException(); |
396 |
< |
} |
375 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
376 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
377 |
> |
public void realRun() throws InterruptedException { |
378 |
> |
q.put(new Object()); |
379 |
> |
q.put(new Object()); |
380 |
> |
long startTime = System.nanoTime(); |
381 |
> |
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
382 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
383 |
> |
pleaseInterrupt.countDown(); |
384 |
> |
try { |
385 |
> |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
386 |
> |
shouldThrow(); |
387 |
> |
} catch (InterruptedException success) {} |
388 |
> |
}}); |
389 |
> |
|
390 |
> |
await(pleaseInterrupt); |
391 |
> |
assertThreadStaysAlive(t); |
392 |
> |
t.interrupt(); |
393 |
> |
awaitTermination(t); |
394 |
|
} |
395 |
|
|
396 |
|
/** |
397 |
|
* take retrieves elements in FIFO order |
398 |
|
*/ |
399 |
< |
public void testTake() { |
400 |
< |
try { |
401 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
402 |
< |
for (int i = 0; i < SIZE; ++i) { |
406 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
407 |
< |
} |
408 |
< |
} catch (InterruptedException e) { |
409 |
< |
unexpectedException(); |
399 |
> |
public void testTake() throws InterruptedException { |
400 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
401 |
> |
for (int i = 0; i < SIZE; ++i) { |
402 |
> |
assertEquals(i, q.take()); |
403 |
|
} |
404 |
|
} |
405 |
|
|
406 |
|
/** |
407 |
< |
* take blocks interruptibly when empty |
407 |
> |
* Take removes existing elements until empty, then blocks interruptibly |
408 |
|
*/ |
409 |
< |
public void testTakeFromEmpty() { |
410 |
< |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
411 |
< |
Thread t = new Thread(new Runnable() { |
412 |
< |
public void run() { |
413 |
< |
try { |
414 |
< |
q.take(); |
415 |
< |
threadShouldThrow(); |
423 |
< |
} catch (InterruptedException success) { } |
409 |
> |
public void testBlockingTake() throws InterruptedException { |
410 |
> |
final BlockingQueue q = populatedQueue(SIZE); |
411 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
412 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
413 |
> |
public void realRun() throws InterruptedException { |
414 |
> |
for (int i = 0; i < SIZE; ++i) { |
415 |
> |
assertEquals(i, q.take()); |
416 |
|
} |
425 |
– |
}); |
426 |
– |
try { |
427 |
– |
t.start(); |
428 |
– |
Thread.sleep(SHORT_DELAY_MS); |
429 |
– |
t.interrupt(); |
430 |
– |
t.join(); |
431 |
– |
} catch (Exception e) { |
432 |
– |
unexpectedException(); |
433 |
– |
} |
434 |
– |
} |
417 |
|
|
418 |
< |
/** |
419 |
< |
* Take removes existing elements until empty, then blocks interruptibly |
420 |
< |
*/ |
421 |
< |
public void testBlockingTake() { |
422 |
< |
Thread t = new Thread(new Runnable() { |
423 |
< |
public void run() { |
442 |
< |
try { |
443 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
444 |
< |
for (int i = 0; i < SIZE; ++i) { |
445 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
446 |
< |
} |
447 |
< |
q.take(); |
448 |
< |
threadShouldThrow(); |
449 |
< |
} catch (InterruptedException success) { |
450 |
< |
} |
451 |
< |
}}); |
452 |
< |
t.start(); |
453 |
< |
try { |
454 |
< |
Thread.sleep(SHORT_DELAY_MS); |
455 |
< |
t.interrupt(); |
456 |
< |
t.join(); |
457 |
< |
} |
458 |
< |
catch (InterruptedException ie) { |
459 |
< |
unexpectedException(); |
460 |
< |
} |
461 |
< |
} |
418 |
> |
Thread.currentThread().interrupt(); |
419 |
> |
try { |
420 |
> |
q.take(); |
421 |
> |
shouldThrow(); |
422 |
> |
} catch (InterruptedException success) {} |
423 |
> |
assertFalse(Thread.interrupted()); |
424 |
|
|
425 |
+ |
pleaseInterrupt.countDown(); |
426 |
+ |
try { |
427 |
+ |
q.take(); |
428 |
+ |
shouldThrow(); |
429 |
+ |
} catch (InterruptedException success) {} |
430 |
+ |
assertFalse(Thread.interrupted()); |
431 |
+ |
}}); |
432 |
+ |
|
433 |
+ |
await(pleaseInterrupt); |
434 |
+ |
assertThreadStaysAlive(t); |
435 |
+ |
t.interrupt(); |
436 |
+ |
awaitTermination(t); |
437 |
+ |
} |
438 |
|
|
439 |
|
/** |
440 |
|
* poll succeeds unless empty |
442 |
|
public void testPoll() { |
443 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
444 |
|
for (int i = 0; i < SIZE; ++i) { |
445 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
445 |
> |
assertEquals(i, q.poll()); |
446 |
|
} |
447 |
|
assertNull(q.poll()); |
448 |
|
} |
449 |
|
|
450 |
|
/** |
451 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
451 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
452 |
|
*/ |
453 |
< |
public void testTimedPoll0() { |
454 |
< |
try { |
455 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
456 |
< |
for (int i = 0; i < SIZE; ++i) { |
482 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
483 |
< |
} |
484 |
< |
assertNull(q.poll(0, MILLISECONDS)); |
485 |
< |
} catch (InterruptedException e) { |
486 |
< |
unexpectedException(); |
453 |
> |
public void testTimedPoll0() throws InterruptedException { |
454 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
455 |
> |
for (int i = 0; i < SIZE; ++i) { |
456 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
457 |
|
} |
458 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
459 |
|
} |
460 |
|
|
461 |
|
/** |
462 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
462 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
463 |
|
*/ |
464 |
< |
public void testTimedPoll() { |
465 |
< |
try { |
466 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
467 |
< |
for (int i = 0; i < SIZE; ++i) { |
468 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
469 |
< |
} |
470 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
471 |
< |
} catch (InterruptedException e) { |
472 |
< |
unexpectedException(); |
473 |
< |
} |
464 |
> |
public void testTimedPoll() throws InterruptedException { |
465 |
> |
LinkedBlockingQueue<Integer> q = populatedQueue(SIZE); |
466 |
> |
for (int i = 0; i < SIZE; ++i) { |
467 |
> |
long startTime = System.nanoTime(); |
468 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
469 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
470 |
> |
} |
471 |
> |
long startTime = System.nanoTime(); |
472 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
473 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
474 |
> |
checkEmpty(q); |
475 |
|
} |
476 |
|
|
477 |
|
/** |
478 |
|
* Interrupted timed poll throws InterruptedException instead of |
479 |
|
* returning timeout status |
480 |
|
*/ |
481 |
< |
public void testInterruptedTimedPoll() { |
482 |
< |
Thread t = new Thread(new Runnable() { |
483 |
< |
public void run() { |
484 |
< |
try { |
485 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
486 |
< |
for (int i = 0; i < SIZE; ++i) { |
487 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
488 |
< |
} |
489 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
518 |
< |
} catch (InterruptedException success) { |
519 |
< |
} |
520 |
< |
}}); |
521 |
< |
t.start(); |
522 |
< |
try { |
523 |
< |
Thread.sleep(SHORT_DELAY_MS); |
524 |
< |
t.interrupt(); |
525 |
< |
t.join(); |
526 |
< |
} |
527 |
< |
catch (InterruptedException ie) { |
528 |
< |
unexpectedException(); |
529 |
< |
} |
530 |
< |
} |
531 |
< |
|
532 |
< |
/** |
533 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
534 |
< |
* on interruption throws |
535 |
< |
*/ |
536 |
< |
public void testTimedPollWithOffer() { |
537 |
< |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
538 |
< |
Thread t = new Thread(new Runnable() { |
539 |
< |
public void run() { |
540 |
< |
try { |
541 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
542 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
543 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
544 |
< |
threadShouldThrow(); |
545 |
< |
} catch (InterruptedException success) { } |
481 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
482 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
483 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
484 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
485 |
> |
public void realRun() throws InterruptedException { |
486 |
> |
for (int i = 0; i < SIZE; ++i) { |
487 |
> |
long t0 = System.nanoTime(); |
488 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
489 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
490 |
|
} |
491 |
< |
}); |
492 |
< |
try { |
493 |
< |
t.start(); |
494 |
< |
Thread.sleep(SMALL_DELAY_MS); |
495 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
496 |
< |
t.interrupt(); |
497 |
< |
t.join(); |
498 |
< |
} catch (Exception e) { |
499 |
< |
unexpectedException(); |
500 |
< |
} |
491 |
> |
long t0 = System.nanoTime(); |
492 |
> |
aboutToWait.countDown(); |
493 |
> |
try { |
494 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
495 |
> |
shouldThrow(); |
496 |
> |
} catch (InterruptedException success) { |
497 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
498 |
> |
} |
499 |
> |
}}); |
500 |
> |
|
501 |
> |
aboutToWait.await(); |
502 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
503 |
> |
t.interrupt(); |
504 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
505 |
> |
checkEmpty(q); |
506 |
|
} |
507 |
|
|
508 |
|
/** |
511 |
|
public void testPeek() { |
512 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
513 |
|
for (int i = 0; i < SIZE; ++i) { |
514 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
515 |
< |
q.poll(); |
514 |
> |
assertEquals(i, q.peek()); |
515 |
> |
assertEquals(i, q.poll()); |
516 |
|
assertTrue(q.peek() == null || |
517 |
< |
i != ((Integer)q.peek()).intValue()); |
517 |
> |
!q.peek().equals(i)); |
518 |
|
} |
519 |
|
assertNull(q.peek()); |
520 |
|
} |
525 |
|
public void testElement() { |
526 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
527 |
|
for (int i = 0; i < SIZE; ++i) { |
528 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
529 |
< |
q.poll(); |
528 |
> |
assertEquals(i, q.element()); |
529 |
> |
assertEquals(i, q.poll()); |
530 |
|
} |
531 |
|
try { |
532 |
|
q.element(); |
533 |
|
shouldThrow(); |
534 |
< |
} |
586 |
< |
catch (NoSuchElementException success) {} |
534 |
> |
} catch (NoSuchElementException success) {} |
535 |
|
} |
536 |
|
|
537 |
|
/** |
540 |
|
public void testRemove() { |
541 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
542 |
|
for (int i = 0; i < SIZE; ++i) { |
543 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
543 |
> |
assertEquals(i, q.remove()); |
544 |
|
} |
545 |
|
try { |
546 |
|
q.remove(); |
547 |
|
shouldThrow(); |
548 |
< |
} catch (NoSuchElementException success) { |
601 |
< |
} |
548 |
> |
} catch (NoSuchElementException success) {} |
549 |
|
} |
550 |
|
|
551 |
|
/** |
554 |
|
public void testRemoveElement() { |
555 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
556 |
|
for (int i = 1; i < SIZE; i+=2) { |
557 |
< |
assertTrue(q.remove(new Integer(i))); |
557 |
> |
assertTrue(q.contains(i)); |
558 |
> |
assertTrue(q.remove(i)); |
559 |
> |
assertFalse(q.contains(i)); |
560 |
> |
assertTrue(q.contains(i-1)); |
561 |
|
} |
562 |
|
for (int i = 0; i < SIZE; i+=2) { |
563 |
< |
assertTrue(q.remove(new Integer(i))); |
564 |
< |
assertFalse(q.remove(new Integer(i+1))); |
563 |
> |
assertTrue(q.contains(i)); |
564 |
> |
assertTrue(q.remove(i)); |
565 |
> |
assertFalse(q.contains(i)); |
566 |
> |
assertFalse(q.remove(i+1)); |
567 |
> |
assertFalse(q.contains(i+1)); |
568 |
|
} |
569 |
|
assertTrue(q.isEmpty()); |
570 |
|
} |
572 |
|
/** |
573 |
|
* An add following remove(x) succeeds |
574 |
|
*/ |
575 |
< |
public void testRemoveElementAndAdd() { |
576 |
< |
try { |
577 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
578 |
< |
assertTrue(q.add(new Integer(1))); |
579 |
< |
assertTrue(q.add(new Integer(2))); |
580 |
< |
assertTrue(q.remove(new Integer(1))); |
581 |
< |
assertTrue(q.remove(new Integer(2))); |
582 |
< |
assertTrue(q.add(new Integer(3))); |
630 |
< |
assertTrue(q.take() != null); |
631 |
< |
} catch (Exception e) { |
632 |
< |
unexpectedException(); |
633 |
< |
} |
575 |
> |
public void testRemoveElementAndAdd() throws InterruptedException { |
576 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
577 |
> |
assertTrue(q.add(new Integer(1))); |
578 |
> |
assertTrue(q.add(new Integer(2))); |
579 |
> |
assertTrue(q.remove(new Integer(1))); |
580 |
> |
assertTrue(q.remove(new Integer(2))); |
581 |
> |
assertTrue(q.add(new Integer(3))); |
582 |
> |
assertTrue(q.take() != null); |
583 |
|
} |
584 |
|
|
585 |
|
/** |
660 |
|
} |
661 |
|
|
662 |
|
/** |
663 |
< |
* toArray contains all elements |
663 |
> |
* toArray contains all elements in FIFO order |
664 |
|
*/ |
665 |
|
public void testToArray() { |
666 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
667 |
|
Object[] o = q.toArray(); |
719 |
– |
try { |
668 |
|
for (int i = 0; i < o.length; i++) |
669 |
< |
assertEquals(o[i], q.take()); |
722 |
< |
} catch (InterruptedException e) { |
723 |
< |
unexpectedException(); |
724 |
< |
} |
669 |
> |
assertSame(o[i], q.poll()); |
670 |
|
} |
671 |
|
|
672 |
|
/** |
673 |
< |
* toArray(a) contains all elements |
673 |
> |
* toArray(a) contains all elements in FIFO order |
674 |
|
*/ |
675 |
< |
public void testToArray2() { |
676 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
675 |
> |
public void testToArray2() throws InterruptedException { |
676 |
> |
LinkedBlockingQueue<Integer> q = populatedQueue(SIZE); |
677 |
|
Integer[] ints = new Integer[SIZE]; |
678 |
< |
ints = (Integer[])q.toArray(ints); |
679 |
< |
try { |
680 |
< |
for (int i = 0; i < ints.length; i++) |
681 |
< |
assertEquals(ints[i], q.take()); |
737 |
< |
} catch (InterruptedException e) { |
738 |
< |
unexpectedException(); |
739 |
< |
} |
678 |
> |
Integer[] array = q.toArray(ints); |
679 |
> |
assertSame(ints, array); |
680 |
> |
for (int i = 0; i < ints.length; i++) |
681 |
> |
assertSame(ints[i], q.poll()); |
682 |
|
} |
683 |
|
|
684 |
|
/** |
685 |
< |
* toArray(null) throws NPE |
685 |
> |
* toArray(null) throws NullPointerException |
686 |
|
*/ |
687 |
< |
public void testToArray_BadArg() { |
687 |
> |
public void testToArray_NullArg() { |
688 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
689 |
|
try { |
690 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
748 |
< |
Object o[] = q.toArray(null); |
690 |
> |
q.toArray(null); |
691 |
|
shouldThrow(); |
692 |
|
} catch (NullPointerException success) {} |
693 |
|
} |
694 |
|
|
695 |
|
/** |
696 |
< |
* toArray with incompatible array type throws CCE |
696 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
697 |
|
*/ |
698 |
|
public void testToArray1_BadArg() { |
699 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
700 |
|
try { |
701 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
759 |
< |
Object o[] = q.toArray(new String[10] ); |
701 |
> |
q.toArray(new String[10]); |
702 |
|
shouldThrow(); |
703 |
< |
} catch (ArrayStoreException success) {} |
703 |
> |
} catch (ArrayStoreException success) {} |
704 |
|
} |
705 |
|
|
764 |
– |
|
706 |
|
/** |
707 |
|
* iterator iterates through all elements |
708 |
|
*/ |
709 |
< |
public void testIterator() { |
709 |
> |
public void testIterator() throws InterruptedException { |
710 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
711 |
|
Iterator it = q.iterator(); |
712 |
< |
try { |
713 |
< |
while (it.hasNext()) { |
773 |
< |
assertEquals(it.next(), q.take()); |
774 |
< |
} |
775 |
< |
} catch (InterruptedException e) { |
776 |
< |
unexpectedException(); |
712 |
> |
while (it.hasNext()) { |
713 |
> |
assertEquals(it.next(), q.take()); |
714 |
|
} |
715 |
|
} |
716 |
|
|
717 |
|
/** |
718 |
|
* iterator.remove removes current element |
719 |
|
*/ |
720 |
< |
public void testIteratorRemove () { |
720 |
> |
public void testIteratorRemove() { |
721 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
722 |
|
q.add(two); |
723 |
|
q.add(one); |
728 |
|
it.remove(); |
729 |
|
|
730 |
|
it = q.iterator(); |
731 |
< |
assertEquals(it.next(), one); |
732 |
< |
assertEquals(it.next(), three); |
731 |
> |
assertSame(it.next(), one); |
732 |
> |
assertSame(it.next(), three); |
733 |
|
assertFalse(it.hasNext()); |
734 |
|
} |
735 |
|
|
799 |
– |
|
736 |
|
/** |
737 |
|
* iterator ordering is FIFO |
738 |
|
*/ |
744 |
|
assertEquals(0, q.remainingCapacity()); |
745 |
|
int k = 0; |
746 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
747 |
< |
int i = ((Integer)(it.next())).intValue(); |
812 |
< |
assertEquals(++k, i); |
747 |
> |
assertEquals(++k, it.next()); |
748 |
|
} |
749 |
|
assertEquals(3, k); |
750 |
|
} |
752 |
|
/** |
753 |
|
* Modifications do not cause iterators to fail |
754 |
|
*/ |
755 |
< |
public void testWeaklyConsistentIteration () { |
755 |
> |
public void testWeaklyConsistentIteration() { |
756 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
757 |
|
q.add(one); |
758 |
|
q.add(two); |
759 |
|
q.add(three); |
760 |
< |
try { |
761 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
762 |
< |
q.remove(); |
828 |
< |
it.next(); |
829 |
< |
} |
830 |
< |
} |
831 |
< |
catch (ConcurrentModificationException e) { |
832 |
< |
unexpectedException(); |
760 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
761 |
> |
q.remove(); |
762 |
> |
it.next(); |
763 |
|
} |
764 |
|
assertEquals(0, q.size()); |
765 |
|
} |
766 |
|
|
837 |
– |
|
767 |
|
/** |
768 |
|
* toString contains toStrings of elements |
769 |
|
*/ |
771 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
772 |
|
String s = q.toString(); |
773 |
|
for (int i = 0; i < SIZE; ++i) { |
774 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
774 |
> |
assertTrue(s.contains(String.valueOf(i))); |
775 |
|
} |
776 |
|
} |
777 |
|
|
849 |
– |
|
778 |
|
/** |
779 |
|
* offer transfers elements across Executor tasks |
780 |
|
*/ |
783 |
|
q.add(one); |
784 |
|
q.add(two); |
785 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
786 |
< |
executor.execute(new Runnable() { |
787 |
< |
public void run() { |
788 |
< |
threadAssertFalse(q.offer(three)); |
789 |
< |
try { |
790 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
791 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
792 |
< |
} |
793 |
< |
catch (InterruptedException e) { |
794 |
< |
threadUnexpectedException(); |
795 |
< |
} |
796 |
< |
} |
797 |
< |
}); |
798 |
< |
|
799 |
< |
executor.execute(new Runnable() { |
872 |
< |
public void run() { |
873 |
< |
try { |
874 |
< |
Thread.sleep(SMALL_DELAY_MS); |
875 |
< |
threadAssertEquals(one, q.take()); |
876 |
< |
} |
877 |
< |
catch (InterruptedException e) { |
878 |
< |
threadUnexpectedException(); |
879 |
< |
} |
880 |
< |
} |
881 |
< |
}); |
786 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
787 |
> |
executor.execute(new CheckedRunnable() { |
788 |
> |
public void realRun() throws InterruptedException { |
789 |
> |
assertFalse(q.offer(three)); |
790 |
> |
threadsStarted.await(); |
791 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
792 |
> |
assertEquals(0, q.remainingCapacity()); |
793 |
> |
}}); |
794 |
> |
|
795 |
> |
executor.execute(new CheckedRunnable() { |
796 |
> |
public void realRun() throws InterruptedException { |
797 |
> |
threadsStarted.await(); |
798 |
> |
assertSame(one, q.take()); |
799 |
> |
}}); |
800 |
|
|
801 |
|
joinPool(executor); |
802 |
|
} |
803 |
|
|
804 |
|
/** |
805 |
< |
* poll retrieves elements across Executor threads |
805 |
> |
* timed poll retrieves elements across Executor threads |
806 |
|
*/ |
807 |
|
public void testPollInExecutor() { |
808 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
809 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
810 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
811 |
< |
executor.execute(new Runnable() { |
812 |
< |
public void run() { |
813 |
< |
threadAssertNull(q.poll()); |
814 |
< |
try { |
815 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
816 |
< |
threadAssertTrue(q.isEmpty()); |
817 |
< |
} |
818 |
< |
catch (InterruptedException e) { |
819 |
< |
threadUnexpectedException(); |
820 |
< |
} |
821 |
< |
} |
822 |
< |
}); |
823 |
< |
|
905 |
< |
executor.execute(new Runnable() { |
906 |
< |
public void run() { |
907 |
< |
try { |
908 |
< |
Thread.sleep(SMALL_DELAY_MS); |
909 |
< |
q.put(one); |
910 |
< |
} |
911 |
< |
catch (InterruptedException e) { |
912 |
< |
threadUnexpectedException(); |
913 |
< |
} |
914 |
< |
} |
915 |
< |
}); |
811 |
> |
executor.execute(new CheckedRunnable() { |
812 |
> |
public void realRun() throws InterruptedException { |
813 |
> |
assertNull(q.poll()); |
814 |
> |
threadsStarted.await(); |
815 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
816 |
> |
checkEmpty(q); |
817 |
> |
}}); |
818 |
> |
|
819 |
> |
executor.execute(new CheckedRunnable() { |
820 |
> |
public void realRun() throws InterruptedException { |
821 |
> |
threadsStarted.await(); |
822 |
> |
q.put(one); |
823 |
> |
}}); |
824 |
|
|
825 |
|
joinPool(executor); |
826 |
|
} |
828 |
|
/** |
829 |
|
* A deserialized serialized queue has same elements in same order |
830 |
|
*/ |
831 |
< |
public void testSerialization() { |
831 |
> |
public void testSerialization() throws Exception { |
832 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
833 |
|
|
834 |
< |
try { |
835 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
836 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
837 |
< |
out.writeObject(q); |
838 |
< |
out.close(); |
839 |
< |
|
840 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
841 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
842 |
< |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
843 |
< |
assertEquals(q.size(), r.size()); |
844 |
< |
while (!q.isEmpty()) |
937 |
< |
assertEquals(q.remove(), r.remove()); |
938 |
< |
} catch (Exception e) { |
939 |
< |
unexpectedException(); |
940 |
< |
} |
834 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
835 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
836 |
> |
out.writeObject(q); |
837 |
> |
out.close(); |
838 |
> |
|
839 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
840 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
841 |
> |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
842 |
> |
assertEquals(q.size(), r.size()); |
843 |
> |
while (!q.isEmpty()) |
844 |
> |
assertEquals(q.remove(), r.remove()); |
845 |
|
} |
846 |
|
|
847 |
|
/** |
852 |
|
try { |
853 |
|
q.drainTo(null); |
854 |
|
shouldThrow(); |
855 |
< |
} catch (NullPointerException success) { |
952 |
< |
} |
855 |
> |
} catch (NullPointerException success) {} |
856 |
|
} |
857 |
|
|
858 |
|
/** |
863 |
|
try { |
864 |
|
q.drainTo(q); |
865 |
|
shouldThrow(); |
866 |
< |
} catch (IllegalArgumentException success) { |
964 |
< |
} |
866 |
> |
} catch (IllegalArgumentException success) {} |
867 |
|
} |
868 |
|
|
869 |
|
/** |
893 |
|
/** |
894 |
|
* drainTo empties full queue, unblocking a waiting put. |
895 |
|
*/ |
896 |
< |
public void testDrainToWithActivePut() { |
896 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
897 |
|
final LinkedBlockingQueue q = populatedQueue(SIZE); |
898 |
< |
Thread t = new Thread(new Runnable() { |
899 |
< |
public void run() { |
900 |
< |
try { |
901 |
< |
q.put(new Integer(SIZE+1)); |
902 |
< |
} catch (InterruptedException ie) { |
903 |
< |
threadUnexpectedException(); |
904 |
< |
} |
905 |
< |
} |
906 |
< |
}); |
907 |
< |
try { |
908 |
< |
t.start(); |
909 |
< |
ArrayList l = new ArrayList(); |
910 |
< |
q.drainTo(l); |
1009 |
< |
assertTrue(l.size() >= SIZE); |
1010 |
< |
for (int i = 0; i < SIZE; ++i) |
1011 |
< |
assertEquals(l.get(i), new Integer(i)); |
1012 |
< |
t.join(); |
1013 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
1014 |
< |
} catch (Exception e) { |
1015 |
< |
unexpectedException(); |
1016 |
< |
} |
898 |
> |
Thread t = new Thread(new CheckedRunnable() { |
899 |
> |
public void realRun() throws InterruptedException { |
900 |
> |
q.put(new Integer(SIZE+1)); |
901 |
> |
}}); |
902 |
> |
|
903 |
> |
t.start(); |
904 |
> |
ArrayList l = new ArrayList(); |
905 |
> |
q.drainTo(l); |
906 |
> |
assertTrue(l.size() >= SIZE); |
907 |
> |
for (int i = 0; i < SIZE; ++i) |
908 |
> |
assertEquals(l.get(i), new Integer(i)); |
909 |
> |
t.join(); |
910 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
911 |
|
} |
912 |
|
|
913 |
|
/** |
918 |
|
try { |
919 |
|
q.drainTo(null, 0); |
920 |
|
shouldThrow(); |
921 |
< |
} catch (NullPointerException success) { |
1028 |
< |
} |
921 |
> |
} catch (NullPointerException success) {} |
922 |
|
} |
923 |
|
|
924 |
|
/** |
929 |
|
try { |
930 |
|
q.drainTo(q, 0); |
931 |
|
shouldThrow(); |
932 |
< |
} catch (IllegalArgumentException success) { |
1040 |
< |
} |
932 |
> |
} catch (IllegalArgumentException success) {} |
933 |
|
} |
934 |
|
|
935 |
|
/** |
936 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
936 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
937 |
|
*/ |
938 |
|
public void testDrainToN() { |
939 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
942 |
|
assertTrue(q.offer(new Integer(j))); |
943 |
|
ArrayList l = new ArrayList(); |
944 |
|
q.drainTo(l, i); |
945 |
< |
int k = (i < SIZE)? i : SIZE; |
945 |
> |
int k = (i < SIZE) ? i : SIZE; |
946 |
|
assertEquals(l.size(), k); |
947 |
|
assertEquals(q.size(), SIZE-k); |
948 |
|
for (int j = 0; j < k; ++j) |