35394
|
1 |
/*
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
3 |
*
|
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
|
6 |
* published by the Free Software Foundation.
|
|
7 |
*
|
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
12 |
* accompanied this code).
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License version
|
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
17 |
*
|
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
19 |
* or visit www.oracle.com if you need additional information or have any
|
|
20 |
* questions.
|
|
21 |
*/
|
|
22 |
|
|
23 |
/*
|
|
24 |
* This file is available under and governed by the GNU General Public
|
|
25 |
* License version 2 only, as published by the Free Software Foundation.
|
|
26 |
* However, the following notice accompanied the original version of this
|
|
27 |
* file:
|
|
28 |
*
|
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166
|
|
30 |
* Expert Group and released to the public domain, as explained at
|
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/
|
|
32 |
* Other contributors include Andrew Wright, Jeffrey Hayes,
|
|
33 |
* Pat Fisher, Mike Judd.
|
|
34 |
*/
|
|
35 |
|
|
36 |
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
|
37 |
|
|
38 |
import java.util.ArrayList;
|
|
39 |
import java.util.Arrays;
|
|
40 |
import java.util.Collection;
|
|
41 |
import java.util.Iterator;
|
|
42 |
import java.util.NoSuchElementException;
|
|
43 |
import java.util.concurrent.BlockingQueue;
|
|
44 |
import java.util.concurrent.CountDownLatch;
|
|
45 |
import java.util.concurrent.Delayed;
|
|
46 |
import java.util.concurrent.DelayQueue;
|
|
47 |
import java.util.concurrent.Executors;
|
|
48 |
import java.util.concurrent.ExecutorService;
|
|
49 |
import java.util.concurrent.TimeUnit;
|
|
50 |
|
|
51 |
import junit.framework.Test;
|
|
52 |
|
|
53 |
public class DelayQueueTest extends JSR166TestCase {
|
|
54 |
|
|
55 |
public static class Generic extends BlockingQueueTest {
|
|
56 |
protected BlockingQueue emptyCollection() {
|
|
57 |
return new DelayQueue();
|
|
58 |
}
|
|
59 |
protected PDelay makeElement(int i) {
|
|
60 |
return new PDelay(i);
|
|
61 |
}
|
|
62 |
}
|
|
63 |
|
|
64 |
public static void main(String[] args) {
|
|
65 |
main(suite(), args);
|
|
66 |
}
|
|
67 |
|
|
68 |
public static Test suite() {
|
|
69 |
return newTestSuite(DelayQueueTest.class,
|
|
70 |
new Generic().testSuite());
|
|
71 |
}
|
|
72 |
|
|
73 |
/**
|
|
74 |
* A delayed implementation for testing.
|
|
75 |
* Most tests use Pseudodelays, where delays are all elapsed
|
|
76 |
* (so, no blocking solely for delays) but are still ordered
|
|
77 |
*/
|
|
78 |
static class PDelay implements Delayed {
|
|
79 |
int pseudodelay;
|
|
80 |
PDelay(int i) { pseudodelay = i; }
|
|
81 |
public int compareTo(PDelay other) {
|
|
82 |
int a = this.pseudodelay;
|
|
83 |
int b = other.pseudodelay;
|
|
84 |
return (a < b) ? -1 : (a > b) ? 1 : 0;
|
|
85 |
}
|
|
86 |
public int compareTo(Delayed y) {
|
|
87 |
return compareTo((PDelay)y);
|
|
88 |
}
|
|
89 |
public boolean equals(Object other) {
|
|
90 |
return (other instanceof PDelay) &&
|
|
91 |
this.pseudodelay == ((PDelay)other).pseudodelay;
|
|
92 |
}
|
|
93 |
// suppress [overrides] javac warning
|
|
94 |
public int hashCode() { return pseudodelay; }
|
|
95 |
public long getDelay(TimeUnit ignore) {
|
|
96 |
return Integer.MIN_VALUE + pseudodelay;
|
|
97 |
}
|
|
98 |
public String toString() {
|
|
99 |
return String.valueOf(pseudodelay);
|
|
100 |
}
|
|
101 |
}
|
|
102 |
|
|
103 |
/**
|
|
104 |
* Delayed implementation that actually delays
|
|
105 |
*/
|
|
106 |
static class NanoDelay implements Delayed {
|
|
107 |
long trigger;
|
|
108 |
NanoDelay(long i) {
|
|
109 |
trigger = System.nanoTime() + i;
|
|
110 |
}
|
|
111 |
public int compareTo(NanoDelay y) {
|
|
112 |
long i = trigger;
|
|
113 |
long j = y.trigger;
|
|
114 |
if (i < j) return -1;
|
|
115 |
if (i > j) return 1;
|
|
116 |
return 0;
|
|
117 |
}
|
|
118 |
|
|
119 |
public int compareTo(Delayed y) {
|
|
120 |
return compareTo((NanoDelay)y);
|
|
121 |
}
|
|
122 |
|
|
123 |
public boolean equals(Object other) {
|
|
124 |
return equals((NanoDelay)other);
|
|
125 |
}
|
|
126 |
public boolean equals(NanoDelay other) {
|
|
127 |
return other.trigger == trigger;
|
|
128 |
}
|
|
129 |
|
|
130 |
// suppress [overrides] javac warning
|
|
131 |
public int hashCode() { return (int) trigger; }
|
|
132 |
|
|
133 |
public long getDelay(TimeUnit unit) {
|
|
134 |
long n = trigger - System.nanoTime();
|
|
135 |
return unit.convert(n, TimeUnit.NANOSECONDS);
|
|
136 |
}
|
|
137 |
|
|
138 |
public long getTriggerTime() {
|
|
139 |
return trigger;
|
|
140 |
}
|
|
141 |
|
|
142 |
public String toString() {
|
|
143 |
return String.valueOf(trigger);
|
|
144 |
}
|
|
145 |
}
|
|
146 |
|
|
147 |
/**
|
|
148 |
* Returns a new queue of given size containing consecutive
|
|
149 |
* PDelays 0 ... n.
|
|
150 |
*/
|
|
151 |
private DelayQueue<PDelay> populatedQueue(int n) {
|
|
152 |
DelayQueue<PDelay> q = new DelayQueue<PDelay>();
|
|
153 |
assertTrue(q.isEmpty());
|
|
154 |
for (int i = n - 1; i >= 0; i -= 2)
|
|
155 |
assertTrue(q.offer(new PDelay(i)));
|
|
156 |
for (int i = (n & 1); i < n; i += 2)
|
|
157 |
assertTrue(q.offer(new PDelay(i)));
|
|
158 |
assertFalse(q.isEmpty());
|
|
159 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
|
|
160 |
assertEquals(n, q.size());
|
|
161 |
return q;
|
|
162 |
}
|
|
163 |
|
|
164 |
/**
|
|
165 |
* A new queue has unbounded capacity
|
|
166 |
*/
|
|
167 |
public void testConstructor1() {
|
|
168 |
assertEquals(Integer.MAX_VALUE, new DelayQueue().remainingCapacity());
|
|
169 |
}
|
|
170 |
|
|
171 |
/**
|
|
172 |
* Initializing from null Collection throws NPE
|
|
173 |
*/
|
|
174 |
public void testConstructor3() {
|
|
175 |
try {
|
|
176 |
new DelayQueue(null);
|
|
177 |
shouldThrow();
|
|
178 |
} catch (NullPointerException success) {}
|
|
179 |
}
|
|
180 |
|
|
181 |
/**
|
|
182 |
* Initializing from Collection of null elements throws NPE
|
|
183 |
*/
|
|
184 |
public void testConstructor4() {
|
|
185 |
try {
|
|
186 |
new DelayQueue(Arrays.asList(new PDelay[SIZE]));
|
|
187 |
shouldThrow();
|
|
188 |
} catch (NullPointerException success) {}
|
|
189 |
}
|
|
190 |
|
|
191 |
/**
|
|
192 |
* Initializing from Collection with some null elements throws NPE
|
|
193 |
*/
|
|
194 |
public void testConstructor5() {
|
|
195 |
PDelay[] a = new PDelay[SIZE];
|
|
196 |
for (int i = 0; i < SIZE - 1; ++i)
|
|
197 |
a[i] = new PDelay(i);
|
|
198 |
try {
|
|
199 |
new DelayQueue(Arrays.asList(a));
|
|
200 |
shouldThrow();
|
|
201 |
} catch (NullPointerException success) {}
|
|
202 |
}
|
|
203 |
|
|
204 |
/**
|
|
205 |
* Queue contains all elements of collection used to initialize
|
|
206 |
*/
|
|
207 |
public void testConstructor6() {
|
|
208 |
PDelay[] ints = new PDelay[SIZE];
|
|
209 |
for (int i = 0; i < SIZE; ++i)
|
|
210 |
ints[i] = new PDelay(i);
|
|
211 |
DelayQueue q = new DelayQueue(Arrays.asList(ints));
|
|
212 |
for (int i = 0; i < SIZE; ++i)
|
|
213 |
assertEquals(ints[i], q.poll());
|
|
214 |
}
|
|
215 |
|
|
216 |
/**
|
|
217 |
* isEmpty is true before add, false after
|
|
218 |
*/
|
|
219 |
public void testEmpty() {
|
|
220 |
DelayQueue q = new DelayQueue();
|
|
221 |
assertTrue(q.isEmpty());
|
|
222 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
|
|
223 |
q.add(new PDelay(1));
|
|
224 |
assertFalse(q.isEmpty());
|
|
225 |
q.add(new PDelay(2));
|
|
226 |
q.remove();
|
|
227 |
q.remove();
|
|
228 |
assertTrue(q.isEmpty());
|
|
229 |
}
|
|
230 |
|
|
231 |
/**
|
|
232 |
* remainingCapacity() always returns Integer.MAX_VALUE
|
|
233 |
*/
|
|
234 |
public void testRemainingCapacity() {
|
|
235 |
BlockingQueue q = populatedQueue(SIZE);
|
|
236 |
for (int i = 0; i < SIZE; ++i) {
|
|
237 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
|
|
238 |
assertEquals(SIZE - i, q.size());
|
|
239 |
assertTrue(q.remove() instanceof PDelay);
|
|
240 |
}
|
|
241 |
for (int i = 0; i < SIZE; ++i) {
|
|
242 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
|
|
243 |
assertEquals(i, q.size());
|
|
244 |
assertTrue(q.add(new PDelay(i)));
|
|
245 |
}
|
|
246 |
}
|
|
247 |
|
|
248 |
/**
|
|
249 |
* offer non-null succeeds
|
|
250 |
*/
|
|
251 |
public void testOffer() {
|
|
252 |
DelayQueue q = new DelayQueue();
|
|
253 |
assertTrue(q.offer(new PDelay(0)));
|
|
254 |
assertTrue(q.offer(new PDelay(1)));
|
|
255 |
}
|
|
256 |
|
|
257 |
/**
|
|
258 |
* add succeeds
|
|
259 |
*/
|
|
260 |
public void testAdd() {
|
|
261 |
DelayQueue q = new DelayQueue();
|
|
262 |
for (int i = 0; i < SIZE; ++i) {
|
|
263 |
assertEquals(i, q.size());
|
|
264 |
assertTrue(q.add(new PDelay(i)));
|
|
265 |
}
|
|
266 |
}
|
|
267 |
|
|
268 |
/**
|
|
269 |
* addAll(this) throws IAE
|
|
270 |
*/
|
|
271 |
public void testAddAllSelf() {
|
|
272 |
DelayQueue q = populatedQueue(SIZE);
|
|
273 |
try {
|
|
274 |
q.addAll(q);
|
|
275 |
shouldThrow();
|
|
276 |
} catch (IllegalArgumentException success) {}
|
|
277 |
}
|
|
278 |
|
|
279 |
/**
|
|
280 |
* addAll of a collection with any null elements throws NPE after
|
|
281 |
* possibly adding some elements
|
|
282 |
*/
|
|
283 |
public void testAddAll3() {
|
|
284 |
DelayQueue q = new DelayQueue();
|
|
285 |
PDelay[] a = new PDelay[SIZE];
|
|
286 |
for (int i = 0; i < SIZE - 1; ++i)
|
|
287 |
a[i] = new PDelay(i);
|
|
288 |
try {
|
|
289 |
q.addAll(Arrays.asList(a));
|
|
290 |
shouldThrow();
|
|
291 |
} catch (NullPointerException success) {}
|
|
292 |
}
|
|
293 |
|
|
294 |
/**
|
|
295 |
* Queue contains all elements of successful addAll
|
|
296 |
*/
|
|
297 |
public void testAddAll5() {
|
|
298 |
PDelay[] empty = new PDelay[0];
|
|
299 |
PDelay[] ints = new PDelay[SIZE];
|
|
300 |
for (int i = SIZE - 1; i >= 0; --i)
|
|
301 |
ints[i] = new PDelay(i);
|
|
302 |
DelayQueue q = new DelayQueue();
|
|
303 |
assertFalse(q.addAll(Arrays.asList(empty)));
|
|
304 |
assertTrue(q.addAll(Arrays.asList(ints)));
|
|
305 |
for (int i = 0; i < SIZE; ++i)
|
|
306 |
assertEquals(ints[i], q.poll());
|
|
307 |
}
|
|
308 |
|
|
309 |
/**
|
|
310 |
* all elements successfully put are contained
|
|
311 |
*/
|
|
312 |
public void testPut() {
|
|
313 |
DelayQueue q = new DelayQueue();
|
|
314 |
for (int i = 0; i < SIZE; ++i) {
|
|
315 |
PDelay x = new PDelay(i);
|
|
316 |
q.put(x);
|
|
317 |
assertTrue(q.contains(x));
|
|
318 |
}
|
|
319 |
assertEquals(SIZE, q.size());
|
|
320 |
}
|
|
321 |
|
|
322 |
/**
|
|
323 |
* put doesn't block waiting for take
|
|
324 |
*/
|
|
325 |
public void testPutWithTake() throws InterruptedException {
|
|
326 |
final DelayQueue q = new DelayQueue();
|
|
327 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
328 |
public void realRun() {
|
|
329 |
q.put(new PDelay(0));
|
|
330 |
q.put(new PDelay(0));
|
|
331 |
q.put(new PDelay(0));
|
|
332 |
q.put(new PDelay(0));
|
|
333 |
}});
|
|
334 |
|
|
335 |
awaitTermination(t);
|
|
336 |
assertEquals(4, q.size());
|
|
337 |
}
|
|
338 |
|
|
339 |
/**
|
|
340 |
* timed offer does not time out
|
|
341 |
*/
|
|
342 |
public void testTimedOffer() throws InterruptedException {
|
|
343 |
final DelayQueue q = new DelayQueue();
|
|
344 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
345 |
public void realRun() throws InterruptedException {
|
|
346 |
q.put(new PDelay(0));
|
|
347 |
q.put(new PDelay(0));
|
|
348 |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS));
|
|
349 |
assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS));
|
|
350 |
}});
|
|
351 |
|
|
352 |
awaitTermination(t);
|
|
353 |
}
|
|
354 |
|
|
355 |
/**
|
|
356 |
* take retrieves elements in priority order
|
|
357 |
*/
|
|
358 |
public void testTake() throws InterruptedException {
|
|
359 |
DelayQueue q = populatedQueue(SIZE);
|
|
360 |
for (int i = 0; i < SIZE; ++i) {
|
|
361 |
assertEquals(new PDelay(i), q.take());
|
|
362 |
}
|
|
363 |
}
|
|
364 |
|
|
365 |
/**
|
|
366 |
* Take removes existing elements until empty, then blocks interruptibly
|
|
367 |
*/
|
|
368 |
public void testBlockingTake() throws InterruptedException {
|
|
369 |
final DelayQueue q = populatedQueue(SIZE);
|
|
370 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
|
|
371 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
372 |
public void realRun() throws InterruptedException {
|
|
373 |
for (int i = 0; i < SIZE; ++i) {
|
|
374 |
assertEquals(new PDelay(i), ((PDelay)q.take()));
|
|
375 |
}
|
|
376 |
|
|
377 |
Thread.currentThread().interrupt();
|
|
378 |
try {
|
|
379 |
q.take();
|
|
380 |
shouldThrow();
|
|
381 |
} catch (InterruptedException success) {}
|
|
382 |
assertFalse(Thread.interrupted());
|
|
383 |
|
|
384 |
pleaseInterrupt.countDown();
|
|
385 |
try {
|
|
386 |
q.take();
|
|
387 |
shouldThrow();
|
|
388 |
} catch (InterruptedException success) {}
|
|
389 |
assertFalse(Thread.interrupted());
|
|
390 |
}});
|
|
391 |
|
|
392 |
await(pleaseInterrupt);
|
|
393 |
assertThreadStaysAlive(t);
|
|
394 |
t.interrupt();
|
|
395 |
awaitTermination(t);
|
|
396 |
}
|
|
397 |
|
|
398 |
/**
|
|
399 |
* poll succeeds unless empty
|
|
400 |
*/
|
|
401 |
public void testPoll() {
|
|
402 |
DelayQueue q = populatedQueue(SIZE);
|
|
403 |
for (int i = 0; i < SIZE; ++i) {
|
|
404 |
assertEquals(new PDelay(i), q.poll());
|
|
405 |
}
|
|
406 |
assertNull(q.poll());
|
|
407 |
}
|
|
408 |
|
|
409 |
/**
|
|
410 |
* timed poll with zero timeout succeeds when non-empty, else times out
|
|
411 |
*/
|
|
412 |
public void testTimedPoll0() throws InterruptedException {
|
|
413 |
DelayQueue q = populatedQueue(SIZE);
|
|
414 |
for (int i = 0; i < SIZE; ++i) {
|
|
415 |
assertEquals(new PDelay(i), q.poll(0, MILLISECONDS));
|
|
416 |
}
|
|
417 |
assertNull(q.poll(0, MILLISECONDS));
|
|
418 |
}
|
|
419 |
|
|
420 |
/**
|
|
421 |
* timed poll with nonzero timeout succeeds when non-empty, else times out
|
|
422 |
*/
|
|
423 |
public void testTimedPoll() throws InterruptedException {
|
|
424 |
DelayQueue q = populatedQueue(SIZE);
|
|
425 |
for (int i = 0; i < SIZE; ++i) {
|
|
426 |
long startTime = System.nanoTime();
|
|
427 |
assertEquals(new PDelay(i), q.poll(LONG_DELAY_MS, MILLISECONDS));
|
|
428 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
|
|
429 |
}
|
|
430 |
long startTime = System.nanoTime();
|
|
431 |
assertNull(q.poll(timeoutMillis(), MILLISECONDS));
|
|
432 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
|
|
433 |
checkEmpty(q);
|
|
434 |
}
|
|
435 |
|
|
436 |
/**
|
|
437 |
* Interrupted timed poll throws InterruptedException instead of
|
|
438 |
* returning timeout status
|
|
439 |
*/
|
|
440 |
public void testInterruptedTimedPoll() throws InterruptedException {
|
|
441 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
|
|
442 |
final DelayQueue q = populatedQueue(SIZE);
|
|
443 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
444 |
public void realRun() throws InterruptedException {
|
|
445 |
long startTime = System.nanoTime();
|
|
446 |
for (int i = 0; i < SIZE; ++i) {
|
|
447 |
assertEquals(new PDelay(i),
|
|
448 |
((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS)));
|
|
449 |
}
|
|
450 |
|
|
451 |
Thread.currentThread().interrupt();
|
|
452 |
try {
|
|
453 |
q.poll(LONG_DELAY_MS, MILLISECONDS);
|
|
454 |
shouldThrow();
|
|
455 |
} catch (InterruptedException success) {}
|
|
456 |
assertFalse(Thread.interrupted());
|
|
457 |
|
|
458 |
pleaseInterrupt.countDown();
|
|
459 |
try {
|
|
460 |
q.poll(LONG_DELAY_MS, MILLISECONDS);
|
|
461 |
shouldThrow();
|
|
462 |
} catch (InterruptedException success) {}
|
|
463 |
assertFalse(Thread.interrupted());
|
|
464 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
|
|
465 |
}});
|
|
466 |
|
|
467 |
await(pleaseInterrupt);
|
|
468 |
assertThreadStaysAlive(t);
|
|
469 |
t.interrupt();
|
|
470 |
awaitTermination(t);
|
|
471 |
checkEmpty(q);
|
|
472 |
}
|
|
473 |
|
|
474 |
/**
|
|
475 |
* peek returns next element, or null if empty
|
|
476 |
*/
|
|
477 |
public void testPeek() {
|
|
478 |
DelayQueue q = populatedQueue(SIZE);
|
|
479 |
for (int i = 0; i < SIZE; ++i) {
|
|
480 |
assertEquals(new PDelay(i), q.peek());
|
|
481 |
assertEquals(new PDelay(i), q.poll());
|
|
482 |
if (q.isEmpty())
|
|
483 |
assertNull(q.peek());
|
|
484 |
else
|
|
485 |
assertFalse(new PDelay(i).equals(q.peek()));
|
|
486 |
}
|
|
487 |
assertNull(q.peek());
|
|
488 |
}
|
|
489 |
|
|
490 |
/**
|
|
491 |
* element returns next element, or throws NSEE if empty
|
|
492 |
*/
|
|
493 |
public void testElement() {
|
|
494 |
DelayQueue q = populatedQueue(SIZE);
|
|
495 |
for (int i = 0; i < SIZE; ++i) {
|
|
496 |
assertEquals(new PDelay(i), q.element());
|
|
497 |
q.poll();
|
|
498 |
}
|
|
499 |
try {
|
|
500 |
q.element();
|
|
501 |
shouldThrow();
|
|
502 |
} catch (NoSuchElementException success) {}
|
|
503 |
}
|
|
504 |
|
|
505 |
/**
|
|
506 |
* remove removes next element, or throws NSEE if empty
|
|
507 |
*/
|
|
508 |
public void testRemove() {
|
|
509 |
DelayQueue q = populatedQueue(SIZE);
|
|
510 |
for (int i = 0; i < SIZE; ++i) {
|
|
511 |
assertEquals(new PDelay(i), q.remove());
|
|
512 |
}
|
|
513 |
try {
|
|
514 |
q.remove();
|
|
515 |
shouldThrow();
|
|
516 |
} catch (NoSuchElementException success) {}
|
|
517 |
}
|
|
518 |
|
|
519 |
/**
|
|
520 |
* contains(x) reports true when elements added but not yet removed
|
|
521 |
*/
|
|
522 |
public void testContains() {
|
|
523 |
DelayQueue q = populatedQueue(SIZE);
|
|
524 |
for (int i = 0; i < SIZE; ++i) {
|
|
525 |
assertTrue(q.contains(new PDelay(i)));
|
|
526 |
q.poll();
|
|
527 |
assertFalse(q.contains(new PDelay(i)));
|
|
528 |
}
|
|
529 |
}
|
|
530 |
|
|
531 |
/**
|
|
532 |
* clear removes all elements
|
|
533 |
*/
|
|
534 |
public void testClear() {
|
|
535 |
DelayQueue q = populatedQueue(SIZE);
|
|
536 |
q.clear();
|
|
537 |
assertTrue(q.isEmpty());
|
|
538 |
assertEquals(0, q.size());
|
|
539 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
|
|
540 |
PDelay x = new PDelay(1);
|
|
541 |
q.add(x);
|
|
542 |
assertFalse(q.isEmpty());
|
|
543 |
assertTrue(q.contains(x));
|
|
544 |
q.clear();
|
|
545 |
assertTrue(q.isEmpty());
|
|
546 |
}
|
|
547 |
|
|
548 |
/**
|
|
549 |
* containsAll(c) is true when c contains a subset of elements
|
|
550 |
*/
|
|
551 |
public void testContainsAll() {
|
|
552 |
DelayQueue q = populatedQueue(SIZE);
|
|
553 |
DelayQueue p = new DelayQueue();
|
|
554 |
for (int i = 0; i < SIZE; ++i) {
|
|
555 |
assertTrue(q.containsAll(p));
|
|
556 |
assertFalse(p.containsAll(q));
|
|
557 |
p.add(new PDelay(i));
|
|
558 |
}
|
|
559 |
assertTrue(p.containsAll(q));
|
|
560 |
}
|
|
561 |
|
|
562 |
/**
|
|
563 |
* retainAll(c) retains only those elements of c and reports true if changed
|
|
564 |
*/
|
|
565 |
public void testRetainAll() {
|
|
566 |
DelayQueue q = populatedQueue(SIZE);
|
|
567 |
DelayQueue p = populatedQueue(SIZE);
|
|
568 |
for (int i = 0; i < SIZE; ++i) {
|
|
569 |
boolean changed = q.retainAll(p);
|
|
570 |
if (i == 0)
|
|
571 |
assertFalse(changed);
|
|
572 |
else
|
|
573 |
assertTrue(changed);
|
|
574 |
|
|
575 |
assertTrue(q.containsAll(p));
|
|
576 |
assertEquals(SIZE - i, q.size());
|
|
577 |
p.remove();
|
|
578 |
}
|
|
579 |
}
|
|
580 |
|
|
581 |
/**
|
|
582 |
* removeAll(c) removes only those elements of c and reports true if changed
|
|
583 |
*/
|
|
584 |
public void testRemoveAll() {
|
|
585 |
for (int i = 1; i < SIZE; ++i) {
|
|
586 |
DelayQueue q = populatedQueue(SIZE);
|
|
587 |
DelayQueue p = populatedQueue(i);
|
|
588 |
assertTrue(q.removeAll(p));
|
|
589 |
assertEquals(SIZE - i, q.size());
|
|
590 |
for (int j = 0; j < i; ++j) {
|
|
591 |
PDelay x = (PDelay)(p.remove());
|
|
592 |
assertFalse(q.contains(x));
|
|
593 |
}
|
|
594 |
}
|
|
595 |
}
|
|
596 |
|
|
597 |
/**
|
|
598 |
* toArray contains all elements
|
|
599 |
*/
|
|
600 |
public void testToArray() throws InterruptedException {
|
|
601 |
DelayQueue q = populatedQueue(SIZE);
|
|
602 |
Object[] o = q.toArray();
|
|
603 |
Arrays.sort(o);
|
|
604 |
for (int i = 0; i < o.length; i++)
|
|
605 |
assertSame(o[i], q.take());
|
|
606 |
}
|
|
607 |
|
|
608 |
/**
|
|
609 |
* toArray(a) contains all elements
|
|
610 |
*/
|
|
611 |
public void testToArray2() {
|
|
612 |
DelayQueue<PDelay> q = populatedQueue(SIZE);
|
|
613 |
PDelay[] ints = new PDelay[SIZE];
|
|
614 |
PDelay[] array = q.toArray(ints);
|
|
615 |
assertSame(ints, array);
|
|
616 |
Arrays.sort(ints);
|
|
617 |
for (int i = 0; i < ints.length; i++)
|
|
618 |
assertSame(ints[i], q.remove());
|
|
619 |
}
|
|
620 |
|
|
621 |
/**
|
|
622 |
* toArray(incompatible array type) throws ArrayStoreException
|
|
623 |
*/
|
|
624 |
public void testToArray1_BadArg() {
|
|
625 |
DelayQueue q = populatedQueue(SIZE);
|
|
626 |
try {
|
|
627 |
q.toArray(new String[10]);
|
|
628 |
shouldThrow();
|
|
629 |
} catch (ArrayStoreException success) {}
|
|
630 |
}
|
|
631 |
|
|
632 |
/**
|
|
633 |
* iterator iterates through all elements
|
|
634 |
*/
|
|
635 |
public void testIterator() {
|
|
636 |
DelayQueue q = populatedQueue(SIZE);
|
|
637 |
int i = 0;
|
|
638 |
Iterator it = q.iterator();
|
|
639 |
while (it.hasNext()) {
|
|
640 |
assertTrue(q.contains(it.next()));
|
|
641 |
++i;
|
|
642 |
}
|
|
643 |
assertEquals(i, SIZE);
|
|
644 |
assertIteratorExhausted(it);
|
|
645 |
}
|
|
646 |
|
|
647 |
/**
|
|
648 |
* iterator of empty collection has no elements
|
|
649 |
*/
|
|
650 |
public void testEmptyIterator() {
|
|
651 |
assertIteratorExhausted(new DelayQueue().iterator());
|
|
652 |
}
|
|
653 |
|
|
654 |
/**
|
|
655 |
* iterator.remove removes current element
|
|
656 |
*/
|
|
657 |
public void testIteratorRemove() {
|
|
658 |
final DelayQueue q = new DelayQueue();
|
|
659 |
q.add(new PDelay(2));
|
|
660 |
q.add(new PDelay(1));
|
|
661 |
q.add(new PDelay(3));
|
|
662 |
Iterator it = q.iterator();
|
|
663 |
it.next();
|
|
664 |
it.remove();
|
|
665 |
it = q.iterator();
|
|
666 |
assertEquals(new PDelay(2), it.next());
|
|
667 |
assertEquals(new PDelay(3), it.next());
|
|
668 |
assertFalse(it.hasNext());
|
|
669 |
}
|
|
670 |
|
|
671 |
/**
|
|
672 |
* toString contains toStrings of elements
|
|
673 |
*/
|
|
674 |
public void testToString() {
|
|
675 |
DelayQueue q = populatedQueue(SIZE);
|
|
676 |
String s = q.toString();
|
|
677 |
for (Object e : q)
|
|
678 |
assertTrue(s.contains(e.toString()));
|
|
679 |
}
|
|
680 |
|
|
681 |
/**
|
|
682 |
* timed poll transfers elements across Executor tasks
|
|
683 |
*/
|
|
684 |
public void testPollInExecutor() {
|
|
685 |
final DelayQueue q = new DelayQueue();
|
|
686 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
|
|
687 |
final ExecutorService executor = Executors.newFixedThreadPool(2);
|
|
688 |
try (PoolCleaner cleaner = cleaner(executor)) {
|
|
689 |
executor.execute(new CheckedRunnable() {
|
|
690 |
public void realRun() throws InterruptedException {
|
|
691 |
assertNull(q.poll());
|
|
692 |
threadsStarted.await();
|
|
693 |
assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS));
|
|
694 |
checkEmpty(q);
|
|
695 |
}});
|
|
696 |
|
|
697 |
executor.execute(new CheckedRunnable() {
|
|
698 |
public void realRun() throws InterruptedException {
|
|
699 |
threadsStarted.await();
|
|
700 |
q.put(new PDelay(1));
|
|
701 |
}});
|
|
702 |
}
|
|
703 |
}
|
|
704 |
|
|
705 |
/**
|
|
706 |
* Delayed actions do not occur until their delay elapses
|
|
707 |
*/
|
|
708 |
public void testDelay() throws InterruptedException {
|
|
709 |
DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>();
|
|
710 |
for (int i = 0; i < SIZE; ++i)
|
|
711 |
q.add(new NanoDelay(1000000L * (SIZE - i)));
|
|
712 |
|
|
713 |
long last = 0;
|
|
714 |
for (int i = 0; i < SIZE; ++i) {
|
|
715 |
NanoDelay e = q.take();
|
|
716 |
long tt = e.getTriggerTime();
|
|
717 |
assertTrue(System.nanoTime() - tt >= 0);
|
|
718 |
if (i != 0)
|
|
719 |
assertTrue(tt >= last);
|
|
720 |
last = tt;
|
|
721 |
}
|
|
722 |
assertTrue(q.isEmpty());
|
|
723 |
}
|
|
724 |
|
|
725 |
/**
|
|
726 |
* peek of a non-empty queue returns non-null even if not expired
|
|
727 |
*/
|
|
728 |
public void testPeekDelayed() {
|
|
729 |
DelayQueue q = new DelayQueue();
|
|
730 |
q.add(new NanoDelay(Long.MAX_VALUE));
|
|
731 |
assertNotNull(q.peek());
|
|
732 |
}
|
|
733 |
|
|
734 |
/**
|
|
735 |
* poll of a non-empty queue returns null if no expired elements.
|
|
736 |
*/
|
|
737 |
public void testPollDelayed() {
|
|
738 |
DelayQueue q = new DelayQueue();
|
|
739 |
q.add(new NanoDelay(Long.MAX_VALUE));
|
|
740 |
assertNull(q.poll());
|
|
741 |
}
|
|
742 |
|
|
743 |
/**
|
|
744 |
* timed poll of a non-empty queue returns null if no expired elements.
|
|
745 |
*/
|
|
746 |
public void testTimedPollDelayed() throws InterruptedException {
|
|
747 |
DelayQueue q = new DelayQueue();
|
|
748 |
q.add(new NanoDelay(LONG_DELAY_MS * 1000000L));
|
|
749 |
assertNull(q.poll(timeoutMillis(), MILLISECONDS));
|
|
750 |
}
|
|
751 |
|
|
752 |
/**
|
|
753 |
* drainTo(c) empties queue into another collection c
|
|
754 |
*/
|
|
755 |
public void testDrainTo() {
|
|
756 |
DelayQueue q = new DelayQueue();
|
|
757 |
PDelay[] elems = new PDelay[SIZE];
|
|
758 |
for (int i = 0; i < SIZE; ++i) {
|
|
759 |
elems[i] = new PDelay(i);
|
|
760 |
q.add(elems[i]);
|
|
761 |
}
|
|
762 |
ArrayList l = new ArrayList();
|
|
763 |
q.drainTo(l);
|
|
764 |
assertEquals(0, q.size());
|
|
765 |
for (int i = 0; i < SIZE; ++i)
|
|
766 |
assertEquals(elems[i], l.get(i));
|
|
767 |
q.add(elems[0]);
|
|
768 |
q.add(elems[1]);
|
|
769 |
assertFalse(q.isEmpty());
|
|
770 |
assertTrue(q.contains(elems[0]));
|
|
771 |
assertTrue(q.contains(elems[1]));
|
|
772 |
l.clear();
|
|
773 |
q.drainTo(l);
|
|
774 |
assertEquals(0, q.size());
|
|
775 |
assertEquals(2, l.size());
|
|
776 |
for (int i = 0; i < 2; ++i)
|
|
777 |
assertEquals(elems[i], l.get(i));
|
|
778 |
}
|
|
779 |
|
|
780 |
/**
|
|
781 |
* drainTo empties queue
|
|
782 |
*/
|
|
783 |
public void testDrainToWithActivePut() throws InterruptedException {
|
|
784 |
final DelayQueue q = populatedQueue(SIZE);
|
|
785 |
Thread t = new Thread(new CheckedRunnable() {
|
|
786 |
public void realRun() {
|
|
787 |
q.put(new PDelay(SIZE + 1));
|
|
788 |
}});
|
|
789 |
|
|
790 |
t.start();
|
|
791 |
ArrayList l = new ArrayList();
|
|
792 |
q.drainTo(l);
|
|
793 |
assertTrue(l.size() >= SIZE);
|
|
794 |
t.join();
|
|
795 |
assertTrue(q.size() + l.size() >= SIZE);
|
|
796 |
}
|
|
797 |
|
|
798 |
/**
|
|
799 |
* drainTo(c, n) empties first min(n, size) elements of queue into c
|
|
800 |
*/
|
|
801 |
public void testDrainToN() {
|
|
802 |
for (int i = 0; i < SIZE + 2; ++i) {
|
|
803 |
DelayQueue q = populatedQueue(SIZE);
|
|
804 |
ArrayList l = new ArrayList();
|
|
805 |
q.drainTo(l, i);
|
|
806 |
int k = (i < SIZE) ? i : SIZE;
|
|
807 |
assertEquals(SIZE - k, q.size());
|
|
808 |
assertEquals(k, l.size());
|
|
809 |
}
|
|
810 |
}
|
|
811 |
|
|
812 |
/**
|
|
813 |
* remove(null), contains(null) always return false
|
|
814 |
*/
|
|
815 |
public void testNeverContainsNull() {
|
|
816 |
Collection<?> q = populatedQueue(SIZE);
|
|
817 |
assertFalse(q.contains(null));
|
|
818 |
assertFalse(q.remove(null));
|
|
819 |
}
|
|
820 |
}
|