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.55 |
return 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 |
dl |
1.6 |
* addAll(this) throws IAE |
238 |
|
|
*/ |
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.20 |
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.20 |
} |
417 |
jsr166 |
1.50 |
|
418 |
|
|
Thread.currentThread().interrupt(); |
419 |
|
|
try { |
420 |
|
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
421 |
|
|
shouldThrow(); |
422 |
|
|
} catch (InterruptedException success) {} |
423 |
|
|
assertFalse(Thread.interrupted()); |
424 |
|
|
|
425 |
|
|
pleaseInterrupt.countDown(); |
426 |
jsr166 |
1.20 |
try { |
427 |
jsr166 |
1.50 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
428 |
jsr166 |
1.20 |
shouldThrow(); |
429 |
|
|
} catch (InterruptedException success) {} |
430 |
jsr166 |
1.50 |
assertFalse(Thread.interrupted()); |
431 |
jsr166 |
1.86 |
|
432 |
jsr166 |
1.76 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
433 |
jsr166 |
1.20 |
}}); |
434 |
|
|
|
435 |
jsr166 |
1.50 |
await(pleaseInterrupt); |
436 |
jsr166 |
1.85 |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
437 |
jsr166 |
1.20 |
t.interrupt(); |
438 |
jsr166 |
1.50 |
awaitTermination(t); |
439 |
jsr166 |
1.76 |
checkEmpty(q); |
440 |
dl |
1.1 |
} |
441 |
|
|
|
442 |
dl |
1.4 |
/** |
443 |
dl |
1.5 |
* peek returns next element, or null if empty |
444 |
dl |
1.4 |
*/ |
445 |
|
|
public void testPeek() { |
446 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
447 |
|
|
for (int i = 0; i < SIZE; ++i) { |
448 |
jsr166 |
1.67 |
assertEquals(new PDelay(i), q.peek()); |
449 |
|
|
assertEquals(new PDelay(i), q.poll()); |
450 |
dl |
1.12 |
if (q.isEmpty()) |
451 |
|
|
assertNull(q.peek()); |
452 |
|
|
else |
453 |
jsr166 |
1.26 |
assertFalse(new PDelay(i).equals(q.peek())); |
454 |
dl |
1.1 |
} |
455 |
jsr166 |
1.18 |
assertNull(q.peek()); |
456 |
dl |
1.1 |
} |
457 |
|
|
|
458 |
dl |
1.4 |
/** |
459 |
dl |
1.5 |
* element returns next element, or throws NSEE if empty |
460 |
dl |
1.4 |
*/ |
461 |
|
|
public void testElement() { |
462 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
463 |
|
|
for (int i = 0; i < SIZE; ++i) { |
464 |
jsr166 |
1.67 |
assertEquals(new PDelay(i), q.element()); |
465 |
dl |
1.1 |
q.poll(); |
466 |
|
|
} |
467 |
|
|
try { |
468 |
|
|
q.element(); |
469 |
dl |
1.4 |
shouldThrow(); |
470 |
jsr166 |
1.20 |
} catch (NoSuchElementException success) {} |
471 |
dl |
1.1 |
} |
472 |
|
|
|
473 |
dl |
1.4 |
/** |
474 |
dl |
1.5 |
* remove removes next element, or throws NSEE if empty |
475 |
dl |
1.4 |
*/ |
476 |
|
|
public void testRemove() { |
477 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
478 |
|
|
for (int i = 0; i < SIZE; ++i) { |
479 |
jsr166 |
1.67 |
assertEquals(new PDelay(i), q.remove()); |
480 |
dl |
1.1 |
} |
481 |
|
|
try { |
482 |
|
|
q.remove(); |
483 |
dl |
1.4 |
shouldThrow(); |
484 |
jsr166 |
1.20 |
} catch (NoSuchElementException success) {} |
485 |
dl |
1.1 |
} |
486 |
|
|
|
487 |
dl |
1.4 |
/** |
488 |
dl |
1.5 |
* contains(x) reports true when elements added but not yet removed |
489 |
dl |
1.4 |
*/ |
490 |
|
|
public void testContains() { |
491 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
492 |
|
|
for (int i = 0; i < SIZE; ++i) { |
493 |
dl |
1.1 |
assertTrue(q.contains(new PDelay(i))); |
494 |
|
|
q.poll(); |
495 |
|
|
assertFalse(q.contains(new PDelay(i))); |
496 |
|
|
} |
497 |
|
|
} |
498 |
|
|
|
499 |
dl |
1.4 |
/** |
500 |
dl |
1.5 |
* clear removes all elements |
501 |
dl |
1.4 |
*/ |
502 |
|
|
public void testClear() { |
503 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
504 |
dl |
1.1 |
q.clear(); |
505 |
|
|
assertTrue(q.isEmpty()); |
506 |
|
|
assertEquals(0, q.size()); |
507 |
jsr166 |
1.70 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
508 |
dl |
1.11 |
PDelay x = new PDelay(1); |
509 |
|
|
q.add(x); |
510 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
511 |
dl |
1.11 |
assertTrue(q.contains(x)); |
512 |
dl |
1.1 |
q.clear(); |
513 |
|
|
assertTrue(q.isEmpty()); |
514 |
|
|
} |
515 |
|
|
|
516 |
dl |
1.4 |
/** |
517 |
dl |
1.5 |
* containsAll(c) is true when c contains a subset of elements |
518 |
dl |
1.4 |
*/ |
519 |
|
|
public void testContainsAll() { |
520 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
521 |
dl |
1.1 |
DelayQueue p = new DelayQueue(); |
522 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
523 |
dl |
1.1 |
assertTrue(q.containsAll(p)); |
524 |
|
|
assertFalse(p.containsAll(q)); |
525 |
|
|
p.add(new PDelay(i)); |
526 |
|
|
} |
527 |
|
|
assertTrue(p.containsAll(q)); |
528 |
|
|
} |
529 |
|
|
|
530 |
dl |
1.4 |
/** |
531 |
dl |
1.5 |
* retainAll(c) retains only those elements of c and reports true if changed |
532 |
dl |
1.4 |
*/ |
533 |
|
|
public void testRetainAll() { |
534 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
535 |
|
|
DelayQueue p = populatedQueue(SIZE); |
536 |
|
|
for (int i = 0; i < SIZE; ++i) { |
537 |
dl |
1.1 |
boolean changed = q.retainAll(p); |
538 |
|
|
if (i == 0) |
539 |
|
|
assertFalse(changed); |
540 |
|
|
else |
541 |
|
|
assertTrue(changed); |
542 |
|
|
|
543 |
|
|
assertTrue(q.containsAll(p)); |
544 |
jsr166 |
1.73 |
assertEquals(SIZE - i, q.size()); |
545 |
dl |
1.1 |
p.remove(); |
546 |
|
|
} |
547 |
|
|
} |
548 |
|
|
|
549 |
dl |
1.4 |
/** |
550 |
dl |
1.5 |
* removeAll(c) removes only those elements of c and reports true if changed |
551 |
dl |
1.4 |
*/ |
552 |
|
|
public void testRemoveAll() { |
553 |
dl |
1.3 |
for (int i = 1; i < SIZE; ++i) { |
554 |
|
|
DelayQueue q = populatedQueue(SIZE); |
555 |
|
|
DelayQueue p = populatedQueue(i); |
556 |
dl |
1.1 |
assertTrue(q.removeAll(p)); |
557 |
jsr166 |
1.73 |
assertEquals(SIZE - i, q.size()); |
558 |
dl |
1.1 |
for (int j = 0; j < i; ++j) { |
559 |
jsr166 |
1.66 |
PDelay x = (PDelay)(p.remove()); |
560 |
|
|
assertFalse(q.contains(x)); |
561 |
dl |
1.1 |
} |
562 |
|
|
} |
563 |
|
|
} |
564 |
|
|
|
565 |
dl |
1.4 |
/** |
566 |
dl |
1.5 |
* toArray contains all elements |
567 |
dl |
1.4 |
*/ |
568 |
jsr166 |
1.20 |
public void testToArray() throws InterruptedException { |
569 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
570 |
jsr166 |
1.18 |
Object[] o = q.toArray(); |
571 |
dl |
1.1 |
Arrays.sort(o); |
572 |
jsr166 |
1.18 |
for (int i = 0; i < o.length; i++) |
573 |
jsr166 |
1.44 |
assertSame(o[i], q.take()); |
574 |
dl |
1.1 |
} |
575 |
|
|
|
576 |
dl |
1.4 |
/** |
577 |
dl |
1.5 |
* toArray(a) contains all elements |
578 |
dl |
1.4 |
*/ |
579 |
jsr166 |
1.44 |
public void testToArray2() { |
580 |
jsr166 |
1.45 |
DelayQueue<PDelay> q = populatedQueue(SIZE); |
581 |
jsr166 |
1.18 |
PDelay[] ints = new PDelay[SIZE]; |
582 |
jsr166 |
1.45 |
PDelay[] array = q.toArray(ints); |
583 |
|
|
assertSame(ints, array); |
584 |
dl |
1.1 |
Arrays.sort(ints); |
585 |
jsr166 |
1.20 |
for (int i = 0; i < ints.length; i++) |
586 |
jsr166 |
1.44 |
assertSame(ints[i], q.remove()); |
587 |
dl |
1.1 |
} |
588 |
dl |
1.6 |
|
589 |
|
|
/** |
590 |
jsr166 |
1.42 |
* toArray(incompatible array type) throws ArrayStoreException |
591 |
dl |
1.6 |
*/ |
592 |
|
|
public void testToArray1_BadArg() { |
593 |
jsr166 |
1.26 |
DelayQueue q = populatedQueue(SIZE); |
594 |
jsr166 |
1.18 |
try { |
595 |
jsr166 |
1.42 |
q.toArray(new String[10]); |
596 |
jsr166 |
1.18 |
shouldThrow(); |
597 |
jsr166 |
1.20 |
} catch (ArrayStoreException success) {} |
598 |
dl |
1.6 |
} |
599 |
jsr166 |
1.15 |
|
600 |
dl |
1.4 |
/** |
601 |
dl |
1.5 |
* iterator iterates through all elements |
602 |
dl |
1.4 |
*/ |
603 |
|
|
public void testIterator() { |
604 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
605 |
dl |
1.1 |
int i = 0; |
606 |
jsr166 |
1.18 |
Iterator it = q.iterator(); |
607 |
jsr166 |
1.16 |
while (it.hasNext()) { |
608 |
dl |
1.1 |
assertTrue(q.contains(it.next())); |
609 |
|
|
++i; |
610 |
|
|
} |
611 |
dl |
1.3 |
assertEquals(i, SIZE); |
612 |
jsr166 |
1.68 |
assertIteratorExhausted(it); |
613 |
|
|
} |
614 |
|
|
|
615 |
|
|
/** |
616 |
|
|
* iterator of empty collection has no elements |
617 |
|
|
*/ |
618 |
|
|
public void testEmptyIterator() { |
619 |
|
|
assertIteratorExhausted(new DelayQueue().iterator()); |
620 |
dl |
1.1 |
} |
621 |
|
|
|
622 |
dl |
1.4 |
/** |
623 |
dl |
1.5 |
* iterator.remove removes current element |
624 |
dl |
1.4 |
*/ |
625 |
jsr166 |
1.31 |
public void testIteratorRemove() { |
626 |
dl |
1.1 |
final DelayQueue q = new DelayQueue(); |
627 |
|
|
q.add(new PDelay(2)); |
628 |
|
|
q.add(new PDelay(1)); |
629 |
|
|
q.add(new PDelay(3)); |
630 |
|
|
Iterator it = q.iterator(); |
631 |
|
|
it.next(); |
632 |
|
|
it.remove(); |
633 |
|
|
it = q.iterator(); |
634 |
jsr166 |
1.58 |
assertEquals(new PDelay(2), it.next()); |
635 |
|
|
assertEquals(new PDelay(3), it.next()); |
636 |
dl |
1.1 |
assertFalse(it.hasNext()); |
637 |
|
|
} |
638 |
|
|
|
639 |
dl |
1.4 |
/** |
640 |
dl |
1.5 |
* toString contains toStrings of elements |
641 |
dl |
1.4 |
*/ |
642 |
|
|
public void testToString() { |
643 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
644 |
dl |
1.1 |
String s = q.toString(); |
645 |
jsr166 |
1.55 |
for (Object e : q) |
646 |
|
|
assertTrue(s.contains(e.toString())); |
647 |
jsr166 |
1.15 |
} |
648 |
dl |
1.1 |
|
649 |
dl |
1.4 |
/** |
650 |
jsr166 |
1.50 |
* timed poll transfers elements across Executor tasks |
651 |
dl |
1.4 |
*/ |
652 |
dl |
1.1 |
public void testPollInExecutor() { |
653 |
|
|
final DelayQueue q = new DelayQueue(); |
654 |
jsr166 |
1.50 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
655 |
jsr166 |
1.75 |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
656 |
|
|
try (PoolCleaner cleaner = cleaner(executor)) { |
657 |
|
|
executor.execute(new CheckedRunnable() { |
658 |
|
|
public void realRun() throws InterruptedException { |
659 |
|
|
assertNull(q.poll()); |
660 |
|
|
threadsStarted.await(); |
661 |
|
|
assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); |
662 |
|
|
checkEmpty(q); |
663 |
|
|
}}); |
664 |
|
|
|
665 |
|
|
executor.execute(new CheckedRunnable() { |
666 |
|
|
public void realRun() throws InterruptedException { |
667 |
|
|
threadsStarted.await(); |
668 |
|
|
q.put(new PDelay(1)); |
669 |
|
|
}}); |
670 |
|
|
} |
671 |
dl |
1.1 |
} |
672 |
|
|
|
673 |
dl |
1.4 |
/** |
674 |
dl |
1.8 |
* Delayed actions do not occur until their delay elapses |
675 |
dl |
1.4 |
*/ |
676 |
jsr166 |
1.20 |
public void testDelay() throws InterruptedException { |
677 |
jsr166 |
1.81 |
DelayQueue<NanoDelay> q = new DelayQueue<>(); |
678 |
jsr166 |
1.37 |
for (int i = 0; i < SIZE; ++i) |
679 |
|
|
q.add(new NanoDelay(1000000L * (SIZE - i))); |
680 |
dl |
1.1 |
|
681 |
jsr166 |
1.20 |
long last = 0; |
682 |
|
|
for (int i = 0; i < SIZE; ++i) { |
683 |
jsr166 |
1.37 |
NanoDelay e = q.take(); |
684 |
jsr166 |
1.20 |
long tt = e.getTriggerTime(); |
685 |
jsr166 |
1.37 |
assertTrue(System.nanoTime() - tt >= 0); |
686 |
jsr166 |
1.20 |
if (i != 0) |
687 |
|
|
assertTrue(tt >= last); |
688 |
|
|
last = tt; |
689 |
dl |
1.1 |
} |
690 |
jsr166 |
1.37 |
assertTrue(q.isEmpty()); |
691 |
dl |
1.1 |
} |
692 |
dl |
1.6 |
|
693 |
dl |
1.12 |
/** |
694 |
|
|
* peek of a non-empty queue returns non-null even if not expired |
695 |
|
|
*/ |
696 |
|
|
public void testPeekDelayed() { |
697 |
|
|
DelayQueue q = new DelayQueue(); |
698 |
|
|
q.add(new NanoDelay(Long.MAX_VALUE)); |
699 |
jsr166 |
1.33 |
assertNotNull(q.peek()); |
700 |
dl |
1.12 |
} |
701 |
|
|
|
702 |
|
|
/** |
703 |
|
|
* poll of a non-empty queue returns null if no expired elements. |
704 |
|
|
*/ |
705 |
|
|
public void testPollDelayed() { |
706 |
|
|
DelayQueue q = new DelayQueue(); |
707 |
|
|
q.add(new NanoDelay(Long.MAX_VALUE)); |
708 |
|
|
assertNull(q.poll()); |
709 |
|
|
} |
710 |
dl |
1.6 |
|
711 |
|
|
/** |
712 |
dl |
1.13 |
* timed poll of a non-empty queue returns null if no expired elements. |
713 |
|
|
*/ |
714 |
jsr166 |
1.20 |
public void testTimedPollDelayed() throws InterruptedException { |
715 |
dl |
1.13 |
DelayQueue q = new DelayQueue(); |
716 |
dl |
1.14 |
q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); |
717 |
jsr166 |
1.84 |
long startTime = System.nanoTime(); |
718 |
jsr166 |
1.52 |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
719 |
jsr166 |
1.84 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
720 |
dl |
1.13 |
} |
721 |
|
|
|
722 |
|
|
/** |
723 |
dl |
1.6 |
* drainTo(c) empties queue into another collection c |
724 |
jsr166 |
1.15 |
*/ |
725 |
dl |
1.6 |
public void testDrainTo() { |
726 |
dl |
1.11 |
DelayQueue q = new DelayQueue(); |
727 |
|
|
PDelay[] elems = new PDelay[SIZE]; |
728 |
|
|
for (int i = 0; i < SIZE; ++i) { |
729 |
|
|
elems[i] = new PDelay(i); |
730 |
|
|
q.add(elems[i]); |
731 |
|
|
} |
732 |
dl |
1.6 |
ArrayList l = new ArrayList(); |
733 |
|
|
q.drainTo(l); |
734 |
jsr166 |
1.57 |
assertEquals(0, q.size()); |
735 |
jsr166 |
1.15 |
for (int i = 0; i < SIZE; ++i) |
736 |
jsr166 |
1.58 |
assertEquals(elems[i], l.get(i)); |
737 |
dl |
1.11 |
q.add(elems[0]); |
738 |
|
|
q.add(elems[1]); |
739 |
|
|
assertFalse(q.isEmpty()); |
740 |
|
|
assertTrue(q.contains(elems[0])); |
741 |
|
|
assertTrue(q.contains(elems[1])); |
742 |
|
|
l.clear(); |
743 |
|
|
q.drainTo(l); |
744 |
jsr166 |
1.57 |
assertEquals(0, q.size()); |
745 |
|
|
assertEquals(2, l.size()); |
746 |
jsr166 |
1.15 |
for (int i = 0; i < 2; ++i) |
747 |
jsr166 |
1.58 |
assertEquals(elems[i], l.get(i)); |
748 |
dl |
1.6 |
} |
749 |
|
|
|
750 |
|
|
/** |
751 |
|
|
* drainTo empties queue |
752 |
jsr166 |
1.15 |
*/ |
753 |
jsr166 |
1.20 |
public void testDrainToWithActivePut() throws InterruptedException { |
754 |
dl |
1.6 |
final DelayQueue q = populatedQueue(SIZE); |
755 |
jsr166 |
1.20 |
Thread t = new Thread(new CheckedRunnable() { |
756 |
|
|
public void realRun() { |
757 |
jsr166 |
1.73 |
q.put(new PDelay(SIZE + 1)); |
758 |
jsr166 |
1.20 |
}}); |
759 |
|
|
|
760 |
|
|
t.start(); |
761 |
|
|
ArrayList l = new ArrayList(); |
762 |
|
|
q.drainTo(l); |
763 |
|
|
assertTrue(l.size() >= SIZE); |
764 |
|
|
t.join(); |
765 |
|
|
assertTrue(q.size() + l.size() >= SIZE); |
766 |
dl |
1.6 |
} |
767 |
|
|
|
768 |
|
|
/** |
769 |
jsr166 |
1.38 |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
770 |
jsr166 |
1.15 |
*/ |
771 |
dl |
1.6 |
public void testDrainToN() { |
772 |
|
|
for (int i = 0; i < SIZE + 2; ++i) { |
773 |
|
|
DelayQueue q = populatedQueue(SIZE); |
774 |
|
|
ArrayList l = new ArrayList(); |
775 |
|
|
q.drainTo(l, i); |
776 |
jsr166 |
1.39 |
int k = (i < SIZE) ? i : SIZE; |
777 |
jsr166 |
1.73 |
assertEquals(SIZE - k, q.size()); |
778 |
jsr166 |
1.58 |
assertEquals(k, l.size()); |
779 |
dl |
1.6 |
} |
780 |
|
|
} |
781 |
|
|
|
782 |
jsr166 |
1.63 |
/** |
783 |
|
|
* remove(null), contains(null) always return false |
784 |
|
|
*/ |
785 |
|
|
public void testNeverContainsNull() { |
786 |
|
|
Collection<?> q = populatedQueue(SIZE); |
787 |
|
|
assertFalse(q.contains(null)); |
788 |
|
|
assertFalse(q.remove(null)); |
789 |
|
|
} |
790 |
dl |
1.1 |
} |