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