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