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