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