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