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 |
|
|
41 |
|
* Create a queue of given size containing consecutive |
42 |
|
* Integers 0 ... n. |
43 |
|
*/ |
44 |
< |
private LinkedBlockingQueue populatedQueue(int n) { |
45 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(n); |
44 |
> |
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
45 |
> |
LinkedBlockingQueue<Integer> q = |
46 |
> |
new LinkedBlockingQueue<Integer>(n); |
47 |
|
assertTrue(q.isEmpty()); |
48 |
< |
for(int i = 0; i < n; i++) |
49 |
< |
assertTrue(q.offer(new Integer(i))); |
48 |
> |
for (int i = 0; i < n; i++) |
49 |
> |
assertTrue(q.offer(new Integer(i))); |
50 |
|
assertFalse(q.isEmpty()); |
51 |
|
assertEquals(0, q.remainingCapacity()); |
52 |
< |
assertEquals(n, q.size()); |
52 |
> |
assertEquals(n, q.size()); |
53 |
|
return q; |
54 |
|
} |
55 |
< |
|
55 |
> |
|
56 |
|
/** |
57 |
|
* A new queue has the indicated capacity, or Integer.MAX_VALUE if |
58 |
|
* none given |
63 |
|
} |
64 |
|
|
65 |
|
/** |
66 |
< |
* Constructor throws IAE if capacity argument nonpositive |
66 |
> |
* Constructor throws IAE if capacity argument nonpositive |
67 |
|
*/ |
68 |
|
public void testConstructor2() { |
69 |
|
try { |
70 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(0); |
71 |
|
shouldThrow(); |
72 |
< |
} |
57 |
< |
catch (IllegalArgumentException success) {} |
72 |
> |
} catch (IllegalArgumentException success) {} |
73 |
|
} |
74 |
|
|
75 |
|
/** |
79 |
|
try { |
80 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(null); |
81 |
|
shouldThrow(); |
82 |
< |
} |
68 |
< |
catch (NullPointerException success) {} |
82 |
> |
} catch (NullPointerException success) {} |
83 |
|
} |
84 |
|
|
85 |
|
/** |
90 |
|
Integer[] ints = new Integer[SIZE]; |
91 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
92 |
|
shouldThrow(); |
93 |
< |
} |
80 |
< |
catch (NullPointerException success) {} |
93 |
> |
} catch (NullPointerException success) {} |
94 |
|
} |
95 |
|
|
96 |
|
/** |
103 |
|
ints[i] = new Integer(i); |
104 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
105 |
|
shouldThrow(); |
106 |
< |
} |
94 |
< |
catch (NullPointerException success) {} |
106 |
> |
} catch (NullPointerException success) {} |
107 |
|
} |
108 |
|
|
109 |
|
/** |
110 |
|
* Queue contains all elements of collection used to initialize |
111 |
|
*/ |
112 |
|
public void testConstructor6() { |
113 |
< |
try { |
114 |
< |
Integer[] ints = new Integer[SIZE]; |
115 |
< |
for (int i = 0; i < SIZE; ++i) |
116 |
< |
ints[i] = new Integer(i); |
117 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
118 |
< |
for (int i = 0; i < SIZE; ++i) |
107 |
< |
assertEquals(ints[i], q.poll()); |
108 |
< |
} |
109 |
< |
finally {} |
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) |
118 |
> |
assertEquals(ints[i], q.poll()); |
119 |
|
} |
120 |
|
|
121 |
|
/** |
154 |
|
* offer(null) throws NPE |
155 |
|
*/ |
156 |
|
public void testOfferNull() { |
157 |
< |
try { |
157 |
> |
try { |
158 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
159 |
|
q.offer(null); |
160 |
|
shouldThrow(); |
161 |
< |
} catch (NullPointerException success) { } |
161 |
> |
} catch (NullPointerException success) {} |
162 |
|
} |
163 |
|
|
164 |
|
/** |
165 |
|
* add(null) throws NPE |
166 |
|
*/ |
167 |
|
public void testAddNull() { |
168 |
< |
try { |
168 |
> |
try { |
169 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
170 |
|
q.add(null); |
171 |
|
shouldThrow(); |
172 |
< |
} catch (NullPointerException success) { } |
172 |
> |
} catch (NullPointerException success) {} |
173 |
|
} |
174 |
|
|
175 |
|
/** |
185 |
|
* add succeeds if not full; throws ISE if full |
186 |
|
*/ |
187 |
|
public void testAdd() { |
188 |
< |
try { |
188 |
> |
try { |
189 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
190 |
|
for (int i = 0; i < SIZE; ++i) { |
191 |
|
assertTrue(q.add(new Integer(i))); |
192 |
|
} |
193 |
|
assertEquals(0, q.remainingCapacity()); |
194 |
|
q.add(new Integer(SIZE)); |
195 |
< |
} catch (IllegalStateException success){ |
196 |
< |
} |
195 |
> |
shouldThrow(); |
196 |
> |
} catch (IllegalStateException success) {} |
197 |
|
} |
198 |
|
|
199 |
|
/** |
204 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
205 |
|
q.addAll(null); |
206 |
|
shouldThrow(); |
207 |
< |
} |
199 |
< |
catch (NullPointerException success) {} |
207 |
> |
} catch (NullPointerException success) {} |
208 |
|
} |
209 |
|
|
210 |
|
/** |
215 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
216 |
|
q.addAll(q); |
217 |
|
shouldThrow(); |
218 |
< |
} |
211 |
< |
catch (IllegalArgumentException success) {} |
218 |
> |
} catch (IllegalArgumentException success) {} |
219 |
|
} |
220 |
|
|
221 |
|
/** |
227 |
|
Integer[] ints = new Integer[SIZE]; |
228 |
|
q.addAll(Arrays.asList(ints)); |
229 |
|
shouldThrow(); |
230 |
< |
} |
224 |
< |
catch (NullPointerException success) {} |
230 |
> |
} catch (NullPointerException success) {} |
231 |
|
} |
232 |
+ |
|
233 |
|
/** |
234 |
|
* addAll of a collection with any null elements throws NPE after |
235 |
|
* possibly adding some elements |
242 |
|
ints[i] = new Integer(i); |
243 |
|
q.addAll(Arrays.asList(ints)); |
244 |
|
shouldThrow(); |
245 |
< |
} |
239 |
< |
catch (NullPointerException success) {} |
245 |
> |
} catch (NullPointerException success) {} |
246 |
|
} |
247 |
+ |
|
248 |
|
/** |
249 |
|
* addAll throws ISE if not enough room |
250 |
|
*/ |
256 |
|
ints[i] = new Integer(i); |
257 |
|
q.addAll(Arrays.asList(ints)); |
258 |
|
shouldThrow(); |
259 |
< |
} |
253 |
< |
catch (IllegalStateException success) {} |
259 |
> |
} catch (IllegalStateException success) {} |
260 |
|
} |
261 |
+ |
|
262 |
|
/** |
263 |
|
* Queue contains all elements, in traversal order, of successful addAll |
264 |
|
*/ |
265 |
|
public void testAddAll5() { |
266 |
< |
try { |
267 |
< |
Integer[] empty = new Integer[0]; |
268 |
< |
Integer[] ints = new Integer[SIZE]; |
269 |
< |
for (int i = 0; i < SIZE; ++i) |
270 |
< |
ints[i] = new Integer(i); |
271 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
272 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
273 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
274 |
< |
for (int i = 0; i < SIZE; ++i) |
268 |
< |
assertEquals(ints[i], q.poll()); |
269 |
< |
} |
270 |
< |
finally {} |
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) |
274 |
> |
assertEquals(ints[i], q.poll()); |
275 |
|
} |
276 |
|
|
277 |
|
/** |
278 |
|
* put(null) throws NPE |
279 |
|
*/ |
280 |
< |
public void testPutNull() { |
281 |
< |
try { |
280 |
> |
public void testPutNull() throws InterruptedException { |
281 |
> |
try { |
282 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
283 |
|
q.put(null); |
284 |
|
shouldThrow(); |
285 |
< |
} |
282 |
< |
catch (NullPointerException success){ |
283 |
< |
} |
284 |
< |
catch (InterruptedException ie) { |
285 |
< |
unexpectedException(); |
286 |
< |
} |
285 |
> |
} catch (NullPointerException success) {} |
286 |
|
} |
287 |
|
|
288 |
|
/** |
289 |
|
* all elements successfully put are contained |
290 |
|
*/ |
291 |
< |
public void testPut() { |
292 |
< |
try { |
293 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
294 |
< |
for (int i = 0; i < SIZE; ++i) { |
295 |
< |
Integer I = new Integer(i); |
296 |
< |
q.put(I); |
298 |
< |
assertTrue(q.contains(I)); |
299 |
< |
} |
300 |
< |
assertEquals(0, q.remainingCapacity()); |
301 |
< |
} |
302 |
< |
catch (InterruptedException ie) { |
303 |
< |
unexpectedException(); |
291 |
> |
public void testPut() throws InterruptedException { |
292 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
293 |
> |
for (int i = 0; i < SIZE; ++i) { |
294 |
> |
Integer I = new Integer(i); |
295 |
> |
q.put(I); |
296 |
> |
assertTrue(q.contains(I)); |
297 |
|
} |
298 |
+ |
assertEquals(0, q.remainingCapacity()); |
299 |
|
} |
300 |
|
|
301 |
|
/** |
302 |
|
* put blocks interruptibly if full |
303 |
|
*/ |
304 |
< |
public void testBlockingPut() { |
305 |
< |
Thread t = new Thread(new Runnable() { |
306 |
< |
public void run() { |
307 |
< |
int added = 0; |
308 |
< |
try { |
309 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
310 |
< |
for (int i = 0; i < SIZE; ++i) { |
311 |
< |
q.put(new Integer(i)); |
312 |
< |
++added; |
313 |
< |
} |
314 |
< |
q.put(new Integer(SIZE)); |
315 |
< |
threadShouldThrow(); |
316 |
< |
} catch (InterruptedException ie){ |
317 |
< |
threadAssertEquals(added, SIZE); |
324 |
< |
} |
325 |
< |
}}); |
304 |
> |
public void testBlockingPut() throws InterruptedException { |
305 |
> |
final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
306 |
> |
Thread t = new Thread(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 |
> |
try { |
313 |
> |
q.put(99); |
314 |
> |
shouldThrow(); |
315 |
> |
} catch (InterruptedException success) {} |
316 |
> |
}}); |
317 |
> |
|
318 |
|
t.start(); |
319 |
< |
try { |
320 |
< |
Thread.sleep(SHORT_DELAY_MS); |
321 |
< |
t.interrupt(); |
322 |
< |
t.join(); |
323 |
< |
} |
332 |
< |
catch (InterruptedException ie) { |
333 |
< |
unexpectedException(); |
334 |
< |
} |
319 |
> |
Thread.sleep(SHORT_DELAY_MS); |
320 |
> |
t.interrupt(); |
321 |
> |
t.join(); |
322 |
> |
assertEquals(SIZE, q.size()); |
323 |
> |
assertEquals(0, q.remainingCapacity()); |
324 |
|
} |
325 |
|
|
326 |
|
/** |
327 |
|
* put blocks waiting for take when full |
328 |
|
*/ |
329 |
< |
public void testPutWithTake() { |
329 |
> |
public void testPutWithTake() throws InterruptedException { |
330 |
> |
final int capacity = 2; |
331 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
332 |
< |
Thread t = new Thread(new Runnable() { |
333 |
< |
public void run() { |
334 |
< |
int added = 0; |
335 |
< |
try { |
336 |
< |
q.put(new Object()); |
337 |
< |
++added; |
338 |
< |
q.put(new Object()); |
339 |
< |
++added; |
340 |
< |
q.put(new Object()); |
341 |
< |
++added; |
342 |
< |
q.put(new Object()); |
343 |
< |
++added; |
344 |
< |
threadShouldThrow(); |
345 |
< |
} catch (InterruptedException e){ |
346 |
< |
threadAssertTrue(added >= 2); |
347 |
< |
} |
348 |
< |
} |
349 |
< |
}); |
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 |
< |
} |
332 |
> |
Thread t = new Thread(new CheckedRunnable() { |
333 |
> |
public void realRun() throws InterruptedException { |
334 |
> |
for (int i = 0; i < capacity + 1; i++) |
335 |
> |
q.put(i); |
336 |
> |
try { |
337 |
> |
q.put(99); |
338 |
> |
shouldThrow(); |
339 |
> |
} catch (InterruptedException success) {} |
340 |
> |
}}); |
341 |
> |
|
342 |
> |
t.start(); |
343 |
> |
Thread.sleep(SHORT_DELAY_MS); |
344 |
> |
assertEquals(q.remainingCapacity(), 0); |
345 |
> |
assertEquals(0, q.take()); |
346 |
> |
Thread.sleep(SHORT_DELAY_MS); |
347 |
> |
t.interrupt(); |
348 |
> |
t.join(); |
349 |
> |
assertEquals(q.remainingCapacity(), 0); |
350 |
|
} |
351 |
|
|
352 |
|
/** |
353 |
|
* timed offer times out if full and elements not taken |
354 |
|
*/ |
355 |
< |
public void testTimedOffer() { |
355 |
> |
public void testTimedOffer() throws InterruptedException { |
356 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
357 |
< |
Thread t = new Thread(new Runnable() { |
358 |
< |
public void run() { |
359 |
< |
try { |
360 |
< |
q.put(new Object()); |
361 |
< |
q.put(new Object()); |
362 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
363 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
364 |
< |
threadShouldThrow(); |
365 |
< |
} catch (InterruptedException success){} |
366 |
< |
} |
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 |
< |
} |
357 |
> |
Thread t = new Thread(new CheckedRunnable() { |
358 |
> |
public void realRun() throws InterruptedException { |
359 |
> |
q.put(new Object()); |
360 |
> |
q.put(new Object()); |
361 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
362 |
> |
try { |
363 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
364 |
> |
shouldThrow(); |
365 |
> |
} catch (InterruptedException success) {} |
366 |
> |
}}); |
367 |
|
|
368 |
< |
/** |
369 |
< |
* take retrieves elements in FIFO order |
370 |
< |
*/ |
371 |
< |
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 |
< |
} |
368 |
> |
t.start(); |
369 |
> |
Thread.sleep(SMALL_DELAY_MS); |
370 |
> |
t.interrupt(); |
371 |
> |
t.join(); |
372 |
|
} |
373 |
|
|
374 |
|
/** |
375 |
< |
* take blocks interruptibly when empty |
375 |
> |
* take retrieves elements in FIFO order |
376 |
|
*/ |
377 |
< |
public void testTakeFromEmpty() { |
378 |
< |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
379 |
< |
Thread t = new Thread(new Runnable() { |
380 |
< |
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(); |
377 |
> |
public void testTake() throws InterruptedException { |
378 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
379 |
> |
for (int i = 0; i < SIZE; ++i) { |
380 |
> |
assertEquals(i, q.take()); |
381 |
|
} |
382 |
|
} |
383 |
|
|
384 |
|
/** |
385 |
|
* Take removes existing elements until empty, then blocks interruptibly |
386 |
|
*/ |
387 |
< |
public void testBlockingTake() { |
388 |
< |
Thread t = new Thread(new Runnable() { |
389 |
< |
public void run() { |
390 |
< |
try { |
391 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
392 |
< |
for (int i = 0; i < SIZE; ++i) { |
393 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
394 |
< |
} |
395 |
< |
q.take(); |
396 |
< |
threadShouldThrow(); |
397 |
< |
} catch (InterruptedException success){ |
398 |
< |
} |
399 |
< |
}}); |
387 |
> |
public void testBlockingTake() throws InterruptedException { |
388 |
> |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
389 |
> |
Thread t = new Thread(new CheckedRunnable() { |
390 |
> |
public void realRun() throws InterruptedException { |
391 |
> |
for (int i = 0; i < SIZE; ++i) { |
392 |
> |
assertEquals(i, q.take()); |
393 |
> |
} |
394 |
> |
try { |
395 |
> |
q.take(); |
396 |
> |
shouldThrow(); |
397 |
> |
} catch (InterruptedException success) {} |
398 |
> |
}}); |
399 |
> |
|
400 |
|
t.start(); |
401 |
< |
try { |
402 |
< |
Thread.sleep(SHORT_DELAY_MS); |
403 |
< |
t.interrupt(); |
455 |
< |
t.join(); |
456 |
< |
} |
457 |
< |
catch (InterruptedException ie) { |
458 |
< |
unexpectedException(); |
459 |
< |
} |
401 |
> |
Thread.sleep(SHORT_DELAY_MS); |
402 |
> |
t.interrupt(); |
403 |
> |
t.join(); |
404 |
|
} |
405 |
|
|
462 |
– |
|
406 |
|
/** |
407 |
|
* poll succeeds unless empty |
408 |
|
*/ |
409 |
|
public void testPoll() { |
410 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
411 |
|
for (int i = 0; i < SIZE; ++i) { |
412 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
412 |
> |
assertEquals(i, q.poll()); |
413 |
|
} |
414 |
< |
assertNull(q.poll()); |
414 |
> |
assertNull(q.poll()); |
415 |
|
} |
416 |
|
|
417 |
|
/** |
418 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
418 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
419 |
|
*/ |
420 |
< |
public void testTimedPoll0() { |
421 |
< |
try { |
422 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
423 |
< |
for (int i = 0; i < SIZE; ++i) { |
424 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
425 |
< |
} |
483 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
484 |
< |
} catch (InterruptedException e){ |
485 |
< |
unexpectedException(); |
486 |
< |
} |
420 |
> |
public void testTimedPoll0() throws InterruptedException { |
421 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
422 |
> |
for (int i = 0; i < SIZE; ++i) { |
423 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
424 |
> |
} |
425 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
426 |
|
} |
427 |
|
|
428 |
|
/** |
429 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
429 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
430 |
|
*/ |
431 |
< |
public void testTimedPoll() { |
432 |
< |
try { |
433 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
434 |
< |
for (int i = 0; i < SIZE; ++i) { |
435 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
436 |
< |
} |
498 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
499 |
< |
} catch (InterruptedException e){ |
500 |
< |
unexpectedException(); |
501 |
< |
} |
431 |
> |
public void testTimedPoll() throws InterruptedException { |
432 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
433 |
> |
for (int i = 0; i < SIZE; ++i) { |
434 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
435 |
> |
} |
436 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
437 |
|
} |
438 |
|
|
439 |
|
/** |
440 |
|
* Interrupted timed poll throws InterruptedException instead of |
441 |
|
* returning timeout status |
442 |
|
*/ |
443 |
< |
public void testInterruptedTimedPoll() { |
444 |
< |
Thread t = new Thread(new Runnable() { |
445 |
< |
public void run() { |
446 |
< |
try { |
447 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
448 |
< |
for (int i = 0; i < SIZE; ++i) { |
449 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
450 |
< |
} |
451 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
452 |
< |
} catch (InterruptedException success){ |
453 |
< |
} |
454 |
< |
}}); |
443 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
444 |
> |
Thread t = new Thread(new CheckedRunnable() { |
445 |
> |
public void realRun() throws InterruptedException { |
446 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
447 |
> |
for (int i = 0; i < SIZE; ++i) { |
448 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
449 |
> |
} |
450 |
> |
try { |
451 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
452 |
> |
shouldThrow(); |
453 |
> |
} catch (InterruptedException success) {} |
454 |
> |
}}); |
455 |
> |
|
456 |
|
t.start(); |
457 |
< |
try { |
458 |
< |
Thread.sleep(SHORT_DELAY_MS); |
459 |
< |
t.interrupt(); |
524 |
< |
t.join(); |
525 |
< |
} |
526 |
< |
catch (InterruptedException ie) { |
527 |
< |
unexpectedException(); |
528 |
< |
} |
457 |
> |
Thread.sleep(SHORT_DELAY_MS); |
458 |
> |
t.interrupt(); |
459 |
> |
t.join(); |
460 |
|
} |
461 |
|
|
462 |
|
/** |
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 |
– |
/** |
463 |
|
* peek returns next element, or null if empty |
464 |
|
*/ |
465 |
|
public void testPeek() { |
466 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
467 |
|
for (int i = 0; i < SIZE; ++i) { |
468 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
469 |
< |
q.poll(); |
468 |
> |
assertEquals(i, q.peek()); |
469 |
> |
assertEquals(i, q.poll()); |
470 |
|
assertTrue(q.peek() == null || |
471 |
< |
i != ((Integer)q.peek()).intValue()); |
471 |
> |
!q.peek().equals(i)); |
472 |
|
} |
473 |
< |
assertNull(q.peek()); |
473 |
> |
assertNull(q.peek()); |
474 |
|
} |
475 |
|
|
476 |
|
/** |
479 |
|
public void testElement() { |
480 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
481 |
|
for (int i = 0; i < SIZE; ++i) { |
482 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
483 |
< |
q.poll(); |
482 |
> |
assertEquals(i, q.element()); |
483 |
> |
assertEquals(i, q.poll()); |
484 |
|
} |
485 |
|
try { |
486 |
|
q.element(); |
487 |
|
shouldThrow(); |
488 |
< |
} |
585 |
< |
catch (NoSuchElementException success) {} |
488 |
> |
} catch (NoSuchElementException success) {} |
489 |
|
} |
490 |
|
|
491 |
|
/** |
494 |
|
public void testRemove() { |
495 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
496 |
|
for (int i = 0; i < SIZE; ++i) { |
497 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
497 |
> |
assertEquals(i, q.remove()); |
498 |
|
} |
499 |
|
try { |
500 |
|
q.remove(); |
501 |
|
shouldThrow(); |
502 |
< |
} catch (NoSuchElementException success){ |
600 |
< |
} |
502 |
> |
} catch (NoSuchElementException success) {} |
503 |
|
} |
504 |
|
|
505 |
|
/** |
508 |
|
public void testRemoveElement() { |
509 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
510 |
|
for (int i = 1; i < SIZE; i+=2) { |
511 |
< |
assertTrue(q.remove(new Integer(i))); |
511 |
> |
assertTrue(q.contains(i)); |
512 |
> |
assertTrue(q.remove(i)); |
513 |
> |
assertFalse(q.contains(i)); |
514 |
> |
assertTrue(q.contains(i-1)); |
515 |
|
} |
516 |
|
for (int i = 0; i < SIZE; i+=2) { |
517 |
< |
assertTrue(q.remove(new Integer(i))); |
518 |
< |
assertFalse(q.remove(new Integer(i+1))); |
517 |
> |
assertTrue(q.contains(i)); |
518 |
> |
assertTrue(q.remove(i)); |
519 |
> |
assertFalse(q.contains(i)); |
520 |
> |
assertFalse(q.remove(i+1)); |
521 |
> |
assertFalse(q.contains(i+1)); |
522 |
|
} |
523 |
|
assertTrue(q.isEmpty()); |
524 |
|
} |
525 |
< |
|
525 |
> |
|
526 |
> |
/** |
527 |
> |
* An add following remove(x) succeeds |
528 |
> |
*/ |
529 |
> |
public void testRemoveElementAndAdd() throws InterruptedException { |
530 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
531 |
> |
assertTrue(q.add(new Integer(1))); |
532 |
> |
assertTrue(q.add(new Integer(2))); |
533 |
> |
assertTrue(q.remove(new Integer(1))); |
534 |
> |
assertTrue(q.remove(new Integer(2))); |
535 |
> |
assertTrue(q.add(new Integer(3))); |
536 |
> |
assertTrue(q.take() != null); |
537 |
> |
} |
538 |
> |
|
539 |
|
/** |
540 |
|
* contains(x) reports true when elements added but not yet removed |
541 |
|
*/ |
559 |
|
assertEquals(SIZE, q.remainingCapacity()); |
560 |
|
q.add(one); |
561 |
|
assertFalse(q.isEmpty()); |
562 |
+ |
assertTrue(q.contains(one)); |
563 |
|
q.clear(); |
564 |
|
assertTrue(q.isEmpty()); |
565 |
|
} |
614 |
|
} |
615 |
|
|
616 |
|
/** |
617 |
< |
* toArray contains all elements |
617 |
> |
* toArray contains all elements in FIFO order |
618 |
|
*/ |
619 |
|
public void testToArray() { |
620 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
621 |
< |
Object[] o = q.toArray(); |
622 |
< |
try { |
623 |
< |
for(int i = 0; i < o.length; i++) |
702 |
< |
assertEquals(o[i], q.take()); |
703 |
< |
} catch (InterruptedException e){ |
704 |
< |
unexpectedException(); |
705 |
< |
} |
621 |
> |
Object[] o = q.toArray(); |
622 |
> |
for (int i = 0; i < o.length; i++) |
623 |
> |
assertSame(o[i], q.poll()); |
624 |
|
} |
625 |
|
|
626 |
|
/** |
627 |
< |
* toArray(a) contains all elements |
627 |
> |
* toArray(a) contains all elements in FIFO order |
628 |
|
*/ |
629 |
< |
public void testToArray2() { |
630 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
631 |
< |
Integer[] ints = new Integer[SIZE]; |
632 |
< |
ints = (Integer[])q.toArray(ints); |
633 |
< |
try { |
634 |
< |
for(int i = 0; i < ints.length; i++) |
635 |
< |
assertEquals(ints[i], q.take()); |
718 |
< |
} catch (InterruptedException e){ |
719 |
< |
unexpectedException(); |
720 |
< |
} |
629 |
> |
public void testToArray2() throws InterruptedException { |
630 |
> |
LinkedBlockingQueue<Integer> q = populatedQueue(SIZE); |
631 |
> |
Integer[] ints = new Integer[SIZE]; |
632 |
> |
Integer[] array = q.toArray(ints); |
633 |
> |
assertSame(ints, array); |
634 |
> |
for (int i = 0; i < ints.length; i++) |
635 |
> |
assertSame(ints[i], q.poll()); |
636 |
|
} |
637 |
|
|
638 |
|
/** |
639 |
< |
* toArray(null) throws NPE |
639 |
> |
* toArray(null) throws NullPointerException |
640 |
|
*/ |
641 |
< |
public void testToArray_BadArg() { |
642 |
< |
try { |
643 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
644 |
< |
Object o[] = q.toArray(null); |
645 |
< |
shouldThrow(); |
646 |
< |
} catch(NullPointerException success){} |
641 |
> |
public void testToArray_NullArg() { |
642 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
643 |
> |
try { |
644 |
> |
q.toArray(null); |
645 |
> |
shouldThrow(); |
646 |
> |
} catch (NullPointerException success) {} |
647 |
|
} |
648 |
|
|
649 |
|
/** |
650 |
< |
* toArray with incompatable array type throws CCE |
650 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
651 |
|
*/ |
652 |
|
public void testToArray1_BadArg() { |
653 |
< |
try { |
654 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
655 |
< |
Object o[] = q.toArray(new String[10] ); |
656 |
< |
shouldThrow(); |
657 |
< |
} catch(ArrayStoreException success){} |
653 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
654 |
> |
try { |
655 |
> |
q.toArray(new String[10]); |
656 |
> |
shouldThrow(); |
657 |
> |
} catch (ArrayStoreException success) {} |
658 |
|
} |
659 |
|
|
660 |
< |
|
660 |
> |
|
661 |
|
/** |
662 |
|
* iterator iterates through all elements |
663 |
|
*/ |
664 |
< |
public void testIterator() { |
664 |
> |
public void testIterator() throws InterruptedException { |
665 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
666 |
< |
Iterator it = q.iterator(); |
667 |
< |
try { |
668 |
< |
while(it.hasNext()){ |
669 |
< |
assertEquals(it.next(), q.take()); |
755 |
< |
} |
756 |
< |
} catch (InterruptedException e){ |
757 |
< |
unexpectedException(); |
758 |
< |
} |
666 |
> |
Iterator it = q.iterator(); |
667 |
> |
while (it.hasNext()) { |
668 |
> |
assertEquals(it.next(), q.take()); |
669 |
> |
} |
670 |
|
} |
671 |
|
|
672 |
|
/** |
673 |
|
* iterator.remove removes current element |
674 |
|
*/ |
675 |
< |
public void testIteratorRemove () { |
675 |
> |
public void testIteratorRemove() { |
676 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
677 |
|
q.add(two); |
678 |
|
q.add(one); |
681 |
|
Iterator it = q.iterator(); |
682 |
|
it.next(); |
683 |
|
it.remove(); |
684 |
< |
|
684 |
> |
|
685 |
|
it = q.iterator(); |
686 |
< |
assertEquals(it.next(), one); |
687 |
< |
assertEquals(it.next(), three); |
686 |
> |
assertSame(it.next(), one); |
687 |
> |
assertSame(it.next(), three); |
688 |
|
assertFalse(it.hasNext()); |
689 |
|
} |
690 |
|
|
700 |
|
assertEquals(0, q.remainingCapacity()); |
701 |
|
int k = 0; |
702 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
703 |
< |
int i = ((Integer)(it.next())).intValue(); |
793 |
< |
assertEquals(++k, i); |
703 |
> |
assertEquals(++k, it.next()); |
704 |
|
} |
705 |
|
assertEquals(3, k); |
706 |
|
} |
708 |
|
/** |
709 |
|
* Modifications do not cause iterators to fail |
710 |
|
*/ |
711 |
< |
public void testWeaklyConsistentIteration () { |
711 |
> |
public void testWeaklyConsistentIteration() { |
712 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
713 |
|
q.add(one); |
714 |
|
q.add(two); |
715 |
|
q.add(three); |
716 |
< |
try { |
717 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
718 |
< |
q.remove(); |
809 |
< |
it.next(); |
810 |
< |
} |
811 |
< |
} |
812 |
< |
catch (ConcurrentModificationException e) { |
813 |
< |
unexpectedException(); |
716 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
717 |
> |
q.remove(); |
718 |
> |
it.next(); |
719 |
|
} |
720 |
|
assertEquals(0, q.size()); |
721 |
|
} |
730 |
|
for (int i = 0; i < SIZE; ++i) { |
731 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
732 |
|
} |
733 |
< |
} |
733 |
> |
} |
734 |
|
|
735 |
|
|
736 |
|
/** |
741 |
|
q.add(one); |
742 |
|
q.add(two); |
743 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
744 |
< |
executor.execute(new Runnable() { |
745 |
< |
public void run() { |
746 |
< |
threadAssertFalse(q.offer(three)); |
747 |
< |
try { |
748 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
749 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
750 |
< |
} |
751 |
< |
catch (InterruptedException e) { |
752 |
< |
threadUnexpectedException(); |
753 |
< |
} |
754 |
< |
} |
755 |
< |
}); |
744 |
> |
executor.execute(new CheckedRunnable() { |
745 |
> |
public void realRun() throws InterruptedException { |
746 |
> |
assertFalse(q.offer(three)); |
747 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
748 |
> |
assertEquals(0, q.remainingCapacity()); |
749 |
> |
}}); |
750 |
> |
|
751 |
> |
executor.execute(new CheckedRunnable() { |
752 |
> |
public void realRun() throws InterruptedException { |
753 |
> |
Thread.sleep(SMALL_DELAY_MS); |
754 |
> |
assertSame(one, q.take()); |
755 |
> |
}}); |
756 |
|
|
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 |
– |
|
757 |
|
joinPool(executor); |
758 |
|
} |
759 |
|
|
763 |
|
public void testPollInExecutor() { |
764 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
765 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
766 |
< |
executor.execute(new Runnable() { |
767 |
< |
public void run() { |
768 |
< |
threadAssertNull(q.poll()); |
769 |
< |
try { |
770 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
771 |
< |
threadAssertTrue(q.isEmpty()); |
772 |
< |
} |
773 |
< |
catch (InterruptedException e) { |
774 |
< |
threadUnexpectedException(); |
775 |
< |
} |
776 |
< |
} |
777 |
< |
}); |
766 |
> |
executor.execute(new CheckedRunnable() { |
767 |
> |
public void realRun() throws InterruptedException { |
768 |
> |
assertNull(q.poll()); |
769 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
770 |
> |
assertTrue(q.isEmpty()); |
771 |
> |
}}); |
772 |
> |
|
773 |
> |
executor.execute(new CheckedRunnable() { |
774 |
> |
public void realRun() throws InterruptedException { |
775 |
> |
Thread.sleep(SMALL_DELAY_MS); |
776 |
> |
q.put(one); |
777 |
> |
}}); |
778 |
|
|
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 |
– |
|
779 |
|
joinPool(executor); |
780 |
|
} |
781 |
|
|
782 |
|
/** |
783 |
|
* A deserialized serialized queue has same elements in same order |
784 |
|
*/ |
785 |
< |
public void testSerialization() { |
785 |
> |
public void testSerialization() throws Exception { |
786 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
787 |
|
|
788 |
< |
try { |
789 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
790 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
791 |
< |
out.writeObject(q); |
792 |
< |
out.close(); |
793 |
< |
|
794 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
795 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
796 |
< |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
797 |
< |
assertEquals(q.size(), r.size()); |
798 |
< |
while (!q.isEmpty()) |
918 |
< |
assertEquals(q.remove(), r.remove()); |
919 |
< |
} catch(Exception e){ |
920 |
< |
unexpectedException(); |
921 |
< |
} |
788 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
789 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
790 |
> |
out.writeObject(q); |
791 |
> |
out.close(); |
792 |
> |
|
793 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
794 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
795 |
> |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
796 |
> |
assertEquals(q.size(), r.size()); |
797 |
> |
while (!q.isEmpty()) |
798 |
> |
assertEquals(q.remove(), r.remove()); |
799 |
|
} |
800 |
|
|
801 |
|
/** |
802 |
|
* drainTo(null) throws NPE |
803 |
< |
*/ |
803 |
> |
*/ |
804 |
|
public void testDrainToNull() { |
805 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
806 |
|
try { |
807 |
|
q.drainTo(null); |
808 |
|
shouldThrow(); |
809 |
< |
} catch(NullPointerException success) { |
933 |
< |
} |
809 |
> |
} catch (NullPointerException success) {} |
810 |
|
} |
811 |
|
|
812 |
|
/** |
813 |
|
* drainTo(this) throws IAE |
814 |
< |
*/ |
814 |
> |
*/ |
815 |
|
public void testDrainToSelf() { |
816 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
817 |
|
try { |
818 |
|
q.drainTo(q); |
819 |
|
shouldThrow(); |
820 |
< |
} catch(IllegalArgumentException success) { |
945 |
< |
} |
820 |
> |
} catch (IllegalArgumentException success) {} |
821 |
|
} |
822 |
|
|
823 |
|
/** |
824 |
|
* drainTo(c) empties queue into another collection c |
825 |
< |
*/ |
825 |
> |
*/ |
826 |
|
public void testDrainTo() { |
827 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
828 |
|
ArrayList l = new ArrayList(); |
829 |
|
q.drainTo(l); |
830 |
|
assertEquals(q.size(), 0); |
831 |
|
assertEquals(l.size(), SIZE); |
832 |
< |
for (int i = 0; i < SIZE; ++i) |
832 |
> |
for (int i = 0; i < SIZE; ++i) |
833 |
> |
assertEquals(l.get(i), new Integer(i)); |
834 |
> |
q.add(zero); |
835 |
> |
q.add(one); |
836 |
> |
assertFalse(q.isEmpty()); |
837 |
> |
assertTrue(q.contains(zero)); |
838 |
> |
assertTrue(q.contains(one)); |
839 |
> |
l.clear(); |
840 |
> |
q.drainTo(l); |
841 |
> |
assertEquals(q.size(), 0); |
842 |
> |
assertEquals(l.size(), 2); |
843 |
> |
for (int i = 0; i < 2; ++i) |
844 |
|
assertEquals(l.get(i), new Integer(i)); |
845 |
|
} |
846 |
|
|
847 |
|
/** |
848 |
|
* drainTo empties full queue, unblocking a waiting put. |
849 |
< |
*/ |
850 |
< |
public void testDrainToWithActivePut() { |
849 |
> |
*/ |
850 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
851 |
|
final LinkedBlockingQueue q = populatedQueue(SIZE); |
852 |
< |
Thread t = new Thread(new Runnable() { |
853 |
< |
public void run() { |
854 |
< |
try { |
855 |
< |
q.put(new Integer(SIZE+1)); |
856 |
< |
} catch (InterruptedException ie){ |
857 |
< |
threadUnexpectedException(); |
858 |
< |
} |
859 |
< |
} |
860 |
< |
}); |
861 |
< |
try { |
862 |
< |
t.start(); |
863 |
< |
ArrayList l = new ArrayList(); |
864 |
< |
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+1); |
984 |
< |
} catch(Exception e){ |
985 |
< |
unexpectedException(); |
986 |
< |
} |
852 |
> |
Thread t = new Thread(new CheckedRunnable() { |
853 |
> |
public void realRun() throws InterruptedException { |
854 |
> |
q.put(new Integer(SIZE+1)); |
855 |
> |
}}); |
856 |
> |
|
857 |
> |
t.start(); |
858 |
> |
ArrayList l = new ArrayList(); |
859 |
> |
q.drainTo(l); |
860 |
> |
assertTrue(l.size() >= SIZE); |
861 |
> |
for (int i = 0; i < SIZE; ++i) |
862 |
> |
assertEquals(l.get(i), new Integer(i)); |
863 |
> |
t.join(); |
864 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
865 |
|
} |
866 |
|
|
867 |
|
/** |
868 |
|
* drainTo(null, n) throws NPE |
869 |
< |
*/ |
869 |
> |
*/ |
870 |
|
public void testDrainToNullN() { |
871 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
872 |
|
try { |
873 |
|
q.drainTo(null, 0); |
874 |
|
shouldThrow(); |
875 |
< |
} catch(NullPointerException success) { |
998 |
< |
} |
875 |
> |
} catch (NullPointerException success) {} |
876 |
|
} |
877 |
|
|
878 |
|
/** |
879 |
|
* drainTo(this, n) throws IAE |
880 |
< |
*/ |
880 |
> |
*/ |
881 |
|
public void testDrainToSelfN() { |
882 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
883 |
|
try { |
884 |
|
q.drainTo(q, 0); |
885 |
|
shouldThrow(); |
886 |
< |
} catch(IllegalArgumentException success) { |
1010 |
< |
} |
886 |
> |
} catch (IllegalArgumentException success) {} |
887 |
|
} |
888 |
|
|
889 |
|
/** |
890 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
891 |
< |
*/ |
890 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
891 |
> |
*/ |
892 |
|
public void testDrainToN() { |
893 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
894 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
895 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
895 |
> |
for (int j = 0; j < SIZE; j++) |
896 |
> |
assertTrue(q.offer(new Integer(j))); |
897 |
|
ArrayList l = new ArrayList(); |
898 |
|
q.drainTo(l, i); |
899 |
< |
int k = (i < SIZE)? i : SIZE; |
1022 |
< |
assertEquals(q.size(), SIZE-k); |
899 |
> |
int k = (i < SIZE) ? i : SIZE; |
900 |
|
assertEquals(l.size(), k); |
901 |
< |
for (int j = 0; j < k; ++j) |
901 |
> |
assertEquals(q.size(), SIZE-k); |
902 |
> |
for (int j = 0; j < k; ++j) |
903 |
|
assertEquals(l.get(j), new Integer(j)); |
904 |
+ |
while (q.poll() != null) ; |
905 |
|
} |
906 |
|
} |
907 |
|
|