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 { |
48 |
|
} |
49 |
|
|
50 |
|
/** |
51 |
< |
* Constructor throws IAE if capacity argument nonpositive |
51 |
> |
* Constructor throws IAE if capacity argument nonpositive |
52 |
|
*/ |
53 |
|
public void testConstructor2() { |
54 |
|
try { |
55 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(0); |
56 |
|
shouldThrow(); |
57 |
< |
} |
57 |
< |
catch (IllegalArgumentException success) {} |
57 |
> |
} catch (IllegalArgumentException success) {} |
58 |
|
} |
59 |
|
|
60 |
|
/** |
64 |
|
try { |
65 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(null); |
66 |
|
shouldThrow(); |
67 |
< |
} |
68 |
< |
catch (NullPointerException success) {} |
67 |
> |
} catch (NullPointerException success) {} |
68 |
|
} |
69 |
|
|
70 |
|
/** |
75 |
|
Integer[] ints = new Integer[SIZE]; |
76 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
77 |
|
shouldThrow(); |
78 |
< |
} |
80 |
< |
catch (NullPointerException success) {} |
78 |
> |
} catch (NullPointerException success) {} |
79 |
|
} |
80 |
|
|
81 |
|
/** |
88 |
|
ints[i] = new Integer(i); |
89 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
90 |
|
shouldThrow(); |
91 |
< |
} |
94 |
< |
catch (NullPointerException success) {} |
91 |
> |
} catch (NullPointerException success) {} |
92 |
|
} |
93 |
|
|
94 |
|
/** |
95 |
|
* Queue contains all elements of collection used to initialize |
96 |
|
*/ |
97 |
|
public void testConstructor6() { |
98 |
< |
try { |
99 |
< |
Integer[] ints = new Integer[SIZE]; |
100 |
< |
for (int i = 0; i < SIZE; ++i) |
101 |
< |
ints[i] = new Integer(i); |
102 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
103 |
< |
for (int i = 0; i < SIZE; ++i) |
107 |
< |
assertEquals(ints[i], q.poll()); |
108 |
< |
} |
109 |
< |
finally {} |
98 |
> |
Integer[] ints = new Integer[SIZE]; |
99 |
> |
for (int i = 0; i < SIZE; ++i) |
100 |
> |
ints[i] = new Integer(i); |
101 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
102 |
> |
for (int i = 0; i < SIZE; ++i) |
103 |
> |
assertEquals(ints[i], q.poll()); |
104 |
|
} |
105 |
|
|
106 |
|
/** |
143 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
144 |
|
q.offer(null); |
145 |
|
shouldThrow(); |
146 |
< |
} catch (NullPointerException success) { } |
146 |
> |
} catch (NullPointerException success) {} |
147 |
|
} |
148 |
|
|
149 |
|
/** |
154 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
155 |
|
q.add(null); |
156 |
|
shouldThrow(); |
157 |
< |
} catch (NullPointerException success) { } |
157 |
> |
} catch (NullPointerException success) {} |
158 |
|
} |
159 |
|
|
160 |
|
/** |
177 |
|
} |
178 |
|
assertEquals(0, q.remainingCapacity()); |
179 |
|
q.add(new Integer(SIZE)); |
180 |
< |
} catch (IllegalStateException success) { |
181 |
< |
} |
180 |
> |
shouldThrow(); |
181 |
> |
} catch (IllegalStateException success) {} |
182 |
|
} |
183 |
|
|
184 |
|
/** |
189 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
190 |
|
q.addAll(null); |
191 |
|
shouldThrow(); |
192 |
< |
} |
199 |
< |
catch (NullPointerException success) {} |
192 |
> |
} catch (NullPointerException success) {} |
193 |
|
} |
194 |
|
|
195 |
|
/** |
200 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
201 |
|
q.addAll(q); |
202 |
|
shouldThrow(); |
203 |
< |
} |
211 |
< |
catch (IllegalArgumentException success) {} |
203 |
> |
} catch (IllegalArgumentException success) {} |
204 |
|
} |
205 |
|
|
206 |
|
/** |
212 |
|
Integer[] ints = new Integer[SIZE]; |
213 |
|
q.addAll(Arrays.asList(ints)); |
214 |
|
shouldThrow(); |
215 |
< |
} |
224 |
< |
catch (NullPointerException success) {} |
215 |
> |
} catch (NullPointerException success) {} |
216 |
|
} |
217 |
|
/** |
218 |
|
* addAll of a collection with any null elements throws NPE after |
226 |
|
ints[i] = new Integer(i); |
227 |
|
q.addAll(Arrays.asList(ints)); |
228 |
|
shouldThrow(); |
229 |
< |
} |
239 |
< |
catch (NullPointerException success) {} |
229 |
> |
} catch (NullPointerException success) {} |
230 |
|
} |
231 |
|
/** |
232 |
|
* addAll throws ISE if not enough room |
239 |
|
ints[i] = new Integer(i); |
240 |
|
q.addAll(Arrays.asList(ints)); |
241 |
|
shouldThrow(); |
242 |
< |
} |
253 |
< |
catch (IllegalStateException success) {} |
242 |
> |
} catch (IllegalStateException success) {} |
243 |
|
} |
244 |
|
/** |
245 |
|
* Queue contains all elements, in traversal order, of successful addAll |
246 |
|
*/ |
247 |
|
public void testAddAll5() { |
248 |
< |
try { |
249 |
< |
Integer[] empty = new Integer[0]; |
250 |
< |
Integer[] ints = new Integer[SIZE]; |
251 |
< |
for (int i = 0; i < SIZE; ++i) |
252 |
< |
ints[i] = new Integer(i); |
253 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
254 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
255 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
256 |
< |
for (int i = 0; i < SIZE; ++i) |
268 |
< |
assertEquals(ints[i], q.poll()); |
269 |
< |
} |
270 |
< |
finally {} |
248 |
> |
Integer[] empty = new Integer[0]; |
249 |
> |
Integer[] ints = new Integer[SIZE]; |
250 |
> |
for (int i = 0; i < SIZE; ++i) |
251 |
> |
ints[i] = new Integer(i); |
252 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
253 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
254 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
255 |
> |
for (int i = 0; i < SIZE; ++i) |
256 |
> |
assertEquals(ints[i], q.poll()); |
257 |
|
} |
258 |
|
|
259 |
|
/** |
260 |
|
* put(null) throws NPE |
261 |
|
*/ |
262 |
< |
public void testPutNull() { |
262 |
> |
public void testPutNull() throws InterruptedException { |
263 |
|
try { |
264 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
265 |
|
q.put(null); |
266 |
|
shouldThrow(); |
267 |
< |
} |
282 |
< |
catch (NullPointerException success) { |
283 |
< |
} |
284 |
< |
catch (InterruptedException ie) { |
285 |
< |
unexpectedException(); |
286 |
< |
} |
267 |
> |
} catch (NullPointerException success) {} |
268 |
|
} |
269 |
|
|
270 |
|
/** |
271 |
|
* all elements successfully put are contained |
272 |
|
*/ |
273 |
< |
public void testPut() { |
274 |
< |
try { |
275 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
276 |
< |
for (int i = 0; i < SIZE; ++i) { |
277 |
< |
Integer I = new Integer(i); |
278 |
< |
q.put(I); |
298 |
< |
assertTrue(q.contains(I)); |
299 |
< |
} |
300 |
< |
assertEquals(0, q.remainingCapacity()); |
301 |
< |
} |
302 |
< |
catch (InterruptedException ie) { |
303 |
< |
unexpectedException(); |
273 |
> |
public void testPut() throws InterruptedException { |
274 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
275 |
> |
for (int i = 0; i < SIZE; ++i) { |
276 |
> |
Integer I = new Integer(i); |
277 |
> |
q.put(I); |
278 |
> |
assertTrue(q.contains(I)); |
279 |
|
} |
280 |
+ |
assertEquals(0, q.remainingCapacity()); |
281 |
|
} |
282 |
|
|
283 |
|
/** |
284 |
|
* put blocks interruptibly if full |
285 |
|
*/ |
286 |
< |
public void testBlockingPut() { |
287 |
< |
Thread t = new Thread(new Runnable() { |
288 |
< |
public void run() { |
289 |
< |
int added = 0; |
290 |
< |
try { |
291 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
292 |
< |
for (int i = 0; i < SIZE; ++i) { |
293 |
< |
q.put(new Integer(i)); |
294 |
< |
++added; |
295 |
< |
} |
296 |
< |
q.put(new Integer(SIZE)); |
297 |
< |
threadShouldThrow(); |
298 |
< |
} catch (InterruptedException ie) { |
299 |
< |
threadAssertEquals(added, SIZE); |
324 |
< |
} |
325 |
< |
}}); |
286 |
> |
public void testBlockingPut() throws InterruptedException { |
287 |
> |
final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
288 |
> |
Thread t = new Thread(new CheckedRunnable() { |
289 |
> |
public void realRun() throws InterruptedException { |
290 |
> |
for (int i = 0; i < SIZE; ++i) |
291 |
> |
q.put(i); |
292 |
> |
assertEquals(SIZE, q.size()); |
293 |
> |
assertEquals(0, q.remainingCapacity()); |
294 |
> |
try { |
295 |
> |
q.put(99); |
296 |
> |
shouldThrow(); |
297 |
> |
} catch (InterruptedException success) {} |
298 |
> |
}}); |
299 |
> |
|
300 |
|
t.start(); |
301 |
< |
try { |
302 |
< |
Thread.sleep(SHORT_DELAY_MS); |
303 |
< |
t.interrupt(); |
304 |
< |
t.join(); |
305 |
< |
} |
332 |
< |
catch (InterruptedException ie) { |
333 |
< |
unexpectedException(); |
334 |
< |
} |
301 |
> |
Thread.sleep(SHORT_DELAY_MS); |
302 |
> |
t.interrupt(); |
303 |
> |
t.join(); |
304 |
> |
assertEquals(SIZE, q.size()); |
305 |
> |
assertEquals(0, q.remainingCapacity()); |
306 |
|
} |
307 |
|
|
308 |
|
/** |
309 |
|
* put blocks waiting for take when full |
310 |
|
*/ |
311 |
< |
public void testPutWithTake() { |
311 |
> |
public void testPutWithTake() throws InterruptedException { |
312 |
> |
final int capacity = 2; |
313 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
314 |
< |
Thread t = new Thread(new Runnable() { |
315 |
< |
public void run() { |
316 |
< |
int added = 0; |
317 |
< |
try { |
318 |
< |
q.put(new Object()); |
319 |
< |
++added; |
320 |
< |
q.put(new Object()); |
321 |
< |
++added; |
322 |
< |
q.put(new Object()); |
323 |
< |
++added; |
324 |
< |
q.put(new Object()); |
325 |
< |
++added; |
326 |
< |
threadShouldThrow(); |
327 |
< |
} catch (InterruptedException e) { |
328 |
< |
threadAssertTrue(added >= 2); |
329 |
< |
} |
330 |
< |
} |
331 |
< |
}); |
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 |
< |
} |
314 |
> |
Thread t = new Thread(new CheckedRunnable() { |
315 |
> |
public void realRun() throws InterruptedException { |
316 |
> |
for (int i = 0; i < capacity + 1; i++) |
317 |
> |
q.put(i); |
318 |
> |
try { |
319 |
> |
q.put(99); |
320 |
> |
shouldThrow(); |
321 |
> |
} catch (InterruptedException success) {} |
322 |
> |
}}); |
323 |
> |
|
324 |
> |
t.start(); |
325 |
> |
Thread.sleep(SHORT_DELAY_MS); |
326 |
> |
assertEquals(q.remainingCapacity(), 0); |
327 |
> |
assertEquals(0, q.take()); |
328 |
> |
Thread.sleep(SHORT_DELAY_MS); |
329 |
> |
t.interrupt(); |
330 |
> |
t.join(); |
331 |
> |
assertEquals(q.remainingCapacity(), 0); |
332 |
|
} |
333 |
|
|
334 |
|
/** |
335 |
|
* timed offer times out if full and elements not taken |
336 |
|
*/ |
337 |
< |
public void testTimedOffer() { |
337 |
> |
public void testTimedOffer() throws InterruptedException { |
338 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
339 |
< |
Thread t = new Thread(new Runnable() { |
340 |
< |
public void run() { |
341 |
< |
try { |
342 |
< |
q.put(new Object()); |
343 |
< |
q.put(new Object()); |
344 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
345 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
346 |
< |
threadShouldThrow(); |
347 |
< |
} catch (InterruptedException success) {} |
348 |
< |
} |
386 |
< |
}); |
339 |
> |
Thread t = new Thread(new CheckedRunnable() { |
340 |
> |
public void realRun() throws InterruptedException { |
341 |
> |
q.put(new Object()); |
342 |
> |
q.put(new Object()); |
343 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
344 |
> |
try { |
345 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
346 |
> |
shouldThrow(); |
347 |
> |
} catch (InterruptedException success) {} |
348 |
> |
}}); |
349 |
|
|
350 |
< |
try { |
351 |
< |
t.start(); |
352 |
< |
Thread.sleep(SMALL_DELAY_MS); |
353 |
< |
t.interrupt(); |
392 |
< |
t.join(); |
393 |
< |
} catch (Exception e) { |
394 |
< |
unexpectedException(); |
395 |
< |
} |
350 |
> |
t.start(); |
351 |
> |
Thread.sleep(SMALL_DELAY_MS); |
352 |
> |
t.interrupt(); |
353 |
> |
t.join(); |
354 |
|
} |
355 |
|
|
356 |
|
/** |
357 |
|
* take retrieves elements in FIFO order |
358 |
|
*/ |
359 |
< |
public void testTake() { |
360 |
< |
try { |
361 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
362 |
< |
for (int i = 0; i < SIZE; ++i) { |
405 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
406 |
< |
} |
407 |
< |
} catch (InterruptedException e) { |
408 |
< |
unexpectedException(); |
359 |
> |
public void testTake() throws InterruptedException { |
360 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
361 |
> |
for (int i = 0; i < SIZE; ++i) { |
362 |
> |
assertEquals(i, q.take()); |
363 |
|
} |
364 |
|
} |
365 |
|
|
366 |
|
/** |
367 |
|
* take blocks interruptibly when empty |
368 |
|
*/ |
369 |
< |
public void testTakeFromEmpty() { |
369 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
370 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
371 |
< |
Thread t = new Thread(new Runnable() { |
372 |
< |
public void run() { |
373 |
< |
try { |
374 |
< |
q.take(); |
375 |
< |
threadShouldThrow(); |
376 |
< |
} catch (InterruptedException success) { } |
377 |
< |
} |
378 |
< |
}); |
379 |
< |
try { |
426 |
< |
t.start(); |
427 |
< |
Thread.sleep(SHORT_DELAY_MS); |
428 |
< |
t.interrupt(); |
429 |
< |
t.join(); |
430 |
< |
} catch (Exception e) { |
431 |
< |
unexpectedException(); |
432 |
< |
} |
371 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
372 |
> |
public void realRun() throws InterruptedException { |
373 |
> |
q.take(); |
374 |
> |
}}; |
375 |
> |
|
376 |
> |
t.start(); |
377 |
> |
Thread.sleep(SHORT_DELAY_MS); |
378 |
> |
t.interrupt(); |
379 |
> |
t.join(); |
380 |
|
} |
381 |
|
|
382 |
|
/** |
383 |
|
* Take removes existing elements until empty, then blocks interruptibly |
384 |
|
*/ |
385 |
< |
public void testBlockingTake() { |
386 |
< |
Thread t = new Thread(new Runnable() { |
387 |
< |
public void run() { |
388 |
< |
try { |
389 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
390 |
< |
for (int i = 0; i < SIZE; ++i) { |
391 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
392 |
< |
} |
393 |
< |
q.take(); |
394 |
< |
threadShouldThrow(); |
395 |
< |
} catch (InterruptedException success) { |
396 |
< |
} |
397 |
< |
}}); |
385 |
> |
public void testBlockingTake() throws InterruptedException { |
386 |
> |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
387 |
> |
Thread t = new Thread(new CheckedRunnable() { |
388 |
> |
public void realRun() throws InterruptedException { |
389 |
> |
for (int i = 0; i < SIZE; ++i) { |
390 |
> |
assertEquals(i, q.take()); |
391 |
> |
} |
392 |
> |
try { |
393 |
> |
q.take(); |
394 |
> |
shouldThrow(); |
395 |
> |
} catch (InterruptedException success) {} |
396 |
> |
}}); |
397 |
> |
|
398 |
|
t.start(); |
399 |
< |
try { |
400 |
< |
Thread.sleep(SHORT_DELAY_MS); |
401 |
< |
t.interrupt(); |
455 |
< |
t.join(); |
456 |
< |
} |
457 |
< |
catch (InterruptedException ie) { |
458 |
< |
unexpectedException(); |
459 |
< |
} |
399 |
> |
Thread.sleep(SHORT_DELAY_MS); |
400 |
> |
t.interrupt(); |
401 |
> |
t.join(); |
402 |
|
} |
403 |
|
|
462 |
– |
|
404 |
|
/** |
405 |
|
* poll succeeds unless empty |
406 |
|
*/ |
407 |
|
public void testPoll() { |
408 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
409 |
|
for (int i = 0; i < SIZE; ++i) { |
410 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
410 |
> |
assertEquals(i, q.poll()); |
411 |
|
} |
412 |
|
assertNull(q.poll()); |
413 |
|
} |
415 |
|
/** |
416 |
|
* timed pool with zero timeout succeeds when non-empty, else times out |
417 |
|
*/ |
418 |
< |
public void testTimedPoll0() { |
419 |
< |
try { |
420 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
421 |
< |
for (int i = 0; i < SIZE; ++i) { |
481 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
482 |
< |
} |
483 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
484 |
< |
} catch (InterruptedException e) { |
485 |
< |
unexpectedException(); |
418 |
> |
public void testTimedPoll0() throws InterruptedException { |
419 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
420 |
> |
for (int i = 0; i < SIZE; ++i) { |
421 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
422 |
|
} |
423 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
424 |
|
} |
425 |
|
|
426 |
|
/** |
427 |
|
* timed pool with nonzero timeout succeeds when non-empty, else times out |
428 |
|
*/ |
429 |
< |
public void testTimedPoll() { |
430 |
< |
try { |
431 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
432 |
< |
for (int i = 0; i < SIZE; ++i) { |
496 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
497 |
< |
} |
498 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
499 |
< |
} catch (InterruptedException e) { |
500 |
< |
unexpectedException(); |
429 |
> |
public void testTimedPoll() throws InterruptedException { |
430 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
431 |
> |
for (int i = 0; i < SIZE; ++i) { |
432 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
433 |
|
} |
434 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
435 |
|
} |
436 |
|
|
437 |
|
/** |
438 |
|
* Interrupted timed poll throws InterruptedException instead of |
439 |
|
* returning timeout status |
440 |
|
*/ |
441 |
< |
public void testInterruptedTimedPoll() { |
442 |
< |
Thread t = new Thread(new Runnable() { |
443 |
< |
public void run() { |
444 |
< |
try { |
445 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
446 |
< |
for (int i = 0; i < SIZE; ++i) { |
447 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
448 |
< |
} |
449 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
450 |
< |
} catch (InterruptedException success) { |
451 |
< |
} |
452 |
< |
}}); |
441 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
442 |
> |
Thread t = new Thread(new CheckedRunnable() { |
443 |
> |
public void realRun() throws InterruptedException { |
444 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
445 |
> |
for (int i = 0; i < SIZE; ++i) { |
446 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
447 |
> |
} |
448 |
> |
try { |
449 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
450 |
> |
shouldThrow(); |
451 |
> |
} catch (InterruptedException success) {} |
452 |
> |
}}); |
453 |
> |
|
454 |
|
t.start(); |
455 |
< |
try { |
456 |
< |
Thread.sleep(SHORT_DELAY_MS); |
457 |
< |
t.interrupt(); |
524 |
< |
t.join(); |
525 |
< |
} |
526 |
< |
catch (InterruptedException ie) { |
527 |
< |
unexpectedException(); |
528 |
< |
} |
455 |
> |
Thread.sleep(SHORT_DELAY_MS); |
456 |
> |
t.interrupt(); |
457 |
> |
t.join(); |
458 |
|
} |
459 |
|
|
460 |
|
/** |
461 |
|
* timed poll before a delayed offer fails; after offer succeeds; |
462 |
|
* on interruption throws |
463 |
|
*/ |
464 |
< |
public void testTimedPollWithOffer() { |
464 |
> |
public void testTimedPollWithOffer() throws InterruptedException { |
465 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
466 |
< |
Thread t = new Thread(new Runnable() { |
467 |
< |
public void run() { |
468 |
< |
try { |
469 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
470 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
471 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
472 |
< |
threadShouldThrow(); |
473 |
< |
} catch (InterruptedException success) { } |
474 |
< |
} |
475 |
< |
}); |
476 |
< |
try { |
477 |
< |
t.start(); |
478 |
< |
Thread.sleep(SMALL_DELAY_MS); |
479 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
480 |
< |
t.interrupt(); |
552 |
< |
t.join(); |
553 |
< |
} catch (Exception e) { |
554 |
< |
unexpectedException(); |
555 |
< |
} |
466 |
> |
Thread t = new Thread(new CheckedRunnable() { |
467 |
> |
public void realRun() throws InterruptedException { |
468 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
469 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
470 |
> |
try { |
471 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
472 |
> |
shouldThrow(); |
473 |
> |
} catch (InterruptedException success) {} |
474 |
> |
}}); |
475 |
> |
|
476 |
> |
t.start(); |
477 |
> |
Thread.sleep(SMALL_DELAY_MS); |
478 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
479 |
> |
t.interrupt(); |
480 |
> |
t.join(); |
481 |
|
} |
482 |
|
|
483 |
|
/** |
486 |
|
public void testPeek() { |
487 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
488 |
|
for (int i = 0; i < SIZE; ++i) { |
489 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
490 |
< |
q.poll(); |
489 |
> |
assertEquals(i, q.peek()); |
490 |
> |
assertEquals(i, q.poll()); |
491 |
|
assertTrue(q.peek() == null || |
492 |
< |
i != ((Integer)q.peek()).intValue()); |
492 |
> |
!q.peek().equals(i)); |
493 |
|
} |
494 |
|
assertNull(q.peek()); |
495 |
|
} |
500 |
|
public void testElement() { |
501 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
502 |
|
for (int i = 0; i < SIZE; ++i) { |
503 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
504 |
< |
q.poll(); |
503 |
> |
assertEquals(i, q.element()); |
504 |
> |
assertEquals(i, q.poll()); |
505 |
|
} |
506 |
|
try { |
507 |
|
q.element(); |
508 |
|
shouldThrow(); |
509 |
< |
} |
585 |
< |
catch (NoSuchElementException success) {} |
509 |
> |
} catch (NoSuchElementException success) {} |
510 |
|
} |
511 |
|
|
512 |
|
/** |
515 |
|
public void testRemove() { |
516 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
517 |
|
for (int i = 0; i < SIZE; ++i) { |
518 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
518 |
> |
assertEquals(i, q.remove()); |
519 |
|
} |
520 |
|
try { |
521 |
|
q.remove(); |
522 |
|
shouldThrow(); |
523 |
< |
} catch (NoSuchElementException success) { |
600 |
< |
} |
523 |
> |
} catch (NoSuchElementException success) {} |
524 |
|
} |
525 |
|
|
526 |
|
/** |
541 |
|
/** |
542 |
|
* An add following remove(x) succeeds |
543 |
|
*/ |
544 |
< |
public void testRemoveElementAndAdd() { |
545 |
< |
try { |
546 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
547 |
< |
assertTrue(q.add(new Integer(1))); |
548 |
< |
assertTrue(q.add(new Integer(2))); |
549 |
< |
assertTrue(q.remove(new Integer(1))); |
550 |
< |
assertTrue(q.remove(new Integer(2))); |
551 |
< |
assertTrue(q.add(new Integer(3))); |
629 |
< |
assertTrue(q.take() != null); |
630 |
< |
} catch (Exception e) { |
631 |
< |
unexpectedException(); |
632 |
< |
} |
544 |
> |
public void testRemoveElementAndAdd() throws InterruptedException { |
545 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
546 |
> |
assertTrue(q.add(new Integer(1))); |
547 |
> |
assertTrue(q.add(new Integer(2))); |
548 |
> |
assertTrue(q.remove(new Integer(1))); |
549 |
> |
assertTrue(q.remove(new Integer(2))); |
550 |
> |
assertTrue(q.add(new Integer(3))); |
551 |
> |
assertTrue(q.take() != null); |
552 |
|
} |
553 |
|
|
554 |
|
/** |
631 |
|
/** |
632 |
|
* toArray contains all elements |
633 |
|
*/ |
634 |
< |
public void testToArray() { |
634 |
> |
public void testToArray() throws InterruptedException { |
635 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
636 |
|
Object[] o = q.toArray(); |
718 |
– |
try { |
637 |
|
for (int i = 0; i < o.length; i++) |
638 |
|
assertEquals(o[i], q.take()); |
721 |
– |
} catch (InterruptedException e) { |
722 |
– |
unexpectedException(); |
723 |
– |
} |
639 |
|
} |
640 |
|
|
641 |
|
/** |
642 |
|
* toArray(a) contains all elements |
643 |
|
*/ |
644 |
< |
public void testToArray2() { |
644 |
> |
public void testToArray2() throws InterruptedException { |
645 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
646 |
|
Integer[] ints = new Integer[SIZE]; |
647 |
|
ints = (Integer[])q.toArray(ints); |
648 |
< |
try { |
649 |
< |
for (int i = 0; i < ints.length; i++) |
735 |
< |
assertEquals(ints[i], q.take()); |
736 |
< |
} catch (InterruptedException e) { |
737 |
< |
unexpectedException(); |
738 |
< |
} |
648 |
> |
for (int i = 0; i < ints.length; i++) |
649 |
> |
assertEquals(ints[i], q.take()); |
650 |
|
} |
651 |
|
|
652 |
|
/** |
653 |
|
* toArray(null) throws NPE |
654 |
|
*/ |
655 |
|
public void testToArray_BadArg() { |
656 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
657 |
|
try { |
746 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
658 |
|
Object o[] = q.toArray(null); |
659 |
|
shouldThrow(); |
660 |
|
} catch (NullPointerException success) {} |
664 |
|
* toArray with incompatible array type throws CCE |
665 |
|
*/ |
666 |
|
public void testToArray1_BadArg() { |
667 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
668 |
|
try { |
669 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
758 |
< |
Object o[] = q.toArray(new String[10] ); |
669 |
> |
Object o[] = q.toArray(new String[10]); |
670 |
|
shouldThrow(); |
671 |
< |
} catch (ArrayStoreException success) {} |
671 |
> |
} catch (ArrayStoreException success) {} |
672 |
|
} |
673 |
|
|
674 |
|
|
675 |
|
/** |
676 |
|
* iterator iterates through all elements |
677 |
|
*/ |
678 |
< |
public void testIterator() { |
678 |
> |
public void testIterator() throws InterruptedException { |
679 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
680 |
|
Iterator it = q.iterator(); |
681 |
< |
try { |
682 |
< |
while (it.hasNext()) { |
772 |
< |
assertEquals(it.next(), q.take()); |
773 |
< |
} |
774 |
< |
} catch (InterruptedException e) { |
775 |
< |
unexpectedException(); |
681 |
> |
while (it.hasNext()) { |
682 |
> |
assertEquals(it.next(), q.take()); |
683 |
|
} |
684 |
|
} |
685 |
|
|
697 |
|
it.remove(); |
698 |
|
|
699 |
|
it = q.iterator(); |
700 |
< |
assertEquals(it.next(), one); |
701 |
< |
assertEquals(it.next(), three); |
700 |
> |
assertSame(it.next(), one); |
701 |
> |
assertSame(it.next(), three); |
702 |
|
assertFalse(it.hasNext()); |
703 |
|
} |
704 |
|
|
714 |
|
assertEquals(0, q.remainingCapacity()); |
715 |
|
int k = 0; |
716 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
717 |
< |
int i = ((Integer)(it.next())).intValue(); |
811 |
< |
assertEquals(++k, i); |
717 |
> |
assertEquals(++k, it.next()); |
718 |
|
} |
719 |
|
assertEquals(3, k); |
720 |
|
} |
727 |
|
q.add(one); |
728 |
|
q.add(two); |
729 |
|
q.add(three); |
730 |
< |
try { |
731 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
732 |
< |
q.remove(); |
827 |
< |
it.next(); |
828 |
< |
} |
829 |
< |
} |
830 |
< |
catch (ConcurrentModificationException e) { |
831 |
< |
unexpectedException(); |
730 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
731 |
> |
q.remove(); |
732 |
> |
it.next(); |
733 |
|
} |
734 |
|
assertEquals(0, q.size()); |
735 |
|
} |
755 |
|
q.add(one); |
756 |
|
q.add(two); |
757 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
758 |
< |
executor.execute(new Runnable() { |
759 |
< |
public void run() { |
760 |
< |
threadAssertFalse(q.offer(three)); |
761 |
< |
try { |
762 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
763 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
764 |
< |
} |
765 |
< |
catch (InterruptedException e) { |
766 |
< |
threadUnexpectedException(); |
767 |
< |
} |
768 |
< |
} |
769 |
< |
}); |
869 |
< |
|
870 |
< |
executor.execute(new Runnable() { |
871 |
< |
public void run() { |
872 |
< |
try { |
873 |
< |
Thread.sleep(SMALL_DELAY_MS); |
874 |
< |
threadAssertEquals(one, q.take()); |
875 |
< |
} |
876 |
< |
catch (InterruptedException e) { |
877 |
< |
threadUnexpectedException(); |
878 |
< |
} |
879 |
< |
} |
880 |
< |
}); |
758 |
> |
executor.execute(new CheckedRunnable() { |
759 |
> |
public void realRun() throws InterruptedException { |
760 |
> |
assertFalse(q.offer(three)); |
761 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
762 |
> |
assertEquals(0, q.remainingCapacity()); |
763 |
> |
}}); |
764 |
> |
|
765 |
> |
executor.execute(new CheckedRunnable() { |
766 |
> |
public void realRun() throws InterruptedException { |
767 |
> |
Thread.sleep(SMALL_DELAY_MS); |
768 |
> |
assertSame(one, q.take()); |
769 |
> |
}}); |
770 |
|
|
771 |
|
joinPool(executor); |
772 |
|
} |
777 |
|
public void testPollInExecutor() { |
778 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
779 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
780 |
< |
executor.execute(new Runnable() { |
781 |
< |
public void run() { |
782 |
< |
threadAssertNull(q.poll()); |
783 |
< |
try { |
784 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
785 |
< |
threadAssertTrue(q.isEmpty()); |
786 |
< |
} |
787 |
< |
catch (InterruptedException e) { |
788 |
< |
threadUnexpectedException(); |
789 |
< |
} |
790 |
< |
} |
791 |
< |
}); |
903 |
< |
|
904 |
< |
executor.execute(new Runnable() { |
905 |
< |
public void run() { |
906 |
< |
try { |
907 |
< |
Thread.sleep(SMALL_DELAY_MS); |
908 |
< |
q.put(one); |
909 |
< |
} |
910 |
< |
catch (InterruptedException e) { |
911 |
< |
threadUnexpectedException(); |
912 |
< |
} |
913 |
< |
} |
914 |
< |
}); |
780 |
> |
executor.execute(new CheckedRunnable() { |
781 |
> |
public void realRun() throws InterruptedException { |
782 |
> |
assertNull(q.poll()); |
783 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
784 |
> |
assertTrue(q.isEmpty()); |
785 |
> |
}}); |
786 |
> |
|
787 |
> |
executor.execute(new CheckedRunnable() { |
788 |
> |
public void realRun() throws InterruptedException { |
789 |
> |
Thread.sleep(SMALL_DELAY_MS); |
790 |
> |
q.put(one); |
791 |
> |
}}); |
792 |
|
|
793 |
|
joinPool(executor); |
794 |
|
} |
796 |
|
/** |
797 |
|
* A deserialized serialized queue has same elements in same order |
798 |
|
*/ |
799 |
< |
public void testSerialization() { |
799 |
> |
public void testSerialization() throws Exception { |
800 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
801 |
|
|
802 |
< |
try { |
803 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
804 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
805 |
< |
out.writeObject(q); |
806 |
< |
out.close(); |
807 |
< |
|
808 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
809 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
810 |
< |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
811 |
< |
assertEquals(q.size(), r.size()); |
812 |
< |
while (!q.isEmpty()) |
936 |
< |
assertEquals(q.remove(), r.remove()); |
937 |
< |
} catch (Exception e) { |
938 |
< |
unexpectedException(); |
939 |
< |
} |
802 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
803 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
804 |
> |
out.writeObject(q); |
805 |
> |
out.close(); |
806 |
> |
|
807 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
808 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
809 |
> |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
810 |
> |
assertEquals(q.size(), r.size()); |
811 |
> |
while (!q.isEmpty()) |
812 |
> |
assertEquals(q.remove(), r.remove()); |
813 |
|
} |
814 |
|
|
815 |
|
/** |
820 |
|
try { |
821 |
|
q.drainTo(null); |
822 |
|
shouldThrow(); |
823 |
< |
} catch (NullPointerException success) { |
951 |
< |
} |
823 |
> |
} catch (NullPointerException success) {} |
824 |
|
} |
825 |
|
|
826 |
|
/** |
831 |
|
try { |
832 |
|
q.drainTo(q); |
833 |
|
shouldThrow(); |
834 |
< |
} catch (IllegalArgumentException success) { |
963 |
< |
} |
834 |
> |
} catch (IllegalArgumentException success) {} |
835 |
|
} |
836 |
|
|
837 |
|
/** |
861 |
|
/** |
862 |
|
* drainTo empties full queue, unblocking a waiting put. |
863 |
|
*/ |
864 |
< |
public void testDrainToWithActivePut() { |
864 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
865 |
|
final LinkedBlockingQueue q = populatedQueue(SIZE); |
866 |
< |
Thread t = new Thread(new Runnable() { |
867 |
< |
public void run() { |
868 |
< |
try { |
869 |
< |
q.put(new Integer(SIZE+1)); |
870 |
< |
} catch (InterruptedException ie) { |
871 |
< |
threadUnexpectedException(); |
872 |
< |
} |
873 |
< |
} |
874 |
< |
}); |
875 |
< |
try { |
876 |
< |
t.start(); |
877 |
< |
ArrayList l = new ArrayList(); |
878 |
< |
q.drainTo(l); |
1008 |
< |
assertTrue(l.size() >= SIZE); |
1009 |
< |
for (int i = 0; i < SIZE; ++i) |
1010 |
< |
assertEquals(l.get(i), new Integer(i)); |
1011 |
< |
t.join(); |
1012 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
1013 |
< |
} catch (Exception e) { |
1014 |
< |
unexpectedException(); |
1015 |
< |
} |
866 |
> |
Thread t = new Thread(new CheckedRunnable() { |
867 |
> |
public void realRun() throws InterruptedException { |
868 |
> |
q.put(new Integer(SIZE+1)); |
869 |
> |
}}); |
870 |
> |
|
871 |
> |
t.start(); |
872 |
> |
ArrayList l = new ArrayList(); |
873 |
> |
q.drainTo(l); |
874 |
> |
assertTrue(l.size() >= SIZE); |
875 |
> |
for (int i = 0; i < SIZE; ++i) |
876 |
> |
assertEquals(l.get(i), new Integer(i)); |
877 |
> |
t.join(); |
878 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
879 |
|
} |
880 |
|
|
881 |
|
/** |
886 |
|
try { |
887 |
|
q.drainTo(null, 0); |
888 |
|
shouldThrow(); |
889 |
< |
} catch (NullPointerException success) { |
1027 |
< |
} |
889 |
> |
} catch (NullPointerException success) {} |
890 |
|
} |
891 |
|
|
892 |
|
/** |
897 |
|
try { |
898 |
|
q.drainTo(q, 0); |
899 |
|
shouldThrow(); |
900 |
< |
} catch (IllegalArgumentException success) { |
1039 |
< |
} |
900 |
> |
} catch (IllegalArgumentException success) {} |
901 |
|
} |
902 |
|
|
903 |
|
/** |