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