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