1 |
|
/* |
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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.Iterator; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.concurrent.BlockingQueue; |
16 |
> |
import java.util.concurrent.CountDownLatch; |
17 |
> |
import java.util.concurrent.Delayed; |
18 |
> |
import java.util.concurrent.DelayQueue; |
19 |
> |
import java.util.concurrent.Executors; |
20 |
> |
import java.util.concurrent.ExecutorService; |
21 |
> |
import java.util.concurrent.TimeUnit; |
22 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
12 |
– |
import java.util.concurrent.*; |
23 |
|
|
24 |
|
public class DelayQueueTest extends JSR166TestCase { |
25 |
+ |
|
26 |
+ |
public static class Generic extends BlockingQueueTest { |
27 |
+ |
protected BlockingQueue emptyCollection() { |
28 |
+ |
return new DelayQueue(); |
29 |
+ |
} |
30 |
+ |
protected PDelay makeElement(int i) { |
31 |
+ |
return new PDelay(i); |
32 |
+ |
} |
33 |
+ |
} |
34 |
+ |
|
35 |
|
public static void main(String[] args) { |
36 |
< |
junit.textui.TestRunner.run (suite()); |
36 |
> |
junit.textui.TestRunner.run(suite()); |
37 |
|
} |
38 |
|
|
39 |
|
public static Test suite() { |
40 |
< |
return new TestSuite(DelayQueueTest.class); |
40 |
> |
return newTestSuite(DelayQueueTest.class, |
41 |
> |
new Generic().testSuite()); |
42 |
|
} |
43 |
|
|
44 |
|
private static final int NOCAP = Integer.MAX_VALUE; |
50 |
|
*/ |
51 |
|
static class PDelay implements Delayed { |
52 |
|
int pseudodelay; |
53 |
< |
PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } |
54 |
< |
public int compareTo(PDelay y) { |
55 |
< |
int i = pseudodelay; |
56 |
< |
int j = y.pseudodelay; |
57 |
< |
if (i < j) return -1; |
37 |
< |
if (i > j) return 1; |
38 |
< |
return 0; |
53 |
> |
PDelay(int i) { pseudodelay = i; } |
54 |
> |
public int compareTo(PDelay other) { |
55 |
> |
int a = this.pseudodelay; |
56 |
> |
int b = other.pseudodelay; |
57 |
> |
return (a < b) ? -1 : (a > b) ? 1 : 0; |
58 |
|
} |
40 |
– |
|
59 |
|
public int compareTo(Delayed y) { |
60 |
|
return compareTo((PDelay)y); |
61 |
|
} |
44 |
– |
|
62 |
|
public boolean equals(Object other) { |
63 |
< |
return equals((PDelay)other); |
64 |
< |
} |
48 |
< |
public boolean equals(PDelay other) { |
49 |
< |
return other.pseudodelay == pseudodelay; |
63 |
> |
return (other instanceof PDelay) && |
64 |
> |
this.pseudodelay == ((PDelay)other).pseudodelay; |
65 |
|
} |
66 |
< |
|
67 |
< |
|
66 |
> |
// suppress [overrides] javac warning |
67 |
> |
public int hashCode() { return pseudodelay; } |
68 |
|
public long getDelay(TimeUnit ignore) { |
69 |
< |
return pseudodelay; |
55 |
< |
} |
56 |
< |
public int intValue() { |
57 |
< |
return pseudodelay; |
69 |
> |
return Integer.MIN_VALUE + pseudodelay; |
70 |
|
} |
59 |
– |
|
71 |
|
public String toString() { |
72 |
|
return String.valueOf(pseudodelay); |
73 |
|
} |
74 |
|
} |
75 |
|
|
65 |
– |
|
76 |
|
/** |
77 |
|
* Delayed implementation that actually delays |
78 |
|
*/ |
100 |
|
return other.trigger == trigger; |
101 |
|
} |
102 |
|
|
103 |
+ |
// suppress [overrides] javac warning |
104 |
+ |
public int hashCode() { return (int) trigger; } |
105 |
+ |
|
106 |
|
public long getDelay(TimeUnit unit) { |
107 |
|
long n = trigger - System.nanoTime(); |
108 |
|
return unit.convert(n, TimeUnit.NANOSECONDS); |
117 |
|
} |
118 |
|
} |
119 |
|
|
107 |
– |
|
120 |
|
/** |
121 |
< |
* Create a queue of given size containing consecutive |
121 |
> |
* Returns a new queue of given size containing consecutive |
122 |
|
* PDelays 0 ... n. |
123 |
|
*/ |
124 |
< |
private DelayQueue populatedQueue(int n) { |
125 |
< |
DelayQueue q = new DelayQueue(); |
124 |
> |
private DelayQueue<PDelay> populatedQueue(int n) { |
125 |
> |
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
126 |
|
assertTrue(q.isEmpty()); |
127 |
|
for (int i = n-1; i >= 0; i-=2) |
128 |
|
assertTrue(q.offer(new PDelay(i))); |
203 |
|
} |
204 |
|
|
205 |
|
/** |
206 |
< |
* remainingCapacity does not change when elementa added or removed, |
206 |
> |
* remainingCapacity does not change when elements added or removed, |
207 |
|
* but size does |
208 |
|
*/ |
209 |
|
public void testRemainingCapacity() { |
221 |
|
} |
222 |
|
|
223 |
|
/** |
212 |
– |
* offer(null) throws NPE |
213 |
– |
*/ |
214 |
– |
public void testOfferNull() { |
215 |
– |
try { |
216 |
– |
DelayQueue q = new DelayQueue(); |
217 |
– |
q.offer(null); |
218 |
– |
shouldThrow(); |
219 |
– |
} catch (NullPointerException success) {} |
220 |
– |
} |
221 |
– |
|
222 |
– |
/** |
223 |
– |
* add(null) throws NPE |
224 |
– |
*/ |
225 |
– |
public void testAddNull() { |
226 |
– |
try { |
227 |
– |
DelayQueue q = new DelayQueue(); |
228 |
– |
q.add(null); |
229 |
– |
shouldThrow(); |
230 |
– |
} catch (NullPointerException success) {} |
231 |
– |
} |
232 |
– |
|
233 |
– |
/** |
224 |
|
* offer non-null succeeds |
225 |
|
*/ |
226 |
|
public void testOffer() { |
241 |
|
} |
242 |
|
|
243 |
|
/** |
254 |
– |
* addAll(null) throws NPE |
255 |
– |
*/ |
256 |
– |
public void testAddAll1() { |
257 |
– |
try { |
258 |
– |
DelayQueue q = new DelayQueue(); |
259 |
– |
q.addAll(null); |
260 |
– |
shouldThrow(); |
261 |
– |
} catch (NullPointerException success) {} |
262 |
– |
} |
263 |
– |
|
264 |
– |
|
265 |
– |
/** |
244 |
|
* addAll(this) throws IAE |
245 |
|
*/ |
246 |
|
public void testAddAllSelf() { |
252 |
|
} |
253 |
|
|
254 |
|
/** |
277 |
– |
* addAll of a collection with null elements throws NPE |
278 |
– |
*/ |
279 |
– |
public void testAddAll2() { |
280 |
– |
try { |
281 |
– |
DelayQueue q = new DelayQueue(); |
282 |
– |
PDelay[] ints = new PDelay[SIZE]; |
283 |
– |
q.addAll(Arrays.asList(ints)); |
284 |
– |
shouldThrow(); |
285 |
– |
} catch (NullPointerException success) {} |
286 |
– |
} |
287 |
– |
/** |
255 |
|
* addAll of a collection with any null elements throws NPE after |
256 |
|
* possibly adding some elements |
257 |
|
*/ |
282 |
|
} |
283 |
|
|
284 |
|
/** |
318 |
– |
* put(null) throws NPE |
319 |
– |
*/ |
320 |
– |
public void testPutNull() { |
321 |
– |
try { |
322 |
– |
DelayQueue q = new DelayQueue(); |
323 |
– |
q.put(null); |
324 |
– |
shouldThrow(); |
325 |
– |
} catch (NullPointerException success) {} |
326 |
– |
} |
327 |
– |
|
328 |
– |
/** |
285 |
|
* all elements successfully put are contained |
286 |
|
*/ |
287 |
< |
public void testPut() { |
288 |
< |
DelayQueue q = new DelayQueue(); |
289 |
< |
for (int i = 0; i < SIZE; ++i) { |
290 |
< |
PDelay I = new PDelay(i); |
291 |
< |
q.put(I); |
292 |
< |
assertTrue(q.contains(I)); |
293 |
< |
} |
294 |
< |
assertEquals(SIZE, q.size()); |
287 |
> |
public void testPut() { |
288 |
> |
DelayQueue q = new DelayQueue(); |
289 |
> |
for (int i = 0; i < SIZE; ++i) { |
290 |
> |
PDelay I = new PDelay(i); |
291 |
> |
q.put(I); |
292 |
> |
assertTrue(q.contains(I)); |
293 |
> |
} |
294 |
> |
assertEquals(SIZE, q.size()); |
295 |
|
} |
296 |
|
|
297 |
|
/** |
299 |
|
*/ |
300 |
|
public void testPutWithTake() throws InterruptedException { |
301 |
|
final DelayQueue q = new DelayQueue(); |
302 |
< |
Thread t = new Thread(new CheckedRunnable() { |
302 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
303 |
|
public void realRun() { |
304 |
|
q.put(new PDelay(0)); |
305 |
|
q.put(new PDelay(0)); |
307 |
|
q.put(new PDelay(0)); |
308 |
|
}}); |
309 |
|
|
310 |
< |
t.start(); |
311 |
< |
Thread.sleep(SHORT_DELAY_MS); |
356 |
< |
q.take(); |
357 |
< |
t.interrupt(); |
358 |
< |
t.join(); |
310 |
> |
awaitTermination(t); |
311 |
> |
assertEquals(4, q.size()); |
312 |
|
} |
313 |
|
|
314 |
|
/** |
316 |
|
*/ |
317 |
|
public void testTimedOffer() throws InterruptedException { |
318 |
|
final DelayQueue q = new DelayQueue(); |
319 |
< |
Thread t = new Thread(new CheckedRunnable() { |
319 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
320 |
|
public void realRun() throws InterruptedException { |
321 |
|
q.put(new PDelay(0)); |
322 |
|
q.put(new PDelay(0)); |
324 |
|
assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); |
325 |
|
}}); |
326 |
|
|
327 |
< |
t.start(); |
375 |
< |
Thread.sleep(SMALL_DELAY_MS); |
376 |
< |
t.interrupt(); |
377 |
< |
t.join(); |
327 |
> |
awaitTermination(t); |
328 |
|
} |
329 |
|
|
330 |
|
/** |
338 |
|
} |
339 |
|
|
340 |
|
/** |
391 |
– |
* take blocks interruptibly when empty |
392 |
– |
*/ |
393 |
– |
public void testTakeFromEmpty() throws InterruptedException { |
394 |
– |
final DelayQueue q = new DelayQueue(); |
395 |
– |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
396 |
– |
public void realRun() throws InterruptedException { |
397 |
– |
q.take(); |
398 |
– |
}}; |
399 |
– |
|
400 |
– |
t.start(); |
401 |
– |
Thread.sleep(SHORT_DELAY_MS); |
402 |
– |
t.interrupt(); |
403 |
– |
t.join(); |
404 |
– |
} |
405 |
– |
|
406 |
– |
/** |
341 |
|
* Take removes existing elements until empty, then blocks interruptibly |
342 |
|
*/ |
343 |
|
public void testBlockingTake() throws InterruptedException { |
344 |
|
final DelayQueue q = populatedQueue(SIZE); |
345 |
< |
Thread t = new Thread(new CheckedRunnable() { |
345 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
346 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
347 |
|
public void realRun() throws InterruptedException { |
348 |
|
for (int i = 0; i < SIZE; ++i) { |
349 |
|
assertEquals(new PDelay(i), ((PDelay)q.take())); |
350 |
|
} |
351 |
+ |
|
352 |
+ |
Thread.currentThread().interrupt(); |
353 |
|
try { |
354 |
|
q.take(); |
355 |
|
shouldThrow(); |
356 |
|
} catch (InterruptedException success) {} |
357 |
+ |
assertFalse(Thread.interrupted()); |
358 |
+ |
|
359 |
+ |
pleaseInterrupt.countDown(); |
360 |
+ |
try { |
361 |
+ |
q.take(); |
362 |
+ |
shouldThrow(); |
363 |
+ |
} catch (InterruptedException success) {} |
364 |
+ |
assertFalse(Thread.interrupted()); |
365 |
|
}}); |
366 |
|
|
367 |
< |
t.start(); |
368 |
< |
Thread.sleep(SHORT_DELAY_MS); |
367 |
> |
await(pleaseInterrupt); |
368 |
> |
assertThreadStaysAlive(t); |
369 |
|
t.interrupt(); |
370 |
< |
t.join(); |
370 |
> |
awaitTermination(t); |
371 |
|
} |
372 |
|
|
428 |
– |
|
373 |
|
/** |
374 |
|
* poll succeeds unless empty |
375 |
|
*/ |
382 |
|
} |
383 |
|
|
384 |
|
/** |
385 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
385 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
386 |
|
*/ |
387 |
|
public void testTimedPoll0() throws InterruptedException { |
388 |
|
DelayQueue q = populatedQueue(SIZE); |
393 |
|
} |
394 |
|
|
395 |
|
/** |
396 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
396 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
397 |
|
*/ |
398 |
|
public void testTimedPoll() throws InterruptedException { |
399 |
|
DelayQueue q = populatedQueue(SIZE); |
400 |
|
for (int i = 0; i < SIZE; ++i) { |
401 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
402 |
< |
} |
403 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
401 |
> |
long startTime = System.nanoTime(); |
402 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); |
403 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
404 |
> |
} |
405 |
> |
long startTime = System.nanoTime(); |
406 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
407 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
408 |
> |
checkEmpty(q); |
409 |
|
} |
410 |
|
|
411 |
|
/** |
413 |
|
* returning timeout status |
414 |
|
*/ |
415 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
416 |
< |
Thread t = new Thread(new CheckedRunnable() { |
416 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
417 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
418 |
|
public void realRun() throws InterruptedException { |
419 |
|
DelayQueue q = populatedQueue(SIZE); |
420 |
|
for (int i = 0; i < SIZE; ++i) { |
421 |
|
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
422 |
|
} |
423 |
+ |
|
424 |
+ |
Thread.currentThread().interrupt(); |
425 |
|
try { |
426 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
426 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
427 |
|
shouldThrow(); |
428 |
|
} catch (InterruptedException success) {} |
429 |
< |
}}); |
478 |
< |
|
479 |
< |
t.start(); |
480 |
< |
Thread.sleep(SHORT_DELAY_MS); |
481 |
< |
t.interrupt(); |
482 |
< |
t.join(); |
483 |
< |
} |
429 |
> |
assertFalse(Thread.interrupted()); |
430 |
|
|
431 |
< |
/** |
486 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
487 |
< |
* on interruption throws |
488 |
< |
*/ |
489 |
< |
public void testTimedPollWithOffer() throws InterruptedException { |
490 |
< |
final DelayQueue q = new DelayQueue(); |
491 |
< |
final PDelay pdelay = new PDelay(0); |
492 |
< |
Thread t = new Thread(new CheckedRunnable() { |
493 |
< |
public void realRun() throws InterruptedException { |
494 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
495 |
< |
assertSame(pdelay, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
431 |
> |
pleaseInterrupt.countDown(); |
432 |
|
try { |
433 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
434 |
|
shouldThrow(); |
435 |
|
} catch (InterruptedException success) {} |
436 |
+ |
assertFalse(Thread.interrupted()); |
437 |
|
}}); |
438 |
|
|
439 |
< |
t.start(); |
440 |
< |
Thread.sleep(SMALL_DELAY_MS); |
504 |
< |
assertTrue(q.offer(pdelay, SHORT_DELAY_MS, MILLISECONDS)); |
439 |
> |
await(pleaseInterrupt); |
440 |
> |
assertThreadStaysAlive(t); |
441 |
|
t.interrupt(); |
442 |
< |
t.join(); |
442 |
> |
awaitTermination(t); |
443 |
|
} |
444 |
|
|
509 |
– |
|
445 |
|
/** |
446 |
|
* peek returns next element, or null if empty |
447 |
|
*/ |
488 |
|
} |
489 |
|
|
490 |
|
/** |
556 |
– |
* remove(x) removes x and returns true if present |
557 |
– |
*/ |
558 |
– |
public void testRemoveElement() { |
559 |
– |
DelayQueue q = populatedQueue(SIZE); |
560 |
– |
for (int i = 1; i < SIZE; i+=2) { |
561 |
– |
assertTrue(q.remove(new PDelay(i))); |
562 |
– |
} |
563 |
– |
for (int i = 0; i < SIZE; i+=2) { |
564 |
– |
assertTrue(q.remove(new PDelay(i))); |
565 |
– |
assertFalse(q.remove(new PDelay(i+1))); |
566 |
– |
} |
567 |
– |
assertTrue(q.isEmpty()); |
568 |
– |
} |
569 |
– |
|
570 |
– |
/** |
491 |
|
* contains(x) reports true when elements added but not yet removed |
492 |
|
*/ |
493 |
|
public void testContains() { |
573 |
|
Object[] o = q.toArray(); |
574 |
|
Arrays.sort(o); |
575 |
|
for (int i = 0; i < o.length; i++) |
576 |
< |
assertEquals(o[i], q.take()); |
576 |
> |
assertSame(o[i], q.take()); |
577 |
|
} |
578 |
|
|
579 |
|
/** |
580 |
|
* toArray(a) contains all elements |
581 |
|
*/ |
582 |
< |
public void testToArray2() throws InterruptedException { |
583 |
< |
DelayQueue q = populatedQueue(SIZE); |
582 |
> |
public void testToArray2() { |
583 |
> |
DelayQueue<PDelay> q = populatedQueue(SIZE); |
584 |
|
PDelay[] ints = new PDelay[SIZE]; |
585 |
< |
ints = (PDelay[])q.toArray(ints); |
585 |
> |
PDelay[] array = q.toArray(ints); |
586 |
> |
assertSame(ints, array); |
587 |
|
Arrays.sort(ints); |
588 |
|
for (int i = 0; i < ints.length; i++) |
589 |
< |
assertEquals(ints[i], q.take()); |
669 |
< |
} |
670 |
< |
|
671 |
< |
|
672 |
< |
/** |
673 |
< |
* toArray(null) throws NPE |
674 |
< |
*/ |
675 |
< |
public void testToArray_BadArg() { |
676 |
< |
DelayQueue q = populatedQueue(SIZE); |
677 |
< |
try { |
678 |
< |
Object o[] = q.toArray(null); |
679 |
< |
shouldThrow(); |
680 |
< |
} catch (NullPointerException success) {} |
589 |
> |
assertSame(ints[i], q.remove()); |
590 |
|
} |
591 |
|
|
592 |
|
/** |
593 |
< |
* toArray with incompatible array type throws CCE |
593 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
594 |
|
*/ |
595 |
|
public void testToArray1_BadArg() { |
596 |
|
DelayQueue q = populatedQueue(SIZE); |
597 |
|
try { |
598 |
< |
Object o[] = q.toArray(new String[10]); |
598 |
> |
q.toArray(new String[10]); |
599 |
|
shouldThrow(); |
600 |
|
} catch (ArrayStoreException success) {} |
601 |
|
} |
617 |
|
/** |
618 |
|
* iterator.remove removes current element |
619 |
|
*/ |
620 |
< |
public void testIteratorRemove () { |
620 |
> |
public void testIteratorRemove() { |
621 |
|
final DelayQueue q = new DelayQueue(); |
622 |
|
q.add(new PDelay(2)); |
623 |
|
q.add(new PDelay(1)); |
626 |
|
it.next(); |
627 |
|
it.remove(); |
628 |
|
it = q.iterator(); |
629 |
< |
assertEquals(it.next(), new PDelay(2)); |
630 |
< |
assertEquals(it.next(), new PDelay(3)); |
629 |
> |
assertEquals(new PDelay(2), it.next()); |
630 |
> |
assertEquals(new PDelay(3), it.next()); |
631 |
|
assertFalse(it.hasNext()); |
632 |
|
} |
633 |
|
|
725 |
– |
|
634 |
|
/** |
635 |
|
* toString contains toStrings of elements |
636 |
|
*/ |
637 |
|
public void testToString() { |
638 |
|
DelayQueue q = populatedQueue(SIZE); |
639 |
|
String s = q.toString(); |
640 |
< |
for (int i = 0; i < SIZE; ++i) { |
641 |
< |
assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0); |
734 |
< |
} |
640 |
> |
for (Object e : q) |
641 |
> |
assertTrue(s.contains(e.toString())); |
642 |
|
} |
643 |
|
|
644 |
|
/** |
645 |
< |
* offer transfers elements across Executor tasks |
645 |
> |
* timed poll transfers elements across Executor tasks |
646 |
|
*/ |
647 |
|
public void testPollInExecutor() { |
648 |
|
final DelayQueue q = new DelayQueue(); |
649 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
650 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
651 |
|
executor.execute(new CheckedRunnable() { |
652 |
|
public void realRun() throws InterruptedException { |
653 |
|
assertNull(q.poll()); |
654 |
< |
assertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
655 |
< |
assertTrue(q.isEmpty()); |
654 |
> |
threadsStarted.await(); |
655 |
> |
assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); |
656 |
> |
checkEmpty(q); |
657 |
|
}}); |
658 |
|
|
659 |
|
executor.execute(new CheckedRunnable() { |
660 |
|
public void realRun() throws InterruptedException { |
661 |
< |
Thread.sleep(SHORT_DELAY_MS); |
661 |
> |
threadsStarted.await(); |
662 |
|
q.put(new PDelay(1)); |
663 |
|
}}); |
664 |
|
|
665 |
|
joinPool(executor); |
757 |
– |
|
666 |
|
} |
667 |
|
|
760 |
– |
|
668 |
|
/** |
669 |
|
* Delayed actions do not occur until their delay elapses |
670 |
|
*/ |
671 |
|
public void testDelay() throws InterruptedException { |
672 |
< |
DelayQueue q = new DelayQueue(); |
673 |
< |
NanoDelay[] elements = new NanoDelay[SIZE]; |
674 |
< |
for (int i = 0; i < SIZE; ++i) { |
768 |
< |
elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i)); |
769 |
< |
} |
770 |
< |
for (int i = 0; i < SIZE; ++i) { |
771 |
< |
q.add(elements[i]); |
772 |
< |
} |
672 |
> |
DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>(); |
673 |
> |
for (int i = 0; i < SIZE; ++i) |
674 |
> |
q.add(new NanoDelay(1000000L * (SIZE - i))); |
675 |
|
|
676 |
|
long last = 0; |
677 |
|
for (int i = 0; i < SIZE; ++i) { |
678 |
< |
NanoDelay e = (NanoDelay)(q.take()); |
678 |
> |
NanoDelay e = q.take(); |
679 |
|
long tt = e.getTriggerTime(); |
680 |
< |
assertTrue(tt <= System.nanoTime()); |
680 |
> |
assertTrue(System.nanoTime() - tt >= 0); |
681 |
|
if (i != 0) |
682 |
|
assertTrue(tt >= last); |
683 |
|
last = tt; |
684 |
|
} |
685 |
+ |
assertTrue(q.isEmpty()); |
686 |
|
} |
687 |
|
|
688 |
|
/** |
691 |
|
public void testPeekDelayed() { |
692 |
|
DelayQueue q = new DelayQueue(); |
693 |
|
q.add(new NanoDelay(Long.MAX_VALUE)); |
694 |
< |
assert(q.peek() != null); |
694 |
> |
assertNotNull(q.peek()); |
695 |
|
} |
696 |
|
|
794 |
– |
|
697 |
|
/** |
698 |
|
* poll of a non-empty queue returns null if no expired elements. |
699 |
|
*/ |
709 |
|
public void testTimedPollDelayed() throws InterruptedException { |
710 |
|
DelayQueue q = new DelayQueue(); |
711 |
|
q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); |
712 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
811 |
< |
} |
812 |
< |
|
813 |
< |
/** |
814 |
< |
* drainTo(null) throws NPE |
815 |
< |
*/ |
816 |
< |
public void testDrainToNull() { |
817 |
< |
DelayQueue q = populatedQueue(SIZE); |
818 |
< |
try { |
819 |
< |
q.drainTo(null); |
820 |
< |
shouldThrow(); |
821 |
< |
} catch (NullPointerException success) {} |
822 |
< |
} |
823 |
< |
|
824 |
< |
/** |
825 |
< |
* drainTo(this) throws IAE |
826 |
< |
*/ |
827 |
< |
public void testDrainToSelf() { |
828 |
< |
DelayQueue q = populatedQueue(SIZE); |
829 |
< |
try { |
830 |
< |
q.drainTo(q); |
831 |
< |
shouldThrow(); |
832 |
< |
} catch (IllegalArgumentException success) {} |
712 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
713 |
|
} |
714 |
|
|
715 |
|
/** |
724 |
|
} |
725 |
|
ArrayList l = new ArrayList(); |
726 |
|
q.drainTo(l); |
727 |
< |
assertEquals(q.size(), 0); |
727 |
> |
assertEquals(0, q.size()); |
728 |
|
for (int i = 0; i < SIZE; ++i) |
729 |
< |
assertEquals(l.get(i), elems[i]); |
729 |
> |
assertEquals(elems[i], l.get(i)); |
730 |
|
q.add(elems[0]); |
731 |
|
q.add(elems[1]); |
732 |
|
assertFalse(q.isEmpty()); |
734 |
|
assertTrue(q.contains(elems[1])); |
735 |
|
l.clear(); |
736 |
|
q.drainTo(l); |
737 |
< |
assertEquals(q.size(), 0); |
738 |
< |
assertEquals(l.size(), 2); |
737 |
> |
assertEquals(0, q.size()); |
738 |
> |
assertEquals(2, l.size()); |
739 |
|
for (int i = 0; i < 2; ++i) |
740 |
< |
assertEquals(l.get(i), elems[i]); |
740 |
> |
assertEquals(elems[i], l.get(i)); |
741 |
|
} |
742 |
|
|
743 |
|
/** |
759 |
|
} |
760 |
|
|
761 |
|
/** |
762 |
< |
* drainTo(null, n) throws NPE |
883 |
< |
*/ |
884 |
< |
public void testDrainToNullN() { |
885 |
< |
DelayQueue q = populatedQueue(SIZE); |
886 |
< |
try { |
887 |
< |
q.drainTo(null, 0); |
888 |
< |
shouldThrow(); |
889 |
< |
} catch (NullPointerException success) {} |
890 |
< |
} |
891 |
< |
|
892 |
< |
/** |
893 |
< |
* drainTo(this, n) throws IAE |
894 |
< |
*/ |
895 |
< |
public void testDrainToSelfN() { |
896 |
< |
DelayQueue q = populatedQueue(SIZE); |
897 |
< |
try { |
898 |
< |
q.drainTo(q, 0); |
899 |
< |
shouldThrow(); |
900 |
< |
} catch (IllegalArgumentException success) {} |
901 |
< |
} |
902 |
< |
|
903 |
< |
/** |
904 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
762 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
763 |
|
*/ |
764 |
|
public void testDrainToN() { |
765 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
766 |
|
DelayQueue q = populatedQueue(SIZE); |
767 |
|
ArrayList l = new ArrayList(); |
768 |
|
q.drainTo(l, i); |
769 |
< |
int k = (i < SIZE)? i : SIZE; |
770 |
< |
assertEquals(q.size(), SIZE-k); |
771 |
< |
assertEquals(l.size(), k); |
769 |
> |
int k = (i < SIZE) ? i : SIZE; |
770 |
> |
assertEquals(SIZE-k, q.size()); |
771 |
> |
assertEquals(k, l.size()); |
772 |
|
} |
773 |
|
} |
774 |
|
|
775 |
< |
|
775 |
> |
/** |
776 |
> |
* remove(null), contains(null) always return false |
777 |
> |
*/ |
778 |
> |
public void testNeverContainsNull() { |
779 |
> |
Collection<?> q = populatedQueue(SIZE); |
780 |
> |
assertFalse(q.contains(null)); |
781 |
> |
assertFalse(q.remove(null)); |
782 |
> |
} |
783 |
|
} |