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) |
127 |
> |
for (int i = n - 1; i >= 0; i -= 2) |
128 |
|
assertTrue(q.offer(new PDelay(i))); |
129 |
|
for (int i = (n & 1); i < n; i += 2) |
130 |
|
assertTrue(q.offer(new PDelay(i))); |
166 |
|
*/ |
167 |
|
public void testConstructor5() { |
168 |
|
PDelay[] a = new PDelay[SIZE]; |
169 |
< |
for (int i = 0; i < SIZE-1; ++i) |
169 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
170 |
|
a[i] = new PDelay(i); |
171 |
|
try { |
172 |
|
new DelayQueue(Arrays.asList(a)); |
208 |
|
BlockingQueue q = populatedQueue(SIZE); |
209 |
|
for (int i = 0; i < SIZE; ++i) { |
210 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
211 |
< |
assertEquals(SIZE-i, q.size()); |
211 |
> |
assertEquals(SIZE - i, q.size()); |
212 |
|
assertTrue(q.remove() instanceof PDelay); |
213 |
|
} |
214 |
|
for (int i = 0; i < SIZE; ++i) { |
256 |
|
public void testAddAll3() { |
257 |
|
DelayQueue q = new DelayQueue(); |
258 |
|
PDelay[] a = new PDelay[SIZE]; |
259 |
< |
for (int i = 0; i < SIZE-1; ++i) |
259 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
260 |
|
a[i] = new PDelay(i); |
261 |
|
try { |
262 |
|
q.addAll(Arrays.asList(a)); |
270 |
|
public void testAddAll5() { |
271 |
|
PDelay[] empty = new PDelay[0]; |
272 |
|
PDelay[] ints = new PDelay[SIZE]; |
273 |
< |
for (int i = SIZE-1; i >= 0; --i) |
273 |
> |
for (int i = SIZE - 1; i >= 0; --i) |
274 |
|
ints[i] = new PDelay(i); |
275 |
|
DelayQueue q = new DelayQueue(); |
276 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
412 |
|
*/ |
413 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
414 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
415 |
+ |
final DelayQueue q = populatedQueue(SIZE); |
416 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
417 |
|
public void realRun() throws InterruptedException { |
418 |
< |
DelayQueue q = populatedQueue(SIZE); |
418 |
> |
long startTime = System.nanoTime(); |
419 |
|
for (int i = 0; i < SIZE; ++i) { |
420 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
420 |
> |
assertEquals(new PDelay(i), |
421 |
> |
((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); |
422 |
|
} |
423 |
|
|
424 |
|
Thread.currentThread().interrupt(); |
434 |
|
shouldThrow(); |
435 |
|
} catch (InterruptedException success) {} |
436 |
|
assertFalse(Thread.interrupted()); |
437 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
438 |
|
}}); |
439 |
|
|
440 |
|
await(pleaseInterrupt); |
441 |
|
assertThreadStaysAlive(t); |
442 |
|
t.interrupt(); |
443 |
|
awaitTermination(t); |
444 |
+ |
checkEmpty(q); |
445 |
|
} |
446 |
|
|
447 |
|
/** |
546 |
|
assertTrue(changed); |
547 |
|
|
548 |
|
assertTrue(q.containsAll(p)); |
549 |
< |
assertEquals(SIZE-i, q.size()); |
549 |
> |
assertEquals(SIZE - i, q.size()); |
550 |
|
p.remove(); |
551 |
|
} |
552 |
|
} |
559 |
|
DelayQueue q = populatedQueue(SIZE); |
560 |
|
DelayQueue p = populatedQueue(i); |
561 |
|
assertTrue(q.removeAll(p)); |
562 |
< |
assertEquals(SIZE-i, q.size()); |
562 |
> |
assertEquals(SIZE - i, q.size()); |
563 |
|
for (int j = 0; j < i; ++j) { |
564 |
|
PDelay x = (PDelay)(p.remove()); |
565 |
|
assertFalse(q.contains(x)); |
657 |
|
public void testPollInExecutor() { |
658 |
|
final DelayQueue q = new DelayQueue(); |
659 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
660 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
661 |
< |
executor.execute(new CheckedRunnable() { |
662 |
< |
public void realRun() throws InterruptedException { |
663 |
< |
assertNull(q.poll()); |
664 |
< |
threadsStarted.await(); |
665 |
< |
assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); |
666 |
< |
checkEmpty(q); |
667 |
< |
}}); |
668 |
< |
|
669 |
< |
executor.execute(new CheckedRunnable() { |
670 |
< |
public void realRun() throws InterruptedException { |
671 |
< |
threadsStarted.await(); |
672 |
< |
q.put(new PDelay(1)); |
673 |
< |
}}); |
674 |
< |
|
675 |
< |
joinPool(executor); |
660 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
661 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
662 |
> |
executor.execute(new CheckedRunnable() { |
663 |
> |
public void realRun() throws InterruptedException { |
664 |
> |
assertNull(q.poll()); |
665 |
> |
threadsStarted.await(); |
666 |
> |
assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); |
667 |
> |
checkEmpty(q); |
668 |
> |
}}); |
669 |
> |
|
670 |
> |
executor.execute(new CheckedRunnable() { |
671 |
> |
public void realRun() throws InterruptedException { |
672 |
> |
threadsStarted.await(); |
673 |
> |
q.put(new PDelay(1)); |
674 |
> |
}}); |
675 |
> |
} |
676 |
|
} |
677 |
|
|
678 |
|
/** |
757 |
|
final DelayQueue q = populatedQueue(SIZE); |
758 |
|
Thread t = new Thread(new CheckedRunnable() { |
759 |
|
public void realRun() { |
760 |
< |
q.put(new PDelay(SIZE+1)); |
760 |
> |
q.put(new PDelay(SIZE + 1)); |
761 |
|
}}); |
762 |
|
|
763 |
|
t.start(); |
777 |
|
ArrayList l = new ArrayList(); |
778 |
|
q.drainTo(l, i); |
779 |
|
int k = (i < SIZE) ? i : SIZE; |
780 |
< |
assertEquals(SIZE-k, q.size()); |
780 |
> |
assertEquals(SIZE - k, q.size()); |
781 |
|
assertEquals(k, l.size()); |
782 |
|
} |
783 |
|
} |