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