1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
21 |
|
|
22 |
|
private static final int NOCAP = Integer.MAX_VALUE; |
23 |
|
|
24 |
< |
// Most Q/BQ tests use Pseudodelays, where delays are all elapsed |
25 |
< |
// (so, no blocking solely for delays) but are still ordered |
26 |
< |
|
24 |
> |
/** |
25 |
> |
* A delayed implementation for testing. |
26 |
> |
* Most tests use Pseudodelays, where delays are all elapsed |
27 |
> |
* (so, no blocking solely for delays) but are still ordered |
28 |
> |
*/ |
29 |
|
static class PDelay implements Delayed { |
30 |
|
int pseudodelay; |
31 |
|
PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } |
32 |
< |
public int compareTo(Object y) { |
32 |
> |
public int compareTo(PDelay y) { |
33 |
|
int i = pseudodelay; |
34 |
|
int j = ((PDelay)y).pseudodelay; |
35 |
|
if (i < j) return -1; |
37 |
|
return 0; |
38 |
|
} |
39 |
|
|
40 |
< |
public int compareTo(PDelay y) { |
40 |
> |
public int compareTo(Delayed y) { |
41 |
|
int i = pseudodelay; |
42 |
|
int j = ((PDelay)y).pseudodelay; |
43 |
|
if (i < j) return -1; |
66 |
|
} |
67 |
|
|
68 |
|
|
69 |
+ |
/** |
70 |
+ |
* Delayed implementation that actually delays |
71 |
+ |
*/ |
72 |
+ |
static class NanoDelay implements Delayed { |
73 |
+ |
long trigger; |
74 |
+ |
NanoDelay(long i) { |
75 |
+ |
trigger = System.nanoTime() + i; |
76 |
+ |
} |
77 |
+ |
public int compareTo(NanoDelay y) { |
78 |
+ |
long i = trigger; |
79 |
+ |
long j = ((NanoDelay)y).trigger; |
80 |
+ |
if (i < j) return -1; |
81 |
+ |
if (i > j) return 1; |
82 |
+ |
return 0; |
83 |
+ |
} |
84 |
+ |
|
85 |
+ |
public int compareTo(Delayed y) { |
86 |
+ |
long i = trigger; |
87 |
+ |
long j = ((NanoDelay)y).trigger; |
88 |
+ |
if (i < j) return -1; |
89 |
+ |
if (i > j) return 1; |
90 |
+ |
return 0; |
91 |
+ |
} |
92 |
+ |
|
93 |
+ |
public boolean equals(Object other) { |
94 |
+ |
return ((NanoDelay)other).trigger == trigger; |
95 |
+ |
} |
96 |
+ |
public boolean equals(NanoDelay other) { |
97 |
+ |
return ((NanoDelay)other).trigger == trigger; |
98 |
+ |
} |
99 |
+ |
|
100 |
+ |
public long getDelay(TimeUnit unit) { |
101 |
+ |
long n = trigger - System.nanoTime(); |
102 |
+ |
return unit.convert(n, TimeUnit.NANOSECONDS); |
103 |
+ |
} |
104 |
+ |
|
105 |
+ |
public long getTriggerTime() { |
106 |
+ |
return trigger; |
107 |
+ |
} |
108 |
+ |
|
109 |
+ |
public String toString() { |
110 |
+ |
return String.valueOf(trigger); |
111 |
+ |
} |
112 |
+ |
} |
113 |
|
|
114 |
|
|
115 |
|
/** |
129 |
|
return q; |
130 |
|
} |
131 |
|
|
132 |
< |
public void testConstructor1(){ |
132 |
> |
/** |
133 |
> |
* A new queue has unbounded capacity |
134 |
> |
*/ |
135 |
> |
public void testConstructor1() { |
136 |
|
assertEquals(NOCAP, new DelayQueue().remainingCapacity()); |
137 |
|
} |
138 |
|
|
139 |
< |
public void testConstructor3(){ |
140 |
< |
|
139 |
> |
/** |
140 |
> |
* Initializing from null Collection throws NPE |
141 |
> |
*/ |
142 |
> |
public void testConstructor3() { |
143 |
|
try { |
144 |
|
DelayQueue q = new DelayQueue(null); |
145 |
< |
fail("Cannot make from null collection"); |
145 |
> |
shouldThrow(); |
146 |
|
} |
147 |
|
catch (NullPointerException success) {} |
148 |
|
} |
149 |
|
|
150 |
< |
public void testConstructor4(){ |
150 |
> |
/** |
151 |
> |
* Initializing from Collection of null elements throws NPE |
152 |
> |
*/ |
153 |
> |
public void testConstructor4() { |
154 |
|
try { |
155 |
|
PDelay[] ints = new PDelay[SIZE]; |
156 |
|
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
157 |
< |
fail("Cannot make with null elements"); |
157 |
> |
shouldThrow(); |
158 |
|
} |
159 |
|
catch (NullPointerException success) {} |
160 |
|
} |
161 |
|
|
162 |
< |
public void testConstructor5(){ |
162 |
> |
/** |
163 |
> |
* Initializing from Collection with some null elements throws NPE |
164 |
> |
*/ |
165 |
> |
public void testConstructor5() { |
166 |
|
try { |
167 |
|
PDelay[] ints = new PDelay[SIZE]; |
168 |
|
for (int i = 0; i < SIZE-1; ++i) |
169 |
|
ints[i] = new PDelay(i); |
170 |
|
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
171 |
< |
fail("Cannot make with null elements"); |
171 |
> |
shouldThrow(); |
172 |
|
} |
173 |
|
catch (NullPointerException success) {} |
174 |
|
} |
175 |
|
|
176 |
< |
public void testConstructor6(){ |
176 |
> |
/** |
177 |
> |
* Queue contains all elements of collection used to initialize |
178 |
> |
*/ |
179 |
> |
public void testConstructor6() { |
180 |
|
try { |
181 |
|
PDelay[] ints = new PDelay[SIZE]; |
182 |
|
for (int i = 0; i < SIZE; ++i) |
188 |
|
finally {} |
189 |
|
} |
190 |
|
|
191 |
+ |
/** |
192 |
+ |
* isEmpty is true before add, false after |
193 |
+ |
*/ |
194 |
|
public void testEmpty() { |
195 |
|
DelayQueue q = new DelayQueue(); |
196 |
|
assertTrue(q.isEmpty()); |
203 |
|
assertTrue(q.isEmpty()); |
204 |
|
} |
205 |
|
|
206 |
< |
public void testRemainingCapacity(){ |
206 |
> |
/** |
207 |
> |
* remainingCapacity does not change when elementa added or removed, |
208 |
> |
* but size does |
209 |
> |
*/ |
210 |
> |
public void testRemainingCapacity() { |
211 |
|
DelayQueue q = populatedQueue(SIZE); |
212 |
|
for (int i = 0; i < SIZE; ++i) { |
213 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
221 |
|
} |
222 |
|
} |
223 |
|
|
224 |
< |
public void testOfferNull(){ |
224 |
> |
/** |
225 |
> |
* offer(null) throws NPE |
226 |
> |
*/ |
227 |
> |
public void testOfferNull() { |
228 |
|
try { |
229 |
|
DelayQueue q = new DelayQueue(); |
230 |
|
q.offer(null); |
231 |
< |
fail("should throw NPE"); |
231 |
> |
shouldThrow(); |
232 |
|
} catch (NullPointerException success) { } |
233 |
|
} |
234 |
|
|
235 |
+ |
/** |
236 |
+ |
* add(null) throws NPE |
237 |
+ |
*/ |
238 |
+ |
public void testAddNull() { |
239 |
+ |
try { |
240 |
+ |
DelayQueue q = new DelayQueue(); |
241 |
+ |
q.add(null); |
242 |
+ |
shouldThrow(); |
243 |
+ |
} catch (NullPointerException success) { } |
244 |
+ |
} |
245 |
+ |
|
246 |
+ |
/** |
247 |
+ |
* offer non-null succeeds |
248 |
+ |
*/ |
249 |
|
public void testOffer() { |
250 |
|
DelayQueue q = new DelayQueue(); |
251 |
|
assertTrue(q.offer(new PDelay(0))); |
252 |
|
assertTrue(q.offer(new PDelay(1))); |
253 |
|
} |
254 |
|
|
255 |
< |
public void testAdd(){ |
255 |
> |
/** |
256 |
> |
* add succeeds |
257 |
> |
*/ |
258 |
> |
public void testAdd() { |
259 |
|
DelayQueue q = new DelayQueue(); |
260 |
|
for (int i = 0; i < SIZE; ++i) { |
261 |
|
assertEquals(i, q.size()); |
263 |
|
} |
264 |
|
} |
265 |
|
|
266 |
< |
public void testAddAll1(){ |
266 |
> |
/** |
267 |
> |
* addAll(null) throws NPE |
268 |
> |
*/ |
269 |
> |
public void testAddAll1() { |
270 |
|
try { |
271 |
|
DelayQueue q = new DelayQueue(); |
272 |
|
q.addAll(null); |
273 |
< |
fail("Cannot add null collection"); |
273 |
> |
shouldThrow(); |
274 |
|
} |
275 |
|
catch (NullPointerException success) {} |
276 |
|
} |
277 |
< |
public void testAddAll2(){ |
277 |
> |
|
278 |
> |
|
279 |
> |
/** |
280 |
> |
* addAll(this) throws IAE |
281 |
> |
*/ |
282 |
> |
public void testAddAllSelf() { |
283 |
> |
try { |
284 |
> |
DelayQueue q = populatedQueue(SIZE); |
285 |
> |
q.addAll(q); |
286 |
> |
shouldThrow(); |
287 |
> |
} |
288 |
> |
catch (IllegalArgumentException success) {} |
289 |
> |
} |
290 |
> |
|
291 |
> |
/** |
292 |
> |
* addAll of a collection with null elements throws NPE |
293 |
> |
*/ |
294 |
> |
public void testAddAll2() { |
295 |
|
try { |
296 |
|
DelayQueue q = new DelayQueue(); |
297 |
|
PDelay[] ints = new PDelay[SIZE]; |
298 |
|
q.addAll(Arrays.asList(ints)); |
299 |
< |
fail("Cannot add null elements"); |
299 |
> |
shouldThrow(); |
300 |
|
} |
301 |
|
catch (NullPointerException success) {} |
302 |
|
} |
303 |
< |
public void testAddAll3(){ |
303 |
> |
/** |
304 |
> |
* addAll of a collection with any null elements throws NPE after |
305 |
> |
* possibly adding some elements |
306 |
> |
*/ |
307 |
> |
public void testAddAll3() { |
308 |
|
try { |
309 |
|
DelayQueue q = new DelayQueue(); |
310 |
|
PDelay[] ints = new PDelay[SIZE]; |
311 |
|
for (int i = 0; i < SIZE-1; ++i) |
312 |
|
ints[i] = new PDelay(i); |
313 |
|
q.addAll(Arrays.asList(ints)); |
314 |
< |
fail("Cannot add null elements"); |
314 |
> |
shouldThrow(); |
315 |
|
} |
316 |
|
catch (NullPointerException success) {} |
317 |
|
} |
318 |
|
|
319 |
< |
public void testAddAll5(){ |
319 |
> |
/** |
320 |
> |
* Queue contains all elements of successful addAll |
321 |
> |
*/ |
322 |
> |
public void testAddAll5() { |
323 |
|
try { |
324 |
|
PDelay[] empty = new PDelay[0]; |
325 |
|
PDelay[] ints = new PDelay[SIZE]; |
334 |
|
finally {} |
335 |
|
} |
336 |
|
|
337 |
+ |
/** |
338 |
+ |
* put(null) throws NPE |
339 |
+ |
*/ |
340 |
|
public void testPutNull() { |
341 |
|
try { |
342 |
|
DelayQueue q = new DelayQueue(); |
343 |
|
q.put(null); |
344 |
< |
fail("put should throw NPE"); |
344 |
> |
shouldThrow(); |
345 |
|
} |
346 |
|
catch (NullPointerException success){ |
347 |
|
} |
348 |
|
} |
349 |
|
|
350 |
+ |
/** |
351 |
+ |
* all elements successfully put are contained |
352 |
+ |
*/ |
353 |
|
public void testPut() { |
354 |
|
try { |
355 |
|
DelayQueue q = new DelayQueue(); |
364 |
|
} |
365 |
|
} |
366 |
|
|
367 |
+ |
/** |
368 |
+ |
* put doesn't block waiting for take |
369 |
+ |
*/ |
370 |
|
public void testPutWithTake() { |
371 |
|
final DelayQueue q = new DelayQueue(); |
372 |
|
Thread t = new Thread(new Runnable() { |
373 |
< |
public void run(){ |
373 |
> |
public void run() { |
374 |
|
int added = 0; |
375 |
|
try { |
376 |
|
q.put(new PDelay(0)); |
393 |
|
t.interrupt(); |
394 |
|
t.join(); |
395 |
|
} catch (Exception e){ |
396 |
< |
fail("Unexpected exception"); |
396 |
> |
unexpectedException(); |
397 |
|
} |
398 |
|
} |
399 |
|
|
400 |
+ |
/** |
401 |
+ |
* timed offer does not time out |
402 |
+ |
*/ |
403 |
|
public void testTimedOffer() { |
404 |
|
final DelayQueue q = new DelayQueue(); |
405 |
|
Thread t = new Thread(new Runnable() { |
406 |
< |
public void run(){ |
406 |
> |
public void run() { |
407 |
|
try { |
408 |
|
q.put(new PDelay(0)); |
409 |
|
q.put(new PDelay(0)); |
419 |
|
t.interrupt(); |
420 |
|
t.join(); |
421 |
|
} catch (Exception e){ |
422 |
< |
fail("Unexpected exception"); |
422 |
> |
unexpectedException(); |
423 |
|
} |
424 |
|
} |
425 |
|
|
426 |
< |
public void testTake(){ |
426 |
> |
/** |
427 |
> |
* take retrieves elements in priority order |
428 |
> |
*/ |
429 |
> |
public void testTake() { |
430 |
|
try { |
431 |
|
DelayQueue q = populatedQueue(SIZE); |
432 |
|
for (int i = 0; i < SIZE; ++i) { |
433 |
|
assertEquals(new PDelay(i), ((PDelay)q.take())); |
434 |
|
} |
435 |
|
} catch (InterruptedException e){ |
436 |
< |
fail("Unexpected exception"); |
436 |
> |
unexpectedException(); |
437 |
|
} |
438 |
|
} |
439 |
|
|
440 |
+ |
/** |
441 |
+ |
* take blocks interruptibly when empty |
442 |
+ |
*/ |
443 |
|
public void testTakeFromEmpty() { |
444 |
|
final DelayQueue q = new DelayQueue(); |
445 |
|
Thread t = new Thread(new Runnable() { |
446 |
< |
public void run(){ |
446 |
> |
public void run() { |
447 |
|
try { |
448 |
|
q.take(); |
449 |
< |
threadFail("Should block"); |
449 |
> |
threadShouldThrow(); |
450 |
|
} catch (InterruptedException success){ } |
451 |
|
} |
452 |
|
}); |
456 |
|
t.interrupt(); |
457 |
|
t.join(); |
458 |
|
} catch (Exception e){ |
459 |
< |
fail("Unexpected exception"); |
459 |
> |
unexpectedException(); |
460 |
|
} |
461 |
|
} |
462 |
|
|
463 |
< |
public void testBlockingTake(){ |
463 |
> |
/** |
464 |
> |
* Take removes existing elements until empty, then blocks interruptibly |
465 |
> |
*/ |
466 |
> |
public void testBlockingTake() { |
467 |
|
Thread t = new Thread(new Runnable() { |
468 |
|
public void run() { |
469 |
|
try { |
472 |
|
threadAssertEquals(new PDelay(i), ((PDelay)q.take())); |
473 |
|
} |
474 |
|
q.take(); |
475 |
< |
threadFail("take should block"); |
475 |
> |
threadShouldThrow(); |
476 |
|
} catch (InterruptedException success){ |
477 |
|
} |
478 |
|
}}); |
483 |
|
t.join(); |
484 |
|
} |
485 |
|
catch (InterruptedException ie) { |
486 |
< |
fail("Unexpected exception"); |
486 |
> |
unexpectedException(); |
487 |
|
} |
488 |
|
} |
489 |
|
|
490 |
|
|
491 |
< |
public void testPoll(){ |
491 |
> |
/** |
492 |
> |
* poll succeeds unless empty |
493 |
> |
*/ |
494 |
> |
public void testPoll() { |
495 |
|
DelayQueue q = populatedQueue(SIZE); |
496 |
|
for (int i = 0; i < SIZE; ++i) { |
497 |
|
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
499 |
|
assertNull(q.poll()); |
500 |
|
} |
501 |
|
|
502 |
+ |
/** |
503 |
+ |
* timed pool with zero timeout succeeds when non-empty, else times out |
504 |
+ |
*/ |
505 |
|
public void testTimedPoll0() { |
506 |
|
try { |
507 |
|
DelayQueue q = populatedQueue(SIZE); |
510 |
|
} |
511 |
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
512 |
|
} catch (InterruptedException e){ |
513 |
< |
fail("Unexpected exception"); |
513 |
> |
unexpectedException(); |
514 |
|
} |
515 |
|
} |
516 |
|
|
517 |
+ |
/** |
518 |
+ |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
519 |
+ |
*/ |
520 |
|
public void testTimedPoll() { |
521 |
|
try { |
522 |
|
DelayQueue q = populatedQueue(SIZE); |
525 |
|
} |
526 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
527 |
|
} catch (InterruptedException e){ |
528 |
< |
fail("Unexpected exception"); |
528 |
> |
unexpectedException(); |
529 |
|
} |
530 |
|
} |
531 |
|
|
532 |
< |
public void testInterruptedTimedPoll(){ |
532 |
> |
/** |
533 |
> |
* Interrupted timed poll throws InterruptedException instead of |
534 |
> |
* returning timeout status |
535 |
> |
*/ |
536 |
> |
public void testInterruptedTimedPoll() { |
537 |
|
Thread t = new Thread(new Runnable() { |
538 |
|
public void run() { |
539 |
|
try { |
552 |
|
t.join(); |
553 |
|
} |
554 |
|
catch (InterruptedException ie) { |
555 |
< |
fail("Unexpected exception"); |
555 |
> |
unexpectedException(); |
556 |
|
} |
557 |
|
} |
558 |
|
|
559 |
< |
public void testTimedPollWithOffer(){ |
559 |
> |
/** |
560 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
561 |
> |
* on interruption throws |
562 |
> |
*/ |
563 |
> |
public void testTimedPollWithOffer() { |
564 |
|
final DelayQueue q = new DelayQueue(); |
565 |
|
Thread t = new Thread(new Runnable() { |
566 |
< |
public void run(){ |
566 |
> |
public void run() { |
567 |
|
try { |
568 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
569 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
579 |
|
t.interrupt(); |
580 |
|
t.join(); |
581 |
|
} catch (Exception e){ |
582 |
< |
fail("Unexpected exception"); |
582 |
> |
unexpectedException(); |
583 |
|
} |
584 |
|
} |
585 |
|
|
586 |
|
|
587 |
< |
public void testPeek(){ |
587 |
> |
/** |
588 |
> |
* peek returns next element, or null if empty |
589 |
> |
*/ |
590 |
> |
public void testPeek() { |
591 |
|
DelayQueue q = populatedQueue(SIZE); |
592 |
|
for (int i = 0; i < SIZE; ++i) { |
593 |
|
assertEquals(new PDelay(i), ((PDelay)q.peek())); |
594 |
|
q.poll(); |
595 |
< |
assertTrue(q.peek() == null || |
596 |
< |
i != ((PDelay)q.peek()).intValue()); |
595 |
> |
if (q.isEmpty()) |
596 |
> |
assertNull(q.peek()); |
597 |
> |
else |
598 |
> |
assertTrue(i != ((PDelay)q.peek()).intValue()); |
599 |
|
} |
600 |
|
assertNull(q.peek()); |
601 |
|
} |
602 |
|
|
603 |
< |
public void testElement(){ |
603 |
> |
/** |
604 |
> |
* element returns next element, or throws NSEE if empty |
605 |
> |
*/ |
606 |
> |
public void testElement() { |
607 |
|
DelayQueue q = populatedQueue(SIZE); |
608 |
|
for (int i = 0; i < SIZE; ++i) { |
609 |
|
assertEquals(new PDelay(i), ((PDelay)q.element())); |
611 |
|
} |
612 |
|
try { |
613 |
|
q.element(); |
614 |
< |
fail("no such element"); |
614 |
> |
shouldThrow(); |
615 |
|
} |
616 |
|
catch (NoSuchElementException success) {} |
617 |
|
} |
618 |
|
|
619 |
< |
public void testRemove(){ |
619 |
> |
/** |
620 |
> |
* remove removes next element, or throws NSEE if empty |
621 |
> |
*/ |
622 |
> |
public void testRemove() { |
623 |
|
DelayQueue q = populatedQueue(SIZE); |
624 |
|
for (int i = 0; i < SIZE; ++i) { |
625 |
|
assertEquals(new PDelay(i), ((PDelay)q.remove())); |
626 |
|
} |
627 |
|
try { |
628 |
|
q.remove(); |
629 |
< |
fail("remove should throw"); |
629 |
> |
shouldThrow(); |
630 |
|
} catch (NoSuchElementException success){ |
631 |
|
} |
632 |
|
} |
633 |
|
|
634 |
< |
public void testRemoveElement(){ |
634 |
> |
/** |
635 |
> |
* remove(x) removes x and returns true if present |
636 |
> |
*/ |
637 |
> |
public void testRemoveElement() { |
638 |
|
DelayQueue q = populatedQueue(SIZE); |
639 |
|
for (int i = 1; i < SIZE; i+=2) { |
640 |
|
assertTrue(q.remove(new PDelay(i))); |
646 |
|
assertTrue(q.isEmpty()); |
647 |
|
} |
648 |
|
|
649 |
< |
public void testContains(){ |
649 |
> |
/** |
650 |
> |
* contains(x) reports true when elements added but not yet removed |
651 |
> |
*/ |
652 |
> |
public void testContains() { |
653 |
|
DelayQueue q = populatedQueue(SIZE); |
654 |
|
for (int i = 0; i < SIZE; ++i) { |
655 |
|
assertTrue(q.contains(new PDelay(i))); |
658 |
|
} |
659 |
|
} |
660 |
|
|
661 |
< |
public void testClear(){ |
661 |
> |
/** |
662 |
> |
* clear removes all elements |
663 |
> |
*/ |
664 |
> |
public void testClear() { |
665 |
|
DelayQueue q = populatedQueue(SIZE); |
666 |
|
q.clear(); |
667 |
|
assertTrue(q.isEmpty()); |
668 |
|
assertEquals(0, q.size()); |
669 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
670 |
< |
q.add(new PDelay(1)); |
670 |
> |
PDelay x = new PDelay(1); |
671 |
> |
q.add(x); |
672 |
|
assertFalse(q.isEmpty()); |
673 |
+ |
assertTrue(q.contains(x)); |
674 |
|
q.clear(); |
675 |
|
assertTrue(q.isEmpty()); |
676 |
|
} |
677 |
|
|
678 |
< |
public void testContainsAll(){ |
678 |
> |
/** |
679 |
> |
* containsAll(c) is true when c contains a subset of elements |
680 |
> |
*/ |
681 |
> |
public void testContainsAll() { |
682 |
|
DelayQueue q = populatedQueue(SIZE); |
683 |
|
DelayQueue p = new DelayQueue(); |
684 |
|
for (int i = 0; i < SIZE; ++i) { |
689 |
|
assertTrue(p.containsAll(q)); |
690 |
|
} |
691 |
|
|
692 |
< |
public void testRetainAll(){ |
692 |
> |
/** |
693 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
694 |
> |
*/ |
695 |
> |
public void testRetainAll() { |
696 |
|
DelayQueue q = populatedQueue(SIZE); |
697 |
|
DelayQueue p = populatedQueue(SIZE); |
698 |
|
for (int i = 0; i < SIZE; ++i) { |
708 |
|
} |
709 |
|
} |
710 |
|
|
711 |
< |
public void testRemoveAll(){ |
711 |
> |
/** |
712 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
713 |
> |
*/ |
714 |
> |
public void testRemoveAll() { |
715 |
|
for (int i = 1; i < SIZE; ++i) { |
716 |
|
DelayQueue q = populatedQueue(SIZE); |
717 |
|
DelayQueue p = populatedQueue(i); |
724 |
|
} |
725 |
|
} |
726 |
|
|
727 |
< |
public void testToArray(){ |
727 |
> |
/** |
728 |
> |
* toArray contains all elements |
729 |
> |
*/ |
730 |
> |
public void testToArray() { |
731 |
|
DelayQueue q = populatedQueue(SIZE); |
732 |
|
Object[] o = q.toArray(); |
733 |
|
Arrays.sort(o); |
735 |
|
for(int i = 0; i < o.length; i++) |
736 |
|
assertEquals(o[i], q.take()); |
737 |
|
} catch (InterruptedException e){ |
738 |
< |
fail("Unexpected exception"); |
738 |
> |
unexpectedException(); |
739 |
|
} |
740 |
|
} |
741 |
|
|
742 |
< |
public void testToArray2(){ |
742 |
> |
/** |
743 |
> |
* toArray(a) contains all elements |
744 |
> |
*/ |
745 |
> |
public void testToArray2() { |
746 |
|
DelayQueue q = populatedQueue(SIZE); |
747 |
|
PDelay[] ints = new PDelay[SIZE]; |
748 |
|
ints = (PDelay[])q.toArray(ints); |
751 |
|
for(int i = 0; i < ints.length; i++) |
752 |
|
assertEquals(ints[i], q.take()); |
753 |
|
} catch (InterruptedException e){ |
754 |
< |
fail("Unexpected exception"); |
754 |
> |
unexpectedException(); |
755 |
|
} |
756 |
|
} |
757 |
+ |
|
758 |
+ |
|
759 |
+ |
/** |
760 |
+ |
* toArray(null) throws NPE |
761 |
+ |
*/ |
762 |
+ |
public void testToArray_BadArg() { |
763 |
+ |
try { |
764 |
+ |
DelayQueue q = populatedQueue(SIZE); |
765 |
+ |
Object o[] = q.toArray(null); |
766 |
+ |
shouldThrow(); |
767 |
+ |
} catch(NullPointerException success){} |
768 |
+ |
} |
769 |
+ |
|
770 |
+ |
/** |
771 |
+ |
* toArray with incompatible array type throws CCE |
772 |
+ |
*/ |
773 |
+ |
public void testToArray1_BadArg() { |
774 |
+ |
try { |
775 |
+ |
DelayQueue q = populatedQueue(SIZE); |
776 |
+ |
Object o[] = q.toArray(new String[10] ); |
777 |
+ |
shouldThrow(); |
778 |
+ |
} catch(ArrayStoreException success){} |
779 |
+ |
} |
780 |
|
|
781 |
< |
public void testIterator(){ |
781 |
> |
/** |
782 |
> |
* iterator iterates through all elements |
783 |
> |
*/ |
784 |
> |
public void testIterator() { |
785 |
|
DelayQueue q = populatedQueue(SIZE); |
786 |
|
int i = 0; |
787 |
|
Iterator it = q.iterator(); |
792 |
|
assertEquals(i, SIZE); |
793 |
|
} |
794 |
|
|
795 |
+ |
/** |
796 |
+ |
* iterator.remove removes current element |
797 |
+ |
*/ |
798 |
|
public void testIteratorRemove () { |
580 |
– |
|
799 |
|
final DelayQueue q = new DelayQueue(); |
582 |
– |
|
800 |
|
q.add(new PDelay(2)); |
801 |
|
q.add(new PDelay(1)); |
802 |
|
q.add(new PDelay(3)); |
586 |
– |
|
803 |
|
Iterator it = q.iterator(); |
804 |
|
it.next(); |
805 |
|
it.remove(); |
590 |
– |
|
806 |
|
it = q.iterator(); |
807 |
|
assertEquals(it.next(), new PDelay(2)); |
808 |
|
assertEquals(it.next(), new PDelay(3)); |
810 |
|
} |
811 |
|
|
812 |
|
|
813 |
< |
public void testToString(){ |
813 |
> |
/** |
814 |
> |
* toString contains toStrings of elements |
815 |
> |
*/ |
816 |
> |
public void testToString() { |
817 |
|
DelayQueue q = populatedQueue(SIZE); |
818 |
|
String s = q.toString(); |
819 |
|
for (int i = 0; i < SIZE; ++i) { |
821 |
|
} |
822 |
|
} |
823 |
|
|
824 |
+ |
/** |
825 |
+ |
* offer transfers elements across Executor tasks |
826 |
+ |
*/ |
827 |
|
public void testPollInExecutor() { |
607 |
– |
|
828 |
|
final DelayQueue q = new DelayQueue(); |
609 |
– |
|
829 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
611 |
– |
|
830 |
|
executor.execute(new Runnable() { |
831 |
|
public void run() { |
832 |
|
threadAssertNull(q.poll()); |
835 |
|
threadAssertTrue(q.isEmpty()); |
836 |
|
} |
837 |
|
catch (InterruptedException e) { |
838 |
< |
threadFail("should not be interrupted"); |
838 |
> |
threadUnexpectedException(); |
839 |
|
} |
840 |
|
} |
841 |
|
}); |
847 |
|
q.put(new PDelay(1)); |
848 |
|
} |
849 |
|
catch (InterruptedException e) { |
850 |
< |
threadFail("should not be interrupted"); |
850 |
> |
threadUnexpectedException(); |
851 |
|
} |
852 |
|
} |
853 |
|
}); |
636 |
– |
|
854 |
|
joinPool(executor); |
855 |
|
|
856 |
|
} |
857 |
|
|
641 |
– |
static class NanoDelay implements Delayed { |
642 |
– |
long trigger; |
643 |
– |
NanoDelay(long i) { |
644 |
– |
trigger = System.nanoTime() + i; |
645 |
– |
} |
646 |
– |
public int compareTo(Object y) { |
647 |
– |
long i = trigger; |
648 |
– |
long j = ((NanoDelay)y).trigger; |
649 |
– |
if (i < j) return -1; |
650 |
– |
if (i > j) return 1; |
651 |
– |
return 0; |
652 |
– |
} |
653 |
– |
|
654 |
– |
public int compareTo(NanoDelay y) { |
655 |
– |
long i = trigger; |
656 |
– |
long j = ((NanoDelay)y).trigger; |
657 |
– |
if (i < j) return -1; |
658 |
– |
if (i > j) return 1; |
659 |
– |
return 0; |
660 |
– |
} |
661 |
– |
|
662 |
– |
public boolean equals(Object other) { |
663 |
– |
return ((NanoDelay)other).trigger == trigger; |
664 |
– |
} |
665 |
– |
public boolean equals(NanoDelay other) { |
666 |
– |
return ((NanoDelay)other).trigger == trigger; |
667 |
– |
} |
668 |
– |
|
669 |
– |
public long getDelay(TimeUnit unit) { |
670 |
– |
long n = trigger - System.nanoTime(); |
671 |
– |
return unit.convert(n, TimeUnit.NANOSECONDS); |
672 |
– |
} |
673 |
– |
|
674 |
– |
public long getTriggerTime() { |
675 |
– |
return trigger; |
676 |
– |
} |
677 |
– |
|
678 |
– |
public String toString() { |
679 |
– |
return String.valueOf(trigger); |
680 |
– |
} |
681 |
– |
} |
858 |
|
|
859 |
+ |
/** |
860 |
+ |
* Delayed actions do not occur until their delay elapses |
861 |
+ |
*/ |
862 |
|
public void testDelay() { |
863 |
|
DelayQueue q = new DelayQueue(); |
864 |
|
NanoDelay[] elements = new NanoDelay[SIZE]; |
881 |
|
} |
882 |
|
} |
883 |
|
catch(InterruptedException ie) { |
884 |
< |
fail("Unexpected Exception"); |
884 |
> |
unexpectedException(); |
885 |
> |
} |
886 |
> |
} |
887 |
> |
|
888 |
> |
/** |
889 |
> |
* peek of a non-empty queue returns non-null even if not expired |
890 |
> |
*/ |
891 |
> |
public void testPeekDelayed() { |
892 |
> |
DelayQueue q = new DelayQueue(); |
893 |
> |
q.add(new NanoDelay(Long.MAX_VALUE)); |
894 |
> |
assert(q.peek() != null); |
895 |
> |
} |
896 |
> |
|
897 |
> |
/** |
898 |
> |
* poll of a non-empty queue returns null if no expired elements. |
899 |
> |
*/ |
900 |
> |
public void testPollDelayed() { |
901 |
> |
DelayQueue q = new DelayQueue(); |
902 |
> |
q.add(new NanoDelay(Long.MAX_VALUE)); |
903 |
> |
assertNull(q.poll()); |
904 |
> |
} |
905 |
> |
|
906 |
> |
/** |
907 |
> |
* drainTo(null) throws NPE |
908 |
> |
*/ |
909 |
> |
public void testDrainToNull() { |
910 |
> |
DelayQueue q = populatedQueue(SIZE); |
911 |
> |
try { |
912 |
> |
q.drainTo(null); |
913 |
> |
shouldThrow(); |
914 |
> |
} catch(NullPointerException success) { |
915 |
> |
} |
916 |
> |
} |
917 |
> |
|
918 |
> |
/** |
919 |
> |
* drainTo(this) throws IAE |
920 |
> |
*/ |
921 |
> |
public void testDrainToSelf() { |
922 |
> |
DelayQueue q = populatedQueue(SIZE); |
923 |
> |
try { |
924 |
> |
q.drainTo(q); |
925 |
> |
shouldThrow(); |
926 |
> |
} catch(IllegalArgumentException success) { |
927 |
> |
} |
928 |
> |
} |
929 |
> |
|
930 |
> |
/** |
931 |
> |
* drainTo(c) empties queue into another collection c |
932 |
> |
*/ |
933 |
> |
public void testDrainTo() { |
934 |
> |
DelayQueue q = new DelayQueue(); |
935 |
> |
PDelay[] elems = new PDelay[SIZE]; |
936 |
> |
for (int i = 0; i < SIZE; ++i) { |
937 |
> |
elems[i] = new PDelay(i); |
938 |
> |
q.add(elems[i]); |
939 |
> |
} |
940 |
> |
ArrayList l = new ArrayList(); |
941 |
> |
q.drainTo(l); |
942 |
> |
assertEquals(q.size(), 0); |
943 |
> |
for (int i = 0; i < SIZE; ++i) |
944 |
> |
assertEquals(l.get(i), elems[i]); |
945 |
> |
q.add(elems[0]); |
946 |
> |
q.add(elems[1]); |
947 |
> |
assertFalse(q.isEmpty()); |
948 |
> |
assertTrue(q.contains(elems[0])); |
949 |
> |
assertTrue(q.contains(elems[1])); |
950 |
> |
l.clear(); |
951 |
> |
q.drainTo(l); |
952 |
> |
assertEquals(q.size(), 0); |
953 |
> |
assertEquals(l.size(), 2); |
954 |
> |
for (int i = 0; i < 2; ++i) |
955 |
> |
assertEquals(l.get(i), elems[i]); |
956 |
> |
} |
957 |
> |
|
958 |
> |
/** |
959 |
> |
* drainTo empties queue |
960 |
> |
*/ |
961 |
> |
public void testDrainToWithActivePut() { |
962 |
> |
final DelayQueue q = populatedQueue(SIZE); |
963 |
> |
Thread t = new Thread(new Runnable() { |
964 |
> |
public void run() { |
965 |
> |
q.put(new PDelay(SIZE+1)); |
966 |
> |
} |
967 |
> |
}); |
968 |
> |
try { |
969 |
> |
t.start(); |
970 |
> |
ArrayList l = new ArrayList(); |
971 |
> |
q.drainTo(l); |
972 |
> |
assertTrue(l.size() >= SIZE); |
973 |
> |
t.join(); |
974 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
975 |
> |
} catch(Exception e){ |
976 |
> |
unexpectedException(); |
977 |
> |
} |
978 |
> |
} |
979 |
> |
|
980 |
> |
/** |
981 |
> |
* drainTo(null, n) throws NPE |
982 |
> |
*/ |
983 |
> |
public void testDrainToNullN() { |
984 |
> |
DelayQueue q = populatedQueue(SIZE); |
985 |
> |
try { |
986 |
> |
q.drainTo(null, 0); |
987 |
> |
shouldThrow(); |
988 |
> |
} catch(NullPointerException success) { |
989 |
|
} |
990 |
|
} |
991 |
|
|
992 |
+ |
/** |
993 |
+ |
* drainTo(this, n) throws IAE |
994 |
+ |
*/ |
995 |
+ |
public void testDrainToSelfN() { |
996 |
+ |
DelayQueue q = populatedQueue(SIZE); |
997 |
+ |
try { |
998 |
+ |
q.drainTo(q, 0); |
999 |
+ |
shouldThrow(); |
1000 |
+ |
} catch(IllegalArgumentException success) { |
1001 |
+ |
} |
1002 |
+ |
} |
1003 |
+ |
|
1004 |
+ |
/** |
1005 |
+ |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
1006 |
+ |
*/ |
1007 |
+ |
public void testDrainToN() { |
1008 |
+ |
for (int i = 0; i < SIZE + 2; ++i) { |
1009 |
+ |
DelayQueue q = populatedQueue(SIZE); |
1010 |
+ |
ArrayList l = new ArrayList(); |
1011 |
+ |
q.drainTo(l, i); |
1012 |
+ |
int k = (i < SIZE)? i : SIZE; |
1013 |
+ |
assertEquals(q.size(), SIZE-k); |
1014 |
+ |
assertEquals(l.size(), k); |
1015 |
+ |
} |
1016 |
+ |
} |
1017 |
+ |
|
1018 |
+ |
|
1019 |
|
} |