144 |
|
try { |
145 |
|
DelayQueue q = new DelayQueue(null); |
146 |
|
shouldThrow(); |
147 |
< |
} |
148 |
< |
catch (NullPointerException success) {} |
147 |
> |
} catch (NullPointerException success) {} |
148 |
|
} |
149 |
|
|
150 |
|
/** |
155 |
|
PDelay[] ints = new PDelay[SIZE]; |
156 |
|
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
157 |
|
shouldThrow(); |
158 |
< |
} |
160 |
< |
catch (NullPointerException success) {} |
158 |
> |
} catch (NullPointerException success) {} |
159 |
|
} |
160 |
|
|
161 |
|
/** |
168 |
|
ints[i] = new PDelay(i); |
169 |
|
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
170 |
|
shouldThrow(); |
171 |
< |
} |
174 |
< |
catch (NullPointerException success) {} |
171 |
> |
} catch (NullPointerException success) {} |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
|
* Queue contains all elements of collection used to initialize |
176 |
|
*/ |
177 |
|
public void testConstructor6() { |
178 |
< |
try { |
179 |
< |
PDelay[] ints = new PDelay[SIZE]; |
180 |
< |
for (int i = 0; i < SIZE; ++i) |
181 |
< |
ints[i] = new PDelay(i); |
182 |
< |
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
183 |
< |
for (int i = 0; i < SIZE; ++i) |
187 |
< |
assertEquals(ints[i], q.poll()); |
188 |
< |
} |
189 |
< |
finally {} |
178 |
> |
PDelay[] ints = new PDelay[SIZE]; |
179 |
> |
for (int i = 0; i < SIZE; ++i) |
180 |
> |
ints[i] = new PDelay(i); |
181 |
> |
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
182 |
> |
for (int i = 0; i < SIZE; ++i) |
183 |
> |
assertEquals(ints[i], q.poll()); |
184 |
|
} |
185 |
|
|
186 |
|
/** |
224 |
|
DelayQueue q = new DelayQueue(); |
225 |
|
q.offer(null); |
226 |
|
shouldThrow(); |
227 |
< |
} catch (NullPointerException success) { } |
227 |
> |
} catch (NullPointerException success) {} |
228 |
|
} |
229 |
|
|
230 |
|
/** |
235 |
|
DelayQueue q = new DelayQueue(); |
236 |
|
q.add(null); |
237 |
|
shouldThrow(); |
238 |
< |
} catch (NullPointerException success) { } |
238 |
> |
} catch (NullPointerException success) {} |
239 |
|
} |
240 |
|
|
241 |
|
/** |
266 |
|
DelayQueue q = new DelayQueue(); |
267 |
|
q.addAll(null); |
268 |
|
shouldThrow(); |
269 |
< |
} |
276 |
< |
catch (NullPointerException success) {} |
269 |
> |
} catch (NullPointerException success) {} |
270 |
|
} |
271 |
|
|
272 |
|
|
278 |
|
DelayQueue q = populatedQueue(SIZE); |
279 |
|
q.addAll(q); |
280 |
|
shouldThrow(); |
281 |
< |
} |
289 |
< |
catch (IllegalArgumentException success) {} |
281 |
> |
} catch (IllegalArgumentException success) {} |
282 |
|
} |
283 |
|
|
284 |
|
/** |
290 |
|
PDelay[] ints = new PDelay[SIZE]; |
291 |
|
q.addAll(Arrays.asList(ints)); |
292 |
|
shouldThrow(); |
293 |
< |
} |
302 |
< |
catch (NullPointerException success) {} |
293 |
> |
} catch (NullPointerException success) {} |
294 |
|
} |
295 |
|
/** |
296 |
|
* addAll of a collection with any null elements throws NPE after |
304 |
|
ints[i] = new PDelay(i); |
305 |
|
q.addAll(Arrays.asList(ints)); |
306 |
|
shouldThrow(); |
307 |
< |
} |
317 |
< |
catch (NullPointerException success) {} |
307 |
> |
} catch (NullPointerException success) {} |
308 |
|
} |
309 |
|
|
310 |
|
/** |
311 |
|
* Queue contains all elements of successful addAll |
312 |
|
*/ |
313 |
|
public void testAddAll5() { |
314 |
< |
try { |
315 |
< |
PDelay[] empty = new PDelay[0]; |
316 |
< |
PDelay[] ints = new PDelay[SIZE]; |
317 |
< |
for (int i = SIZE-1; i >= 0; --i) |
318 |
< |
ints[i] = new PDelay(i); |
319 |
< |
DelayQueue q = new DelayQueue(); |
320 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
321 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
322 |
< |
for (int i = 0; i < SIZE; ++i) |
333 |
< |
assertEquals(ints[i], q.poll()); |
334 |
< |
} |
335 |
< |
finally {} |
314 |
> |
PDelay[] empty = new PDelay[0]; |
315 |
> |
PDelay[] ints = new PDelay[SIZE]; |
316 |
> |
for (int i = SIZE-1; i >= 0; --i) |
317 |
> |
ints[i] = new PDelay(i); |
318 |
> |
DelayQueue q = new DelayQueue(); |
319 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
320 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
321 |
> |
for (int i = 0; i < SIZE; ++i) |
322 |
> |
assertEquals(ints[i], q.poll()); |
323 |
|
} |
324 |
|
|
325 |
|
/** |
330 |
|
DelayQueue q = new DelayQueue(); |
331 |
|
q.put(null); |
332 |
|
shouldThrow(); |
333 |
< |
} |
347 |
< |
catch (NullPointerException success) { |
348 |
< |
} |
333 |
> |
} catch (NullPointerException success) {} |
334 |
|
} |
335 |
|
|
336 |
|
/** |
337 |
|
* all elements successfully put are contained |
338 |
|
*/ |
339 |
|
public void testPut() { |
340 |
< |
try { |
341 |
< |
DelayQueue q = new DelayQueue(); |
342 |
< |
for (int i = 0; i < SIZE; ++i) { |
343 |
< |
PDelay I = new PDelay(i); |
344 |
< |
q.put(I); |
360 |
< |
assertTrue(q.contains(I)); |
361 |
< |
} |
362 |
< |
assertEquals(SIZE, q.size()); |
340 |
> |
DelayQueue q = new DelayQueue(); |
341 |
> |
for (int i = 0; i < SIZE; ++i) { |
342 |
> |
PDelay I = new PDelay(i); |
343 |
> |
q.put(I); |
344 |
> |
assertTrue(q.contains(I)); |
345 |
|
} |
346 |
< |
finally { |
365 |
< |
} |
346 |
> |
assertEquals(SIZE, q.size()); |
347 |
|
} |
348 |
|
|
349 |
|
/** |
350 |
|
* put doesn't block waiting for take |
351 |
|
*/ |
352 |
< |
public void testPutWithTake() { |
352 |
> |
public void testPutWithTake() throws InterruptedException { |
353 |
|
final DelayQueue q = new DelayQueue(); |
354 |
< |
Thread t = new Thread(new Runnable() { |
355 |
< |
public void run() { |
356 |
< |
int added = 0; |
357 |
< |
try { |
358 |
< |
q.put(new PDelay(0)); |
359 |
< |
++added; |
360 |
< |
q.put(new PDelay(0)); |
361 |
< |
++added; |
362 |
< |
q.put(new PDelay(0)); |
363 |
< |
++added; |
364 |
< |
q.put(new PDelay(0)); |
365 |
< |
++added; |
366 |
< |
threadAssertTrue(added == 4); |
386 |
< |
} finally { |
387 |
< |
} |
388 |
< |
} |
389 |
< |
}); |
390 |
< |
try { |
391 |
< |
t.start(); |
392 |
< |
Thread.sleep(SHORT_DELAY_MS); |
393 |
< |
q.take(); |
394 |
< |
t.interrupt(); |
395 |
< |
t.join(); |
396 |
< |
} catch (Exception e) { |
397 |
< |
unexpectedException(); |
398 |
< |
} |
354 |
> |
Thread t = new Thread(new CheckedRunnable() { |
355 |
> |
public void realRun() { |
356 |
> |
q.put(new PDelay(0)); |
357 |
> |
q.put(new PDelay(0)); |
358 |
> |
q.put(new PDelay(0)); |
359 |
> |
q.put(new PDelay(0)); |
360 |
> |
}}); |
361 |
> |
|
362 |
> |
t.start(); |
363 |
> |
Thread.sleep(SHORT_DELAY_MS); |
364 |
> |
q.take(); |
365 |
> |
t.interrupt(); |
366 |
> |
t.join(); |
367 |
|
} |
368 |
|
|
369 |
|
/** |
370 |
|
* timed offer does not time out |
371 |
|
*/ |
372 |
< |
public void testTimedOffer() { |
372 |
> |
public void testTimedOffer() throws InterruptedException { |
373 |
|
final DelayQueue q = new DelayQueue(); |
374 |
< |
Thread t = new Thread(new Runnable() { |
375 |
< |
public void run() { |
376 |
< |
try { |
377 |
< |
q.put(new PDelay(0)); |
378 |
< |
q.put(new PDelay(0)); |
379 |
< |
threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
380 |
< |
threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); |
413 |
< |
} finally { } |
414 |
< |
} |
415 |
< |
}); |
374 |
> |
Thread t = new Thread(new CheckedRunnable() { |
375 |
> |
public void realRun() throws InterruptedException { |
376 |
> |
q.put(new PDelay(0)); |
377 |
> |
q.put(new PDelay(0)); |
378 |
> |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
379 |
> |
assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); |
380 |
> |
}}); |
381 |
|
|
382 |
< |
try { |
383 |
< |
t.start(); |
384 |
< |
Thread.sleep(SMALL_DELAY_MS); |
385 |
< |
t.interrupt(); |
421 |
< |
t.join(); |
422 |
< |
} catch (Exception e) { |
423 |
< |
unexpectedException(); |
424 |
< |
} |
382 |
> |
t.start(); |
383 |
> |
Thread.sleep(SMALL_DELAY_MS); |
384 |
> |
t.interrupt(); |
385 |
> |
t.join(); |
386 |
|
} |
387 |
|
|
388 |
|
/** |
389 |
|
* take retrieves elements in priority order |
390 |
|
*/ |
391 |
< |
public void testTake() { |
392 |
< |
try { |
393 |
< |
DelayQueue q = populatedQueue(SIZE); |
394 |
< |
for (int i = 0; i < SIZE; ++i) { |
434 |
< |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
435 |
< |
} |
436 |
< |
} catch (InterruptedException e) { |
437 |
< |
unexpectedException(); |
391 |
> |
public void testTake() throws InterruptedException { |
392 |
> |
DelayQueue q = populatedQueue(SIZE); |
393 |
> |
for (int i = 0; i < SIZE; ++i) { |
394 |
> |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
395 |
|
} |
396 |
|
} |
397 |
|
|
398 |
|
/** |
399 |
|
* take blocks interruptibly when empty |
400 |
|
*/ |
401 |
< |
public void testTakeFromEmpty() { |
401 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
402 |
|
final DelayQueue q = new DelayQueue(); |
403 |
< |
Thread t = new Thread(new Runnable() { |
404 |
< |
public void run() { |
405 |
< |
try { |
406 |
< |
q.take(); |
407 |
< |
threadShouldThrow(); |
408 |
< |
} catch (InterruptedException success) { } |
409 |
< |
} |
410 |
< |
}); |
411 |
< |
try { |
455 |
< |
t.start(); |
456 |
< |
Thread.sleep(SHORT_DELAY_MS); |
457 |
< |
t.interrupt(); |
458 |
< |
t.join(); |
459 |
< |
} catch (Exception e) { |
460 |
< |
unexpectedException(); |
461 |
< |
} |
403 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
404 |
> |
public void realRun() throws InterruptedException { |
405 |
> |
q.take(); |
406 |
> |
}}; |
407 |
> |
|
408 |
> |
t.start(); |
409 |
> |
Thread.sleep(SHORT_DELAY_MS); |
410 |
> |
t.interrupt(); |
411 |
> |
t.join(); |
412 |
|
} |
413 |
|
|
414 |
|
/** |
415 |
|
* Take removes existing elements until empty, then blocks interruptibly |
416 |
|
*/ |
417 |
< |
public void testBlockingTake() { |
418 |
< |
Thread t = new Thread(new Runnable() { |
419 |
< |
public void run() { |
420 |
< |
try { |
421 |
< |
DelayQueue q = populatedQueue(SIZE); |
422 |
< |
for (int i = 0; i < SIZE; ++i) { |
423 |
< |
threadAssertEquals(new PDelay(i), ((PDelay)q.take())); |
424 |
< |
} |
425 |
< |
q.take(); |
426 |
< |
threadShouldThrow(); |
427 |
< |
} catch (InterruptedException success) { |
428 |
< |
} |
429 |
< |
}}); |
417 |
> |
public void testBlockingTake() throws InterruptedException { |
418 |
> |
final DelayQueue q = populatedQueue(SIZE); |
419 |
> |
Thread t = new Thread(new CheckedRunnable() { |
420 |
> |
public void realRun() throws InterruptedException { |
421 |
> |
for (int i = 0; i < SIZE; ++i) { |
422 |
> |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
423 |
> |
} |
424 |
> |
try { |
425 |
> |
q.take(); |
426 |
> |
shouldThrow(); |
427 |
> |
} catch (InterruptedException success) {} |
428 |
> |
}}); |
429 |
> |
|
430 |
|
t.start(); |
431 |
< |
try { |
432 |
< |
Thread.sleep(SHORT_DELAY_MS); |
433 |
< |
t.interrupt(); |
484 |
< |
t.join(); |
485 |
< |
} |
486 |
< |
catch (InterruptedException ie) { |
487 |
< |
unexpectedException(); |
488 |
< |
} |
431 |
> |
Thread.sleep(SHORT_DELAY_MS); |
432 |
> |
t.interrupt(); |
433 |
> |
t.join(); |
434 |
|
} |
435 |
|
|
436 |
|
|
448 |
|
/** |
449 |
|
* timed pool with zero timeout succeeds when non-empty, else times out |
450 |
|
*/ |
451 |
< |
public void testTimedPoll0() { |
452 |
< |
try { |
453 |
< |
DelayQueue q = populatedQueue(SIZE); |
454 |
< |
for (int i = 0; i < SIZE; ++i) { |
510 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS))); |
511 |
< |
} |
512 |
< |
assertNull(q.poll(0, MILLISECONDS)); |
513 |
< |
} catch (InterruptedException e) { |
514 |
< |
unexpectedException(); |
451 |
> |
public void testTimedPoll0() throws InterruptedException { |
452 |
> |
DelayQueue q = populatedQueue(SIZE); |
453 |
> |
for (int i = 0; i < SIZE; ++i) { |
454 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS))); |
455 |
|
} |
456 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
457 |
|
} |
458 |
|
|
459 |
|
/** |
460 |
|
* timed pool with nonzero timeout succeeds when non-empty, else times out |
461 |
|
*/ |
462 |
< |
public void testTimedPoll() { |
463 |
< |
try { |
464 |
< |
DelayQueue q = populatedQueue(SIZE); |
465 |
< |
for (int i = 0; i < SIZE; ++i) { |
525 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
526 |
< |
} |
527 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
528 |
< |
} catch (InterruptedException e) { |
529 |
< |
unexpectedException(); |
462 |
> |
public void testTimedPoll() throws InterruptedException { |
463 |
> |
DelayQueue q = populatedQueue(SIZE); |
464 |
> |
for (int i = 0; i < SIZE; ++i) { |
465 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
466 |
|
} |
467 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
468 |
|
} |
469 |
|
|
470 |
|
/** |
471 |
|
* Interrupted timed poll throws InterruptedException instead of |
472 |
|
* returning timeout status |
473 |
|
*/ |
474 |
< |
public void testInterruptedTimedPoll() { |
475 |
< |
Thread t = new Thread(new Runnable() { |
476 |
< |
public void run() { |
477 |
< |
try { |
478 |
< |
DelayQueue q = populatedQueue(SIZE); |
479 |
< |
for (int i = 0; i < SIZE; ++i) { |
480 |
< |
threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
481 |
< |
} |
482 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
483 |
< |
} catch (InterruptedException success) { |
484 |
< |
} |
485 |
< |
}}); |
474 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
475 |
> |
Thread t = new Thread(new CheckedRunnable() { |
476 |
> |
public void realRun() throws InterruptedException { |
477 |
> |
DelayQueue q = populatedQueue(SIZE); |
478 |
> |
for (int i = 0; i < SIZE; ++i) { |
479 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
480 |
> |
} |
481 |
> |
try { |
482 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
483 |
> |
shouldThrow(); |
484 |
> |
} catch (InterruptedException success) {} |
485 |
> |
}}); |
486 |
> |
|
487 |
|
t.start(); |
488 |
< |
try { |
489 |
< |
Thread.sleep(SHORT_DELAY_MS); |
490 |
< |
t.interrupt(); |
553 |
< |
t.join(); |
554 |
< |
} |
555 |
< |
catch (InterruptedException ie) { |
556 |
< |
unexpectedException(); |
557 |
< |
} |
488 |
> |
Thread.sleep(SHORT_DELAY_MS); |
489 |
> |
t.interrupt(); |
490 |
> |
t.join(); |
491 |
|
} |
492 |
|
|
493 |
|
/** |
494 |
|
* timed poll before a delayed offer fails; after offer succeeds; |
495 |
|
* on interruption throws |
496 |
|
*/ |
497 |
< |
public void testTimedPollWithOffer() { |
497 |
> |
public void testTimedPollWithOffer() throws InterruptedException { |
498 |
|
final DelayQueue q = new DelayQueue(); |
499 |
< |
Thread t = new Thread(new Runnable() { |
500 |
< |
public void run() { |
501 |
< |
try { |
502 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
503 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
504 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
505 |
< |
threadFail("Should block"); |
506 |
< |
} catch (InterruptedException success) { } |
507 |
< |
} |
508 |
< |
}); |
509 |
< |
try { |
510 |
< |
t.start(); |
511 |
< |
Thread.sleep(SMALL_DELAY_MS); |
512 |
< |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
513 |
< |
t.interrupt(); |
514 |
< |
t.join(); |
582 |
< |
} catch (Exception e) { |
583 |
< |
unexpectedException(); |
584 |
< |
} |
499 |
> |
final PDelay pdelay = new PDelay(0); |
500 |
> |
Thread t = new Thread(new CheckedRunnable() { |
501 |
> |
public void realRun() throws InterruptedException { |
502 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
503 |
> |
assertSame(pdelay, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
504 |
> |
try { |
505 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
506 |
> |
shouldThrow(); |
507 |
> |
} catch (InterruptedException success) {} |
508 |
> |
}}); |
509 |
> |
|
510 |
> |
t.start(); |
511 |
> |
Thread.sleep(SMALL_DELAY_MS); |
512 |
> |
assertTrue(q.offer(pdelay, SHORT_DELAY_MS, MILLISECONDS)); |
513 |
> |
t.interrupt(); |
514 |
> |
t.join(); |
515 |
|
} |
516 |
|
|
517 |
|
|
522 |
|
DelayQueue q = populatedQueue(SIZE); |
523 |
|
for (int i = 0; i < SIZE; ++i) { |
524 |
|
assertEquals(new PDelay(i), ((PDelay)q.peek())); |
525 |
< |
q.poll(); |
525 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
526 |
|
if (q.isEmpty()) |
527 |
|
assertNull(q.peek()); |
528 |
|
else |
529 |
< |
assertTrue(i != ((PDelay)q.peek()).intValue()); |
529 |
> |
assertFalse(new PDelay(i).equals(q.peek())); |
530 |
|
} |
531 |
|
assertNull(q.peek()); |
532 |
|
} |
543 |
|
try { |
544 |
|
q.element(); |
545 |
|
shouldThrow(); |
546 |
< |
} |
617 |
< |
catch (NoSuchElementException success) {} |
546 |
> |
} catch (NoSuchElementException success) {} |
547 |
|
} |
548 |
|
|
549 |
|
/** |
557 |
|
try { |
558 |
|
q.remove(); |
559 |
|
shouldThrow(); |
560 |
< |
} catch (NoSuchElementException success) { |
632 |
< |
} |
560 |
> |
} catch (NoSuchElementException success) {} |
561 |
|
} |
562 |
|
|
563 |
|
/** |
656 |
|
/** |
657 |
|
* toArray contains all elements |
658 |
|
*/ |
659 |
< |
public void testToArray() { |
659 |
> |
public void testToArray() throws InterruptedException { |
660 |
|
DelayQueue q = populatedQueue(SIZE); |
661 |
|
Object[] o = q.toArray(); |
662 |
|
Arrays.sort(o); |
735 |
– |
try { |
663 |
|
for (int i = 0; i < o.length; i++) |
664 |
|
assertEquals(o[i], q.take()); |
738 |
– |
} catch (InterruptedException e) { |
739 |
– |
unexpectedException(); |
740 |
– |
} |
665 |
|
} |
666 |
|
|
667 |
|
/** |
668 |
|
* toArray(a) contains all elements |
669 |
|
*/ |
670 |
< |
public void testToArray2() { |
670 |
> |
public void testToArray2() throws InterruptedException { |
671 |
|
DelayQueue q = populatedQueue(SIZE); |
672 |
|
PDelay[] ints = new PDelay[SIZE]; |
673 |
|
ints = (PDelay[])q.toArray(ints); |
674 |
|
Arrays.sort(ints); |
675 |
< |
try { |
676 |
< |
for (int i = 0; i < ints.length; i++) |
753 |
< |
assertEquals(ints[i], q.take()); |
754 |
< |
} catch (InterruptedException e) { |
755 |
< |
unexpectedException(); |
756 |
< |
} |
675 |
> |
for (int i = 0; i < ints.length; i++) |
676 |
> |
assertEquals(ints[i], q.take()); |
677 |
|
} |
678 |
|
|
679 |
|
|
681 |
|
* toArray(null) throws NPE |
682 |
|
*/ |
683 |
|
public void testToArray_BadArg() { |
684 |
+ |
DelayQueue q = populatedQueue(SIZE); |
685 |
|
try { |
765 |
– |
DelayQueue q = populatedQueue(SIZE); |
686 |
|
Object o[] = q.toArray(null); |
687 |
|
shouldThrow(); |
688 |
|
} catch (NullPointerException success) {} |
692 |
|
* toArray with incompatible array type throws CCE |
693 |
|
*/ |
694 |
|
public void testToArray1_BadArg() { |
695 |
+ |
DelayQueue q = populatedQueue(SIZE); |
696 |
|
try { |
697 |
< |
DelayQueue q = populatedQueue(SIZE); |
777 |
< |
Object o[] = q.toArray(new String[10] ); |
697 |
> |
Object o[] = q.toArray(new String[10]); |
698 |
|
shouldThrow(); |
699 |
< |
} catch (ArrayStoreException success) {} |
699 |
> |
} catch (ArrayStoreException success) {} |
700 |
|
} |
701 |
|
|
702 |
|
/** |
748 |
|
public void testPollInExecutor() { |
749 |
|
final DelayQueue q = new DelayQueue(); |
750 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
751 |
< |
executor.execute(new Runnable() { |
752 |
< |
public void run() { |
751 |
> |
executor.execute(new CheckedRunnable() { |
752 |
> |
public void realRun() throws InterruptedException { |
753 |
|
threadAssertNull(q.poll()); |
754 |
< |
try { |
755 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
756 |
< |
threadAssertTrue(q.isEmpty()); |
757 |
< |
} |
758 |
< |
catch (InterruptedException e) { |
759 |
< |
threadUnexpectedException(); |
760 |
< |
} |
761 |
< |
} |
762 |
< |
}); |
754 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
755 |
> |
threadAssertTrue(q.isEmpty()); |
756 |
> |
}}); |
757 |
> |
|
758 |
> |
executor.execute(new CheckedRunnable() { |
759 |
> |
public void realRun() throws InterruptedException { |
760 |
> |
Thread.sleep(SHORT_DELAY_MS); |
761 |
> |
q.put(new PDelay(1)); |
762 |
> |
}}); |
763 |
|
|
844 |
– |
executor.execute(new Runnable() { |
845 |
– |
public void run() { |
846 |
– |
try { |
847 |
– |
Thread.sleep(SHORT_DELAY_MS); |
848 |
– |
q.put(new PDelay(1)); |
849 |
– |
} |
850 |
– |
catch (InterruptedException e) { |
851 |
– |
threadUnexpectedException(); |
852 |
– |
} |
853 |
– |
} |
854 |
– |
}); |
764 |
|
joinPool(executor); |
765 |
|
|
766 |
|
} |
769 |
|
/** |
770 |
|
* Delayed actions do not occur until their delay elapses |
771 |
|
*/ |
772 |
< |
public void testDelay() { |
772 |
> |
public void testDelay() throws InterruptedException { |
773 |
|
DelayQueue q = new DelayQueue(); |
774 |
|
NanoDelay[] elements = new NanoDelay[SIZE]; |
775 |
|
for (int i = 0; i < SIZE; ++i) { |
779 |
|
q.add(elements[i]); |
780 |
|
} |
781 |
|
|
782 |
< |
try { |
783 |
< |
long last = 0; |
784 |
< |
for (int i = 0; i < SIZE; ++i) { |
785 |
< |
NanoDelay e = (NanoDelay)(q.take()); |
786 |
< |
long tt = e.getTriggerTime(); |
787 |
< |
assertTrue(tt <= System.nanoTime()); |
788 |
< |
if (i != 0) |
789 |
< |
assertTrue(tt >= last); |
881 |
< |
last = tt; |
882 |
< |
} |
883 |
< |
} |
884 |
< |
catch (InterruptedException ie) { |
885 |
< |
unexpectedException(); |
782 |
> |
long last = 0; |
783 |
> |
for (int i = 0; i < SIZE; ++i) { |
784 |
> |
NanoDelay e = (NanoDelay)(q.take()); |
785 |
> |
long tt = e.getTriggerTime(); |
786 |
> |
assertTrue(tt <= System.nanoTime()); |
787 |
> |
if (i != 0) |
788 |
> |
assertTrue(tt >= last); |
789 |
> |
last = tt; |
790 |
|
} |
791 |
|
} |
792 |
|
|
812 |
|
/** |
813 |
|
* timed poll of a non-empty queue returns null if no expired elements. |
814 |
|
*/ |
815 |
< |
public void testTimedPollDelayed() { |
815 |
> |
public void testTimedPollDelayed() throws InterruptedException { |
816 |
|
DelayQueue q = new DelayQueue(); |
817 |
|
q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); |
818 |
< |
try { |
915 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
916 |
< |
} catch (Exception ex) { |
917 |
< |
unexpectedException(); |
918 |
< |
} |
818 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
819 |
|
} |
820 |
|
|
821 |
|
/** |
826 |
|
try { |
827 |
|
q.drainTo(null); |
828 |
|
shouldThrow(); |
829 |
< |
} catch (NullPointerException success) { |
930 |
< |
} |
829 |
> |
} catch (NullPointerException success) {} |
830 |
|
} |
831 |
|
|
832 |
|
/** |
837 |
|
try { |
838 |
|
q.drainTo(q); |
839 |
|
shouldThrow(); |
840 |
< |
} catch (IllegalArgumentException success) { |
942 |
< |
} |
840 |
> |
} catch (IllegalArgumentException success) {} |
841 |
|
} |
842 |
|
|
843 |
|
/** |
871 |
|
/** |
872 |
|
* drainTo empties queue |
873 |
|
*/ |
874 |
< |
public void testDrainToWithActivePut() { |
874 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
875 |
|
final DelayQueue q = populatedQueue(SIZE); |
876 |
< |
Thread t = new Thread(new Runnable() { |
877 |
< |
public void run() { |
878 |
< |
q.put(new PDelay(SIZE+1)); |
879 |
< |
} |
880 |
< |
}); |
881 |
< |
try { |
882 |
< |
t.start(); |
883 |
< |
ArrayList l = new ArrayList(); |
884 |
< |
q.drainTo(l); |
885 |
< |
assertTrue(l.size() >= SIZE); |
886 |
< |
t.join(); |
989 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
990 |
< |
} catch (Exception e) { |
991 |
< |
unexpectedException(); |
992 |
< |
} |
876 |
> |
Thread t = new Thread(new CheckedRunnable() { |
877 |
> |
public void realRun() { |
878 |
> |
q.put(new PDelay(SIZE+1)); |
879 |
> |
}}); |
880 |
> |
|
881 |
> |
t.start(); |
882 |
> |
ArrayList l = new ArrayList(); |
883 |
> |
q.drainTo(l); |
884 |
> |
assertTrue(l.size() >= SIZE); |
885 |
> |
t.join(); |
886 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
887 |
|
} |
888 |
|
|
889 |
|
/** |
894 |
|
try { |
895 |
|
q.drainTo(null, 0); |
896 |
|
shouldThrow(); |
897 |
< |
} catch (NullPointerException success) { |
1004 |
< |
} |
897 |
> |
} catch (NullPointerException success) {} |
898 |
|
} |
899 |
|
|
900 |
|
/** |
905 |
|
try { |
906 |
|
q.drainTo(q, 0); |
907 |
|
shouldThrow(); |
908 |
< |
} catch (IllegalArgumentException success) { |
1016 |
< |
} |
908 |
> |
} catch (IllegalArgumentException success) {} |
909 |
|
} |
910 |
|
|
911 |
|
/** |