6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.util.concurrent.*; |
10 |
> |
|
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.Arrays; |
13 |
> |
import java.util.Collection; |
14 |
> |
import java.util.Iterator; |
15 |
> |
import java.util.NoSuchElementException; |
16 |
> |
import java.util.concurrent.BlockingQueue; |
17 |
> |
import java.util.concurrent.CountDownLatch; |
18 |
> |
import java.util.concurrent.Delayed; |
19 |
> |
import java.util.concurrent.DelayQueue; |
20 |
> |
import java.util.concurrent.Executors; |
21 |
> |
import java.util.concurrent.ExecutorService; |
22 |
> |
import java.util.concurrent.TimeUnit; |
23 |
> |
|
24 |
> |
import junit.framework.Test; |
25 |
|
|
26 |
|
public class DelayQueueTest extends JSR166TestCase { |
27 |
+ |
|
28 |
+ |
public static class Generic extends BlockingQueueTest { |
29 |
+ |
protected BlockingQueue emptyCollection() { |
30 |
+ |
return new DelayQueue(); |
31 |
+ |
} |
32 |
+ |
protected PDelay makeElement(int i) { |
33 |
+ |
return new PDelay(i); |
34 |
+ |
} |
35 |
+ |
} |
36 |
+ |
|
37 |
|
public static void main(String[] args) { |
38 |
< |
junit.textui.TestRunner.run(suite()); |
38 |
> |
main(suite(), args); |
39 |
|
} |
40 |
|
|
41 |
|
public static Test suite() { |
42 |
< |
return new TestSuite(DelayQueueTest.class); |
42 |
> |
return newTestSuite(DelayQueueTest.class, |
43 |
> |
new Generic().testSuite()); |
44 |
|
} |
45 |
|
|
23 |
– |
private static final int NOCAP = Integer.MAX_VALUE; |
24 |
– |
|
46 |
|
/** |
47 |
|
* A delayed implementation for testing. |
48 |
|
* Most tests use Pseudodelays, where delays are all elapsed |
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 |
< |
|
66 |
> |
// suppress [overrides] javac warning |
67 |
> |
public int hashCode() { return pseudodelay; } |
68 |
|
public long getDelay(TimeUnit ignore) { |
69 |
< |
return pseudodelay; |
54 |
< |
} |
55 |
< |
public int intValue() { |
56 |
< |
return pseudodelay; |
69 |
> |
return Integer.MIN_VALUE + pseudodelay; |
70 |
|
} |
58 |
– |
|
71 |
|
public String toString() { |
72 |
|
return String.valueOf(pseudodelay); |
73 |
|
} |
94 |
|
} |
95 |
|
|
96 |
|
public boolean equals(Object other) { |
97 |
< |
return equals((NanoDelay)other); |
98 |
< |
} |
87 |
< |
public boolean equals(NanoDelay other) { |
88 |
< |
return other.trigger == trigger; |
97 |
> |
return (other instanceof NanoDelay) && |
98 |
> |
this.trigger == ((NanoDelay)other).trigger; |
99 |
|
} |
100 |
|
|
101 |
+ |
// suppress [overrides] javac warning |
102 |
+ |
public int hashCode() { return (int) trigger; } |
103 |
+ |
|
104 |
|
public long getDelay(TimeUnit unit) { |
105 |
|
long n = trigger - System.nanoTime(); |
106 |
|
return unit.convert(n, TimeUnit.NANOSECONDS); |
116 |
|
} |
117 |
|
|
118 |
|
/** |
119 |
< |
* Create a queue of given size containing consecutive |
119 |
> |
* Returns a new queue of given size containing consecutive |
120 |
|
* PDelays 0 ... n. |
121 |
|
*/ |
122 |
|
private DelayQueue<PDelay> populatedQueue(int n) { |
123 |
|
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
124 |
|
assertTrue(q.isEmpty()); |
125 |
< |
for (int i = n-1; i >= 0; i-=2) |
125 |
> |
for (int i = n - 1; i >= 0; i -= 2) |
126 |
|
assertTrue(q.offer(new PDelay(i))); |
127 |
< |
for (int i = (n & 1); i < n; i+=2) |
127 |
> |
for (int i = (n & 1); i < n; i += 2) |
128 |
|
assertTrue(q.offer(new PDelay(i))); |
129 |
|
assertFalse(q.isEmpty()); |
130 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
130 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
131 |
|
assertEquals(n, q.size()); |
132 |
|
return q; |
133 |
|
} |
136 |
|
* A new queue has unbounded capacity |
137 |
|
*/ |
138 |
|
public void testConstructor1() { |
139 |
< |
assertEquals(NOCAP, new DelayQueue().remainingCapacity()); |
139 |
> |
assertEquals(Integer.MAX_VALUE, new DelayQueue().remainingCapacity()); |
140 |
|
} |
141 |
|
|
142 |
|
/** |
144 |
|
*/ |
145 |
|
public void testConstructor3() { |
146 |
|
try { |
147 |
< |
DelayQueue q = new DelayQueue(null); |
147 |
> |
new DelayQueue(null); |
148 |
|
shouldThrow(); |
149 |
|
} catch (NullPointerException success) {} |
150 |
|
} |
154 |
|
*/ |
155 |
|
public void testConstructor4() { |
156 |
|
try { |
157 |
< |
PDelay[] ints = new PDelay[SIZE]; |
145 |
< |
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
157 |
> |
new DelayQueue(Arrays.asList(new PDelay[SIZE])); |
158 |
|
shouldThrow(); |
159 |
|
} catch (NullPointerException success) {} |
160 |
|
} |
163 |
|
* Initializing from Collection with some null elements throws NPE |
164 |
|
*/ |
165 |
|
public void testConstructor5() { |
166 |
+ |
PDelay[] a = new PDelay[SIZE]; |
167 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
168 |
+ |
a[i] = new PDelay(i); |
169 |
|
try { |
170 |
< |
PDelay[] ints = new PDelay[SIZE]; |
156 |
< |
for (int i = 0; i < SIZE-1; ++i) |
157 |
< |
ints[i] = new PDelay(i); |
158 |
< |
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
170 |
> |
new DelayQueue(Arrays.asList(a)); |
171 |
|
shouldThrow(); |
172 |
|
} catch (NullPointerException success) {} |
173 |
|
} |
190 |
|
public void testEmpty() { |
191 |
|
DelayQueue q = new DelayQueue(); |
192 |
|
assertTrue(q.isEmpty()); |
193 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
193 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
194 |
|
q.add(new PDelay(1)); |
195 |
|
assertFalse(q.isEmpty()); |
196 |
|
q.add(new PDelay(2)); |
200 |
|
} |
201 |
|
|
202 |
|
/** |
203 |
< |
* remainingCapacity does not change when elements added or removed, |
192 |
< |
* but size does |
203 |
> |
* remainingCapacity() always returns Integer.MAX_VALUE |
204 |
|
*/ |
205 |
|
public void testRemainingCapacity() { |
206 |
< |
DelayQueue q = populatedQueue(SIZE); |
206 |
> |
BlockingQueue q = populatedQueue(SIZE); |
207 |
|
for (int i = 0; i < SIZE; ++i) { |
208 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
209 |
< |
assertEquals(SIZE-i, q.size()); |
210 |
< |
q.remove(); |
208 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
209 |
> |
assertEquals(SIZE - i, q.size()); |
210 |
> |
assertTrue(q.remove() instanceof PDelay); |
211 |
|
} |
212 |
|
for (int i = 0; i < SIZE; ++i) { |
213 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
213 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
214 |
|
assertEquals(i, q.size()); |
215 |
< |
q.add(new PDelay(i)); |
215 |
> |
assertTrue(q.add(new PDelay(i))); |
216 |
|
} |
217 |
|
} |
218 |
|
|
219 |
|
/** |
209 |
– |
* offer(null) throws NPE |
210 |
– |
*/ |
211 |
– |
public void testOfferNull() { |
212 |
– |
try { |
213 |
– |
DelayQueue q = new DelayQueue(); |
214 |
– |
q.offer(null); |
215 |
– |
shouldThrow(); |
216 |
– |
} catch (NullPointerException success) {} |
217 |
– |
} |
218 |
– |
|
219 |
– |
/** |
220 |
– |
* add(null) throws NPE |
221 |
– |
*/ |
222 |
– |
public void testAddNull() { |
223 |
– |
try { |
224 |
– |
DelayQueue q = new DelayQueue(); |
225 |
– |
q.add(null); |
226 |
– |
shouldThrow(); |
227 |
– |
} catch (NullPointerException success) {} |
228 |
– |
} |
229 |
– |
|
230 |
– |
/** |
220 |
|
* offer non-null succeeds |
221 |
|
*/ |
222 |
|
public void testOffer() { |
237 |
|
} |
238 |
|
|
239 |
|
/** |
251 |
– |
* addAll(null) throws NPE |
252 |
– |
*/ |
253 |
– |
public void testAddAll1() { |
254 |
– |
try { |
255 |
– |
DelayQueue q = new DelayQueue(); |
256 |
– |
q.addAll(null); |
257 |
– |
shouldThrow(); |
258 |
– |
} catch (NullPointerException success) {} |
259 |
– |
} |
260 |
– |
|
261 |
– |
/** |
240 |
|
* addAll(this) throws IAE |
241 |
|
*/ |
242 |
|
public void testAddAllSelf() { |
243 |
+ |
DelayQueue q = populatedQueue(SIZE); |
244 |
|
try { |
266 |
– |
DelayQueue q = populatedQueue(SIZE); |
245 |
|
q.addAll(q); |
246 |
|
shouldThrow(); |
247 |
|
} catch (IllegalArgumentException success) {} |
248 |
|
} |
249 |
|
|
250 |
|
/** |
273 |
– |
* addAll of a collection with null elements throws NPE |
274 |
– |
*/ |
275 |
– |
public void testAddAll2() { |
276 |
– |
try { |
277 |
– |
DelayQueue q = new DelayQueue(); |
278 |
– |
PDelay[] ints = new PDelay[SIZE]; |
279 |
– |
q.addAll(Arrays.asList(ints)); |
280 |
– |
shouldThrow(); |
281 |
– |
} catch (NullPointerException success) {} |
282 |
– |
} |
283 |
– |
|
284 |
– |
/** |
251 |
|
* addAll of a collection with any null elements throws NPE after |
252 |
|
* possibly adding some elements |
253 |
|
*/ |
254 |
|
public void testAddAll3() { |
255 |
+ |
DelayQueue q = new DelayQueue(); |
256 |
+ |
PDelay[] a = new PDelay[SIZE]; |
257 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
258 |
+ |
a[i] = new PDelay(i); |
259 |
|
try { |
260 |
< |
DelayQueue q = new DelayQueue(); |
291 |
< |
PDelay[] ints = new PDelay[SIZE]; |
292 |
< |
for (int i = 0; i < SIZE-1; ++i) |
293 |
< |
ints[i] = new PDelay(i); |
294 |
< |
q.addAll(Arrays.asList(ints)); |
260 |
> |
q.addAll(Arrays.asList(a)); |
261 |
|
shouldThrow(); |
262 |
|
} catch (NullPointerException success) {} |
263 |
|
} |
268 |
|
public void testAddAll5() { |
269 |
|
PDelay[] empty = new PDelay[0]; |
270 |
|
PDelay[] ints = new PDelay[SIZE]; |
271 |
< |
for (int i = SIZE-1; i >= 0; --i) |
271 |
> |
for (int i = SIZE - 1; i >= 0; --i) |
272 |
|
ints[i] = new PDelay(i); |
273 |
|
DelayQueue q = new DelayQueue(); |
274 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
278 |
|
} |
279 |
|
|
280 |
|
/** |
315 |
– |
* put(null) throws NPE |
316 |
– |
*/ |
317 |
– |
public void testPutNull() { |
318 |
– |
try { |
319 |
– |
DelayQueue q = new DelayQueue(); |
320 |
– |
q.put(null); |
321 |
– |
shouldThrow(); |
322 |
– |
} catch (NullPointerException success) {} |
323 |
– |
} |
324 |
– |
|
325 |
– |
/** |
281 |
|
* all elements successfully put are contained |
282 |
|
*/ |
283 |
|
public void testPut() { |
284 |
|
DelayQueue q = new DelayQueue(); |
285 |
|
for (int i = 0; i < SIZE; ++i) { |
286 |
< |
PDelay I = new PDelay(i); |
287 |
< |
q.put(I); |
288 |
< |
assertTrue(q.contains(I)); |
286 |
> |
PDelay x = new PDelay(i); |
287 |
> |
q.put(x); |
288 |
> |
assertTrue(q.contains(x)); |
289 |
|
} |
290 |
|
assertEquals(SIZE, q.size()); |
291 |
|
} |
329 |
|
public void testTake() throws InterruptedException { |
330 |
|
DelayQueue q = populatedQueue(SIZE); |
331 |
|
for (int i = 0; i < SIZE; ++i) { |
332 |
< |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
332 |
> |
assertEquals(new PDelay(i), q.take()); |
333 |
|
} |
334 |
|
} |
335 |
|
|
336 |
|
/** |
382 |
– |
* take blocks interruptibly when empty |
383 |
– |
*/ |
384 |
– |
public void testTakeFromEmptyBlocksInterruptibly() |
385 |
– |
throws InterruptedException { |
386 |
– |
final BlockingQueue q = new DelayQueue(); |
387 |
– |
final CountDownLatch threadStarted = new CountDownLatch(1); |
388 |
– |
Thread t = newStartedThread(new CheckedRunnable() { |
389 |
– |
public void realRun() { |
390 |
– |
threadStarted.countDown(); |
391 |
– |
try { |
392 |
– |
q.take(); |
393 |
– |
shouldThrow(); |
394 |
– |
} catch (InterruptedException success) {} |
395 |
– |
assertFalse(Thread.interrupted()); |
396 |
– |
}}); |
397 |
– |
|
398 |
– |
await(threadStarted); |
399 |
– |
assertThreadStaysAlive(t); |
400 |
– |
t.interrupt(); |
401 |
– |
awaitTermination(t); |
402 |
– |
} |
403 |
– |
|
404 |
– |
/** |
337 |
|
* Take removes existing elements until empty, then blocks interruptibly |
338 |
|
*/ |
339 |
|
public void testBlockingTake() throws InterruptedException { |
372 |
|
public void testPoll() { |
373 |
|
DelayQueue q = populatedQueue(SIZE); |
374 |
|
for (int i = 0; i < SIZE; ++i) { |
375 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
375 |
> |
assertEquals(new PDelay(i), q.poll()); |
376 |
|
} |
377 |
|
assertNull(q.poll()); |
378 |
|
} |
383 |
|
public void testTimedPoll0() throws InterruptedException { |
384 |
|
DelayQueue q = populatedQueue(SIZE); |
385 |
|
for (int i = 0; i < SIZE; ++i) { |
386 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS))); |
386 |
> |
assertEquals(new PDelay(i), q.poll(0, MILLISECONDS)); |
387 |
|
} |
388 |
|
assertNull(q.poll(0, MILLISECONDS)); |
389 |
|
} |
395 |
|
DelayQueue q = populatedQueue(SIZE); |
396 |
|
for (int i = 0; i < SIZE; ++i) { |
397 |
|
long startTime = System.nanoTime(); |
398 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); |
398 |
> |
assertEquals(new PDelay(i), q.poll(LONG_DELAY_MS, MILLISECONDS)); |
399 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
400 |
|
} |
401 |
|
long startTime = System.nanoTime(); |
410 |
|
*/ |
411 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
412 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
413 |
+ |
final DelayQueue q = populatedQueue(SIZE); |
414 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
415 |
|
public void realRun() throws InterruptedException { |
416 |
< |
DelayQueue q = populatedQueue(SIZE); |
416 |
> |
long startTime = System.nanoTime(); |
417 |
|
for (int i = 0; i < SIZE; ++i) { |
418 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
418 |
> |
assertEquals(new PDelay(i), |
419 |
> |
((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); |
420 |
|
} |
421 |
|
|
422 |
|
Thread.currentThread().interrupt(); |
432 |
|
shouldThrow(); |
433 |
|
} catch (InterruptedException success) {} |
434 |
|
assertFalse(Thread.interrupted()); |
435 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
436 |
|
}}); |
437 |
|
|
438 |
|
await(pleaseInterrupt); |
439 |
|
assertThreadStaysAlive(t); |
440 |
|
t.interrupt(); |
441 |
|
awaitTermination(t); |
442 |
< |
} |
508 |
< |
|
509 |
< |
/** |
510 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
511 |
< |
* on interruption throws |
512 |
< |
*/ |
513 |
< |
public void testTimedPollWithOffer() throws InterruptedException { |
514 |
< |
final DelayQueue q = new DelayQueue(); |
515 |
< |
final PDelay pdelay = new PDelay(0); |
516 |
< |
final CheckedBarrier barrier = new CheckedBarrier(2); |
517 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
518 |
< |
public void realRun() throws InterruptedException { |
519 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
520 |
< |
|
521 |
< |
barrier.await(); |
522 |
< |
assertSame(pdelay, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
523 |
< |
|
524 |
< |
Thread.currentThread().interrupt(); |
525 |
< |
try { |
526 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
527 |
< |
shouldThrow(); |
528 |
< |
} catch (InterruptedException success) {} |
529 |
< |
assertFalse(Thread.interrupted()); |
530 |
< |
|
531 |
< |
barrier.await(); |
532 |
< |
try { |
533 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
534 |
< |
shouldThrow(); |
535 |
< |
} catch (InterruptedException success) {} |
536 |
< |
assertFalse(Thread.interrupted()); |
537 |
< |
}}); |
538 |
< |
|
539 |
< |
barrier.await(); |
540 |
< |
long startTime = System.nanoTime(); |
541 |
< |
assertTrue(q.offer(pdelay, LONG_DELAY_MS, MILLISECONDS)); |
542 |
< |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
543 |
< |
|
544 |
< |
barrier.await(); |
545 |
< |
assertThreadStaysAlive(t); |
546 |
< |
t.interrupt(); |
547 |
< |
awaitTermination(t); |
442 |
> |
checkEmpty(q); |
443 |
|
} |
444 |
|
|
445 |
|
/** |
448 |
|
public void testPeek() { |
449 |
|
DelayQueue q = populatedQueue(SIZE); |
450 |
|
for (int i = 0; i < SIZE; ++i) { |
451 |
< |
assertEquals(new PDelay(i), ((PDelay)q.peek())); |
452 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
451 |
> |
assertEquals(new PDelay(i), q.peek()); |
452 |
> |
assertEquals(new PDelay(i), q.poll()); |
453 |
|
if (q.isEmpty()) |
454 |
|
assertNull(q.peek()); |
455 |
|
else |
464 |
|
public void testElement() { |
465 |
|
DelayQueue q = populatedQueue(SIZE); |
466 |
|
for (int i = 0; i < SIZE; ++i) { |
467 |
< |
assertEquals(new PDelay(i), ((PDelay)q.element())); |
467 |
> |
assertEquals(new PDelay(i), q.element()); |
468 |
|
q.poll(); |
469 |
|
} |
470 |
|
try { |
479 |
|
public void testRemove() { |
480 |
|
DelayQueue q = populatedQueue(SIZE); |
481 |
|
for (int i = 0; i < SIZE; ++i) { |
482 |
< |
assertEquals(new PDelay(i), ((PDelay)q.remove())); |
482 |
> |
assertEquals(new PDelay(i), q.remove()); |
483 |
|
} |
484 |
|
try { |
485 |
|
q.remove(); |
488 |
|
} |
489 |
|
|
490 |
|
/** |
596 |
– |
* remove(x) removes x and returns true if present |
597 |
– |
*/ |
598 |
– |
public void testRemoveElement() { |
599 |
– |
DelayQueue q = populatedQueue(SIZE); |
600 |
– |
for (int i = 1; i < SIZE; i+=2) { |
601 |
– |
assertTrue(q.remove(new PDelay(i))); |
602 |
– |
} |
603 |
– |
for (int i = 0; i < SIZE; i+=2) { |
604 |
– |
assertTrue(q.remove(new PDelay(i))); |
605 |
– |
assertFalse(q.remove(new PDelay(i+1))); |
606 |
– |
} |
607 |
– |
assertTrue(q.isEmpty()); |
608 |
– |
} |
609 |
– |
|
610 |
– |
/** |
491 |
|
* contains(x) reports true when elements added but not yet removed |
492 |
|
*/ |
493 |
|
public void testContains() { |
507 |
|
q.clear(); |
508 |
|
assertTrue(q.isEmpty()); |
509 |
|
assertEquals(0, q.size()); |
510 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
510 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
511 |
|
PDelay x = new PDelay(1); |
512 |
|
q.add(x); |
513 |
|
assertFalse(q.isEmpty()); |
544 |
|
assertTrue(changed); |
545 |
|
|
546 |
|
assertTrue(q.containsAll(p)); |
547 |
< |
assertEquals(SIZE-i, q.size()); |
547 |
> |
assertEquals(SIZE - i, q.size()); |
548 |
|
p.remove(); |
549 |
|
} |
550 |
|
} |
557 |
|
DelayQueue q = populatedQueue(SIZE); |
558 |
|
DelayQueue p = populatedQueue(i); |
559 |
|
assertTrue(q.removeAll(p)); |
560 |
< |
assertEquals(SIZE-i, q.size()); |
560 |
> |
assertEquals(SIZE - i, q.size()); |
561 |
|
for (int j = 0; j < i; ++j) { |
562 |
< |
PDelay I = (PDelay)(p.remove()); |
563 |
< |
assertFalse(q.contains(I)); |
562 |
> |
PDelay x = (PDelay)(p.remove()); |
563 |
> |
assertFalse(q.contains(x)); |
564 |
|
} |
565 |
|
} |
566 |
|
} |
590 |
|
} |
591 |
|
|
592 |
|
/** |
713 |
– |
* toArray(null) throws NullPointerException |
714 |
– |
*/ |
715 |
– |
public void testToArray_NullArg() { |
716 |
– |
DelayQueue q = populatedQueue(SIZE); |
717 |
– |
try { |
718 |
– |
q.toArray(null); |
719 |
– |
shouldThrow(); |
720 |
– |
} catch (NullPointerException success) {} |
721 |
– |
} |
722 |
– |
|
723 |
– |
/** |
593 |
|
* toArray(incompatible array type) throws ArrayStoreException |
594 |
|
*/ |
595 |
|
public void testToArray1_BadArg() { |
612 |
|
++i; |
613 |
|
} |
614 |
|
assertEquals(i, SIZE); |
615 |
+ |
assertIteratorExhausted(it); |
616 |
+ |
} |
617 |
+ |
|
618 |
+ |
/** |
619 |
+ |
* iterator of empty collection has no elements |
620 |
+ |
*/ |
621 |
+ |
public void testEmptyIterator() { |
622 |
+ |
assertIteratorExhausted(new DelayQueue().iterator()); |
623 |
|
} |
624 |
|
|
625 |
|
/** |
634 |
|
it.next(); |
635 |
|
it.remove(); |
636 |
|
it = q.iterator(); |
637 |
< |
assertEquals(it.next(), new PDelay(2)); |
638 |
< |
assertEquals(it.next(), new PDelay(3)); |
637 |
> |
assertEquals(new PDelay(2), it.next()); |
638 |
> |
assertEquals(new PDelay(3), it.next()); |
639 |
|
assertFalse(it.hasNext()); |
640 |
|
} |
641 |
|
|
645 |
|
public void testToString() { |
646 |
|
DelayQueue q = populatedQueue(SIZE); |
647 |
|
String s = q.toString(); |
648 |
< |
for (int i = 0; i < SIZE; ++i) { |
649 |
< |
assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i))); |
773 |
< |
} |
648 |
> |
for (Object e : q) |
649 |
> |
assertTrue(s.contains(e.toString())); |
650 |
|
} |
651 |
|
|
652 |
|
/** |
655 |
|
public void testPollInExecutor() { |
656 |
|
final DelayQueue q = new DelayQueue(); |
657 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
658 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
659 |
< |
executor.execute(new CheckedRunnable() { |
660 |
< |
public void realRun() throws InterruptedException { |
661 |
< |
assertNull(q.poll()); |
662 |
< |
threadsStarted.await(); |
663 |
< |
assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS)); |
664 |
< |
checkEmpty(q); |
665 |
< |
}}); |
666 |
< |
|
667 |
< |
executor.execute(new CheckedRunnable() { |
668 |
< |
public void realRun() throws InterruptedException { |
669 |
< |
threadsStarted.await(); |
670 |
< |
q.put(new PDelay(1)); |
671 |
< |
}}); |
672 |
< |
|
673 |
< |
joinPool(executor); |
658 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
659 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
660 |
> |
executor.execute(new CheckedRunnable() { |
661 |
> |
public void realRun() throws InterruptedException { |
662 |
> |
assertNull(q.poll()); |
663 |
> |
threadsStarted.await(); |
664 |
> |
assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); |
665 |
> |
checkEmpty(q); |
666 |
> |
}}); |
667 |
> |
|
668 |
> |
executor.execute(new CheckedRunnable() { |
669 |
> |
public void realRun() throws InterruptedException { |
670 |
> |
threadsStarted.await(); |
671 |
> |
q.put(new PDelay(1)); |
672 |
> |
}}); |
673 |
> |
} |
674 |
|
} |
675 |
|
|
676 |
|
/** |
717 |
|
public void testTimedPollDelayed() throws InterruptedException { |
718 |
|
DelayQueue q = new DelayQueue(); |
719 |
|
q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); |
720 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
845 |
< |
} |
846 |
< |
|
847 |
< |
/** |
848 |
< |
* drainTo(null) throws NPE |
849 |
< |
*/ |
850 |
< |
public void testDrainToNull() { |
851 |
< |
DelayQueue q = populatedQueue(SIZE); |
852 |
< |
try { |
853 |
< |
q.drainTo(null); |
854 |
< |
shouldThrow(); |
855 |
< |
} catch (NullPointerException success) {} |
856 |
< |
} |
857 |
< |
|
858 |
< |
/** |
859 |
< |
* drainTo(this) throws IAE |
860 |
< |
*/ |
861 |
< |
public void testDrainToSelf() { |
862 |
< |
DelayQueue q = populatedQueue(SIZE); |
863 |
< |
try { |
864 |
< |
q.drainTo(q); |
865 |
< |
shouldThrow(); |
866 |
< |
} catch (IllegalArgumentException success) {} |
720 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
721 |
|
} |
722 |
|
|
723 |
|
/** |
732 |
|
} |
733 |
|
ArrayList l = new ArrayList(); |
734 |
|
q.drainTo(l); |
735 |
< |
assertEquals(q.size(), 0); |
735 |
> |
assertEquals(0, q.size()); |
736 |
|
for (int i = 0; i < SIZE; ++i) |
737 |
< |
assertEquals(l.get(i), elems[i]); |
737 |
> |
assertEquals(elems[i], l.get(i)); |
738 |
|
q.add(elems[0]); |
739 |
|
q.add(elems[1]); |
740 |
|
assertFalse(q.isEmpty()); |
742 |
|
assertTrue(q.contains(elems[1])); |
743 |
|
l.clear(); |
744 |
|
q.drainTo(l); |
745 |
< |
assertEquals(q.size(), 0); |
746 |
< |
assertEquals(l.size(), 2); |
745 |
> |
assertEquals(0, q.size()); |
746 |
> |
assertEquals(2, l.size()); |
747 |
|
for (int i = 0; i < 2; ++i) |
748 |
< |
assertEquals(l.get(i), elems[i]); |
748 |
> |
assertEquals(elems[i], l.get(i)); |
749 |
|
} |
750 |
|
|
751 |
|
/** |
755 |
|
final DelayQueue q = populatedQueue(SIZE); |
756 |
|
Thread t = new Thread(new CheckedRunnable() { |
757 |
|
public void realRun() { |
758 |
< |
q.put(new PDelay(SIZE+1)); |
758 |
> |
q.put(new PDelay(SIZE + 1)); |
759 |
|
}}); |
760 |
|
|
761 |
|
t.start(); |
767 |
|
} |
768 |
|
|
769 |
|
/** |
916 |
– |
* drainTo(null, n) throws NPE |
917 |
– |
*/ |
918 |
– |
public void testDrainToNullN() { |
919 |
– |
DelayQueue q = populatedQueue(SIZE); |
920 |
– |
try { |
921 |
– |
q.drainTo(null, 0); |
922 |
– |
shouldThrow(); |
923 |
– |
} catch (NullPointerException success) {} |
924 |
– |
} |
925 |
– |
|
926 |
– |
/** |
927 |
– |
* drainTo(this, n) throws IAE |
928 |
– |
*/ |
929 |
– |
public void testDrainToSelfN() { |
930 |
– |
DelayQueue q = populatedQueue(SIZE); |
931 |
– |
try { |
932 |
– |
q.drainTo(q, 0); |
933 |
– |
shouldThrow(); |
934 |
– |
} catch (IllegalArgumentException success) {} |
935 |
– |
} |
936 |
– |
|
937 |
– |
/** |
770 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
771 |
|
*/ |
772 |
|
public void testDrainToN() { |
775 |
|
ArrayList l = new ArrayList(); |
776 |
|
q.drainTo(l, i); |
777 |
|
int k = (i < SIZE) ? i : SIZE; |
778 |
< |
assertEquals(q.size(), SIZE-k); |
779 |
< |
assertEquals(l.size(), k); |
778 |
> |
assertEquals(SIZE - k, q.size()); |
779 |
> |
assertEquals(k, l.size()); |
780 |
|
} |
781 |
|
} |
782 |
|
|
783 |
+ |
/** |
784 |
+ |
* remove(null), contains(null) always return false |
785 |
+ |
*/ |
786 |
+ |
public void testNeverContainsNull() { |
787 |
+ |
Collection<?> q = populatedQueue(SIZE); |
788 |
+ |
assertFalse(q.contains(null)); |
789 |
+ |
assertFalse(q.remove(null)); |
790 |
+ |
} |
791 |
|
} |