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 |
|
|
import junit.framework.*; |
10 |
jsr166 |
1.53 |
import java.util.Arrays; |
11 |
|
|
import java.util.ArrayList; |
12 |
|
|
import java.util.Iterator; |
13 |
|
|
import java.util.NoSuchElementException; |
14 |
|
|
import java.util.concurrent.BlockingQueue; |
15 |
|
|
import java.util.concurrent.CountDownLatch; |
16 |
|
|
import java.util.concurrent.Delayed; |
17 |
|
|
import java.util.concurrent.DelayQueue; |
18 |
|
|
import java.util.concurrent.Executors; |
19 |
|
|
import java.util.concurrent.ExecutorService; |
20 |
|
|
import java.util.concurrent.TimeUnit; |
21 |
jsr166 |
1.19 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
22 |
dl |
1.1 |
|
23 |
dl |
1.3 |
public class DelayQueueTest extends JSR166TestCase { |
24 |
jsr166 |
1.53 |
|
25 |
|
|
public static class Generic extends BlockingQueueTest { |
26 |
|
|
protected BlockingQueue emptyCollection() { |
27 |
|
|
return new DelayQueue(); |
28 |
|
|
} |
29 |
|
|
protected PDelay makeElement(int i) { |
30 |
|
|
return new PDelay(i); |
31 |
|
|
} |
32 |
|
|
} |
33 |
|
|
|
34 |
dl |
1.1 |
public static void main(String[] args) { |
35 |
jsr166 |
1.31 |
junit.textui.TestRunner.run(suite()); |
36 |
dl |
1.1 |
} |
37 |
|
|
|
38 |
|
|
public static Test suite() { |
39 |
jsr166 |
1.53 |
return newTestSuite(DelayQueueTest.class, |
40 |
|
|
new Generic().testSuite()); |
41 |
dl |
1.1 |
} |
42 |
|
|
|
43 |
dl |
1.3 |
private static final int NOCAP = Integer.MAX_VALUE; |
44 |
|
|
|
45 |
dl |
1.4 |
/** |
46 |
dl |
1.8 |
* A delayed implementation for testing. |
47 |
jsr166 |
1.28 |
* Most tests use Pseudodelays, where delays are all elapsed |
48 |
dl |
1.4 |
* (so, no blocking solely for delays) but are still ordered |
49 |
jsr166 |
1.15 |
*/ |
50 |
|
|
static class PDelay implements Delayed { |
51 |
dl |
1.1 |
int pseudodelay; |
52 |
|
|
PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } |
53 |
dl |
1.10 |
public int compareTo(PDelay y) { |
54 |
dl |
1.1 |
int i = pseudodelay; |
55 |
jsr166 |
1.28 |
int j = y.pseudodelay; |
56 |
dl |
1.1 |
if (i < j) return -1; |
57 |
|
|
if (i > j) return 1; |
58 |
|
|
return 0; |
59 |
|
|
} |
60 |
|
|
|
61 |
dl |
1.10 |
public int compareTo(Delayed y) { |
62 |
jsr166 |
1.28 |
return compareTo((PDelay)y); |
63 |
dl |
1.1 |
} |
64 |
|
|
|
65 |
|
|
public boolean equals(Object other) { |
66 |
jsr166 |
1.28 |
return equals((PDelay)other); |
67 |
dl |
1.1 |
} |
68 |
|
|
public boolean equals(PDelay other) { |
69 |
jsr166 |
1.28 |
return other.pseudodelay == pseudodelay; |
70 |
dl |
1.1 |
} |
71 |
|
|
|
72 |
|
|
public long getDelay(TimeUnit ignore) { |
73 |
|
|
return pseudodelay; |
74 |
|
|
} |
75 |
|
|
public int intValue() { |
76 |
|
|
return pseudodelay; |
77 |
|
|
} |
78 |
|
|
|
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.28 |
return equals((NanoDelay)other); |
106 |
dl |
1.4 |
} |
107 |
|
|
public boolean equals(NanoDelay other) { |
108 |
jsr166 |
1.28 |
return other.trigger == trigger; |
109 |
dl |
1.4 |
} |
110 |
|
|
|
111 |
|
|
public long getDelay(TimeUnit unit) { |
112 |
|
|
long n = trigger - System.nanoTime(); |
113 |
|
|
return unit.convert(n, TimeUnit.NANOSECONDS); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
public long getTriggerTime() { |
117 |
|
|
return trigger; |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
public String toString() { |
121 |
|
|
return String.valueOf(trigger); |
122 |
|
|
} |
123 |
|
|
} |
124 |
dl |
1.1 |
|
125 |
|
|
/** |
126 |
|
|
* Create a queue of given size containing consecutive |
127 |
|
|
* PDelays 0 ... n. |
128 |
|
|
*/ |
129 |
jsr166 |
1.45 |
private DelayQueue<PDelay> populatedQueue(int n) { |
130 |
|
|
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
131 |
dl |
1.1 |
assertTrue(q.isEmpty()); |
132 |
jsr166 |
1.18 |
for (int i = n-1; i >= 0; i-=2) |
133 |
|
|
assertTrue(q.offer(new PDelay(i))); |
134 |
|
|
for (int i = (n & 1); i < n; i+=2) |
135 |
|
|
assertTrue(q.offer(new PDelay(i))); |
136 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
137 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
138 |
jsr166 |
1.18 |
assertEquals(n, q.size()); |
139 |
dl |
1.1 |
return q; |
140 |
|
|
} |
141 |
jsr166 |
1.15 |
|
142 |
dl |
1.4 |
/** |
143 |
dl |
1.5 |
* A new queue has unbounded capacity |
144 |
dl |
1.4 |
*/ |
145 |
|
|
public void testConstructor1() { |
146 |
dl |
1.1 |
assertEquals(NOCAP, new DelayQueue().remainingCapacity()); |
147 |
|
|
} |
148 |
|
|
|
149 |
dl |
1.4 |
/** |
150 |
dl |
1.5 |
* Initializing from null Collection throws NPE |
151 |
dl |
1.4 |
*/ |
152 |
|
|
public void testConstructor3() { |
153 |
dl |
1.1 |
try { |
154 |
|
|
DelayQueue q = new DelayQueue(null); |
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 of null elements throws NPE |
161 |
dl |
1.4 |
*/ |
162 |
|
|
public void testConstructor4() { |
163 |
dl |
1.1 |
try { |
164 |
dl |
1.3 |
PDelay[] ints = new PDelay[SIZE]; |
165 |
dl |
1.1 |
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
166 |
dl |
1.4 |
shouldThrow(); |
167 |
jsr166 |
1.20 |
} catch (NullPointerException success) {} |
168 |
dl |
1.1 |
} |
169 |
|
|
|
170 |
dl |
1.4 |
/** |
171 |
dl |
1.5 |
* Initializing from Collection with some null elements throws NPE |
172 |
dl |
1.4 |
*/ |
173 |
|
|
public void testConstructor5() { |
174 |
dl |
1.1 |
try { |
175 |
dl |
1.3 |
PDelay[] ints = new PDelay[SIZE]; |
176 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
177 |
dl |
1.1 |
ints[i] = new PDelay(i); |
178 |
|
|
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
179 |
dl |
1.4 |
shouldThrow(); |
180 |
jsr166 |
1.20 |
} catch (NullPointerException success) {} |
181 |
dl |
1.1 |
} |
182 |
|
|
|
183 |
dl |
1.4 |
/** |
184 |
dl |
1.5 |
* Queue contains all elements of collection used to initialize |
185 |
dl |
1.4 |
*/ |
186 |
|
|
public void testConstructor6() { |
187 |
jsr166 |
1.21 |
PDelay[] ints = new PDelay[SIZE]; |
188 |
|
|
for (int i = 0; i < SIZE; ++i) |
189 |
|
|
ints[i] = new PDelay(i); |
190 |
|
|
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
191 |
|
|
for (int i = 0; i < SIZE; ++i) |
192 |
|
|
assertEquals(ints[i], q.poll()); |
193 |
dl |
1.1 |
} |
194 |
|
|
|
195 |
dl |
1.4 |
/** |
196 |
dl |
1.5 |
* isEmpty is true before add, false after |
197 |
dl |
1.4 |
*/ |
198 |
dl |
1.1 |
public void testEmpty() { |
199 |
|
|
DelayQueue q = new DelayQueue(); |
200 |
|
|
assertTrue(q.isEmpty()); |
201 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
202 |
|
|
q.add(new PDelay(1)); |
203 |
|
|
assertFalse(q.isEmpty()); |
204 |
|
|
q.add(new PDelay(2)); |
205 |
|
|
q.remove(); |
206 |
|
|
q.remove(); |
207 |
|
|
assertTrue(q.isEmpty()); |
208 |
|
|
} |
209 |
|
|
|
210 |
dl |
1.4 |
/** |
211 |
jsr166 |
1.30 |
* remainingCapacity does not change when elements added or removed, |
212 |
dl |
1.5 |
* but size does |
213 |
dl |
1.4 |
*/ |
214 |
|
|
public void testRemainingCapacity() { |
215 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
216 |
|
|
for (int i = 0; i < SIZE; ++i) { |
217 |
dl |
1.1 |
assertEquals(NOCAP, q.remainingCapacity()); |
218 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
219 |
dl |
1.1 |
q.remove(); |
220 |
|
|
} |
221 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
222 |
dl |
1.1 |
assertEquals(NOCAP, q.remainingCapacity()); |
223 |
|
|
assertEquals(i, q.size()); |
224 |
|
|
q.add(new PDelay(i)); |
225 |
|
|
} |
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 |
|
|
try { |
253 |
|
|
DelayQueue q = populatedQueue(SIZE); |
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 |
dl |
1.1 |
try { |
265 |
|
|
DelayQueue q = new DelayQueue(); |
266 |
dl |
1.3 |
PDelay[] ints = new PDelay[SIZE]; |
267 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
268 |
dl |
1.1 |
ints[i] = new PDelay(i); |
269 |
|
|
q.addAll(Arrays.asList(ints)); |
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 |
|
|
for (int i = SIZE-1; i >= 0; --i) |
281 |
|
|
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 |
|
|
PDelay I = new PDelay(i); |
296 |
|
|
q.put(I); |
297 |
|
|
assertTrue(q.contains(I)); |
298 |
|
|
} |
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 |
|
|
assertEquals(new PDelay(i), ((PDelay)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 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
385 |
|
|
} |
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 |
|
|
assertEquals(new PDelay(i), ((PDelay)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 |
|
|
assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); |
408 |
|
|
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 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
423 |
jsr166 |
1.20 |
public void realRun() throws InterruptedException { |
424 |
|
|
DelayQueue q = populatedQueue(SIZE); |
425 |
|
|
for (int i = 0; i < SIZE; ++i) { |
426 |
|
|
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
427 |
|
|
} |
428 |
jsr166 |
1.50 |
|
429 |
|
|
Thread.currentThread().interrupt(); |
430 |
|
|
try { |
431 |
|
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
432 |
|
|
shouldThrow(); |
433 |
|
|
} catch (InterruptedException success) {} |
434 |
|
|
assertFalse(Thread.interrupted()); |
435 |
|
|
|
436 |
|
|
pleaseInterrupt.countDown(); |
437 |
jsr166 |
1.20 |
try { |
438 |
jsr166 |
1.50 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
439 |
jsr166 |
1.20 |
shouldThrow(); |
440 |
|
|
} catch (InterruptedException success) {} |
441 |
jsr166 |
1.50 |
assertFalse(Thread.interrupted()); |
442 |
jsr166 |
1.20 |
}}); |
443 |
|
|
|
444 |
jsr166 |
1.50 |
await(pleaseInterrupt); |
445 |
|
|
assertThreadStaysAlive(t); |
446 |
jsr166 |
1.20 |
t.interrupt(); |
447 |
jsr166 |
1.50 |
awaitTermination(t); |
448 |
dl |
1.1 |
} |
449 |
|
|
|
450 |
dl |
1.4 |
/** |
451 |
dl |
1.5 |
* peek returns next element, or null if empty |
452 |
dl |
1.4 |
*/ |
453 |
|
|
public void testPeek() { |
454 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
455 |
|
|
for (int i = 0; i < SIZE; ++i) { |
456 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.peek())); |
457 |
jsr166 |
1.26 |
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
458 |
dl |
1.12 |
if (q.isEmpty()) |
459 |
|
|
assertNull(q.peek()); |
460 |
|
|
else |
461 |
jsr166 |
1.26 |
assertFalse(new PDelay(i).equals(q.peek())); |
462 |
dl |
1.1 |
} |
463 |
jsr166 |
1.18 |
assertNull(q.peek()); |
464 |
dl |
1.1 |
} |
465 |
|
|
|
466 |
dl |
1.4 |
/** |
467 |
dl |
1.5 |
* element returns next element, or throws NSEE if empty |
468 |
dl |
1.4 |
*/ |
469 |
|
|
public void testElement() { |
470 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
471 |
|
|
for (int i = 0; i < SIZE; ++i) { |
472 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.element())); |
473 |
|
|
q.poll(); |
474 |
|
|
} |
475 |
|
|
try { |
476 |
|
|
q.element(); |
477 |
dl |
1.4 |
shouldThrow(); |
478 |
jsr166 |
1.20 |
} catch (NoSuchElementException success) {} |
479 |
dl |
1.1 |
} |
480 |
|
|
|
481 |
dl |
1.4 |
/** |
482 |
dl |
1.5 |
* remove removes next element, or throws NSEE if empty |
483 |
dl |
1.4 |
*/ |
484 |
|
|
public void testRemove() { |
485 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
486 |
|
|
for (int i = 0; i < SIZE; ++i) { |
487 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.remove())); |
488 |
|
|
} |
489 |
|
|
try { |
490 |
|
|
q.remove(); |
491 |
dl |
1.4 |
shouldThrow(); |
492 |
jsr166 |
1.20 |
} catch (NoSuchElementException success) {} |
493 |
dl |
1.1 |
} |
494 |
|
|
|
495 |
dl |
1.4 |
/** |
496 |
dl |
1.5 |
* remove(x) removes x and returns true if present |
497 |
dl |
1.4 |
*/ |
498 |
|
|
public void testRemoveElement() { |
499 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
500 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
501 |
dl |
1.1 |
assertTrue(q.remove(new PDelay(i))); |
502 |
|
|
} |
503 |
dl |
1.3 |
for (int i = 0; i < SIZE; i+=2) { |
504 |
dl |
1.1 |
assertTrue(q.remove(new PDelay(i))); |
505 |
|
|
assertFalse(q.remove(new PDelay(i+1))); |
506 |
|
|
} |
507 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
508 |
dl |
1.1 |
} |
509 |
jsr166 |
1.15 |
|
510 |
dl |
1.4 |
/** |
511 |
dl |
1.5 |
* contains(x) reports true when elements added but not yet removed |
512 |
dl |
1.4 |
*/ |
513 |
|
|
public void testContains() { |
514 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
515 |
|
|
for (int i = 0; i < SIZE; ++i) { |
516 |
dl |
1.1 |
assertTrue(q.contains(new PDelay(i))); |
517 |
|
|
q.poll(); |
518 |
|
|
assertFalse(q.contains(new PDelay(i))); |
519 |
|
|
} |
520 |
|
|
} |
521 |
|
|
|
522 |
dl |
1.4 |
/** |
523 |
dl |
1.5 |
* clear removes all elements |
524 |
dl |
1.4 |
*/ |
525 |
|
|
public void testClear() { |
526 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
527 |
dl |
1.1 |
q.clear(); |
528 |
|
|
assertTrue(q.isEmpty()); |
529 |
|
|
assertEquals(0, q.size()); |
530 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
531 |
dl |
1.11 |
PDelay x = new PDelay(1); |
532 |
|
|
q.add(x); |
533 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
534 |
dl |
1.11 |
assertTrue(q.contains(x)); |
535 |
dl |
1.1 |
q.clear(); |
536 |
|
|
assertTrue(q.isEmpty()); |
537 |
|
|
} |
538 |
|
|
|
539 |
dl |
1.4 |
/** |
540 |
dl |
1.5 |
* containsAll(c) is true when c contains a subset of elements |
541 |
dl |
1.4 |
*/ |
542 |
|
|
public void testContainsAll() { |
543 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
544 |
dl |
1.1 |
DelayQueue p = new DelayQueue(); |
545 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
546 |
dl |
1.1 |
assertTrue(q.containsAll(p)); |
547 |
|
|
assertFalse(p.containsAll(q)); |
548 |
|
|
p.add(new PDelay(i)); |
549 |
|
|
} |
550 |
|
|
assertTrue(p.containsAll(q)); |
551 |
|
|
} |
552 |
|
|
|
553 |
dl |
1.4 |
/** |
554 |
dl |
1.5 |
* retainAll(c) retains only those elements of c and reports true if changed |
555 |
dl |
1.4 |
*/ |
556 |
|
|
public void testRetainAll() { |
557 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
558 |
|
|
DelayQueue p = populatedQueue(SIZE); |
559 |
|
|
for (int i = 0; i < SIZE; ++i) { |
560 |
dl |
1.1 |
boolean changed = q.retainAll(p); |
561 |
|
|
if (i == 0) |
562 |
|
|
assertFalse(changed); |
563 |
|
|
else |
564 |
|
|
assertTrue(changed); |
565 |
|
|
|
566 |
|
|
assertTrue(q.containsAll(p)); |
567 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
568 |
dl |
1.1 |
p.remove(); |
569 |
|
|
} |
570 |
|
|
} |
571 |
|
|
|
572 |
dl |
1.4 |
/** |
573 |
dl |
1.5 |
* removeAll(c) removes only those elements of c and reports true if changed |
574 |
dl |
1.4 |
*/ |
575 |
|
|
public void testRemoveAll() { |
576 |
dl |
1.3 |
for (int i = 1; i < SIZE; ++i) { |
577 |
|
|
DelayQueue q = populatedQueue(SIZE); |
578 |
|
|
DelayQueue p = populatedQueue(i); |
579 |
dl |
1.1 |
assertTrue(q.removeAll(p)); |
580 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
581 |
dl |
1.1 |
for (int j = 0; j < i; ++j) { |
582 |
|
|
PDelay I = (PDelay)(p.remove()); |
583 |
|
|
assertFalse(q.contains(I)); |
584 |
|
|
} |
585 |
|
|
} |
586 |
|
|
} |
587 |
|
|
|
588 |
dl |
1.4 |
/** |
589 |
dl |
1.5 |
* toArray contains all elements |
590 |
dl |
1.4 |
*/ |
591 |
jsr166 |
1.20 |
public void testToArray() throws InterruptedException { |
592 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
593 |
jsr166 |
1.18 |
Object[] o = q.toArray(); |
594 |
dl |
1.1 |
Arrays.sort(o); |
595 |
jsr166 |
1.18 |
for (int i = 0; i < o.length; i++) |
596 |
jsr166 |
1.44 |
assertSame(o[i], q.take()); |
597 |
dl |
1.1 |
} |
598 |
|
|
|
599 |
dl |
1.4 |
/** |
600 |
dl |
1.5 |
* toArray(a) contains all elements |
601 |
dl |
1.4 |
*/ |
602 |
jsr166 |
1.44 |
public void testToArray2() { |
603 |
jsr166 |
1.45 |
DelayQueue<PDelay> q = populatedQueue(SIZE); |
604 |
jsr166 |
1.18 |
PDelay[] ints = new PDelay[SIZE]; |
605 |
jsr166 |
1.45 |
PDelay[] array = q.toArray(ints); |
606 |
|
|
assertSame(ints, array); |
607 |
dl |
1.1 |
Arrays.sort(ints); |
608 |
jsr166 |
1.20 |
for (int i = 0; i < ints.length; i++) |
609 |
jsr166 |
1.44 |
assertSame(ints[i], q.remove()); |
610 |
dl |
1.1 |
} |
611 |
dl |
1.6 |
|
612 |
|
|
/** |
613 |
jsr166 |
1.42 |
* toArray(incompatible array type) throws ArrayStoreException |
614 |
dl |
1.6 |
*/ |
615 |
|
|
public void testToArray1_BadArg() { |
616 |
jsr166 |
1.26 |
DelayQueue q = populatedQueue(SIZE); |
617 |
jsr166 |
1.18 |
try { |
618 |
jsr166 |
1.42 |
q.toArray(new String[10]); |
619 |
jsr166 |
1.18 |
shouldThrow(); |
620 |
jsr166 |
1.20 |
} catch (ArrayStoreException success) {} |
621 |
dl |
1.6 |
} |
622 |
jsr166 |
1.15 |
|
623 |
dl |
1.4 |
/** |
624 |
dl |
1.5 |
* iterator iterates through all elements |
625 |
dl |
1.4 |
*/ |
626 |
|
|
public void testIterator() { |
627 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
628 |
dl |
1.1 |
int i = 0; |
629 |
jsr166 |
1.18 |
Iterator it = q.iterator(); |
630 |
jsr166 |
1.16 |
while (it.hasNext()) { |
631 |
dl |
1.1 |
assertTrue(q.contains(it.next())); |
632 |
|
|
++i; |
633 |
|
|
} |
634 |
dl |
1.3 |
assertEquals(i, SIZE); |
635 |
dl |
1.1 |
} |
636 |
|
|
|
637 |
dl |
1.4 |
/** |
638 |
dl |
1.5 |
* iterator.remove removes current element |
639 |
dl |
1.4 |
*/ |
640 |
jsr166 |
1.31 |
public void testIteratorRemove() { |
641 |
dl |
1.1 |
final DelayQueue q = new DelayQueue(); |
642 |
|
|
q.add(new PDelay(2)); |
643 |
|
|
q.add(new PDelay(1)); |
644 |
|
|
q.add(new PDelay(3)); |
645 |
|
|
Iterator it = q.iterator(); |
646 |
|
|
it.next(); |
647 |
|
|
it.remove(); |
648 |
|
|
it = q.iterator(); |
649 |
|
|
assertEquals(it.next(), new PDelay(2)); |
650 |
|
|
assertEquals(it.next(), new PDelay(3)); |
651 |
|
|
assertFalse(it.hasNext()); |
652 |
|
|
} |
653 |
|
|
|
654 |
dl |
1.4 |
/** |
655 |
dl |
1.5 |
* toString contains toStrings of elements |
656 |
dl |
1.4 |
*/ |
657 |
|
|
public void testToString() { |
658 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
659 |
dl |
1.1 |
String s = q.toString(); |
660 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
661 |
jsr166 |
1.50 |
assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i))); |
662 |
dl |
1.1 |
} |
663 |
jsr166 |
1.15 |
} |
664 |
dl |
1.1 |
|
665 |
dl |
1.4 |
/** |
666 |
jsr166 |
1.50 |
* timed poll transfers elements across Executor tasks |
667 |
dl |
1.4 |
*/ |
668 |
dl |
1.1 |
public void testPollInExecutor() { |
669 |
|
|
final DelayQueue q = new DelayQueue(); |
670 |
jsr166 |
1.50 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
671 |
dl |
1.1 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
672 |
jsr166 |
1.20 |
executor.execute(new CheckedRunnable() { |
673 |
|
|
public void realRun() throws InterruptedException { |
674 |
jsr166 |
1.27 |
assertNull(q.poll()); |
675 |
jsr166 |
1.50 |
threadsStarted.await(); |
676 |
|
|
assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS)); |
677 |
|
|
checkEmpty(q); |
678 |
jsr166 |
1.20 |
}}); |
679 |
|
|
|
680 |
|
|
executor.execute(new CheckedRunnable() { |
681 |
|
|
public void realRun() throws InterruptedException { |
682 |
jsr166 |
1.50 |
threadsStarted.await(); |
683 |
jsr166 |
1.20 |
q.put(new PDelay(1)); |
684 |
|
|
}}); |
685 |
dl |
1.1 |
|
686 |
dl |
1.3 |
joinPool(executor); |
687 |
dl |
1.1 |
} |
688 |
|
|
|
689 |
dl |
1.4 |
/** |
690 |
dl |
1.8 |
* Delayed actions do not occur until their delay elapses |
691 |
dl |
1.4 |
*/ |
692 |
jsr166 |
1.20 |
public void testDelay() throws InterruptedException { |
693 |
jsr166 |
1.37 |
DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>(); |
694 |
|
|
for (int i = 0; i < SIZE; ++i) |
695 |
|
|
q.add(new NanoDelay(1000000L * (SIZE - i))); |
696 |
dl |
1.1 |
|
697 |
jsr166 |
1.20 |
long last = 0; |
698 |
|
|
for (int i = 0; i < SIZE; ++i) { |
699 |
jsr166 |
1.37 |
NanoDelay e = q.take(); |
700 |
jsr166 |
1.20 |
long tt = e.getTriggerTime(); |
701 |
jsr166 |
1.37 |
assertTrue(System.nanoTime() - tt >= 0); |
702 |
jsr166 |
1.20 |
if (i != 0) |
703 |
|
|
assertTrue(tt >= last); |
704 |
|
|
last = tt; |
705 |
dl |
1.1 |
} |
706 |
jsr166 |
1.37 |
assertTrue(q.isEmpty()); |
707 |
dl |
1.1 |
} |
708 |
dl |
1.6 |
|
709 |
dl |
1.12 |
/** |
710 |
|
|
* peek of a non-empty queue returns non-null even if not expired |
711 |
|
|
*/ |
712 |
|
|
public void testPeekDelayed() { |
713 |
|
|
DelayQueue q = new DelayQueue(); |
714 |
|
|
q.add(new NanoDelay(Long.MAX_VALUE)); |
715 |
jsr166 |
1.33 |
assertNotNull(q.peek()); |
716 |
dl |
1.12 |
} |
717 |
|
|
|
718 |
|
|
/** |
719 |
|
|
* poll of a non-empty queue returns null if no expired elements. |
720 |
|
|
*/ |
721 |
|
|
public void testPollDelayed() { |
722 |
|
|
DelayQueue q = new DelayQueue(); |
723 |
|
|
q.add(new NanoDelay(Long.MAX_VALUE)); |
724 |
|
|
assertNull(q.poll()); |
725 |
|
|
} |
726 |
dl |
1.6 |
|
727 |
|
|
/** |
728 |
dl |
1.13 |
* timed poll of a non-empty queue returns null if no expired elements. |
729 |
|
|
*/ |
730 |
jsr166 |
1.20 |
public void testTimedPollDelayed() throws InterruptedException { |
731 |
dl |
1.13 |
DelayQueue q = new DelayQueue(); |
732 |
dl |
1.14 |
q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); |
733 |
jsr166 |
1.52 |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
734 |
dl |
1.13 |
} |
735 |
|
|
|
736 |
|
|
/** |
737 |
dl |
1.6 |
* drainTo(c) empties queue into another collection c |
738 |
jsr166 |
1.15 |
*/ |
739 |
dl |
1.6 |
public void testDrainTo() { |
740 |
dl |
1.11 |
DelayQueue q = new DelayQueue(); |
741 |
|
|
PDelay[] elems = new PDelay[SIZE]; |
742 |
|
|
for (int i = 0; i < SIZE; ++i) { |
743 |
|
|
elems[i] = new PDelay(i); |
744 |
|
|
q.add(elems[i]); |
745 |
|
|
} |
746 |
dl |
1.6 |
ArrayList l = new ArrayList(); |
747 |
|
|
q.drainTo(l); |
748 |
|
|
assertEquals(q.size(), 0); |
749 |
jsr166 |
1.15 |
for (int i = 0; i < SIZE; ++i) |
750 |
dl |
1.11 |
assertEquals(l.get(i), elems[i]); |
751 |
|
|
q.add(elems[0]); |
752 |
|
|
q.add(elems[1]); |
753 |
|
|
assertFalse(q.isEmpty()); |
754 |
|
|
assertTrue(q.contains(elems[0])); |
755 |
|
|
assertTrue(q.contains(elems[1])); |
756 |
|
|
l.clear(); |
757 |
|
|
q.drainTo(l); |
758 |
|
|
assertEquals(q.size(), 0); |
759 |
|
|
assertEquals(l.size(), 2); |
760 |
jsr166 |
1.15 |
for (int i = 0; i < 2; ++i) |
761 |
dl |
1.11 |
assertEquals(l.get(i), elems[i]); |
762 |
dl |
1.6 |
} |
763 |
|
|
|
764 |
|
|
/** |
765 |
|
|
* drainTo empties queue |
766 |
jsr166 |
1.15 |
*/ |
767 |
jsr166 |
1.20 |
public void testDrainToWithActivePut() throws InterruptedException { |
768 |
dl |
1.6 |
final DelayQueue q = populatedQueue(SIZE); |
769 |
jsr166 |
1.20 |
Thread t = new Thread(new CheckedRunnable() { |
770 |
|
|
public void realRun() { |
771 |
|
|
q.put(new PDelay(SIZE+1)); |
772 |
|
|
}}); |
773 |
|
|
|
774 |
|
|
t.start(); |
775 |
|
|
ArrayList l = new ArrayList(); |
776 |
|
|
q.drainTo(l); |
777 |
|
|
assertTrue(l.size() >= SIZE); |
778 |
|
|
t.join(); |
779 |
|
|
assertTrue(q.size() + l.size() >= SIZE); |
780 |
dl |
1.6 |
} |
781 |
|
|
|
782 |
|
|
/** |
783 |
jsr166 |
1.38 |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
784 |
jsr166 |
1.15 |
*/ |
785 |
dl |
1.6 |
public void testDrainToN() { |
786 |
|
|
for (int i = 0; i < SIZE + 2; ++i) { |
787 |
|
|
DelayQueue q = populatedQueue(SIZE); |
788 |
|
|
ArrayList l = new ArrayList(); |
789 |
|
|
q.drainTo(l, i); |
790 |
jsr166 |
1.39 |
int k = (i < SIZE) ? i : SIZE; |
791 |
dl |
1.6 |
assertEquals(q.size(), SIZE-k); |
792 |
|
|
assertEquals(l.size(), k); |
793 |
|
|
} |
794 |
|
|
} |
795 |
|
|
|
796 |
dl |
1.1 |
} |