44 |
|
} |
45 |
|
|
46 |
|
/** |
47 |
< |
* Constructor throws IAE if capacity argument nonpositive |
47 |
> |
* Constructor throws IAE if capacity argument nonpositive |
48 |
|
*/ |
49 |
|
public void testConstructor2() { |
50 |
|
try { |
104 |
|
* Queue contains all elements of collection used to initialize |
105 |
|
*/ |
106 |
|
public void testConstructor7() { |
107 |
< |
try { |
108 |
< |
Integer[] ints = new Integer[SIZE]; |
109 |
< |
for (int i = 0; i < SIZE; ++i) |
110 |
< |
ints[i] = new Integer(i); |
111 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); |
112 |
< |
for (int i = 0; i < SIZE; ++i) |
113 |
< |
assertEquals(ints[i], q.poll()); |
114 |
< |
} |
115 |
< |
finally {} |
107 |
> |
Integer[] ints = new Integer[SIZE]; |
108 |
> |
for (int i = 0; i < SIZE; ++i) |
109 |
> |
ints[i] = new Integer(i); |
110 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); |
111 |
> |
for (int i = 0; i < SIZE; ++i) |
112 |
> |
assertEquals(ints[i], q.poll()); |
113 |
|
} |
114 |
|
|
115 |
|
/** |
296 |
|
public void testBlockingPut() throws InterruptedException { |
297 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
298 |
|
Thread t = new Thread(new CheckedRunnable() { |
299 |
< |
public void realRun() { |
300 |
< |
int added = 0; |
299 |
> |
public void realRun() throws InterruptedException { |
300 |
> |
for (int i = 0; i < SIZE; ++i) |
301 |
> |
q.put(i); |
302 |
> |
assertEquals(SIZE, q.size()); |
303 |
> |
assertEquals(0, q.remainingCapacity()); |
304 |
|
try { |
305 |
< |
for (int i = 0; i < SIZE; ++i) { |
306 |
< |
q.put(new Integer(i)); |
307 |
< |
++added; |
308 |
< |
} |
309 |
< |
q.put(new Integer(SIZE)); |
310 |
< |
threadShouldThrow(); |
311 |
< |
} catch (InterruptedException ie) { |
312 |
< |
threadAssertEquals(added, SIZE); |
313 |
< |
}}}); |
305 |
> |
q.put(99); |
306 |
> |
shouldThrow(); |
307 |
> |
} catch (InterruptedException success) {} |
308 |
> |
}}); |
309 |
|
|
310 |
|
t.start(); |
311 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
311 |
> |
Thread.sleep(SHORT_DELAY_MS); |
312 |
|
t.interrupt(); |
313 |
|
t.join(); |
314 |
+ |
assertEquals(SIZE, q.size()); |
315 |
+ |
assertEquals(0, q.remainingCapacity()); |
316 |
|
} |
317 |
|
|
318 |
|
/** |
333 |
|
q.put(new Object()); |
334 |
|
++added; |
335 |
|
threadShouldThrow(); |
336 |
< |
} catch (InterruptedException e) { |
336 |
> |
} catch (InterruptedException success) { |
337 |
|
threadAssertTrue(added >= 2); |
338 |
|
} |
339 |
|
}}); |
350 |
|
*/ |
351 |
|
public void testTimedOffer() throws InterruptedException { |
352 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
353 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
353 |
> |
Thread t = new Thread(new CheckedRunnable() { |
354 |
|
public void realRun() throws InterruptedException { |
355 |
|
q.put(new Object()); |
356 |
|
q.put(new Object()); |
357 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); |
358 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
359 |
< |
}}; |
357 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); |
358 |
> |
try { |
359 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
360 |
> |
shouldThrow(); |
361 |
> |
} catch (InterruptedException success) {} |
362 |
> |
}}); |
363 |
|
|
364 |
|
t.start(); |
365 |
|
Thread.sleep(SHORT_DELAY_MS); |
373 |
|
public void testTake() throws InterruptedException { |
374 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
375 |
|
for (int i = 0; i < SIZE; ++i) { |
376 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
376 |
> |
assertEquals(i, q.take()); |
377 |
|
} |
378 |
|
} |
379 |
|
|
397 |
|
* Take removes existing elements until empty, then blocks interruptibly |
398 |
|
*/ |
399 |
|
public void testBlockingTake() throws InterruptedException { |
400 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
400 |
> |
final ArrayBlockingQueue q = populatedQueue(SIZE); |
401 |
> |
Thread t = new Thread(new CheckedRunnable() { |
402 |
|
public void realRun() throws InterruptedException { |
402 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
403 |
|
for (int i = 0; i < SIZE; ++i) { |
404 |
< |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
404 |
> |
assertEquals(i, q.take()); |
405 |
|
} |
406 |
< |
q.take(); |
407 |
< |
}}; |
406 |
> |
try { |
407 |
> |
q.take(); |
408 |
> |
shouldThrow(); |
409 |
> |
} catch (InterruptedException success) {} |
410 |
> |
}}); |
411 |
|
|
412 |
|
t.start(); |
413 |
< |
Thread.sleep(SHORT_DELAY_MS); |
414 |
< |
t.interrupt(); |
415 |
< |
t.join(); |
413 |
> |
Thread.sleep(SHORT_DELAY_MS); |
414 |
> |
t.interrupt(); |
415 |
> |
t.join(); |
416 |
|
} |
417 |
|
|
418 |
|
|
422 |
|
public void testPoll() { |
423 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
424 |
|
for (int i = 0; i < SIZE; ++i) { |
425 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
425 |
> |
assertEquals(i, q.poll()); |
426 |
|
} |
427 |
|
assertNull(q.poll()); |
428 |
|
} |
433 |
|
public void testTimedPoll0() throws InterruptedException { |
434 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
435 |
|
for (int i = 0; i < SIZE; ++i) { |
436 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
436 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
437 |
|
} |
438 |
|
assertNull(q.poll(0, MILLISECONDS)); |
439 |
|
} |
444 |
|
public void testTimedPoll() throws InterruptedException { |
445 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
446 |
|
for (int i = 0; i < SIZE; ++i) { |
447 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
447 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
448 |
|
} |
449 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
450 |
|
} |
454 |
|
* returning timeout status |
455 |
|
*/ |
456 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
457 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
457 |
> |
Thread t = new Thread(new CheckedRunnable() { |
458 |
|
public void realRun() throws InterruptedException { |
459 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
460 |
|
for (int i = 0; i < SIZE; ++i) { |
461 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
461 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));; |
462 |
|
} |
463 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
464 |
< |
}}; |
463 |
> |
try { |
464 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
465 |
> |
shouldThrow(); |
466 |
> |
} catch (InterruptedException success) {} |
467 |
> |
}}); |
468 |
|
|
469 |
|
t.start(); |
470 |
< |
Thread.sleep(SMALL_DELAY_MS); |
470 |
> |
Thread.sleep(SHORT_DELAY_MS); |
471 |
|
t.interrupt(); |
472 |
|
t.join(); |
473 |
|
} |
478 |
|
*/ |
479 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
480 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
481 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
481 |
> |
Thread t = new Thread(new CheckedRunnable() { |
482 |
|
public void realRun() throws InterruptedException { |
483 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
484 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
485 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
486 |
< |
}}; |
483 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
484 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
485 |
> |
try { |
486 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
487 |
> |
shouldThrow(); |
488 |
> |
} catch (InterruptedException success) {} |
489 |
> |
}}); |
490 |
|
|
491 |
|
t.start(); |
492 |
|
Thread.sleep(SMALL_DELAY_MS); |
671 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
672 |
|
Object o[] = q.toArray(new String[10] ); |
673 |
|
shouldThrow(); |
674 |
< |
} catch (ArrayStoreException success) {} |
674 |
> |
} catch (ArrayStoreException success) {} |
675 |
|
} |
676 |
|
|
677 |
|
|