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.*; |
10 |
|
import java.util.*; |
11 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
12 |
|
import java.util.concurrent.*; |
13 |
|
|
14 |
|
public class DelayQueueTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
16 |
> |
junit.textui.TestRunner.run (suite()); |
17 |
|
} |
18 |
|
|
19 |
|
public static Test suite() { |
20 |
< |
return new TestSuite(DelayQueueTest.class); |
20 |
> |
return new TestSuite(DelayQueueTest.class); |
21 |
|
} |
22 |
|
|
23 |
|
private static final int NOCAP = Integer.MAX_VALUE; |
24 |
|
|
25 |
|
/** |
26 |
< |
* A delayed implmentation for testing. |
26 |
> |
* A delayed implementation for testing. |
27 |
|
* Most tests use Pseudodelays, where delays are all elapsed |
28 |
|
* (so, no blocking solely for delays) but are still ordered |
29 |
< |
*/ |
30 |
< |
static class PDelay implements Delayed { |
29 |
> |
*/ |
30 |
> |
static class PDelay implements Delayed { |
31 |
|
int pseudodelay; |
32 |
|
PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } |
33 |
< |
public int compareTo(Object y) { |
33 |
> |
public int compareTo(PDelay y) { |
34 |
|
int i = pseudodelay; |
35 |
|
int j = ((PDelay)y).pseudodelay; |
36 |
|
if (i < j) return -1; |
38 |
|
return 0; |
39 |
|
} |
40 |
|
|
41 |
< |
public int compareTo(PDelay y) { |
41 |
> |
public int compareTo(Delayed y) { |
42 |
|
int i = pseudodelay; |
43 |
|
int j = ((PDelay)y).pseudodelay; |
44 |
|
if (i < j) return -1; |
70 |
|
/** |
71 |
|
* Delayed implementation that actually delays |
72 |
|
*/ |
73 |
< |
static class NanoDelay implements Delayed { |
73 |
> |
static class NanoDelay implements Delayed { |
74 |
|
long trigger; |
75 |
< |
NanoDelay(long i) { |
75 |
> |
NanoDelay(long i) { |
76 |
|
trigger = System.nanoTime() + i; |
77 |
|
} |
78 |
< |
public int compareTo(Object y) { |
78 |
> |
public int compareTo(NanoDelay y) { |
79 |
|
long i = trigger; |
80 |
|
long j = ((NanoDelay)y).trigger; |
81 |
|
if (i < j) return -1; |
83 |
|
return 0; |
84 |
|
} |
85 |
|
|
86 |
< |
public int compareTo(NanoDelay y) { |
86 |
> |
public int compareTo(Delayed y) { |
87 |
|
long i = trigger; |
88 |
|
long j = ((NanoDelay)y).trigger; |
89 |
|
if (i < j) return -1; |
120 |
|
private DelayQueue populatedQueue(int n) { |
121 |
|
DelayQueue q = new DelayQueue(); |
122 |
|
assertTrue(q.isEmpty()); |
123 |
< |
for(int i = n-1; i >= 0; i-=2) |
124 |
< |
assertTrue(q.offer(new PDelay(i))); |
125 |
< |
for(int i = (n & 1); i < n; i+=2) |
126 |
< |
assertTrue(q.offer(new PDelay(i))); |
123 |
> |
for (int i = n-1; i >= 0; i-=2) |
124 |
> |
assertTrue(q.offer(new PDelay(i))); |
125 |
> |
for (int i = (n & 1); i < n; i+=2) |
126 |
> |
assertTrue(q.offer(new PDelay(i))); |
127 |
|
assertFalse(q.isEmpty()); |
128 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
129 |
< |
assertEquals(n, q.size()); |
129 |
> |
assertEquals(n, q.size()); |
130 |
|
return q; |
131 |
|
} |
132 |
< |
|
132 |
> |
|
133 |
|
/** |
134 |
|
* A new queue has unbounded capacity |
135 |
|
*/ |
144 |
|
try { |
145 |
|
DelayQueue q = new DelayQueue(null); |
146 |
|
shouldThrow(); |
147 |
< |
} |
146 |
< |
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 |
< |
} |
158 |
< |
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 |
< |
} |
172 |
< |
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) |
185 |
< |
assertEquals(ints[i], q.poll()); |
186 |
< |
} |
187 |
< |
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 |
|
/** |
220 |
|
* offer(null) throws NPE |
221 |
|
*/ |
222 |
|
public void testOfferNull() { |
223 |
< |
try { |
223 |
> |
try { |
224 |
|
DelayQueue q = new DelayQueue(); |
225 |
|
q.offer(null); |
226 |
|
shouldThrow(); |
227 |
< |
} catch (NullPointerException success) { } |
227 |
> |
} catch (NullPointerException success) {} |
228 |
|
} |
229 |
|
|
230 |
|
/** |
231 |
|
* add(null) throws NPE |
232 |
|
*/ |
233 |
|
public void testAddNull() { |
234 |
< |
try { |
234 |
> |
try { |
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 |
< |
} |
274 |
< |
catch (NullPointerException success) {} |
269 |
> |
} catch (NullPointerException success) {} |
270 |
|
} |
271 |
|
|
272 |
|
|
278 |
|
DelayQueue q = populatedQueue(SIZE); |
279 |
|
q.addAll(q); |
280 |
|
shouldThrow(); |
281 |
< |
} |
287 |
< |
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 |
< |
} |
300 |
< |
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 |
< |
} |
315 |
< |
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) |
331 |
< |
assertEquals(ints[i], q.poll()); |
332 |
< |
} |
333 |
< |
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 |
|
/** |
326 |
|
* put(null) throws NPE |
327 |
|
*/ |
328 |
|
public void testPutNull() { |
329 |
< |
try { |
329 |
> |
try { |
330 |
|
DelayQueue q = new DelayQueue(); |
331 |
|
q.put(null); |
332 |
|
shouldThrow(); |
333 |
< |
} |
345 |
< |
catch (NullPointerException success){ |
346 |
< |
} |
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); |
358 |
< |
assertTrue(q.contains(I)); |
359 |
< |
} |
360 |
< |
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 { |
363 |
< |
} |
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); |
384 |
< |
} finally { |
385 |
< |
} |
386 |
< |
} |
387 |
< |
}); |
388 |
< |
try { |
389 |
< |
t.start(); |
390 |
< |
Thread.sleep(SHORT_DELAY_MS); |
391 |
< |
q.take(); |
392 |
< |
t.interrupt(); |
393 |
< |
t.join(); |
394 |
< |
} catch (Exception e){ |
395 |
< |
unexpectedException(); |
396 |
< |
} |
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, TimeUnit.MILLISECONDS)); |
380 |
< |
threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
381 |
< |
} finally { } |
382 |
< |
} |
383 |
< |
}); |
384 |
< |
|
385 |
< |
try { |
416 |
< |
t.start(); |
417 |
< |
Thread.sleep(SMALL_DELAY_MS); |
418 |
< |
t.interrupt(); |
419 |
< |
t.join(); |
420 |
< |
} catch (Exception e){ |
421 |
< |
unexpectedException(); |
422 |
< |
} |
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 |
> |
threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
379 |
> |
threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); |
380 |
> |
}}); |
381 |
> |
|
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) { |
395 |
< |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
433 |
< |
} |
434 |
< |
} catch (InterruptedException e){ |
435 |
< |
unexpectedException(); |
436 |
< |
} |
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 { |
453 |
< |
t.start(); |
454 |
< |
Thread.sleep(SHORT_DELAY_MS); |
455 |
< |
t.interrupt(); |
456 |
< |
t.join(); |
457 |
< |
} catch (Exception e){ |
458 |
< |
unexpectedException(); |
459 |
< |
} |
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(); |
475 |
< |
} catch (InterruptedException success){ |
476 |
< |
} |
477 |
< |
}}); |
417 |
> |
public void testBlockingTake() throws InterruptedException { |
418 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
419 |
> |
public void realRun() throws InterruptedException { |
420 |
> |
DelayQueue q = populatedQueue(SIZE); |
421 |
> |
for (int i = 0; i < SIZE; ++i) { |
422 |
> |
threadAssertEquals(new PDelay(i), ((PDelay)q.take())); |
423 |
> |
} |
424 |
> |
q.take(); |
425 |
> |
}}; |
426 |
> |
|
427 |
|
t.start(); |
428 |
< |
try { |
429 |
< |
Thread.sleep(SHORT_DELAY_MS); |
430 |
< |
t.interrupt(); |
482 |
< |
t.join(); |
483 |
< |
} |
484 |
< |
catch (InterruptedException ie) { |
485 |
< |
unexpectedException(); |
486 |
< |
} |
428 |
> |
Thread.sleep(SHORT_DELAY_MS); |
429 |
> |
t.interrupt(); |
430 |
> |
t.join(); |
431 |
|
} |
432 |
|
|
433 |
|
|
439 |
|
for (int i = 0; i < SIZE; ++i) { |
440 |
|
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
441 |
|
} |
442 |
< |
assertNull(q.poll()); |
442 |
> |
assertNull(q.poll()); |
443 |
|
} |
444 |
|
|
445 |
|
/** |
446 |
|
* timed pool with zero timeout succeeds when non-empty, else times out |
447 |
|
*/ |
448 |
< |
public void testTimedPoll0() { |
449 |
< |
try { |
450 |
< |
DelayQueue q = populatedQueue(SIZE); |
451 |
< |
for (int i = 0; i < SIZE; ++i) { |
452 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(0, TimeUnit.MILLISECONDS))); |
453 |
< |
} |
510 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
511 |
< |
} catch (InterruptedException e){ |
512 |
< |
unexpectedException(); |
513 |
< |
} |
448 |
> |
public void testTimedPoll0() throws InterruptedException { |
449 |
> |
DelayQueue q = populatedQueue(SIZE); |
450 |
> |
for (int i = 0; i < SIZE; ++i) { |
451 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS))); |
452 |
> |
} |
453 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
454 |
|
} |
455 |
|
|
456 |
|
/** |
457 |
|
* timed pool with nonzero timeout succeeds when non-empty, else times out |
458 |
|
*/ |
459 |
< |
public void testTimedPoll() { |
460 |
< |
try { |
461 |
< |
DelayQueue q = populatedQueue(SIZE); |
462 |
< |
for (int i = 0; i < SIZE; ++i) { |
463 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS))); |
464 |
< |
} |
525 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
526 |
< |
} catch (InterruptedException e){ |
527 |
< |
unexpectedException(); |
528 |
< |
} |
459 |
> |
public void testTimedPoll() throws InterruptedException { |
460 |
> |
DelayQueue q = populatedQueue(SIZE); |
461 |
> |
for (int i = 0; i < SIZE; ++i) { |
462 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
463 |
> |
} |
464 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
465 |
|
} |
466 |
|
|
467 |
|
/** |
468 |
|
* Interrupted timed poll throws InterruptedException instead of |
469 |
|
* returning timeout status |
470 |
|
*/ |
471 |
< |
public void testInterruptedTimedPoll() { |
472 |
< |
Thread t = new Thread(new Runnable() { |
473 |
< |
public void run() { |
474 |
< |
try { |
475 |
< |
DelayQueue q = populatedQueue(SIZE); |
476 |
< |
for (int i = 0; i < SIZE; ++i) { |
477 |
< |
threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS))); |
478 |
< |
} |
479 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
480 |
< |
} catch (InterruptedException success){ |
481 |
< |
} |
482 |
< |
}}); |
471 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
472 |
> |
Thread t = new Thread(new CheckedRunnable() { |
473 |
> |
public void realRun() throws InterruptedException { |
474 |
> |
DelayQueue q = populatedQueue(SIZE); |
475 |
> |
for (int i = 0; i < SIZE; ++i) { |
476 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
477 |
> |
} |
478 |
> |
try { |
479 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
480 |
> |
shouldThrow(); |
481 |
> |
} catch (InterruptedException success) {} |
482 |
> |
}}); |
483 |
> |
|
484 |
|
t.start(); |
485 |
< |
try { |
486 |
< |
Thread.sleep(SHORT_DELAY_MS); |
487 |
< |
t.interrupt(); |
551 |
< |
t.join(); |
552 |
< |
} |
553 |
< |
catch (InterruptedException ie) { |
554 |
< |
unexpectedException(); |
555 |
< |
} |
485 |
> |
Thread.sleep(SHORT_DELAY_MS); |
486 |
> |
t.interrupt(); |
487 |
> |
t.join(); |
488 |
|
} |
489 |
|
|
490 |
|
/** |
491 |
|
* timed poll before a delayed offer fails; after offer succeeds; |
492 |
|
* on interruption throws |
493 |
|
*/ |
494 |
< |
public void testTimedPollWithOffer() { |
494 |
> |
public void testTimedPollWithOffer() throws InterruptedException { |
495 |
|
final DelayQueue q = new DelayQueue(); |
496 |
< |
Thread t = new Thread(new Runnable() { |
497 |
< |
public void run() { |
498 |
< |
try { |
499 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
500 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
501 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
502 |
< |
threadFail("Should block"); |
503 |
< |
} catch (InterruptedException success) { } |
504 |
< |
} |
505 |
< |
}); |
506 |
< |
try { |
507 |
< |
t.start(); |
508 |
< |
Thread.sleep(SMALL_DELAY_MS); |
509 |
< |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
510 |
< |
t.interrupt(); |
511 |
< |
t.join(); |
580 |
< |
} catch (Exception e){ |
581 |
< |
unexpectedException(); |
582 |
< |
} |
583 |
< |
} |
496 |
> |
Thread t = new Thread(new CheckedRunnable() { |
497 |
> |
public void realRun() throws InterruptedException { |
498 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
499 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
500 |
> |
try { |
501 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
502 |
> |
shouldThrow(); |
503 |
> |
} catch (InterruptedException success) {} |
504 |
> |
}}); |
505 |
> |
|
506 |
> |
t.start(); |
507 |
> |
Thread.sleep(SMALL_DELAY_MS); |
508 |
> |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
509 |
> |
t.interrupt(); |
510 |
> |
t.join(); |
511 |
> |
} |
512 |
|
|
513 |
|
|
514 |
|
/** |
519 |
|
for (int i = 0; i < SIZE; ++i) { |
520 |
|
assertEquals(new PDelay(i), ((PDelay)q.peek())); |
521 |
|
q.poll(); |
522 |
< |
assertTrue(q.peek() == null || |
523 |
< |
i != ((PDelay)q.peek()).intValue()); |
522 |
> |
if (q.isEmpty()) |
523 |
> |
assertNull(q.peek()); |
524 |
> |
else |
525 |
> |
assertTrue(i != ((PDelay)q.peek()).intValue()); |
526 |
|
} |
527 |
< |
assertNull(q.peek()); |
527 |
> |
assertNull(q.peek()); |
528 |
|
} |
529 |
|
|
530 |
|
/** |
539 |
|
try { |
540 |
|
q.element(); |
541 |
|
shouldThrow(); |
542 |
< |
} |
613 |
< |
catch (NoSuchElementException success) {} |
542 |
> |
} catch (NoSuchElementException success) {} |
543 |
|
} |
544 |
|
|
545 |
|
/** |
553 |
|
try { |
554 |
|
q.remove(); |
555 |
|
shouldThrow(); |
556 |
< |
} catch (NoSuchElementException success){ |
628 |
< |
} |
556 |
> |
} catch (NoSuchElementException success) {} |
557 |
|
} |
558 |
|
|
559 |
|
/** |
570 |
|
} |
571 |
|
assertTrue(q.isEmpty()); |
572 |
|
} |
573 |
< |
|
573 |
> |
|
574 |
|
/** |
575 |
|
* contains(x) reports true when elements added but not yet removed |
576 |
|
*/ |
592 |
|
assertTrue(q.isEmpty()); |
593 |
|
assertEquals(0, q.size()); |
594 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
595 |
< |
q.add(new PDelay(1)); |
595 |
> |
PDelay x = new PDelay(1); |
596 |
> |
q.add(x); |
597 |
|
assertFalse(q.isEmpty()); |
598 |
+ |
assertTrue(q.contains(x)); |
599 |
|
q.clear(); |
600 |
|
assertTrue(q.isEmpty()); |
601 |
|
} |
652 |
|
/** |
653 |
|
* toArray contains all elements |
654 |
|
*/ |
655 |
< |
public void testToArray() { |
655 |
> |
public void testToArray() throws InterruptedException { |
656 |
|
DelayQueue q = populatedQueue(SIZE); |
657 |
< |
Object[] o = q.toArray(); |
657 |
> |
Object[] o = q.toArray(); |
658 |
|
Arrays.sort(o); |
659 |
< |
try { |
660 |
< |
for(int i = 0; i < o.length; i++) |
731 |
< |
assertEquals(o[i], q.take()); |
732 |
< |
} catch (InterruptedException e){ |
733 |
< |
unexpectedException(); |
734 |
< |
} |
659 |
> |
for (int i = 0; i < o.length; i++) |
660 |
> |
assertEquals(o[i], q.take()); |
661 |
|
} |
662 |
|
|
663 |
|
/** |
664 |
|
* toArray(a) contains all elements |
665 |
|
*/ |
666 |
< |
public void testToArray2() { |
666 |
> |
public void testToArray2() throws InterruptedException { |
667 |
|
DelayQueue q = populatedQueue(SIZE); |
668 |
< |
PDelay[] ints = new PDelay[SIZE]; |
669 |
< |
ints = (PDelay[])q.toArray(ints); |
668 |
> |
PDelay[] ints = new PDelay[SIZE]; |
669 |
> |
ints = (PDelay[])q.toArray(ints); |
670 |
|
Arrays.sort(ints); |
671 |
< |
try { |
672 |
< |
for(int i = 0; i < ints.length; i++) |
747 |
< |
assertEquals(ints[i], q.take()); |
748 |
< |
} catch (InterruptedException e){ |
749 |
< |
unexpectedException(); |
750 |
< |
} |
671 |
> |
for (int i = 0; i < ints.length; i++) |
672 |
> |
assertEquals(ints[i], q.take()); |
673 |
|
} |
674 |
|
|
675 |
|
|
677 |
|
* toArray(null) throws NPE |
678 |
|
*/ |
679 |
|
public void testToArray_BadArg() { |
680 |
< |
try { |
680 |
> |
try { |
681 |
|
DelayQueue q = populatedQueue(SIZE); |
682 |
< |
Object o[] = q.toArray(null); |
683 |
< |
shouldThrow(); |
684 |
< |
} catch(NullPointerException success){} |
682 |
> |
Object o[] = q.toArray(null); |
683 |
> |
shouldThrow(); |
684 |
> |
} catch (NullPointerException success) {} |
685 |
|
} |
686 |
|
|
687 |
|
/** |
688 |
< |
* toArray with incompatable array type throws CCE |
688 |
> |
* toArray with incompatible array type throws CCE |
689 |
|
*/ |
690 |
|
public void testToArray1_BadArg() { |
691 |
< |
try { |
691 |
> |
try { |
692 |
|
DelayQueue q = populatedQueue(SIZE); |
693 |
< |
Object o[] = q.toArray(new String[10] ); |
694 |
< |
shouldThrow(); |
695 |
< |
} catch(ArrayStoreException success){} |
693 |
> |
Object o[] = q.toArray(new String[10] ); |
694 |
> |
shouldThrow(); |
695 |
> |
} catch (ArrayStoreException success) {} |
696 |
|
} |
697 |
< |
|
697 |
> |
|
698 |
|
/** |
699 |
|
* iterator iterates through all elements |
700 |
|
*/ |
701 |
|
public void testIterator() { |
702 |
|
DelayQueue q = populatedQueue(SIZE); |
703 |
|
int i = 0; |
704 |
< |
Iterator it = q.iterator(); |
705 |
< |
while(it.hasNext()) { |
704 |
> |
Iterator it = q.iterator(); |
705 |
> |
while (it.hasNext()) { |
706 |
|
assertTrue(q.contains(it.next())); |
707 |
|
++i; |
708 |
|
} |
736 |
|
for (int i = 0; i < SIZE; ++i) { |
737 |
|
assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0); |
738 |
|
} |
739 |
< |
} |
739 |
> |
} |
740 |
|
|
741 |
|
/** |
742 |
|
* offer transfers elements across Executor tasks |
744 |
|
public void testPollInExecutor() { |
745 |
|
final DelayQueue q = new DelayQueue(); |
746 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
747 |
< |
executor.execute(new Runnable() { |
748 |
< |
public void run() { |
747 |
> |
executor.execute(new CheckedRunnable() { |
748 |
> |
public void realRun() throws InterruptedException { |
749 |
|
threadAssertNull(q.poll()); |
750 |
< |
try { |
751 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
752 |
< |
threadAssertTrue(q.isEmpty()); |
753 |
< |
} |
754 |
< |
catch (InterruptedException e) { |
755 |
< |
threadUnexpectedException(); |
756 |
< |
} |
757 |
< |
} |
758 |
< |
}); |
750 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
751 |
> |
threadAssertTrue(q.isEmpty()); |
752 |
> |
}}); |
753 |
> |
|
754 |
> |
executor.execute(new CheckedRunnable() { |
755 |
> |
public void realRun() throws InterruptedException { |
756 |
> |
Thread.sleep(SHORT_DELAY_MS); |
757 |
> |
q.put(new PDelay(1)); |
758 |
> |
}}); |
759 |
|
|
838 |
– |
executor.execute(new Runnable() { |
839 |
– |
public void run() { |
840 |
– |
try { |
841 |
– |
Thread.sleep(SHORT_DELAY_MS); |
842 |
– |
q.put(new PDelay(1)); |
843 |
– |
} |
844 |
– |
catch (InterruptedException e) { |
845 |
– |
threadUnexpectedException(); |
846 |
– |
} |
847 |
– |
} |
848 |
– |
}); |
760 |
|
joinPool(executor); |
761 |
|
|
762 |
|
} |
763 |
|
|
764 |
|
|
765 |
|
/** |
766 |
< |
* Dekayed actions do not occur until their delay elapses |
766 |
> |
* Delayed actions do not occur until their delay elapses |
767 |
|
*/ |
768 |
< |
public void testDelay() { |
768 |
> |
public void testDelay() throws InterruptedException { |
769 |
|
DelayQueue q = new DelayQueue(); |
770 |
|
NanoDelay[] elements = new NanoDelay[SIZE]; |
771 |
|
for (int i = 0; i < SIZE; ++i) { |
775 |
|
q.add(elements[i]); |
776 |
|
} |
777 |
|
|
778 |
< |
try { |
779 |
< |
long last = 0; |
780 |
< |
for (int i = 0; i < SIZE; ++i) { |
781 |
< |
NanoDelay e = (NanoDelay)(q.take()); |
782 |
< |
long tt = e.getTriggerTime(); |
783 |
< |
assertTrue(tt <= System.nanoTime()); |
784 |
< |
if (i != 0) |
785 |
< |
assertTrue(tt >= last); |
875 |
< |
last = tt; |
876 |
< |
} |
877 |
< |
} |
878 |
< |
catch(InterruptedException ie) { |
879 |
< |
unexpectedException(); |
778 |
> |
long last = 0; |
779 |
> |
for (int i = 0; i < SIZE; ++i) { |
780 |
> |
NanoDelay e = (NanoDelay)(q.take()); |
781 |
> |
long tt = e.getTriggerTime(); |
782 |
> |
assertTrue(tt <= System.nanoTime()); |
783 |
> |
if (i != 0) |
784 |
> |
assertTrue(tt >= last); |
785 |
> |
last = tt; |
786 |
|
} |
787 |
|
} |
788 |
|
|
789 |
+ |
/** |
790 |
+ |
* peek of a non-empty queue returns non-null even if not expired |
791 |
+ |
*/ |
792 |
+ |
public void testPeekDelayed() { |
793 |
+ |
DelayQueue q = new DelayQueue(); |
794 |
+ |
q.add(new NanoDelay(Long.MAX_VALUE)); |
795 |
+ |
assert(q.peek() != null); |
796 |
+ |
} |
797 |
+ |
|
798 |
+ |
|
799 |
+ |
/** |
800 |
+ |
* poll of a non-empty queue returns null if no expired elements. |
801 |
+ |
*/ |
802 |
+ |
public void testPollDelayed() { |
803 |
+ |
DelayQueue q = new DelayQueue(); |
804 |
+ |
q.add(new NanoDelay(Long.MAX_VALUE)); |
805 |
+ |
assertNull(q.poll()); |
806 |
+ |
} |
807 |
+ |
|
808 |
+ |
/** |
809 |
+ |
* timed poll of a non-empty queue returns null if no expired elements. |
810 |
+ |
*/ |
811 |
+ |
public void testTimedPollDelayed() throws InterruptedException { |
812 |
+ |
DelayQueue q = new DelayQueue(); |
813 |
+ |
q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); |
814 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
815 |
+ |
} |
816 |
|
|
817 |
|
/** |
818 |
|
* drainTo(null) throws NPE |
819 |
< |
*/ |
819 |
> |
*/ |
820 |
|
public void testDrainToNull() { |
821 |
|
DelayQueue q = populatedQueue(SIZE); |
822 |
|
try { |
823 |
|
q.drainTo(null); |
824 |
|
shouldThrow(); |
825 |
< |
} catch(NullPointerException success) { |
893 |
< |
} |
825 |
> |
} catch (NullPointerException success) {} |
826 |
|
} |
827 |
|
|
828 |
|
/** |
829 |
|
* drainTo(this) throws IAE |
830 |
< |
*/ |
830 |
> |
*/ |
831 |
|
public void testDrainToSelf() { |
832 |
|
DelayQueue q = populatedQueue(SIZE); |
833 |
|
try { |
834 |
|
q.drainTo(q); |
835 |
|
shouldThrow(); |
836 |
< |
} catch(IllegalArgumentException success) { |
905 |
< |
} |
836 |
> |
} catch (IllegalArgumentException success) {} |
837 |
|
} |
838 |
|
|
839 |
|
/** |
840 |
|
* drainTo(c) empties queue into another collection c |
841 |
< |
*/ |
841 |
> |
*/ |
842 |
|
public void testDrainTo() { |
843 |
< |
DelayQueue q = populatedQueue(SIZE); |
843 |
> |
DelayQueue q = new DelayQueue(); |
844 |
> |
PDelay[] elems = new PDelay[SIZE]; |
845 |
> |
for (int i = 0; i < SIZE; ++i) { |
846 |
> |
elems[i] = new PDelay(i); |
847 |
> |
q.add(elems[i]); |
848 |
> |
} |
849 |
|
ArrayList l = new ArrayList(); |
850 |
|
q.drainTo(l); |
851 |
|
assertEquals(q.size(), 0); |
852 |
< |
assertEquals(l.size(), SIZE); |
852 |
> |
for (int i = 0; i < SIZE; ++i) |
853 |
> |
assertEquals(l.get(i), elems[i]); |
854 |
> |
q.add(elems[0]); |
855 |
> |
q.add(elems[1]); |
856 |
> |
assertFalse(q.isEmpty()); |
857 |
> |
assertTrue(q.contains(elems[0])); |
858 |
> |
assertTrue(q.contains(elems[1])); |
859 |
> |
l.clear(); |
860 |
> |
q.drainTo(l); |
861 |
> |
assertEquals(q.size(), 0); |
862 |
> |
assertEquals(l.size(), 2); |
863 |
> |
for (int i = 0; i < 2; ++i) |
864 |
> |
assertEquals(l.get(i), elems[i]); |
865 |
|
} |
866 |
|
|
867 |
|
/** |
868 |
|
* drainTo empties queue |
869 |
< |
*/ |
870 |
< |
public void testDrainToWithActivePut() { |
869 |
> |
*/ |
870 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
871 |
|
final DelayQueue q = populatedQueue(SIZE); |
872 |
< |
Thread t = new Thread(new Runnable() { |
873 |
< |
public void run() { |
874 |
< |
q.put(new PDelay(SIZE+1)); |
875 |
< |
} |
876 |
< |
}); |
877 |
< |
try { |
878 |
< |
t.start(); |
879 |
< |
ArrayList l = new ArrayList(); |
880 |
< |
q.drainTo(l); |
881 |
< |
assertTrue(l.size() >= SIZE); |
882 |
< |
t.join(); |
935 |
< |
assertTrue(q.size() + l.size() == SIZE+1); |
936 |
< |
} catch(Exception e){ |
937 |
< |
unexpectedException(); |
938 |
< |
} |
872 |
> |
Thread t = new Thread(new CheckedRunnable() { |
873 |
> |
public void realRun() { |
874 |
> |
q.put(new PDelay(SIZE+1)); |
875 |
> |
}}); |
876 |
> |
|
877 |
> |
t.start(); |
878 |
> |
ArrayList l = new ArrayList(); |
879 |
> |
q.drainTo(l); |
880 |
> |
assertTrue(l.size() >= SIZE); |
881 |
> |
t.join(); |
882 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
883 |
|
} |
884 |
|
|
885 |
|
/** |
886 |
|
* drainTo(null, n) throws NPE |
887 |
< |
*/ |
887 |
> |
*/ |
888 |
|
public void testDrainToNullN() { |
889 |
|
DelayQueue q = populatedQueue(SIZE); |
890 |
|
try { |
891 |
|
q.drainTo(null, 0); |
892 |
|
shouldThrow(); |
893 |
< |
} catch(NullPointerException success) { |
950 |
< |
} |
893 |
> |
} catch (NullPointerException success) {} |
894 |
|
} |
895 |
|
|
896 |
|
/** |
897 |
|
* drainTo(this, n) throws IAE |
898 |
< |
*/ |
898 |
> |
*/ |
899 |
|
public void testDrainToSelfN() { |
900 |
|
DelayQueue q = populatedQueue(SIZE); |
901 |
|
try { |
902 |
|
q.drainTo(q, 0); |
903 |
|
shouldThrow(); |
904 |
< |
} catch(IllegalArgumentException success) { |
962 |
< |
} |
904 |
> |
} catch (IllegalArgumentException success) {} |
905 |
|
} |
906 |
|
|
907 |
|
/** |
908 |
|
* drainTo(c, n) empties first max {n, size} elements of queue into c |
909 |
< |
*/ |
909 |
> |
*/ |
910 |
|
public void testDrainToN() { |
911 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
912 |
|
DelayQueue q = populatedQueue(SIZE); |