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