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 |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
|
import java.io.*; |
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 |
|
|
39 |
|
|
44 |
|
private LinkedBlockingQueue populatedQueue(int n) { |
45 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(n); |
46 |
|
assertTrue(q.isEmpty()); |
47 |
< |
for(int i = 0; i < n; i++) |
48 |
< |
assertTrue(q.offer(new Integer(i))); |
47 |
> |
for (int i = 0; i < n; i++) |
48 |
> |
assertTrue(q.offer(new Integer(i))); |
49 |
|
assertFalse(q.isEmpty()); |
50 |
|
assertEquals(0, q.remainingCapacity()); |
51 |
< |
assertEquals(n, q.size()); |
51 |
> |
assertEquals(n, q.size()); |
52 |
|
return q; |
53 |
|
} |
54 |
< |
|
54 |
> |
|
55 |
|
/** |
56 |
|
* A new queue has the indicated capacity, or Integer.MAX_VALUE if |
57 |
|
* none given |
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 |
< |
} |
57 |
< |
catch (IllegalArgumentException success) {} |
71 |
> |
} catch (IllegalArgumentException success) {} |
72 |
|
} |
73 |
|
|
74 |
|
/** |
78 |
|
try { |
79 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(null); |
80 |
|
shouldThrow(); |
81 |
< |
} |
68 |
< |
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 |
< |
} |
80 |
< |
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 |
< |
} |
94 |
< |
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) |
107 |
< |
assertEquals(ints[i], q.poll()); |
108 |
< |
} |
109 |
< |
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 |
|
/** |
153 |
|
* offer(null) throws NPE |
154 |
|
*/ |
155 |
|
public void testOfferNull() { |
156 |
< |
try { |
156 |
> |
try { |
157 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
158 |
|
q.offer(null); |
159 |
|
shouldThrow(); |
160 |
< |
} catch (NullPointerException success) { } |
160 |
> |
} catch (NullPointerException success) {} |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
|
* add(null) throws NPE |
165 |
|
*/ |
166 |
|
public void testAddNull() { |
167 |
< |
try { |
167 |
> |
try { |
168 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
169 |
|
q.add(null); |
170 |
|
shouldThrow(); |
171 |
< |
} catch (NullPointerException success) { } |
171 |
> |
} catch (NullPointerException success) {} |
172 |
|
} |
173 |
|
|
174 |
|
/** |
184 |
|
* add succeeds if not full; throws ISE if full |
185 |
|
*/ |
186 |
|
public void testAdd() { |
187 |
< |
try { |
187 |
> |
try { |
188 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
189 |
|
for (int i = 0; i < SIZE; ++i) { |
190 |
|
assertTrue(q.add(new Integer(i))); |
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 |
< |
} |
199 |
< |
catch (NullPointerException success) {} |
206 |
> |
} catch (NullPointerException success) {} |
207 |
|
} |
208 |
|
|
209 |
|
/** |
214 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
215 |
|
q.addAll(q); |
216 |
|
shouldThrow(); |
217 |
< |
} |
211 |
< |
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 |
< |
} |
224 |
< |
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 |
< |
} |
239 |
< |
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 |
< |
} |
253 |
< |
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) |
268 |
< |
assertEquals(ints[i], q.poll()); |
269 |
< |
} |
270 |
< |
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() { |
280 |
< |
try { |
279 |
> |
public void testPutNull() throws InterruptedException { |
280 |
> |
try { |
281 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
282 |
|
q.put(null); |
283 |
|
shouldThrow(); |
284 |
< |
} |
282 |
< |
catch (NullPointerException success){ |
283 |
< |
} |
284 |
< |
catch (InterruptedException ie) { |
285 |
< |
unexpectedException(); |
286 |
< |
} |
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); |
298 |
< |
assertTrue(q.contains(I)); |
299 |
< |
} |
300 |
< |
assertEquals(0, q.remainingCapacity()); |
301 |
< |
} |
302 |
< |
catch (InterruptedException ie) { |
303 |
< |
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); |
324 |
< |
} |
325 |
< |
}}); |
303 |
> |
public void testBlockingPut() throws InterruptedException { |
304 |
> |
final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
305 |
> |
Thread t = new Thread(new CheckedRunnable() { |
306 |
> |
public void realRun() throws InterruptedException { |
307 |
> |
for (int i = 0; i < SIZE; ++i) |
308 |
> |
q.put(i); |
309 |
> |
assertEquals(SIZE, q.size()); |
310 |
> |
assertEquals(0, q.remainingCapacity()); |
311 |
> |
try { |
312 |
> |
q.put(99); |
313 |
> |
shouldThrow(); |
314 |
> |
} catch (InterruptedException success) {} |
315 |
> |
}}); |
316 |
> |
|
317 |
|
t.start(); |
318 |
< |
try { |
319 |
< |
Thread.sleep(SHORT_DELAY_MS); |
320 |
< |
t.interrupt(); |
321 |
< |
t.join(); |
322 |
< |
} |
332 |
< |
catch (InterruptedException ie) { |
333 |
< |
unexpectedException(); |
334 |
< |
} |
318 |
> |
Thread.sleep(SHORT_DELAY_MS); |
319 |
> |
t.interrupt(); |
320 |
> |
t.join(); |
321 |
> |
assertEquals(SIZE, q.size()); |
322 |
> |
assertEquals(0, q.remainingCapacity()); |
323 |
|
} |
324 |
|
|
325 |
|
/** |
326 |
|
* put blocks waiting for take when full |
327 |
|
*/ |
328 |
< |
public void testPutWithTake() { |
328 |
> |
public void testPutWithTake() throws InterruptedException { |
329 |
> |
final int capacity = 2; |
330 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
331 |
< |
Thread t = new Thread(new Runnable() { |
332 |
< |
public void run() { |
333 |
< |
int added = 0; |
334 |
< |
try { |
335 |
< |
q.put(new Object()); |
336 |
< |
++added; |
337 |
< |
q.put(new Object()); |
338 |
< |
++added; |
339 |
< |
q.put(new Object()); |
340 |
< |
++added; |
341 |
< |
q.put(new Object()); |
342 |
< |
++added; |
343 |
< |
threadShouldThrow(); |
344 |
< |
} catch (InterruptedException e){ |
345 |
< |
threadAssertTrue(added >= 2); |
346 |
< |
} |
347 |
< |
} |
348 |
< |
}); |
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(); |
368 |
< |
} |
331 |
> |
Thread t = new Thread(new CheckedRunnable() { |
332 |
> |
public void realRun() throws InterruptedException { |
333 |
> |
for (int i = 0; i < capacity + 1; i++) |
334 |
> |
q.put(i); |
335 |
> |
try { |
336 |
> |
q.put(99); |
337 |
> |
shouldThrow(); |
338 |
> |
} catch (InterruptedException success) {} |
339 |
> |
}}); |
340 |
> |
|
341 |
> |
t.start(); |
342 |
> |
Thread.sleep(SHORT_DELAY_MS); |
343 |
> |
assertEquals(q.remainingCapacity(), 0); |
344 |
> |
assertEquals(0, q.take()); |
345 |
> |
Thread.sleep(SHORT_DELAY_MS); |
346 |
> |
t.interrupt(); |
347 |
> |
t.join(); |
348 |
> |
assertEquals(q.remainingCapacity(), 0); |
349 |
|
} |
350 |
|
|
351 |
|
/** |
352 |
|
* timed offer times out if full and elements not taken |
353 |
|
*/ |
354 |
< |
public void testTimedOffer() { |
354 |
> |
public void testTimedOffer() throws InterruptedException { |
355 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
356 |
< |
Thread t = new Thread(new Runnable() { |
357 |
< |
public void run() { |
358 |
< |
try { |
359 |
< |
q.put(new Object()); |
360 |
< |
q.put(new Object()); |
361 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
362 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
363 |
< |
threadShouldThrow(); |
364 |
< |
} catch (InterruptedException success){} |
365 |
< |
} |
386 |
< |
}); |
387 |
< |
|
388 |
< |
try { |
389 |
< |
t.start(); |
390 |
< |
Thread.sleep(SMALL_DELAY_MS); |
391 |
< |
t.interrupt(); |
392 |
< |
t.join(); |
393 |
< |
} catch (Exception e){ |
394 |
< |
unexpectedException(); |
395 |
< |
} |
396 |
< |
} |
356 |
> |
Thread t = new Thread(new CheckedRunnable() { |
357 |
> |
public void realRun() throws InterruptedException { |
358 |
> |
q.put(new Object()); |
359 |
> |
q.put(new Object()); |
360 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
361 |
> |
try { |
362 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
363 |
> |
shouldThrow(); |
364 |
> |
} catch (InterruptedException success) {} |
365 |
> |
}}); |
366 |
|
|
367 |
< |
/** |
368 |
< |
* take retrieves elements in FIFO order |
369 |
< |
*/ |
370 |
< |
public void testTake() { |
402 |
< |
try { |
403 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
404 |
< |
for (int i = 0; i < SIZE; ++i) { |
405 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
406 |
< |
} |
407 |
< |
} catch (InterruptedException e){ |
408 |
< |
unexpectedException(); |
409 |
< |
} |
367 |
> |
t.start(); |
368 |
> |
Thread.sleep(SMALL_DELAY_MS); |
369 |
> |
t.interrupt(); |
370 |
> |
t.join(); |
371 |
|
} |
372 |
|
|
373 |
|
/** |
374 |
< |
* take blocks interruptibly when empty |
374 |
> |
* take retrieves elements in FIFO order |
375 |
|
*/ |
376 |
< |
public void testTakeFromEmpty() { |
377 |
< |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
378 |
< |
Thread t = new Thread(new Runnable() { |
379 |
< |
public void run() { |
419 |
< |
try { |
420 |
< |
q.take(); |
421 |
< |
threadShouldThrow(); |
422 |
< |
} catch (InterruptedException success){ } |
423 |
< |
} |
424 |
< |
}); |
425 |
< |
try { |
426 |
< |
t.start(); |
427 |
< |
Thread.sleep(SHORT_DELAY_MS); |
428 |
< |
t.interrupt(); |
429 |
< |
t.join(); |
430 |
< |
} catch (Exception e){ |
431 |
< |
unexpectedException(); |
376 |
> |
public void testTake() throws InterruptedException { |
377 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
378 |
> |
for (int i = 0; i < SIZE; ++i) { |
379 |
> |
assertEquals(i, q.take()); |
380 |
|
} |
381 |
|
} |
382 |
|
|
383 |
|
/** |
384 |
|
* Take removes existing elements until empty, then blocks interruptibly |
385 |
|
*/ |
386 |
< |
public void testBlockingTake() { |
387 |
< |
Thread t = new Thread(new Runnable() { |
388 |
< |
public void run() { |
389 |
< |
try { |
390 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
391 |
< |
for (int i = 0; i < SIZE; ++i) { |
392 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
393 |
< |
} |
394 |
< |
q.take(); |
395 |
< |
threadShouldThrow(); |
396 |
< |
} catch (InterruptedException success){ |
397 |
< |
} |
398 |
< |
}}); |
386 |
> |
public void testBlockingTake() throws InterruptedException { |
387 |
> |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
388 |
> |
Thread t = new Thread(new CheckedRunnable() { |
389 |
> |
public void realRun() throws InterruptedException { |
390 |
> |
for (int i = 0; i < SIZE; ++i) { |
391 |
> |
assertEquals(i, q.take()); |
392 |
> |
} |
393 |
> |
try { |
394 |
> |
q.take(); |
395 |
> |
shouldThrow(); |
396 |
> |
} catch (InterruptedException success) {} |
397 |
> |
}}); |
398 |
> |
|
399 |
|
t.start(); |
400 |
< |
try { |
401 |
< |
Thread.sleep(SHORT_DELAY_MS); |
402 |
< |
t.interrupt(); |
455 |
< |
t.join(); |
456 |
< |
} |
457 |
< |
catch (InterruptedException ie) { |
458 |
< |
unexpectedException(); |
459 |
< |
} |
400 |
> |
Thread.sleep(SHORT_DELAY_MS); |
401 |
> |
t.interrupt(); |
402 |
> |
t.join(); |
403 |
|
} |
404 |
|
|
462 |
– |
|
405 |
|
/** |
406 |
|
* poll succeeds unless empty |
407 |
|
*/ |
408 |
|
public void testPoll() { |
409 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
410 |
|
for (int i = 0; i < SIZE; ++i) { |
411 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
411 |
> |
assertEquals(i, q.poll()); |
412 |
|
} |
413 |
< |
assertNull(q.poll()); |
413 |
> |
assertNull(q.poll()); |
414 |
|
} |
415 |
|
|
416 |
|
/** |
417 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
417 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
418 |
|
*/ |
419 |
< |
public void testTimedPoll0() { |
420 |
< |
try { |
421 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
422 |
< |
for (int i = 0; i < SIZE; ++i) { |
423 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
424 |
< |
} |
483 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
484 |
< |
} catch (InterruptedException e){ |
485 |
< |
unexpectedException(); |
486 |
< |
} |
419 |
> |
public void testTimedPoll0() throws InterruptedException { |
420 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
421 |
> |
for (int i = 0; i < SIZE; ++i) { |
422 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
423 |
> |
} |
424 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
425 |
|
} |
426 |
|
|
427 |
|
/** |
428 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
428 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
429 |
|
*/ |
430 |
< |
public void testTimedPoll() { |
431 |
< |
try { |
432 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
433 |
< |
for (int i = 0; i < SIZE; ++i) { |
434 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
435 |
< |
} |
498 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
499 |
< |
} catch (InterruptedException e){ |
500 |
< |
unexpectedException(); |
501 |
< |
} |
430 |
> |
public void testTimedPoll() throws InterruptedException { |
431 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
432 |
> |
for (int i = 0; i < SIZE; ++i) { |
433 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
434 |
> |
} |
435 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
436 |
|
} |
437 |
|
|
438 |
|
/** |
439 |
|
* Interrupted timed poll throws InterruptedException instead of |
440 |
|
* returning timeout status |
441 |
|
*/ |
442 |
< |
public void testInterruptedTimedPoll() { |
443 |
< |
Thread t = new Thread(new Runnable() { |
444 |
< |
public void run() { |
445 |
< |
try { |
446 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
447 |
< |
for (int i = 0; i < SIZE; ++i) { |
448 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
449 |
< |
} |
450 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
451 |
< |
} catch (InterruptedException success){ |
452 |
< |
} |
453 |
< |
}}); |
442 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
443 |
> |
Thread t = new Thread(new CheckedRunnable() { |
444 |
> |
public void realRun() throws InterruptedException { |
445 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
446 |
> |
for (int i = 0; i < SIZE; ++i) { |
447 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
448 |
> |
} |
449 |
> |
try { |
450 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
451 |
> |
shouldThrow(); |
452 |
> |
} catch (InterruptedException success) {} |
453 |
> |
}}); |
454 |
> |
|
455 |
|
t.start(); |
456 |
< |
try { |
457 |
< |
Thread.sleep(SHORT_DELAY_MS); |
458 |
< |
t.interrupt(); |
524 |
< |
t.join(); |
525 |
< |
} |
526 |
< |
catch (InterruptedException ie) { |
527 |
< |
unexpectedException(); |
528 |
< |
} |
456 |
> |
Thread.sleep(SHORT_DELAY_MS); |
457 |
> |
t.interrupt(); |
458 |
> |
t.join(); |
459 |
|
} |
460 |
|
|
461 |
|
/** |
532 |
– |
* timed poll before a delayed offer fails; after offer succeeds; |
533 |
– |
* on interruption throws |
534 |
– |
*/ |
535 |
– |
public void testTimedPollWithOffer() { |
536 |
– |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
537 |
– |
Thread t = new Thread(new Runnable() { |
538 |
– |
public void run() { |
539 |
– |
try { |
540 |
– |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
541 |
– |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
542 |
– |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
543 |
– |
threadShouldThrow(); |
544 |
– |
} catch (InterruptedException success) { } |
545 |
– |
} |
546 |
– |
}); |
547 |
– |
try { |
548 |
– |
t.start(); |
549 |
– |
Thread.sleep(SMALL_DELAY_MS); |
550 |
– |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
551 |
– |
t.interrupt(); |
552 |
– |
t.join(); |
553 |
– |
} catch (Exception e){ |
554 |
– |
unexpectedException(); |
555 |
– |
} |
556 |
– |
} |
557 |
– |
|
558 |
– |
/** |
462 |
|
* peek returns next element, or null if empty |
463 |
|
*/ |
464 |
|
public void testPeek() { |
465 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
466 |
|
for (int i = 0; i < SIZE; ++i) { |
467 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
468 |
< |
q.poll(); |
467 |
> |
assertEquals(i, q.peek()); |
468 |
> |
assertEquals(i, q.poll()); |
469 |
|
assertTrue(q.peek() == null || |
470 |
< |
i != ((Integer)q.peek()).intValue()); |
470 |
> |
!q.peek().equals(i)); |
471 |
|
} |
472 |
< |
assertNull(q.peek()); |
472 |
> |
assertNull(q.peek()); |
473 |
|
} |
474 |
|
|
475 |
|
/** |
478 |
|
public void testElement() { |
479 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
480 |
|
for (int i = 0; i < SIZE; ++i) { |
481 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
482 |
< |
q.poll(); |
481 |
> |
assertEquals(i, q.element()); |
482 |
> |
assertEquals(i, q.poll()); |
483 |
|
} |
484 |
|
try { |
485 |
|
q.element(); |
486 |
|
shouldThrow(); |
487 |
< |
} |
585 |
< |
catch (NoSuchElementException success) {} |
487 |
> |
} catch (NoSuchElementException success) {} |
488 |
|
} |
489 |
|
|
490 |
|
/** |
493 |
|
public void testRemove() { |
494 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
495 |
|
for (int i = 0; i < SIZE; ++i) { |
496 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
496 |
> |
assertEquals(i, q.remove()); |
497 |
|
} |
498 |
|
try { |
499 |
|
q.remove(); |
500 |
|
shouldThrow(); |
501 |
< |
} catch (NoSuchElementException success){ |
600 |
< |
} |
501 |
> |
} catch (NoSuchElementException success) {} |
502 |
|
} |
503 |
|
|
504 |
|
/** |
515 |
|
} |
516 |
|
assertTrue(q.isEmpty()); |
517 |
|
} |
518 |
< |
|
518 |
> |
|
519 |
> |
/** |
520 |
> |
* An add following remove(x) succeeds |
521 |
> |
*/ |
522 |
> |
public void testRemoveElementAndAdd() throws InterruptedException { |
523 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
524 |
> |
assertTrue(q.add(new Integer(1))); |
525 |
> |
assertTrue(q.add(new Integer(2))); |
526 |
> |
assertTrue(q.remove(new Integer(1))); |
527 |
> |
assertTrue(q.remove(new Integer(2))); |
528 |
> |
assertTrue(q.add(new Integer(3))); |
529 |
> |
assertTrue(q.take() != null); |
530 |
> |
} |
531 |
> |
|
532 |
|
/** |
533 |
|
* contains(x) reports true when elements added but not yet removed |
534 |
|
*/ |
552 |
|
assertEquals(SIZE, q.remainingCapacity()); |
553 |
|
q.add(one); |
554 |
|
assertFalse(q.isEmpty()); |
555 |
+ |
assertTrue(q.contains(one)); |
556 |
|
q.clear(); |
557 |
|
assertTrue(q.isEmpty()); |
558 |
|
} |
609 |
|
/** |
610 |
|
* toArray contains all elements |
611 |
|
*/ |
612 |
< |
public void testToArray() { |
612 |
> |
public void testToArray() throws InterruptedException { |
613 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
614 |
< |
Object[] o = q.toArray(); |
615 |
< |
try { |
616 |
< |
for(int i = 0; i < o.length; i++) |
702 |
< |
assertEquals(o[i], q.take()); |
703 |
< |
} catch (InterruptedException e){ |
704 |
< |
unexpectedException(); |
705 |
< |
} |
614 |
> |
Object[] o = q.toArray(); |
615 |
> |
for (int i = 0; i < o.length; i++) |
616 |
> |
assertEquals(o[i], q.take()); |
617 |
|
} |
618 |
|
|
619 |
|
/** |
620 |
|
* toArray(a) contains all elements |
621 |
|
*/ |
622 |
< |
public void testToArray2() { |
622 |
> |
public void testToArray2() throws InterruptedException { |
623 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
624 |
< |
Integer[] ints = new Integer[SIZE]; |
625 |
< |
ints = (Integer[])q.toArray(ints); |
626 |
< |
try { |
627 |
< |
for(int i = 0; i < ints.length; i++) |
717 |
< |
assertEquals(ints[i], q.take()); |
718 |
< |
} catch (InterruptedException e){ |
719 |
< |
unexpectedException(); |
720 |
< |
} |
624 |
> |
Integer[] ints = new Integer[SIZE]; |
625 |
> |
ints = (Integer[])q.toArray(ints); |
626 |
> |
for (int i = 0; i < ints.length; i++) |
627 |
> |
assertEquals(ints[i], q.take()); |
628 |
|
} |
629 |
|
|
630 |
|
/** |
631 |
< |
* toArray(null) throws NPE |
631 |
> |
* toArray(null) throws NullPointerException |
632 |
|
*/ |
633 |
< |
public void testToArray_BadArg() { |
634 |
< |
try { |
635 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
636 |
< |
Object o[] = q.toArray(null); |
637 |
< |
shouldThrow(); |
638 |
< |
} catch(NullPointerException success){} |
633 |
> |
public void testToArray_NullArg() { |
634 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
635 |
> |
try { |
636 |
> |
q.toArray(null); |
637 |
> |
shouldThrow(); |
638 |
> |
} catch (NullPointerException success) {} |
639 |
|
} |
640 |
|
|
641 |
|
/** |
642 |
< |
* toArray with incompatible array type throws CCE |
642 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
643 |
|
*/ |
644 |
|
public void testToArray1_BadArg() { |
645 |
< |
try { |
646 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
647 |
< |
Object o[] = q.toArray(new String[10] ); |
648 |
< |
shouldThrow(); |
649 |
< |
} catch(ArrayStoreException success){} |
645 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
646 |
> |
try { |
647 |
> |
q.toArray(new String[10]); |
648 |
> |
shouldThrow(); |
649 |
> |
} catch (ArrayStoreException success) {} |
650 |
|
} |
651 |
|
|
652 |
< |
|
652 |
> |
|
653 |
|
/** |
654 |
|
* iterator iterates through all elements |
655 |
|
*/ |
656 |
< |
public void testIterator() { |
656 |
> |
public void testIterator() throws InterruptedException { |
657 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
658 |
< |
Iterator it = q.iterator(); |
659 |
< |
try { |
660 |
< |
while(it.hasNext()){ |
661 |
< |
assertEquals(it.next(), q.take()); |
755 |
< |
} |
756 |
< |
} catch (InterruptedException e){ |
757 |
< |
unexpectedException(); |
758 |
< |
} |
658 |
> |
Iterator it = q.iterator(); |
659 |
> |
while (it.hasNext()) { |
660 |
> |
assertEquals(it.next(), q.take()); |
661 |
> |
} |
662 |
|
} |
663 |
|
|
664 |
|
/** |
665 |
|
* iterator.remove removes current element |
666 |
|
*/ |
667 |
< |
public void testIteratorRemove () { |
667 |
> |
public void testIteratorRemove() { |
668 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
669 |
|
q.add(two); |
670 |
|
q.add(one); |
673 |
|
Iterator it = q.iterator(); |
674 |
|
it.next(); |
675 |
|
it.remove(); |
676 |
< |
|
676 |
> |
|
677 |
|
it = q.iterator(); |
678 |
< |
assertEquals(it.next(), one); |
679 |
< |
assertEquals(it.next(), three); |
678 |
> |
assertSame(it.next(), one); |
679 |
> |
assertSame(it.next(), three); |
680 |
|
assertFalse(it.hasNext()); |
681 |
|
} |
682 |
|
|
692 |
|
assertEquals(0, q.remainingCapacity()); |
693 |
|
int k = 0; |
694 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
695 |
< |
int i = ((Integer)(it.next())).intValue(); |
793 |
< |
assertEquals(++k, i); |
695 |
> |
assertEquals(++k, it.next()); |
696 |
|
} |
697 |
|
assertEquals(3, k); |
698 |
|
} |
700 |
|
/** |
701 |
|
* Modifications do not cause iterators to fail |
702 |
|
*/ |
703 |
< |
public void testWeaklyConsistentIteration () { |
703 |
> |
public void testWeaklyConsistentIteration() { |
704 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
705 |
|
q.add(one); |
706 |
|
q.add(two); |
707 |
|
q.add(three); |
708 |
< |
try { |
709 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
710 |
< |
q.remove(); |
809 |
< |
it.next(); |
810 |
< |
} |
811 |
< |
} |
812 |
< |
catch (ConcurrentModificationException e) { |
813 |
< |
unexpectedException(); |
708 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
709 |
> |
q.remove(); |
710 |
> |
it.next(); |
711 |
|
} |
712 |
|
assertEquals(0, q.size()); |
713 |
|
} |
722 |
|
for (int i = 0; i < SIZE; ++i) { |
723 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
724 |
|
} |
725 |
< |
} |
725 |
> |
} |
726 |
|
|
727 |
|
|
728 |
|
/** |
733 |
|
q.add(one); |
734 |
|
q.add(two); |
735 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
736 |
< |
executor.execute(new Runnable() { |
737 |
< |
public void run() { |
738 |
< |
threadAssertFalse(q.offer(three)); |
739 |
< |
try { |
740 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
741 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
742 |
< |
} |
743 |
< |
catch (InterruptedException e) { |
744 |
< |
threadUnexpectedException(); |
745 |
< |
} |
746 |
< |
} |
747 |
< |
}); |
736 |
> |
executor.execute(new CheckedRunnable() { |
737 |
> |
public void realRun() throws InterruptedException { |
738 |
> |
assertFalse(q.offer(three)); |
739 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
740 |
> |
assertEquals(0, q.remainingCapacity()); |
741 |
> |
}}); |
742 |
> |
|
743 |
> |
executor.execute(new CheckedRunnable() { |
744 |
> |
public void realRun() throws InterruptedException { |
745 |
> |
Thread.sleep(SMALL_DELAY_MS); |
746 |
> |
assertSame(one, q.take()); |
747 |
> |
}}); |
748 |
|
|
852 |
– |
executor.execute(new Runnable() { |
853 |
– |
public void run() { |
854 |
– |
try { |
855 |
– |
Thread.sleep(SMALL_DELAY_MS); |
856 |
– |
threadAssertEquals(one, q.take()); |
857 |
– |
} |
858 |
– |
catch (InterruptedException e) { |
859 |
– |
threadUnexpectedException(); |
860 |
– |
} |
861 |
– |
} |
862 |
– |
}); |
863 |
– |
|
749 |
|
joinPool(executor); |
750 |
|
} |
751 |
|
|
755 |
|
public void testPollInExecutor() { |
756 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
757 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
758 |
< |
executor.execute(new Runnable() { |
759 |
< |
public void run() { |
760 |
< |
threadAssertNull(q.poll()); |
761 |
< |
try { |
762 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
763 |
< |
threadAssertTrue(q.isEmpty()); |
764 |
< |
} |
765 |
< |
catch (InterruptedException e) { |
766 |
< |
threadUnexpectedException(); |
767 |
< |
} |
768 |
< |
} |
769 |
< |
}); |
758 |
> |
executor.execute(new CheckedRunnable() { |
759 |
> |
public void realRun() throws InterruptedException { |
760 |
> |
assertNull(q.poll()); |
761 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
762 |
> |
assertTrue(q.isEmpty()); |
763 |
> |
}}); |
764 |
> |
|
765 |
> |
executor.execute(new CheckedRunnable() { |
766 |
> |
public void realRun() throws InterruptedException { |
767 |
> |
Thread.sleep(SMALL_DELAY_MS); |
768 |
> |
q.put(one); |
769 |
> |
}}); |
770 |
|
|
886 |
– |
executor.execute(new Runnable() { |
887 |
– |
public void run() { |
888 |
– |
try { |
889 |
– |
Thread.sleep(SMALL_DELAY_MS); |
890 |
– |
q.put(one); |
891 |
– |
} |
892 |
– |
catch (InterruptedException e) { |
893 |
– |
threadUnexpectedException(); |
894 |
– |
} |
895 |
– |
} |
896 |
– |
}); |
897 |
– |
|
771 |
|
joinPool(executor); |
772 |
|
} |
773 |
|
|
774 |
|
/** |
775 |
|
* A deserialized serialized queue has same elements in same order |
776 |
|
*/ |
777 |
< |
public void testSerialization() { |
777 |
> |
public void testSerialization() throws Exception { |
778 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
779 |
|
|
780 |
< |
try { |
781 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
782 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
783 |
< |
out.writeObject(q); |
784 |
< |
out.close(); |
785 |
< |
|
786 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
787 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
788 |
< |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
789 |
< |
assertEquals(q.size(), r.size()); |
790 |
< |
while (!q.isEmpty()) |
918 |
< |
assertEquals(q.remove(), r.remove()); |
919 |
< |
} catch(Exception e){ |
920 |
< |
unexpectedException(); |
921 |
< |
} |
780 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
781 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
782 |
> |
out.writeObject(q); |
783 |
> |
out.close(); |
784 |
> |
|
785 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
786 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
787 |
> |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
788 |
> |
assertEquals(q.size(), r.size()); |
789 |
> |
while (!q.isEmpty()) |
790 |
> |
assertEquals(q.remove(), r.remove()); |
791 |
|
} |
792 |
|
|
793 |
|
/** |
794 |
|
* drainTo(null) throws NPE |
795 |
< |
*/ |
795 |
> |
*/ |
796 |
|
public void testDrainToNull() { |
797 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
798 |
|
try { |
799 |
|
q.drainTo(null); |
800 |
|
shouldThrow(); |
801 |
< |
} catch(NullPointerException success) { |
933 |
< |
} |
801 |
> |
} catch (NullPointerException success) {} |
802 |
|
} |
803 |
|
|
804 |
|
/** |
805 |
|
* drainTo(this) throws IAE |
806 |
< |
*/ |
806 |
> |
*/ |
807 |
|
public void testDrainToSelf() { |
808 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
809 |
|
try { |
810 |
|
q.drainTo(q); |
811 |
|
shouldThrow(); |
812 |
< |
} catch(IllegalArgumentException success) { |
945 |
< |
} |
812 |
> |
} catch (IllegalArgumentException success) {} |
813 |
|
} |
814 |
|
|
815 |
|
/** |
816 |
|
* drainTo(c) empties queue into another collection c |
817 |
< |
*/ |
817 |
> |
*/ |
818 |
|
public void testDrainTo() { |
819 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
820 |
|
ArrayList l = new ArrayList(); |
821 |
|
q.drainTo(l); |
822 |
|
assertEquals(q.size(), 0); |
823 |
|
assertEquals(l.size(), SIZE); |
824 |
< |
for (int i = 0; i < SIZE; ++i) |
824 |
> |
for (int i = 0; i < SIZE; ++i) |
825 |
> |
assertEquals(l.get(i), new Integer(i)); |
826 |
> |
q.add(zero); |
827 |
> |
q.add(one); |
828 |
> |
assertFalse(q.isEmpty()); |
829 |
> |
assertTrue(q.contains(zero)); |
830 |
> |
assertTrue(q.contains(one)); |
831 |
> |
l.clear(); |
832 |
> |
q.drainTo(l); |
833 |
> |
assertEquals(q.size(), 0); |
834 |
> |
assertEquals(l.size(), 2); |
835 |
> |
for (int i = 0; i < 2; ++i) |
836 |
|
assertEquals(l.get(i), new Integer(i)); |
837 |
|
} |
838 |
|
|
839 |
|
/** |
840 |
|
* drainTo empties full queue, unblocking a waiting put. |
841 |
< |
*/ |
842 |
< |
public void testDrainToWithActivePut() { |
841 |
> |
*/ |
842 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
843 |
|
final LinkedBlockingQueue q = populatedQueue(SIZE); |
844 |
< |
Thread t = new Thread(new Runnable() { |
845 |
< |
public void run() { |
846 |
< |
try { |
847 |
< |
q.put(new Integer(SIZE+1)); |
848 |
< |
} catch (InterruptedException ie){ |
849 |
< |
threadUnexpectedException(); |
850 |
< |
} |
851 |
< |
} |
852 |
< |
}); |
853 |
< |
try { |
854 |
< |
t.start(); |
855 |
< |
ArrayList l = new ArrayList(); |
856 |
< |
q.drainTo(l); |
979 |
< |
assertTrue(l.size() >= SIZE); |
980 |
< |
for (int i = 0; i < SIZE; ++i) |
981 |
< |
assertEquals(l.get(i), new Integer(i)); |
982 |
< |
t.join(); |
983 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
984 |
< |
} catch(Exception e){ |
985 |
< |
unexpectedException(); |
986 |
< |
} |
844 |
> |
Thread t = new Thread(new CheckedRunnable() { |
845 |
> |
public void realRun() throws InterruptedException { |
846 |
> |
q.put(new Integer(SIZE+1)); |
847 |
> |
}}); |
848 |
> |
|
849 |
> |
t.start(); |
850 |
> |
ArrayList l = new ArrayList(); |
851 |
> |
q.drainTo(l); |
852 |
> |
assertTrue(l.size() >= SIZE); |
853 |
> |
for (int i = 0; i < SIZE; ++i) |
854 |
> |
assertEquals(l.get(i), new Integer(i)); |
855 |
> |
t.join(); |
856 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
857 |
|
} |
858 |
|
|
859 |
|
/** |
860 |
|
* drainTo(null, n) throws NPE |
861 |
< |
*/ |
861 |
> |
*/ |
862 |
|
public void testDrainToNullN() { |
863 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
864 |
|
try { |
865 |
|
q.drainTo(null, 0); |
866 |
|
shouldThrow(); |
867 |
< |
} catch(NullPointerException success) { |
998 |
< |
} |
867 |
> |
} catch (NullPointerException success) {} |
868 |
|
} |
869 |
|
|
870 |
|
/** |
871 |
|
* drainTo(this, n) throws IAE |
872 |
< |
*/ |
872 |
> |
*/ |
873 |
|
public void testDrainToSelfN() { |
874 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
875 |
|
try { |
876 |
|
q.drainTo(q, 0); |
877 |
|
shouldThrow(); |
878 |
< |
} catch(IllegalArgumentException success) { |
1010 |
< |
} |
878 |
> |
} catch (IllegalArgumentException success) {} |
879 |
|
} |
880 |
|
|
881 |
|
/** |
882 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
883 |
< |
*/ |
882 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
883 |
> |
*/ |
884 |
|
public void testDrainToN() { |
885 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
886 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
887 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
887 |
> |
for (int j = 0; j < SIZE; j++) |
888 |
> |
assertTrue(q.offer(new Integer(j))); |
889 |
|
ArrayList l = new ArrayList(); |
890 |
|
q.drainTo(l, i); |
891 |
< |
int k = (i < SIZE)? i : SIZE; |
1022 |
< |
assertEquals(q.size(), SIZE-k); |
891 |
> |
int k = (i < SIZE) ? i : SIZE; |
892 |
|
assertEquals(l.size(), k); |
893 |
< |
for (int j = 0; j < k; ++j) |
893 |
> |
assertEquals(q.size(), SIZE-k); |
894 |
> |
for (int j = 0; j < k; ++j) |
895 |
|
assertEquals(l.get(j), new Integer(j)); |
896 |
+ |
while (q.poll() != null) ; |
897 |
|
} |
898 |
|
} |
899 |
|
|