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 |
|
|
import java.util.*; |
11 |
jsr166 |
1.19 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
12 |
dl |
1.1 |
import java.util.concurrent.*; |
13 |
|
|
|
14 |
dl |
1.3 |
public class DelayQueueTest extends JSR166TestCase { |
15 |
dl |
1.1 |
public static void main(String[] args) { |
16 |
jsr166 |
1.31 |
junit.textui.TestRunner.run(suite()); |
17 |
dl |
1.1 |
} |
18 |
|
|
|
19 |
|
|
public static Test suite() { |
20 |
jsr166 |
1.18 |
return new TestSuite(DelayQueueTest.class); |
21 |
dl |
1.1 |
} |
22 |
|
|
|
23 |
dl |
1.3 |
private static final int NOCAP = Integer.MAX_VALUE; |
24 |
|
|
|
25 |
dl |
1.4 |
/** |
26 |
dl |
1.8 |
* A delayed implementation for testing. |
27 |
jsr166 |
1.28 |
* Most tests use Pseudodelays, where delays are all elapsed |
28 |
dl |
1.4 |
* (so, no blocking solely for delays) but are still ordered |
29 |
jsr166 |
1.15 |
*/ |
30 |
|
|
static class PDelay implements Delayed { |
31 |
dl |
1.1 |
int pseudodelay; |
32 |
|
|
PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } |
33 |
dl |
1.10 |
public int compareTo(PDelay y) { |
34 |
dl |
1.1 |
int i = pseudodelay; |
35 |
jsr166 |
1.28 |
int j = y.pseudodelay; |
36 |
dl |
1.1 |
if (i < j) return -1; |
37 |
|
|
if (i > j) return 1; |
38 |
|
|
return 0; |
39 |
|
|
} |
40 |
|
|
|
41 |
dl |
1.10 |
public int compareTo(Delayed y) { |
42 |
jsr166 |
1.28 |
return compareTo((PDelay)y); |
43 |
dl |
1.1 |
} |
44 |
|
|
|
45 |
|
|
public boolean equals(Object other) { |
46 |
jsr166 |
1.28 |
return equals((PDelay)other); |
47 |
dl |
1.1 |
} |
48 |
|
|
public boolean equals(PDelay other) { |
49 |
jsr166 |
1.28 |
return other.pseudodelay == pseudodelay; |
50 |
dl |
1.1 |
} |
51 |
|
|
|
52 |
|
|
public long getDelay(TimeUnit ignore) { |
53 |
|
|
return pseudodelay; |
54 |
|
|
} |
55 |
|
|
public int intValue() { |
56 |
|
|
return pseudodelay; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
public String toString() { |
60 |
|
|
return String.valueOf(pseudodelay); |
61 |
|
|
} |
62 |
|
|
} |
63 |
|
|
|
64 |
dl |
1.4 |
/** |
65 |
|
|
* Delayed implementation that actually delays |
66 |
|
|
*/ |
67 |
jsr166 |
1.15 |
static class NanoDelay implements Delayed { |
68 |
dl |
1.4 |
long trigger; |
69 |
jsr166 |
1.15 |
NanoDelay(long i) { |
70 |
dl |
1.4 |
trigger = System.nanoTime() + i; |
71 |
|
|
} |
72 |
dl |
1.10 |
public int compareTo(NanoDelay y) { |
73 |
dl |
1.4 |
long i = trigger; |
74 |
jsr166 |
1.28 |
long j = y.trigger; |
75 |
dl |
1.4 |
if (i < j) return -1; |
76 |
|
|
if (i > j) return 1; |
77 |
|
|
return 0; |
78 |
|
|
} |
79 |
|
|
|
80 |
dl |
1.10 |
public int compareTo(Delayed y) { |
81 |
jsr166 |
1.28 |
return compareTo((NanoDelay)y); |
82 |
dl |
1.4 |
} |
83 |
|
|
|
84 |
|
|
public boolean equals(Object other) { |
85 |
jsr166 |
1.28 |
return equals((NanoDelay)other); |
86 |
dl |
1.4 |
} |
87 |
|
|
public boolean equals(NanoDelay other) { |
88 |
jsr166 |
1.28 |
return other.trigger == trigger; |
89 |
dl |
1.4 |
} |
90 |
|
|
|
91 |
|
|
public long getDelay(TimeUnit unit) { |
92 |
|
|
long n = trigger - System.nanoTime(); |
93 |
|
|
return unit.convert(n, TimeUnit.NANOSECONDS); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
public long getTriggerTime() { |
97 |
|
|
return trigger; |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
public String toString() { |
101 |
|
|
return String.valueOf(trigger); |
102 |
|
|
} |
103 |
|
|
} |
104 |
dl |
1.1 |
|
105 |
|
|
/** |
106 |
|
|
* Create a queue of given size containing consecutive |
107 |
|
|
* PDelays 0 ... n. |
108 |
|
|
*/ |
109 |
jsr166 |
1.45 |
private DelayQueue<PDelay> populatedQueue(int n) { |
110 |
|
|
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
111 |
dl |
1.1 |
assertTrue(q.isEmpty()); |
112 |
jsr166 |
1.18 |
for (int i = n-1; i >= 0; i-=2) |
113 |
|
|
assertTrue(q.offer(new PDelay(i))); |
114 |
|
|
for (int i = (n & 1); i < n; i+=2) |
115 |
|
|
assertTrue(q.offer(new PDelay(i))); |
116 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
117 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
118 |
jsr166 |
1.18 |
assertEquals(n, q.size()); |
119 |
dl |
1.1 |
return q; |
120 |
|
|
} |
121 |
jsr166 |
1.15 |
|
122 |
dl |
1.4 |
/** |
123 |
dl |
1.5 |
* A new queue has unbounded capacity |
124 |
dl |
1.4 |
*/ |
125 |
|
|
public void testConstructor1() { |
126 |
dl |
1.1 |
assertEquals(NOCAP, new DelayQueue().remainingCapacity()); |
127 |
|
|
} |
128 |
|
|
|
129 |
dl |
1.4 |
/** |
130 |
dl |
1.5 |
* Initializing from null Collection throws NPE |
131 |
dl |
1.4 |
*/ |
132 |
|
|
public void testConstructor3() { |
133 |
dl |
1.1 |
try { |
134 |
|
|
DelayQueue q = new DelayQueue(null); |
135 |
dl |
1.4 |
shouldThrow(); |
136 |
jsr166 |
1.20 |
} catch (NullPointerException success) {} |
137 |
dl |
1.1 |
} |
138 |
|
|
|
139 |
dl |
1.4 |
/** |
140 |
dl |
1.5 |
* Initializing from Collection of null elements throws NPE |
141 |
dl |
1.4 |
*/ |
142 |
|
|
public void testConstructor4() { |
143 |
dl |
1.1 |
try { |
144 |
dl |
1.3 |
PDelay[] ints = new PDelay[SIZE]; |
145 |
dl |
1.1 |
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
146 |
dl |
1.4 |
shouldThrow(); |
147 |
jsr166 |
1.20 |
} catch (NullPointerException success) {} |
148 |
dl |
1.1 |
} |
149 |
|
|
|
150 |
dl |
1.4 |
/** |
151 |
dl |
1.5 |
* Initializing from Collection with some null elements throws NPE |
152 |
dl |
1.4 |
*/ |
153 |
|
|
public void testConstructor5() { |
154 |
dl |
1.1 |
try { |
155 |
dl |
1.3 |
PDelay[] ints = new PDelay[SIZE]; |
156 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
157 |
dl |
1.1 |
ints[i] = new PDelay(i); |
158 |
|
|
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
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 |
* Queue contains all elements of collection used to initialize |
165 |
dl |
1.4 |
*/ |
166 |
|
|
public void testConstructor6() { |
167 |
jsr166 |
1.21 |
PDelay[] ints = new PDelay[SIZE]; |
168 |
|
|
for (int i = 0; i < SIZE; ++i) |
169 |
|
|
ints[i] = new PDelay(i); |
170 |
|
|
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
171 |
|
|
for (int i = 0; i < SIZE; ++i) |
172 |
|
|
assertEquals(ints[i], q.poll()); |
173 |
dl |
1.1 |
} |
174 |
|
|
|
175 |
dl |
1.4 |
/** |
176 |
dl |
1.5 |
* isEmpty is true before add, false after |
177 |
dl |
1.4 |
*/ |
178 |
dl |
1.1 |
public void testEmpty() { |
179 |
|
|
DelayQueue q = new DelayQueue(); |
180 |
|
|
assertTrue(q.isEmpty()); |
181 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
182 |
|
|
q.add(new PDelay(1)); |
183 |
|
|
assertFalse(q.isEmpty()); |
184 |
|
|
q.add(new PDelay(2)); |
185 |
|
|
q.remove(); |
186 |
|
|
q.remove(); |
187 |
|
|
assertTrue(q.isEmpty()); |
188 |
|
|
} |
189 |
|
|
|
190 |
dl |
1.4 |
/** |
191 |
jsr166 |
1.30 |
* remainingCapacity does not change when elements added or removed, |
192 |
dl |
1.5 |
* but size does |
193 |
dl |
1.4 |
*/ |
194 |
|
|
public void testRemainingCapacity() { |
195 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
196 |
|
|
for (int i = 0; i < SIZE; ++i) { |
197 |
dl |
1.1 |
assertEquals(NOCAP, q.remainingCapacity()); |
198 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
199 |
dl |
1.1 |
q.remove(); |
200 |
|
|
} |
201 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
202 |
dl |
1.1 |
assertEquals(NOCAP, q.remainingCapacity()); |
203 |
|
|
assertEquals(i, q.size()); |
204 |
|
|
q.add(new PDelay(i)); |
205 |
|
|
} |
206 |
|
|
} |
207 |
|
|
|
208 |
dl |
1.4 |
/** |
209 |
dl |
1.5 |
* offer(null) throws NPE |
210 |
dl |
1.4 |
*/ |
211 |
|
|
public void testOfferNull() { |
212 |
jsr166 |
1.18 |
try { |
213 |
dl |
1.1 |
DelayQueue q = new DelayQueue(); |
214 |
|
|
q.offer(null); |
215 |
dl |
1.4 |
shouldThrow(); |
216 |
jsr166 |
1.20 |
} catch (NullPointerException success) {} |
217 |
dl |
1.1 |
} |
218 |
|
|
|
219 |
dl |
1.4 |
/** |
220 |
dl |
1.6 |
* add(null) throws NPE |
221 |
|
|
*/ |
222 |
|
|
public void testAddNull() { |
223 |
jsr166 |
1.18 |
try { |
224 |
dl |
1.6 |
DelayQueue q = new DelayQueue(); |
225 |
|
|
q.add(null); |
226 |
|
|
shouldThrow(); |
227 |
jsr166 |
1.20 |
} catch (NullPointerException success) {} |
228 |
dl |
1.6 |
} |
229 |
|
|
|
230 |
|
|
/** |
231 |
dl |
1.5 |
* offer non-null succeeds |
232 |
dl |
1.4 |
*/ |
233 |
dl |
1.1 |
public void testOffer() { |
234 |
|
|
DelayQueue q = new DelayQueue(); |
235 |
|
|
assertTrue(q.offer(new PDelay(0))); |
236 |
|
|
assertTrue(q.offer(new PDelay(1))); |
237 |
|
|
} |
238 |
|
|
|
239 |
dl |
1.4 |
/** |
240 |
dl |
1.5 |
* add succeeds |
241 |
dl |
1.4 |
*/ |
242 |
|
|
public void testAdd() { |
243 |
dl |
1.1 |
DelayQueue q = new DelayQueue(); |
244 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
245 |
dl |
1.1 |
assertEquals(i, q.size()); |
246 |
|
|
assertTrue(q.add(new PDelay(i))); |
247 |
|
|
} |
248 |
|
|
} |
249 |
|
|
|
250 |
dl |
1.4 |
/** |
251 |
dl |
1.5 |
* addAll(null) throws NPE |
252 |
dl |
1.4 |
*/ |
253 |
|
|
public void testAddAll1() { |
254 |
dl |
1.1 |
try { |
255 |
|
|
DelayQueue q = new DelayQueue(); |
256 |
|
|
q.addAll(null); |
257 |
dl |
1.4 |
shouldThrow(); |
258 |
jsr166 |
1.20 |
} catch (NullPointerException success) {} |
259 |
dl |
1.1 |
} |
260 |
dl |
1.6 |
|
261 |
|
|
/** |
262 |
|
|
* addAll(this) throws IAE |
263 |
|
|
*/ |
264 |
|
|
public void testAddAllSelf() { |
265 |
|
|
try { |
266 |
|
|
DelayQueue q = populatedQueue(SIZE); |
267 |
|
|
q.addAll(q); |
268 |
|
|
shouldThrow(); |
269 |
jsr166 |
1.20 |
} catch (IllegalArgumentException success) {} |
270 |
dl |
1.6 |
} |
271 |
|
|
|
272 |
dl |
1.4 |
/** |
273 |
dl |
1.5 |
* addAll of a collection with null elements throws NPE |
274 |
dl |
1.4 |
*/ |
275 |
|
|
public void testAddAll2() { |
276 |
dl |
1.1 |
try { |
277 |
|
|
DelayQueue q = new DelayQueue(); |
278 |
dl |
1.3 |
PDelay[] ints = new PDelay[SIZE]; |
279 |
dl |
1.1 |
q.addAll(Arrays.asList(ints)); |
280 |
dl |
1.4 |
shouldThrow(); |
281 |
jsr166 |
1.20 |
} catch (NullPointerException success) {} |
282 |
dl |
1.1 |
} |
283 |
jsr166 |
1.32 |
|
284 |
dl |
1.4 |
/** |
285 |
dl |
1.5 |
* addAll of a collection with any null elements throws NPE after |
286 |
|
|
* possibly adding some elements |
287 |
dl |
1.4 |
*/ |
288 |
|
|
public void testAddAll3() { |
289 |
dl |
1.1 |
try { |
290 |
|
|
DelayQueue q = new DelayQueue(); |
291 |
dl |
1.3 |
PDelay[] ints = new PDelay[SIZE]; |
292 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
293 |
dl |
1.1 |
ints[i] = new PDelay(i); |
294 |
|
|
q.addAll(Arrays.asList(ints)); |
295 |
dl |
1.4 |
shouldThrow(); |
296 |
jsr166 |
1.20 |
} catch (NullPointerException success) {} |
297 |
dl |
1.1 |
} |
298 |
|
|
|
299 |
dl |
1.4 |
/** |
300 |
dl |
1.5 |
* Queue contains all elements of successful addAll |
301 |
dl |
1.4 |
*/ |
302 |
|
|
public void testAddAll5() { |
303 |
jsr166 |
1.21 |
PDelay[] empty = new PDelay[0]; |
304 |
|
|
PDelay[] ints = new PDelay[SIZE]; |
305 |
|
|
for (int i = SIZE-1; i >= 0; --i) |
306 |
|
|
ints[i] = new PDelay(i); |
307 |
|
|
DelayQueue q = new DelayQueue(); |
308 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
309 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
310 |
|
|
for (int i = 0; i < SIZE; ++i) |
311 |
|
|
assertEquals(ints[i], q.poll()); |
312 |
dl |
1.1 |
} |
313 |
|
|
|
314 |
dl |
1.4 |
/** |
315 |
dl |
1.5 |
* put(null) throws NPE |
316 |
dl |
1.4 |
*/ |
317 |
jsr166 |
1.48 |
public void testPutNull() { |
318 |
jsr166 |
1.18 |
try { |
319 |
dl |
1.1 |
DelayQueue q = new DelayQueue(); |
320 |
|
|
q.put(null); |
321 |
dl |
1.4 |
shouldThrow(); |
322 |
jsr166 |
1.20 |
} catch (NullPointerException success) {} |
323 |
jsr166 |
1.48 |
} |
324 |
dl |
1.1 |
|
325 |
dl |
1.4 |
/** |
326 |
dl |
1.5 |
* all elements successfully put are contained |
327 |
dl |
1.4 |
*/ |
328 |
jsr166 |
1.48 |
public void testPut() { |
329 |
|
|
DelayQueue q = new DelayQueue(); |
330 |
|
|
for (int i = 0; i < SIZE; ++i) { |
331 |
|
|
PDelay I = new PDelay(i); |
332 |
|
|
q.put(I); |
333 |
|
|
assertTrue(q.contains(I)); |
334 |
|
|
} |
335 |
|
|
assertEquals(SIZE, q.size()); |
336 |
dl |
1.1 |
} |
337 |
|
|
|
338 |
dl |
1.4 |
/** |
339 |
dl |
1.5 |
* put doesn't block waiting for take |
340 |
dl |
1.4 |
*/ |
341 |
jsr166 |
1.20 |
public void testPutWithTake() throws InterruptedException { |
342 |
dl |
1.1 |
final DelayQueue q = new DelayQueue(); |
343 |
jsr166 |
1.50 |
Thread t = newStartedThread(new CheckedRunnable() { |
344 |
jsr166 |
1.20 |
public void realRun() { |
345 |
|
|
q.put(new PDelay(0)); |
346 |
|
|
q.put(new PDelay(0)); |
347 |
|
|
q.put(new PDelay(0)); |
348 |
|
|
q.put(new PDelay(0)); |
349 |
|
|
}}); |
350 |
|
|
|
351 |
jsr166 |
1.50 |
awaitTermination(t); |
352 |
|
|
assertEquals(4, q.size()); |
353 |
dl |
1.1 |
} |
354 |
|
|
|
355 |
dl |
1.4 |
/** |
356 |
dl |
1.5 |
* timed offer does not time out |
357 |
dl |
1.4 |
*/ |
358 |
jsr166 |
1.20 |
public void testTimedOffer() throws InterruptedException { |
359 |
dl |
1.1 |
final DelayQueue q = new DelayQueue(); |
360 |
jsr166 |
1.50 |
Thread t = newStartedThread(new CheckedRunnable() { |
361 |
jsr166 |
1.20 |
public void realRun() throws InterruptedException { |
362 |
|
|
q.put(new PDelay(0)); |
363 |
|
|
q.put(new PDelay(0)); |
364 |
jsr166 |
1.24 |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
365 |
|
|
assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); |
366 |
jsr166 |
1.20 |
}}); |
367 |
jsr166 |
1.15 |
|
368 |
jsr166 |
1.50 |
awaitTermination(t); |
369 |
dl |
1.1 |
} |
370 |
|
|
|
371 |
dl |
1.4 |
/** |
372 |
dl |
1.5 |
* take retrieves elements in priority order |
373 |
dl |
1.4 |
*/ |
374 |
jsr166 |
1.20 |
public void testTake() throws InterruptedException { |
375 |
|
|
DelayQueue q = populatedQueue(SIZE); |
376 |
|
|
for (int i = 0; i < SIZE; ++i) { |
377 |
|
|
assertEquals(new PDelay(i), ((PDelay)q.take())); |
378 |
jsr166 |
1.18 |
} |
379 |
dl |
1.1 |
} |
380 |
|
|
|
381 |
dl |
1.4 |
/** |
382 |
dl |
1.5 |
* take blocks interruptibly when empty |
383 |
dl |
1.4 |
*/ |
384 |
jsr166 |
1.40 |
public void testTakeFromEmptyBlocksInterruptibly() |
385 |
|
|
throws InterruptedException { |
386 |
|
|
final BlockingQueue q = new DelayQueue(); |
387 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
388 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
389 |
|
|
public void realRun() { |
390 |
|
|
threadStarted.countDown(); |
391 |
|
|
try { |
392 |
|
|
q.take(); |
393 |
|
|
shouldThrow(); |
394 |
jsr166 |
1.46 |
} catch (InterruptedException success) {} |
395 |
jsr166 |
1.50 |
assertFalse(Thread.interrupted()); |
396 |
jsr166 |
1.40 |
}}); |
397 |
jsr166 |
1.50 |
|
398 |
|
|
await(threadStarted); |
399 |
|
|
assertThreadStaysAlive(t); |
400 |
jsr166 |
1.20 |
t.interrupt(); |
401 |
jsr166 |
1.50 |
awaitTermination(t); |
402 |
dl |
1.1 |
} |
403 |
|
|
|
404 |
dl |
1.4 |
/** |
405 |
dl |
1.5 |
* Take removes existing elements until empty, then blocks interruptibly |
406 |
dl |
1.4 |
*/ |
407 |
jsr166 |
1.20 |
public void testBlockingTake() throws InterruptedException { |
408 |
jsr166 |
1.25 |
final DelayQueue q = populatedQueue(SIZE); |
409 |
jsr166 |
1.50 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
410 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
411 |
jsr166 |
1.20 |
public void realRun() throws InterruptedException { |
412 |
|
|
for (int i = 0; i < SIZE; ++i) { |
413 |
jsr166 |
1.25 |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
414 |
jsr166 |
1.20 |
} |
415 |
jsr166 |
1.50 |
|
416 |
|
|
Thread.currentThread().interrupt(); |
417 |
|
|
try { |
418 |
|
|
q.take(); |
419 |
|
|
shouldThrow(); |
420 |
|
|
} catch (InterruptedException success) {} |
421 |
|
|
assertFalse(Thread.interrupted()); |
422 |
|
|
|
423 |
|
|
pleaseInterrupt.countDown(); |
424 |
jsr166 |
1.25 |
try { |
425 |
|
|
q.take(); |
426 |
|
|
shouldThrow(); |
427 |
|
|
} catch (InterruptedException success) {} |
428 |
jsr166 |
1.50 |
assertFalse(Thread.interrupted()); |
429 |
jsr166 |
1.25 |
}}); |
430 |
jsr166 |
1.20 |
|
431 |
jsr166 |
1.50 |
await(pleaseInterrupt); |
432 |
|
|
assertThreadStaysAlive(t); |
433 |
jsr166 |
1.20 |
t.interrupt(); |
434 |
jsr166 |
1.50 |
awaitTermination(t); |
435 |
dl |
1.1 |
} |
436 |
|
|
|
437 |
dl |
1.4 |
/** |
438 |
dl |
1.5 |
* poll succeeds unless empty |
439 |
dl |
1.4 |
*/ |
440 |
|
|
public void testPoll() { |
441 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
442 |
|
|
for (int i = 0; i < SIZE; ++i) { |
443 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
444 |
|
|
} |
445 |
jsr166 |
1.18 |
assertNull(q.poll()); |
446 |
dl |
1.1 |
} |
447 |
|
|
|
448 |
dl |
1.4 |
/** |
449 |
jsr166 |
1.41 |
* timed poll with zero timeout succeeds when non-empty, else times out |
450 |
dl |
1.4 |
*/ |
451 |
jsr166 |
1.20 |
public void testTimedPoll0() throws InterruptedException { |
452 |
|
|
DelayQueue q = populatedQueue(SIZE); |
453 |
|
|
for (int i = 0; i < SIZE; ++i) { |
454 |
|
|
assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS))); |
455 |
jsr166 |
1.18 |
} |
456 |
jsr166 |
1.20 |
assertNull(q.poll(0, MILLISECONDS)); |
457 |
dl |
1.1 |
} |
458 |
|
|
|
459 |
dl |
1.4 |
/** |
460 |
jsr166 |
1.41 |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
461 |
dl |
1.4 |
*/ |
462 |
jsr166 |
1.20 |
public void testTimedPoll() throws InterruptedException { |
463 |
|
|
DelayQueue q = populatedQueue(SIZE); |
464 |
|
|
for (int i = 0; i < SIZE; ++i) { |
465 |
jsr166 |
1.51 |
long startTime = System.nanoTime(); |
466 |
|
|
assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); |
467 |
|
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
468 |
jsr166 |
1.18 |
} |
469 |
jsr166 |
1.51 |
long startTime = System.nanoTime(); |
470 |
|
|
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
471 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
472 |
|
|
checkEmpty(q); |
473 |
dl |
1.1 |
} |
474 |
|
|
|
475 |
dl |
1.4 |
/** |
476 |
dl |
1.5 |
* Interrupted timed poll throws InterruptedException instead of |
477 |
|
|
* returning timeout status |
478 |
dl |
1.4 |
*/ |
479 |
jsr166 |
1.20 |
public void testInterruptedTimedPoll() throws InterruptedException { |
480 |
jsr166 |
1.50 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
481 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
482 |
jsr166 |
1.20 |
public void realRun() throws InterruptedException { |
483 |
|
|
DelayQueue q = populatedQueue(SIZE); |
484 |
|
|
for (int i = 0; i < SIZE; ++i) { |
485 |
|
|
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
486 |
|
|
} |
487 |
jsr166 |
1.50 |
|
488 |
|
|
Thread.currentThread().interrupt(); |
489 |
|
|
try { |
490 |
|
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
491 |
|
|
shouldThrow(); |
492 |
|
|
} catch (InterruptedException success) {} |
493 |
|
|
assertFalse(Thread.interrupted()); |
494 |
|
|
|
495 |
|
|
pleaseInterrupt.countDown(); |
496 |
jsr166 |
1.20 |
try { |
497 |
jsr166 |
1.50 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
498 |
jsr166 |
1.20 |
shouldThrow(); |
499 |
|
|
} catch (InterruptedException success) {} |
500 |
jsr166 |
1.50 |
assertFalse(Thread.interrupted()); |
501 |
jsr166 |
1.20 |
}}); |
502 |
|
|
|
503 |
jsr166 |
1.50 |
await(pleaseInterrupt); |
504 |
|
|
assertThreadStaysAlive(t); |
505 |
jsr166 |
1.20 |
t.interrupt(); |
506 |
jsr166 |
1.50 |
awaitTermination(t); |
507 |
dl |
1.1 |
} |
508 |
|
|
|
509 |
dl |
1.4 |
/** |
510 |
jsr166 |
1.35 |
* timed poll before a delayed offer fails; after offer succeeds; |
511 |
|
|
* on interruption throws |
512 |
dl |
1.4 |
*/ |
513 |
jsr166 |
1.20 |
public void testTimedPollWithOffer() throws InterruptedException { |
514 |
dl |
1.1 |
final DelayQueue q = new DelayQueue(); |
515 |
jsr166 |
1.23 |
final PDelay pdelay = new PDelay(0); |
516 |
jsr166 |
1.35 |
final CheckedBarrier barrier = new CheckedBarrier(2); |
517 |
jsr166 |
1.50 |
Thread t = newStartedThread(new CheckedRunnable() { |
518 |
jsr166 |
1.20 |
public void realRun() throws InterruptedException { |
519 |
jsr166 |
1.35 |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
520 |
|
|
|
521 |
|
|
barrier.await(); |
522 |
jsr166 |
1.50 |
assertSame(pdelay, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
523 |
jsr166 |
1.35 |
|
524 |
|
|
Thread.currentThread().interrupt(); |
525 |
|
|
try { |
526 |
jsr166 |
1.50 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
527 |
jsr166 |
1.35 |
shouldThrow(); |
528 |
|
|
} catch (InterruptedException success) {} |
529 |
jsr166 |
1.50 |
assertFalse(Thread.interrupted()); |
530 |
jsr166 |
1.35 |
|
531 |
|
|
barrier.await(); |
532 |
jsr166 |
1.20 |
try { |
533 |
jsr166 |
1.50 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
534 |
jsr166 |
1.20 |
shouldThrow(); |
535 |
|
|
} catch (InterruptedException success) {} |
536 |
jsr166 |
1.50 |
assertFalse(Thread.interrupted()); |
537 |
jsr166 |
1.20 |
}}); |
538 |
|
|
|
539 |
jsr166 |
1.35 |
barrier.await(); |
540 |
jsr166 |
1.50 |
long startTime = System.nanoTime(); |
541 |
|
|
assertTrue(q.offer(pdelay, LONG_DELAY_MS, MILLISECONDS)); |
542 |
|
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
543 |
|
|
|
544 |
jsr166 |
1.35 |
barrier.await(); |
545 |
jsr166 |
1.50 |
assertThreadStaysAlive(t); |
546 |
jsr166 |
1.20 |
t.interrupt(); |
547 |
jsr166 |
1.50 |
awaitTermination(t); |
548 |
jsr166 |
1.15 |
} |
549 |
dl |
1.1 |
|
550 |
dl |
1.4 |
/** |
551 |
dl |
1.5 |
* peek returns next element, or null if empty |
552 |
dl |
1.4 |
*/ |
553 |
|
|
public void testPeek() { |
554 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
555 |
|
|
for (int i = 0; i < SIZE; ++i) { |
556 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.peek())); |
557 |
jsr166 |
1.26 |
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
558 |
dl |
1.12 |
if (q.isEmpty()) |
559 |
|
|
assertNull(q.peek()); |
560 |
|
|
else |
561 |
jsr166 |
1.26 |
assertFalse(new PDelay(i).equals(q.peek())); |
562 |
dl |
1.1 |
} |
563 |
jsr166 |
1.18 |
assertNull(q.peek()); |
564 |
dl |
1.1 |
} |
565 |
|
|
|
566 |
dl |
1.4 |
/** |
567 |
dl |
1.5 |
* element returns next element, or throws NSEE if empty |
568 |
dl |
1.4 |
*/ |
569 |
|
|
public void testElement() { |
570 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
571 |
|
|
for (int i = 0; i < SIZE; ++i) { |
572 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.element())); |
573 |
|
|
q.poll(); |
574 |
|
|
} |
575 |
|
|
try { |
576 |
|
|
q.element(); |
577 |
dl |
1.4 |
shouldThrow(); |
578 |
jsr166 |
1.20 |
} catch (NoSuchElementException success) {} |
579 |
dl |
1.1 |
} |
580 |
|
|
|
581 |
dl |
1.4 |
/** |
582 |
dl |
1.5 |
* remove removes next element, or throws NSEE if empty |
583 |
dl |
1.4 |
*/ |
584 |
|
|
public void testRemove() { |
585 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
586 |
|
|
for (int i = 0; i < SIZE; ++i) { |
587 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.remove())); |
588 |
|
|
} |
589 |
|
|
try { |
590 |
|
|
q.remove(); |
591 |
dl |
1.4 |
shouldThrow(); |
592 |
jsr166 |
1.20 |
} catch (NoSuchElementException success) {} |
593 |
dl |
1.1 |
} |
594 |
|
|
|
595 |
dl |
1.4 |
/** |
596 |
dl |
1.5 |
* remove(x) removes x and returns true if present |
597 |
dl |
1.4 |
*/ |
598 |
|
|
public void testRemoveElement() { |
599 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
600 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
601 |
dl |
1.1 |
assertTrue(q.remove(new PDelay(i))); |
602 |
|
|
} |
603 |
dl |
1.3 |
for (int i = 0; i < SIZE; i+=2) { |
604 |
dl |
1.1 |
assertTrue(q.remove(new PDelay(i))); |
605 |
|
|
assertFalse(q.remove(new PDelay(i+1))); |
606 |
|
|
} |
607 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
608 |
dl |
1.1 |
} |
609 |
jsr166 |
1.15 |
|
610 |
dl |
1.4 |
/** |
611 |
dl |
1.5 |
* contains(x) reports true when elements added but not yet removed |
612 |
dl |
1.4 |
*/ |
613 |
|
|
public void testContains() { |
614 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
615 |
|
|
for (int i = 0; i < SIZE; ++i) { |
616 |
dl |
1.1 |
assertTrue(q.contains(new PDelay(i))); |
617 |
|
|
q.poll(); |
618 |
|
|
assertFalse(q.contains(new PDelay(i))); |
619 |
|
|
} |
620 |
|
|
} |
621 |
|
|
|
622 |
dl |
1.4 |
/** |
623 |
dl |
1.5 |
* clear removes all elements |
624 |
dl |
1.4 |
*/ |
625 |
|
|
public void testClear() { |
626 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
627 |
dl |
1.1 |
q.clear(); |
628 |
|
|
assertTrue(q.isEmpty()); |
629 |
|
|
assertEquals(0, q.size()); |
630 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
631 |
dl |
1.11 |
PDelay x = new PDelay(1); |
632 |
|
|
q.add(x); |
633 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
634 |
dl |
1.11 |
assertTrue(q.contains(x)); |
635 |
dl |
1.1 |
q.clear(); |
636 |
|
|
assertTrue(q.isEmpty()); |
637 |
|
|
} |
638 |
|
|
|
639 |
dl |
1.4 |
/** |
640 |
dl |
1.5 |
* containsAll(c) is true when c contains a subset of elements |
641 |
dl |
1.4 |
*/ |
642 |
|
|
public void testContainsAll() { |
643 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
644 |
dl |
1.1 |
DelayQueue p = new DelayQueue(); |
645 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
646 |
dl |
1.1 |
assertTrue(q.containsAll(p)); |
647 |
|
|
assertFalse(p.containsAll(q)); |
648 |
|
|
p.add(new PDelay(i)); |
649 |
|
|
} |
650 |
|
|
assertTrue(p.containsAll(q)); |
651 |
|
|
} |
652 |
|
|
|
653 |
dl |
1.4 |
/** |
654 |
dl |
1.5 |
* retainAll(c) retains only those elements of c and reports true if changed |
655 |
dl |
1.4 |
*/ |
656 |
|
|
public void testRetainAll() { |
657 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
658 |
|
|
DelayQueue p = populatedQueue(SIZE); |
659 |
|
|
for (int i = 0; i < SIZE; ++i) { |
660 |
dl |
1.1 |
boolean changed = q.retainAll(p); |
661 |
|
|
if (i == 0) |
662 |
|
|
assertFalse(changed); |
663 |
|
|
else |
664 |
|
|
assertTrue(changed); |
665 |
|
|
|
666 |
|
|
assertTrue(q.containsAll(p)); |
667 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
668 |
dl |
1.1 |
p.remove(); |
669 |
|
|
} |
670 |
|
|
} |
671 |
|
|
|
672 |
dl |
1.4 |
/** |
673 |
dl |
1.5 |
* removeAll(c) removes only those elements of c and reports true if changed |
674 |
dl |
1.4 |
*/ |
675 |
|
|
public void testRemoveAll() { |
676 |
dl |
1.3 |
for (int i = 1; i < SIZE; ++i) { |
677 |
|
|
DelayQueue q = populatedQueue(SIZE); |
678 |
|
|
DelayQueue p = populatedQueue(i); |
679 |
dl |
1.1 |
assertTrue(q.removeAll(p)); |
680 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
681 |
dl |
1.1 |
for (int j = 0; j < i; ++j) { |
682 |
|
|
PDelay I = (PDelay)(p.remove()); |
683 |
|
|
assertFalse(q.contains(I)); |
684 |
|
|
} |
685 |
|
|
} |
686 |
|
|
} |
687 |
|
|
|
688 |
dl |
1.4 |
/** |
689 |
dl |
1.5 |
* toArray contains all elements |
690 |
dl |
1.4 |
*/ |
691 |
jsr166 |
1.20 |
public void testToArray() throws InterruptedException { |
692 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
693 |
jsr166 |
1.18 |
Object[] o = q.toArray(); |
694 |
dl |
1.1 |
Arrays.sort(o); |
695 |
jsr166 |
1.18 |
for (int i = 0; i < o.length; i++) |
696 |
jsr166 |
1.44 |
assertSame(o[i], q.take()); |
697 |
dl |
1.1 |
} |
698 |
|
|
|
699 |
dl |
1.4 |
/** |
700 |
dl |
1.5 |
* toArray(a) contains all elements |
701 |
dl |
1.4 |
*/ |
702 |
jsr166 |
1.44 |
public void testToArray2() { |
703 |
jsr166 |
1.45 |
DelayQueue<PDelay> q = populatedQueue(SIZE); |
704 |
jsr166 |
1.18 |
PDelay[] ints = new PDelay[SIZE]; |
705 |
jsr166 |
1.45 |
PDelay[] array = q.toArray(ints); |
706 |
|
|
assertSame(ints, array); |
707 |
dl |
1.1 |
Arrays.sort(ints); |
708 |
jsr166 |
1.20 |
for (int i = 0; i < ints.length; i++) |
709 |
jsr166 |
1.44 |
assertSame(ints[i], q.remove()); |
710 |
dl |
1.1 |
} |
711 |
dl |
1.6 |
|
712 |
|
|
/** |
713 |
jsr166 |
1.43 |
* toArray(null) throws NullPointerException |
714 |
dl |
1.6 |
*/ |
715 |
jsr166 |
1.43 |
public void testToArray_NullArg() { |
716 |
jsr166 |
1.26 |
DelayQueue q = populatedQueue(SIZE); |
717 |
jsr166 |
1.18 |
try { |
718 |
jsr166 |
1.43 |
q.toArray(null); |
719 |
jsr166 |
1.18 |
shouldThrow(); |
720 |
|
|
} catch (NullPointerException success) {} |
721 |
dl |
1.6 |
} |
722 |
|
|
|
723 |
|
|
/** |
724 |
jsr166 |
1.42 |
* toArray(incompatible array type) throws ArrayStoreException |
725 |
dl |
1.6 |
*/ |
726 |
|
|
public void testToArray1_BadArg() { |
727 |
jsr166 |
1.26 |
DelayQueue q = populatedQueue(SIZE); |
728 |
jsr166 |
1.18 |
try { |
729 |
jsr166 |
1.42 |
q.toArray(new String[10]); |
730 |
jsr166 |
1.18 |
shouldThrow(); |
731 |
jsr166 |
1.20 |
} catch (ArrayStoreException success) {} |
732 |
dl |
1.6 |
} |
733 |
jsr166 |
1.15 |
|
734 |
dl |
1.4 |
/** |
735 |
dl |
1.5 |
* iterator iterates through all elements |
736 |
dl |
1.4 |
*/ |
737 |
|
|
public void testIterator() { |
738 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
739 |
dl |
1.1 |
int i = 0; |
740 |
jsr166 |
1.18 |
Iterator it = q.iterator(); |
741 |
jsr166 |
1.16 |
while (it.hasNext()) { |
742 |
dl |
1.1 |
assertTrue(q.contains(it.next())); |
743 |
|
|
++i; |
744 |
|
|
} |
745 |
dl |
1.3 |
assertEquals(i, SIZE); |
746 |
dl |
1.1 |
} |
747 |
|
|
|
748 |
dl |
1.4 |
/** |
749 |
dl |
1.5 |
* iterator.remove removes current element |
750 |
dl |
1.4 |
*/ |
751 |
jsr166 |
1.31 |
public void testIteratorRemove() { |
752 |
dl |
1.1 |
final DelayQueue q = new DelayQueue(); |
753 |
|
|
q.add(new PDelay(2)); |
754 |
|
|
q.add(new PDelay(1)); |
755 |
|
|
q.add(new PDelay(3)); |
756 |
|
|
Iterator it = q.iterator(); |
757 |
|
|
it.next(); |
758 |
|
|
it.remove(); |
759 |
|
|
it = q.iterator(); |
760 |
|
|
assertEquals(it.next(), new PDelay(2)); |
761 |
|
|
assertEquals(it.next(), new PDelay(3)); |
762 |
|
|
assertFalse(it.hasNext()); |
763 |
|
|
} |
764 |
|
|
|
765 |
dl |
1.4 |
/** |
766 |
dl |
1.5 |
* toString contains toStrings of elements |
767 |
dl |
1.4 |
*/ |
768 |
|
|
public void testToString() { |
769 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
770 |
dl |
1.1 |
String s = q.toString(); |
771 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
772 |
jsr166 |
1.50 |
assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i))); |
773 |
dl |
1.1 |
} |
774 |
jsr166 |
1.15 |
} |
775 |
dl |
1.1 |
|
776 |
dl |
1.4 |
/** |
777 |
jsr166 |
1.50 |
* timed poll transfers elements across Executor tasks |
778 |
dl |
1.4 |
*/ |
779 |
dl |
1.1 |
public void testPollInExecutor() { |
780 |
|
|
final DelayQueue q = new DelayQueue(); |
781 |
jsr166 |
1.50 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
782 |
dl |
1.1 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
783 |
jsr166 |
1.20 |
executor.execute(new CheckedRunnable() { |
784 |
|
|
public void realRun() throws InterruptedException { |
785 |
jsr166 |
1.27 |
assertNull(q.poll()); |
786 |
jsr166 |
1.50 |
threadsStarted.await(); |
787 |
|
|
assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS)); |
788 |
|
|
checkEmpty(q); |
789 |
jsr166 |
1.20 |
}}); |
790 |
|
|
|
791 |
|
|
executor.execute(new CheckedRunnable() { |
792 |
|
|
public void realRun() throws InterruptedException { |
793 |
jsr166 |
1.50 |
threadsStarted.await(); |
794 |
jsr166 |
1.20 |
q.put(new PDelay(1)); |
795 |
|
|
}}); |
796 |
dl |
1.1 |
|
797 |
dl |
1.3 |
joinPool(executor); |
798 |
dl |
1.1 |
} |
799 |
|
|
|
800 |
dl |
1.4 |
/** |
801 |
dl |
1.8 |
* Delayed actions do not occur until their delay elapses |
802 |
dl |
1.4 |
*/ |
803 |
jsr166 |
1.20 |
public void testDelay() throws InterruptedException { |
804 |
jsr166 |
1.37 |
DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>(); |
805 |
|
|
for (int i = 0; i < SIZE; ++i) |
806 |
|
|
q.add(new NanoDelay(1000000L * (SIZE - i))); |
807 |
dl |
1.1 |
|
808 |
jsr166 |
1.20 |
long last = 0; |
809 |
|
|
for (int i = 0; i < SIZE; ++i) { |
810 |
jsr166 |
1.37 |
NanoDelay e = q.take(); |
811 |
jsr166 |
1.20 |
long tt = e.getTriggerTime(); |
812 |
jsr166 |
1.37 |
assertTrue(System.nanoTime() - tt >= 0); |
813 |
jsr166 |
1.20 |
if (i != 0) |
814 |
|
|
assertTrue(tt >= last); |
815 |
|
|
last = tt; |
816 |
dl |
1.1 |
} |
817 |
jsr166 |
1.37 |
assertTrue(q.isEmpty()); |
818 |
dl |
1.1 |
} |
819 |
dl |
1.6 |
|
820 |
dl |
1.12 |
/** |
821 |
|
|
* peek of a non-empty queue returns non-null even if not expired |
822 |
|
|
*/ |
823 |
|
|
public void testPeekDelayed() { |
824 |
|
|
DelayQueue q = new DelayQueue(); |
825 |
|
|
q.add(new NanoDelay(Long.MAX_VALUE)); |
826 |
jsr166 |
1.33 |
assertNotNull(q.peek()); |
827 |
dl |
1.12 |
} |
828 |
|
|
|
829 |
|
|
/** |
830 |
|
|
* poll of a non-empty queue returns null if no expired elements. |
831 |
|
|
*/ |
832 |
|
|
public void testPollDelayed() { |
833 |
|
|
DelayQueue q = new DelayQueue(); |
834 |
|
|
q.add(new NanoDelay(Long.MAX_VALUE)); |
835 |
|
|
assertNull(q.poll()); |
836 |
|
|
} |
837 |
dl |
1.6 |
|
838 |
|
|
/** |
839 |
dl |
1.13 |
* timed poll of a non-empty queue returns null if no expired elements. |
840 |
|
|
*/ |
841 |
jsr166 |
1.20 |
public void testTimedPollDelayed() throws InterruptedException { |
842 |
dl |
1.13 |
DelayQueue q = new DelayQueue(); |
843 |
dl |
1.14 |
q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); |
844 |
jsr166 |
1.20 |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
845 |
dl |
1.13 |
} |
846 |
|
|
|
847 |
|
|
/** |
848 |
dl |
1.6 |
* drainTo(null) throws NPE |
849 |
jsr166 |
1.15 |
*/ |
850 |
dl |
1.6 |
public void testDrainToNull() { |
851 |
|
|
DelayQueue q = populatedQueue(SIZE); |
852 |
|
|
try { |
853 |
|
|
q.drainTo(null); |
854 |
|
|
shouldThrow(); |
855 |
jsr166 |
1.20 |
} catch (NullPointerException success) {} |
856 |
dl |
1.6 |
} |
857 |
|
|
|
858 |
|
|
/** |
859 |
|
|
* drainTo(this) throws IAE |
860 |
jsr166 |
1.15 |
*/ |
861 |
dl |
1.6 |
public void testDrainToSelf() { |
862 |
|
|
DelayQueue q = populatedQueue(SIZE); |
863 |
|
|
try { |
864 |
|
|
q.drainTo(q); |
865 |
|
|
shouldThrow(); |
866 |
jsr166 |
1.20 |
} catch (IllegalArgumentException success) {} |
867 |
dl |
1.6 |
} |
868 |
|
|
|
869 |
|
|
/** |
870 |
|
|
* drainTo(c) empties queue into another collection c |
871 |
jsr166 |
1.15 |
*/ |
872 |
dl |
1.6 |
public void testDrainTo() { |
873 |
dl |
1.11 |
DelayQueue q = new DelayQueue(); |
874 |
|
|
PDelay[] elems = new PDelay[SIZE]; |
875 |
|
|
for (int i = 0; i < SIZE; ++i) { |
876 |
|
|
elems[i] = new PDelay(i); |
877 |
|
|
q.add(elems[i]); |
878 |
|
|
} |
879 |
dl |
1.6 |
ArrayList l = new ArrayList(); |
880 |
|
|
q.drainTo(l); |
881 |
|
|
assertEquals(q.size(), 0); |
882 |
jsr166 |
1.15 |
for (int i = 0; i < SIZE; ++i) |
883 |
dl |
1.11 |
assertEquals(l.get(i), elems[i]); |
884 |
|
|
q.add(elems[0]); |
885 |
|
|
q.add(elems[1]); |
886 |
|
|
assertFalse(q.isEmpty()); |
887 |
|
|
assertTrue(q.contains(elems[0])); |
888 |
|
|
assertTrue(q.contains(elems[1])); |
889 |
|
|
l.clear(); |
890 |
|
|
q.drainTo(l); |
891 |
|
|
assertEquals(q.size(), 0); |
892 |
|
|
assertEquals(l.size(), 2); |
893 |
jsr166 |
1.15 |
for (int i = 0; i < 2; ++i) |
894 |
dl |
1.11 |
assertEquals(l.get(i), elems[i]); |
895 |
dl |
1.6 |
} |
896 |
|
|
|
897 |
|
|
/** |
898 |
|
|
* drainTo empties queue |
899 |
jsr166 |
1.15 |
*/ |
900 |
jsr166 |
1.20 |
public void testDrainToWithActivePut() throws InterruptedException { |
901 |
dl |
1.6 |
final DelayQueue q = populatedQueue(SIZE); |
902 |
jsr166 |
1.20 |
Thread t = new Thread(new CheckedRunnable() { |
903 |
|
|
public void realRun() { |
904 |
|
|
q.put(new PDelay(SIZE+1)); |
905 |
|
|
}}); |
906 |
|
|
|
907 |
|
|
t.start(); |
908 |
|
|
ArrayList l = new ArrayList(); |
909 |
|
|
q.drainTo(l); |
910 |
|
|
assertTrue(l.size() >= SIZE); |
911 |
|
|
t.join(); |
912 |
|
|
assertTrue(q.size() + l.size() >= SIZE); |
913 |
dl |
1.6 |
} |
914 |
|
|
|
915 |
|
|
/** |
916 |
|
|
* drainTo(null, n) throws NPE |
917 |
jsr166 |
1.15 |
*/ |
918 |
dl |
1.6 |
public void testDrainToNullN() { |
919 |
|
|
DelayQueue q = populatedQueue(SIZE); |
920 |
|
|
try { |
921 |
|
|
q.drainTo(null, 0); |
922 |
|
|
shouldThrow(); |
923 |
jsr166 |
1.20 |
} catch (NullPointerException success) {} |
924 |
dl |
1.6 |
} |
925 |
|
|
|
926 |
|
|
/** |
927 |
|
|
* drainTo(this, n) throws IAE |
928 |
jsr166 |
1.15 |
*/ |
929 |
dl |
1.6 |
public void testDrainToSelfN() { |
930 |
|
|
DelayQueue q = populatedQueue(SIZE); |
931 |
|
|
try { |
932 |
|
|
q.drainTo(q, 0); |
933 |
|
|
shouldThrow(); |
934 |
jsr166 |
1.20 |
} catch (IllegalArgumentException success) {} |
935 |
dl |
1.6 |
} |
936 |
|
|
|
937 |
|
|
/** |
938 |
jsr166 |
1.38 |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
939 |
jsr166 |
1.15 |
*/ |
940 |
dl |
1.6 |
public void testDrainToN() { |
941 |
|
|
for (int i = 0; i < SIZE + 2; ++i) { |
942 |
|
|
DelayQueue q = populatedQueue(SIZE); |
943 |
|
|
ArrayList l = new ArrayList(); |
944 |
|
|
q.drainTo(l, i); |
945 |
jsr166 |
1.39 |
int k = (i < SIZE) ? i : SIZE; |
946 |
dl |
1.6 |
assertEquals(q.size(), SIZE-k); |
947 |
|
|
assertEquals(l.size(), k); |
948 |
|
|
} |
949 |
|
|
} |
950 |
|
|
|
951 |
dl |
1.1 |
} |