1 |
dl |
1.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. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import junit.framework.*; |
9 |
|
|
import java.util.*; |
10 |
|
|
import java.util.concurrent.*; |
11 |
|
|
|
12 |
dl |
1.3 |
public class DelayQueueTest extends JSR166TestCase { |
13 |
dl |
1.1 |
public static void main(String[] args) { |
14 |
|
|
junit.textui.TestRunner.run (suite()); |
15 |
|
|
} |
16 |
|
|
|
17 |
|
|
public static Test suite() { |
18 |
|
|
return new TestSuite(DelayQueueTest.class); |
19 |
|
|
} |
20 |
|
|
|
21 |
dl |
1.3 |
private static final int NOCAP = Integer.MAX_VALUE; |
22 |
|
|
|
23 |
dl |
1.4 |
/** |
24 |
|
|
* A delayed implmentation for testing. |
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 |
dl |
1.1 |
static class PDelay implements Delayed { |
29 |
|
|
int pseudodelay; |
30 |
|
|
PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } |
31 |
|
|
public int compareTo(Object y) { |
32 |
|
|
int i = pseudodelay; |
33 |
|
|
int j = ((PDelay)y).pseudodelay; |
34 |
|
|
if (i < j) return -1; |
35 |
|
|
if (i > j) return 1; |
36 |
|
|
return 0; |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
public int compareTo(PDelay y) { |
40 |
|
|
int i = pseudodelay; |
41 |
|
|
int j = ((PDelay)y).pseudodelay; |
42 |
|
|
if (i < j) return -1; |
43 |
|
|
if (i > j) return 1; |
44 |
|
|
return 0; |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
public boolean equals(Object other) { |
48 |
|
|
return ((PDelay)other).pseudodelay == pseudodelay; |
49 |
|
|
} |
50 |
|
|
public boolean equals(PDelay other) { |
51 |
|
|
return ((PDelay)other).pseudodelay == pseudodelay; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
public long getDelay(TimeUnit ignore) { |
56 |
|
|
return pseudodelay; |
57 |
|
|
} |
58 |
|
|
public int intValue() { |
59 |
|
|
return pseudodelay; |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
public String toString() { |
63 |
|
|
return String.valueOf(pseudodelay); |
64 |
|
|
} |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
|
68 |
dl |
1.4 |
/** |
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 |
dl |
1.1 |
|
113 |
|
|
|
114 |
|
|
/** |
115 |
|
|
* Create a queue of given size containing consecutive |
116 |
|
|
* PDelays 0 ... n. |
117 |
|
|
*/ |
118 |
dl |
1.3 |
private DelayQueue populatedQueue(int n) { |
119 |
dl |
1.1 |
DelayQueue q = new DelayQueue(); |
120 |
|
|
assertTrue(q.isEmpty()); |
121 |
|
|
for(int i = n-1; i >= 0; i-=2) |
122 |
|
|
assertTrue(q.offer(new PDelay(i))); |
123 |
|
|
for(int i = (n & 1); i < n; i+=2) |
124 |
|
|
assertTrue(q.offer(new PDelay(i))); |
125 |
|
|
assertFalse(q.isEmpty()); |
126 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
127 |
|
|
assertEquals(n, q.size()); |
128 |
|
|
return q; |
129 |
|
|
} |
130 |
|
|
|
131 |
dl |
1.4 |
/** |
132 |
|
|
* |
133 |
|
|
*/ |
134 |
|
|
public void testConstructor1() { |
135 |
dl |
1.1 |
assertEquals(NOCAP, new DelayQueue().remainingCapacity()); |
136 |
|
|
} |
137 |
|
|
|
138 |
dl |
1.4 |
/** |
139 |
|
|
* |
140 |
|
|
*/ |
141 |
|
|
public void testConstructor3() { |
142 |
dl |
1.1 |
try { |
143 |
|
|
DelayQueue q = new DelayQueue(null); |
144 |
dl |
1.4 |
shouldThrow(); |
145 |
dl |
1.1 |
} |
146 |
|
|
catch (NullPointerException success) {} |
147 |
|
|
} |
148 |
|
|
|
149 |
dl |
1.4 |
/** |
150 |
|
|
* |
151 |
|
|
*/ |
152 |
|
|
public void testConstructor4() { |
153 |
dl |
1.1 |
try { |
154 |
dl |
1.3 |
PDelay[] ints = new PDelay[SIZE]; |
155 |
dl |
1.1 |
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
156 |
dl |
1.4 |
shouldThrow(); |
157 |
dl |
1.1 |
} |
158 |
|
|
catch (NullPointerException success) {} |
159 |
|
|
} |
160 |
|
|
|
161 |
dl |
1.4 |
/** |
162 |
|
|
* |
163 |
|
|
*/ |
164 |
|
|
public void testConstructor5() { |
165 |
dl |
1.1 |
try { |
166 |
dl |
1.3 |
PDelay[] ints = new PDelay[SIZE]; |
167 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
168 |
dl |
1.1 |
ints[i] = new PDelay(i); |
169 |
|
|
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
170 |
dl |
1.4 |
shouldThrow(); |
171 |
dl |
1.1 |
} |
172 |
|
|
catch (NullPointerException success) {} |
173 |
|
|
} |
174 |
|
|
|
175 |
dl |
1.4 |
/** |
176 |
|
|
* |
177 |
|
|
*/ |
178 |
|
|
public void testConstructor6() { |
179 |
dl |
1.1 |
try { |
180 |
dl |
1.3 |
PDelay[] ints = new PDelay[SIZE]; |
181 |
|
|
for (int i = 0; i < SIZE; ++i) |
182 |
dl |
1.1 |
ints[i] = new PDelay(i); |
183 |
|
|
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
184 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) |
185 |
dl |
1.1 |
assertEquals(ints[i], q.poll()); |
186 |
|
|
} |
187 |
|
|
finally {} |
188 |
|
|
} |
189 |
|
|
|
190 |
dl |
1.4 |
/** |
191 |
|
|
* |
192 |
|
|
*/ |
193 |
dl |
1.1 |
public void testEmpty() { |
194 |
|
|
DelayQueue q = new DelayQueue(); |
195 |
|
|
assertTrue(q.isEmpty()); |
196 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
197 |
|
|
q.add(new PDelay(1)); |
198 |
|
|
assertFalse(q.isEmpty()); |
199 |
|
|
q.add(new PDelay(2)); |
200 |
|
|
q.remove(); |
201 |
|
|
q.remove(); |
202 |
|
|
assertTrue(q.isEmpty()); |
203 |
|
|
} |
204 |
|
|
|
205 |
dl |
1.4 |
/** |
206 |
|
|
* |
207 |
|
|
*/ |
208 |
|
|
public void testRemainingCapacity() { |
209 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
210 |
|
|
for (int i = 0; i < SIZE; ++i) { |
211 |
dl |
1.1 |
assertEquals(NOCAP, q.remainingCapacity()); |
212 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
213 |
dl |
1.1 |
q.remove(); |
214 |
|
|
} |
215 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
216 |
dl |
1.1 |
assertEquals(NOCAP, q.remainingCapacity()); |
217 |
|
|
assertEquals(i, q.size()); |
218 |
|
|
q.add(new PDelay(i)); |
219 |
|
|
} |
220 |
|
|
} |
221 |
|
|
|
222 |
dl |
1.4 |
/** |
223 |
|
|
* |
224 |
|
|
*/ |
225 |
|
|
public void testOfferNull() { |
226 |
dl |
1.1 |
try { |
227 |
|
|
DelayQueue q = new DelayQueue(); |
228 |
|
|
q.offer(null); |
229 |
dl |
1.4 |
shouldThrow(); |
230 |
dl |
1.1 |
} catch (NullPointerException success) { } |
231 |
|
|
} |
232 |
|
|
|
233 |
dl |
1.4 |
/** |
234 |
|
|
* |
235 |
|
|
*/ |
236 |
dl |
1.1 |
public void testOffer() { |
237 |
|
|
DelayQueue q = new DelayQueue(); |
238 |
|
|
assertTrue(q.offer(new PDelay(0))); |
239 |
|
|
assertTrue(q.offer(new PDelay(1))); |
240 |
|
|
} |
241 |
|
|
|
242 |
dl |
1.4 |
/** |
243 |
|
|
* |
244 |
|
|
*/ |
245 |
|
|
public void testAdd() { |
246 |
dl |
1.1 |
DelayQueue q = new DelayQueue(); |
247 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
248 |
dl |
1.1 |
assertEquals(i, q.size()); |
249 |
|
|
assertTrue(q.add(new PDelay(i))); |
250 |
|
|
} |
251 |
|
|
} |
252 |
|
|
|
253 |
dl |
1.4 |
/** |
254 |
|
|
* |
255 |
|
|
*/ |
256 |
|
|
public void testAddAll1() { |
257 |
dl |
1.1 |
try { |
258 |
|
|
DelayQueue q = new DelayQueue(); |
259 |
|
|
q.addAll(null); |
260 |
dl |
1.4 |
shouldThrow(); |
261 |
dl |
1.1 |
} |
262 |
|
|
catch (NullPointerException success) {} |
263 |
|
|
} |
264 |
dl |
1.4 |
/** |
265 |
|
|
* |
266 |
|
|
*/ |
267 |
|
|
public void testAddAll2() { |
268 |
dl |
1.1 |
try { |
269 |
|
|
DelayQueue q = new DelayQueue(); |
270 |
dl |
1.3 |
PDelay[] ints = new PDelay[SIZE]; |
271 |
dl |
1.1 |
q.addAll(Arrays.asList(ints)); |
272 |
dl |
1.4 |
shouldThrow(); |
273 |
dl |
1.1 |
} |
274 |
|
|
catch (NullPointerException success) {} |
275 |
|
|
} |
276 |
dl |
1.4 |
/** |
277 |
|
|
* |
278 |
|
|
*/ |
279 |
|
|
public void testAddAll3() { |
280 |
dl |
1.1 |
try { |
281 |
|
|
DelayQueue q = new DelayQueue(); |
282 |
dl |
1.3 |
PDelay[] ints = new PDelay[SIZE]; |
283 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
284 |
dl |
1.1 |
ints[i] = new PDelay(i); |
285 |
|
|
q.addAll(Arrays.asList(ints)); |
286 |
dl |
1.4 |
shouldThrow(); |
287 |
dl |
1.1 |
} |
288 |
|
|
catch (NullPointerException success) {} |
289 |
|
|
} |
290 |
|
|
|
291 |
dl |
1.4 |
/** |
292 |
|
|
* |
293 |
|
|
*/ |
294 |
|
|
public void testAddAll5() { |
295 |
dl |
1.1 |
try { |
296 |
|
|
PDelay[] empty = new PDelay[0]; |
297 |
dl |
1.3 |
PDelay[] ints = new PDelay[SIZE]; |
298 |
|
|
for (int i = SIZE-1; i >= 0; --i) |
299 |
dl |
1.1 |
ints[i] = new PDelay(i); |
300 |
|
|
DelayQueue q = new DelayQueue(); |
301 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
302 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
303 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) |
304 |
dl |
1.1 |
assertEquals(ints[i], q.poll()); |
305 |
|
|
} |
306 |
|
|
finally {} |
307 |
|
|
} |
308 |
|
|
|
309 |
dl |
1.4 |
/** |
310 |
|
|
* |
311 |
|
|
*/ |
312 |
dl |
1.1 |
public void testPutNull() { |
313 |
|
|
try { |
314 |
|
|
DelayQueue q = new DelayQueue(); |
315 |
|
|
q.put(null); |
316 |
dl |
1.4 |
shouldThrow(); |
317 |
dl |
1.1 |
} |
318 |
|
|
catch (NullPointerException success){ |
319 |
|
|
} |
320 |
|
|
} |
321 |
|
|
|
322 |
dl |
1.4 |
/** |
323 |
|
|
* |
324 |
|
|
*/ |
325 |
dl |
1.1 |
public void testPut() { |
326 |
|
|
try { |
327 |
|
|
DelayQueue q = new DelayQueue(); |
328 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
329 |
dl |
1.1 |
PDelay I = new PDelay(i); |
330 |
|
|
q.put(I); |
331 |
|
|
assertTrue(q.contains(I)); |
332 |
|
|
} |
333 |
dl |
1.3 |
assertEquals(SIZE, q.size()); |
334 |
dl |
1.1 |
} |
335 |
|
|
finally { |
336 |
|
|
} |
337 |
|
|
} |
338 |
|
|
|
339 |
dl |
1.4 |
/** |
340 |
|
|
* |
341 |
|
|
*/ |
342 |
dl |
1.1 |
public void testPutWithTake() { |
343 |
|
|
final DelayQueue q = new DelayQueue(); |
344 |
|
|
Thread t = new Thread(new Runnable() { |
345 |
dl |
1.4 |
public void run() { |
346 |
dl |
1.1 |
int added = 0; |
347 |
|
|
try { |
348 |
|
|
q.put(new PDelay(0)); |
349 |
|
|
++added; |
350 |
|
|
q.put(new PDelay(0)); |
351 |
|
|
++added; |
352 |
|
|
q.put(new PDelay(0)); |
353 |
|
|
++added; |
354 |
|
|
q.put(new PDelay(0)); |
355 |
|
|
++added; |
356 |
dl |
1.3 |
threadAssertTrue(added == 4); |
357 |
dl |
1.1 |
} 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 |
dl |
1.4 |
unexpectedException(); |
369 |
dl |
1.1 |
} |
370 |
|
|
} |
371 |
|
|
|
372 |
dl |
1.4 |
/** |
373 |
|
|
* |
374 |
|
|
*/ |
375 |
dl |
1.1 |
public void testTimedOffer() { |
376 |
|
|
final DelayQueue q = new DelayQueue(); |
377 |
|
|
Thread t = new Thread(new Runnable() { |
378 |
dl |
1.4 |
public void run() { |
379 |
dl |
1.1 |
try { |
380 |
|
|
q.put(new PDelay(0)); |
381 |
|
|
q.put(new PDelay(0)); |
382 |
dl |
1.3 |
threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
383 |
|
|
threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
384 |
dl |
1.1 |
} finally { } |
385 |
|
|
} |
386 |
|
|
}); |
387 |
|
|
|
388 |
|
|
try { |
389 |
|
|
t.start(); |
390 |
dl |
1.3 |
Thread.sleep(SMALL_DELAY_MS); |
391 |
dl |
1.1 |
t.interrupt(); |
392 |
|
|
t.join(); |
393 |
|
|
} catch (Exception e){ |
394 |
dl |
1.4 |
unexpectedException(); |
395 |
dl |
1.1 |
} |
396 |
|
|
} |
397 |
|
|
|
398 |
dl |
1.4 |
/** |
399 |
|
|
* |
400 |
|
|
*/ |
401 |
|
|
public void testTake() { |
402 |
dl |
1.1 |
try { |
403 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
404 |
|
|
for (int i = 0; i < SIZE; ++i) { |
405 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
406 |
|
|
} |
407 |
|
|
} catch (InterruptedException e){ |
408 |
dl |
1.4 |
unexpectedException(); |
409 |
dl |
1.1 |
} |
410 |
|
|
} |
411 |
|
|
|
412 |
dl |
1.4 |
/** |
413 |
|
|
* |
414 |
|
|
*/ |
415 |
dl |
1.1 |
public void testTakeFromEmpty() { |
416 |
|
|
final DelayQueue q = new DelayQueue(); |
417 |
|
|
Thread t = new Thread(new Runnable() { |
418 |
dl |
1.4 |
public void run() { |
419 |
dl |
1.1 |
try { |
420 |
|
|
q.take(); |
421 |
dl |
1.4 |
threadShouldThrow(); |
422 |
dl |
1.1 |
} catch (InterruptedException success){ } |
423 |
|
|
} |
424 |
|
|
}); |
425 |
|
|
try { |
426 |
|
|
t.start(); |
427 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
428 |
|
|
t.interrupt(); |
429 |
|
|
t.join(); |
430 |
|
|
} catch (Exception e){ |
431 |
dl |
1.4 |
unexpectedException(); |
432 |
dl |
1.1 |
} |
433 |
|
|
} |
434 |
|
|
|
435 |
dl |
1.4 |
/** |
436 |
|
|
* |
437 |
|
|
*/ |
438 |
|
|
public void testBlockingTake() { |
439 |
dl |
1.1 |
Thread t = new Thread(new Runnable() { |
440 |
|
|
public void run() { |
441 |
|
|
try { |
442 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
443 |
|
|
for (int i = 0; i < SIZE; ++i) { |
444 |
|
|
threadAssertEquals(new PDelay(i), ((PDelay)q.take())); |
445 |
dl |
1.1 |
} |
446 |
|
|
q.take(); |
447 |
dl |
1.4 |
threadShouldThrow(); |
448 |
dl |
1.1 |
} catch (InterruptedException success){ |
449 |
|
|
} |
450 |
|
|
}}); |
451 |
|
|
t.start(); |
452 |
|
|
try { |
453 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
454 |
|
|
t.interrupt(); |
455 |
|
|
t.join(); |
456 |
|
|
} |
457 |
|
|
catch (InterruptedException ie) { |
458 |
dl |
1.4 |
unexpectedException(); |
459 |
dl |
1.1 |
} |
460 |
|
|
} |
461 |
|
|
|
462 |
|
|
|
463 |
dl |
1.4 |
/** |
464 |
|
|
* |
465 |
|
|
*/ |
466 |
|
|
public void testPoll() { |
467 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
468 |
|
|
for (int i = 0; i < SIZE; ++i) { |
469 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
470 |
|
|
} |
471 |
|
|
assertNull(q.poll()); |
472 |
|
|
} |
473 |
|
|
|
474 |
dl |
1.4 |
/** |
475 |
|
|
* |
476 |
|
|
*/ |
477 |
dl |
1.1 |
public void testTimedPoll0() { |
478 |
|
|
try { |
479 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
480 |
|
|
for (int i = 0; i < SIZE; ++i) { |
481 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.poll(0, TimeUnit.MILLISECONDS))); |
482 |
|
|
} |
483 |
|
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
484 |
|
|
} catch (InterruptedException e){ |
485 |
dl |
1.4 |
unexpectedException(); |
486 |
dl |
1.1 |
} |
487 |
|
|
} |
488 |
|
|
|
489 |
dl |
1.4 |
/** |
490 |
|
|
* |
491 |
|
|
*/ |
492 |
dl |
1.1 |
public void testTimedPoll() { |
493 |
|
|
try { |
494 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
495 |
|
|
for (int i = 0; i < SIZE; ++i) { |
496 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS))); |
497 |
|
|
} |
498 |
|
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
499 |
|
|
} catch (InterruptedException e){ |
500 |
dl |
1.4 |
unexpectedException(); |
501 |
dl |
1.1 |
} |
502 |
|
|
} |
503 |
|
|
|
504 |
dl |
1.4 |
/** |
505 |
|
|
* |
506 |
|
|
*/ |
507 |
|
|
public void testInterruptedTimedPoll() { |
508 |
dl |
1.1 |
Thread t = new Thread(new Runnable() { |
509 |
|
|
public void run() { |
510 |
|
|
try { |
511 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
512 |
|
|
for (int i = 0; i < SIZE; ++i) { |
513 |
|
|
threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS))); |
514 |
dl |
1.1 |
} |
515 |
dl |
1.3 |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
516 |
dl |
1.1 |
} catch (InterruptedException success){ |
517 |
|
|
} |
518 |
|
|
}}); |
519 |
|
|
t.start(); |
520 |
|
|
try { |
521 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
522 |
|
|
t.interrupt(); |
523 |
|
|
t.join(); |
524 |
|
|
} |
525 |
|
|
catch (InterruptedException ie) { |
526 |
dl |
1.4 |
unexpectedException(); |
527 |
dl |
1.1 |
} |
528 |
|
|
} |
529 |
|
|
|
530 |
dl |
1.4 |
/** |
531 |
|
|
* |
532 |
|
|
*/ |
533 |
|
|
public void testTimedPollWithOffer() { |
534 |
dl |
1.1 |
final DelayQueue q = new DelayQueue(); |
535 |
|
|
Thread t = new Thread(new Runnable() { |
536 |
dl |
1.4 |
public void run() { |
537 |
dl |
1.1 |
try { |
538 |
dl |
1.3 |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
539 |
dl |
1.1 |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
540 |
|
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
541 |
dl |
1.3 |
threadFail("Should block"); |
542 |
dl |
1.1 |
} catch (InterruptedException success) { } |
543 |
|
|
} |
544 |
|
|
}); |
545 |
|
|
try { |
546 |
|
|
t.start(); |
547 |
dl |
1.3 |
Thread.sleep(SMALL_DELAY_MS); |
548 |
dl |
1.1 |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
549 |
|
|
t.interrupt(); |
550 |
|
|
t.join(); |
551 |
|
|
} catch (Exception e){ |
552 |
dl |
1.4 |
unexpectedException(); |
553 |
dl |
1.1 |
} |
554 |
|
|
} |
555 |
|
|
|
556 |
|
|
|
557 |
dl |
1.4 |
/** |
558 |
|
|
* |
559 |
|
|
*/ |
560 |
|
|
public void testPeek() { |
561 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
562 |
|
|
for (int i = 0; i < SIZE; ++i) { |
563 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.peek())); |
564 |
|
|
q.poll(); |
565 |
|
|
assertTrue(q.peek() == null || |
566 |
|
|
i != ((PDelay)q.peek()).intValue()); |
567 |
|
|
} |
568 |
|
|
assertNull(q.peek()); |
569 |
|
|
} |
570 |
|
|
|
571 |
dl |
1.4 |
/** |
572 |
|
|
* |
573 |
|
|
*/ |
574 |
|
|
public void testElement() { |
575 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
576 |
|
|
for (int i = 0; i < SIZE; ++i) { |
577 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.element())); |
578 |
|
|
q.poll(); |
579 |
|
|
} |
580 |
|
|
try { |
581 |
|
|
q.element(); |
582 |
dl |
1.4 |
shouldThrow(); |
583 |
dl |
1.1 |
} |
584 |
|
|
catch (NoSuchElementException success) {} |
585 |
|
|
} |
586 |
|
|
|
587 |
dl |
1.4 |
/** |
588 |
|
|
* |
589 |
|
|
*/ |
590 |
|
|
public void testRemove() { |
591 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
592 |
|
|
for (int i = 0; i < SIZE; ++i) { |
593 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.remove())); |
594 |
|
|
} |
595 |
|
|
try { |
596 |
|
|
q.remove(); |
597 |
dl |
1.4 |
shouldThrow(); |
598 |
dl |
1.1 |
} catch (NoSuchElementException success){ |
599 |
|
|
} |
600 |
|
|
} |
601 |
|
|
|
602 |
dl |
1.4 |
/** |
603 |
|
|
* |
604 |
|
|
*/ |
605 |
|
|
public void testRemoveElement() { |
606 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
607 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
608 |
dl |
1.1 |
assertTrue(q.remove(new PDelay(i))); |
609 |
|
|
} |
610 |
dl |
1.3 |
for (int i = 0; i < SIZE; i+=2) { |
611 |
dl |
1.1 |
assertTrue(q.remove(new PDelay(i))); |
612 |
|
|
assertFalse(q.remove(new PDelay(i+1))); |
613 |
|
|
} |
614 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
615 |
dl |
1.1 |
} |
616 |
|
|
|
617 |
dl |
1.4 |
/** |
618 |
|
|
* |
619 |
|
|
*/ |
620 |
|
|
public void testContains() { |
621 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
622 |
|
|
for (int i = 0; i < SIZE; ++i) { |
623 |
dl |
1.1 |
assertTrue(q.contains(new PDelay(i))); |
624 |
|
|
q.poll(); |
625 |
|
|
assertFalse(q.contains(new PDelay(i))); |
626 |
|
|
} |
627 |
|
|
} |
628 |
|
|
|
629 |
dl |
1.4 |
/** |
630 |
|
|
* |
631 |
|
|
*/ |
632 |
|
|
public void testClear() { |
633 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
634 |
dl |
1.1 |
q.clear(); |
635 |
|
|
assertTrue(q.isEmpty()); |
636 |
|
|
assertEquals(0, q.size()); |
637 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
638 |
|
|
q.add(new PDelay(1)); |
639 |
|
|
assertFalse(q.isEmpty()); |
640 |
|
|
q.clear(); |
641 |
|
|
assertTrue(q.isEmpty()); |
642 |
|
|
} |
643 |
|
|
|
644 |
dl |
1.4 |
/** |
645 |
|
|
* |
646 |
|
|
*/ |
647 |
|
|
public void testContainsAll() { |
648 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
649 |
dl |
1.1 |
DelayQueue p = new DelayQueue(); |
650 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
651 |
dl |
1.1 |
assertTrue(q.containsAll(p)); |
652 |
|
|
assertFalse(p.containsAll(q)); |
653 |
|
|
p.add(new PDelay(i)); |
654 |
|
|
} |
655 |
|
|
assertTrue(p.containsAll(q)); |
656 |
|
|
} |
657 |
|
|
|
658 |
dl |
1.4 |
/** |
659 |
|
|
* |
660 |
|
|
*/ |
661 |
|
|
public void testRetainAll() { |
662 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
663 |
|
|
DelayQueue p = populatedQueue(SIZE); |
664 |
|
|
for (int i = 0; i < SIZE; ++i) { |
665 |
dl |
1.1 |
boolean changed = q.retainAll(p); |
666 |
|
|
if (i == 0) |
667 |
|
|
assertFalse(changed); |
668 |
|
|
else |
669 |
|
|
assertTrue(changed); |
670 |
|
|
|
671 |
|
|
assertTrue(q.containsAll(p)); |
672 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
673 |
dl |
1.1 |
p.remove(); |
674 |
|
|
} |
675 |
|
|
} |
676 |
|
|
|
677 |
dl |
1.4 |
/** |
678 |
|
|
* |
679 |
|
|
*/ |
680 |
|
|
public void testRemoveAll() { |
681 |
dl |
1.3 |
for (int i = 1; i < SIZE; ++i) { |
682 |
|
|
DelayQueue q = populatedQueue(SIZE); |
683 |
|
|
DelayQueue p = populatedQueue(i); |
684 |
dl |
1.1 |
assertTrue(q.removeAll(p)); |
685 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
686 |
dl |
1.1 |
for (int j = 0; j < i; ++j) { |
687 |
|
|
PDelay I = (PDelay)(p.remove()); |
688 |
|
|
assertFalse(q.contains(I)); |
689 |
|
|
} |
690 |
|
|
} |
691 |
|
|
} |
692 |
|
|
|
693 |
dl |
1.4 |
/** |
694 |
|
|
* |
695 |
|
|
*/ |
696 |
|
|
public void testToArray() { |
697 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
698 |
dl |
1.1 |
Object[] o = q.toArray(); |
699 |
|
|
Arrays.sort(o); |
700 |
|
|
try { |
701 |
|
|
for(int i = 0; i < o.length; i++) |
702 |
|
|
assertEquals(o[i], q.take()); |
703 |
|
|
} catch (InterruptedException e){ |
704 |
dl |
1.4 |
unexpectedException(); |
705 |
dl |
1.1 |
} |
706 |
|
|
} |
707 |
|
|
|
708 |
dl |
1.4 |
/** |
709 |
|
|
* |
710 |
|
|
*/ |
711 |
|
|
public void testToArray2() { |
712 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
713 |
|
|
PDelay[] ints = new PDelay[SIZE]; |
714 |
dl |
1.1 |
ints = (PDelay[])q.toArray(ints); |
715 |
|
|
Arrays.sort(ints); |
716 |
|
|
try { |
717 |
|
|
for(int i = 0; i < ints.length; i++) |
718 |
|
|
assertEquals(ints[i], q.take()); |
719 |
|
|
} catch (InterruptedException e){ |
720 |
dl |
1.4 |
unexpectedException(); |
721 |
dl |
1.1 |
} |
722 |
|
|
} |
723 |
|
|
|
724 |
dl |
1.4 |
/** |
725 |
|
|
* |
726 |
|
|
*/ |
727 |
|
|
public void testIterator() { |
728 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
729 |
dl |
1.1 |
int i = 0; |
730 |
|
|
Iterator it = q.iterator(); |
731 |
|
|
while(it.hasNext()) { |
732 |
|
|
assertTrue(q.contains(it.next())); |
733 |
|
|
++i; |
734 |
|
|
} |
735 |
dl |
1.3 |
assertEquals(i, SIZE); |
736 |
dl |
1.1 |
} |
737 |
|
|
|
738 |
dl |
1.4 |
/** |
739 |
|
|
* |
740 |
|
|
*/ |
741 |
dl |
1.1 |
public void testIteratorRemove () { |
742 |
|
|
|
743 |
|
|
final DelayQueue q = new DelayQueue(); |
744 |
|
|
|
745 |
|
|
q.add(new PDelay(2)); |
746 |
|
|
q.add(new PDelay(1)); |
747 |
|
|
q.add(new PDelay(3)); |
748 |
|
|
|
749 |
|
|
Iterator it = q.iterator(); |
750 |
|
|
it.next(); |
751 |
|
|
it.remove(); |
752 |
|
|
|
753 |
|
|
it = q.iterator(); |
754 |
|
|
assertEquals(it.next(), new PDelay(2)); |
755 |
|
|
assertEquals(it.next(), new PDelay(3)); |
756 |
|
|
assertFalse(it.hasNext()); |
757 |
|
|
} |
758 |
|
|
|
759 |
|
|
|
760 |
dl |
1.4 |
/** |
761 |
|
|
* |
762 |
|
|
*/ |
763 |
|
|
public void testToString() { |
764 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
765 |
dl |
1.1 |
String s = q.toString(); |
766 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
767 |
|
|
assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0); |
768 |
dl |
1.1 |
} |
769 |
|
|
} |
770 |
|
|
|
771 |
dl |
1.4 |
/** |
772 |
|
|
* |
773 |
|
|
*/ |
774 |
dl |
1.1 |
public void testPollInExecutor() { |
775 |
|
|
|
776 |
|
|
final DelayQueue q = new DelayQueue(); |
777 |
|
|
|
778 |
|
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
779 |
|
|
|
780 |
|
|
executor.execute(new Runnable() { |
781 |
|
|
public void run() { |
782 |
dl |
1.3 |
threadAssertNull(q.poll()); |
783 |
dl |
1.1 |
try { |
784 |
dl |
1.3 |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
785 |
|
|
threadAssertTrue(q.isEmpty()); |
786 |
dl |
1.1 |
} |
787 |
|
|
catch (InterruptedException e) { |
788 |
dl |
1.4 |
threadUnexpectedException(); |
789 |
dl |
1.1 |
} |
790 |
|
|
} |
791 |
|
|
}); |
792 |
|
|
|
793 |
|
|
executor.execute(new Runnable() { |
794 |
|
|
public void run() { |
795 |
|
|
try { |
796 |
dl |
1.3 |
Thread.sleep(SHORT_DELAY_MS); |
797 |
dl |
1.1 |
q.put(new PDelay(1)); |
798 |
|
|
} |
799 |
|
|
catch (InterruptedException e) { |
800 |
dl |
1.4 |
threadUnexpectedException(); |
801 |
dl |
1.1 |
} |
802 |
|
|
} |
803 |
|
|
}); |
804 |
|
|
|
805 |
dl |
1.3 |
joinPool(executor); |
806 |
dl |
1.1 |
|
807 |
|
|
} |
808 |
|
|
|
809 |
|
|
|
810 |
dl |
1.4 |
/** |
811 |
|
|
* |
812 |
|
|
*/ |
813 |
dl |
1.1 |
public void testDelay() { |
814 |
|
|
DelayQueue q = new DelayQueue(); |
815 |
dl |
1.3 |
NanoDelay[] elements = new NanoDelay[SIZE]; |
816 |
|
|
for (int i = 0; i < SIZE; ++i) { |
817 |
|
|
elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i)); |
818 |
dl |
1.1 |
} |
819 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
820 |
dl |
1.1 |
q.add(elements[i]); |
821 |
|
|
} |
822 |
|
|
|
823 |
|
|
try { |
824 |
|
|
long last = 0; |
825 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
826 |
dl |
1.1 |
NanoDelay e = (NanoDelay)(q.take()); |
827 |
|
|
long tt = e.getTriggerTime(); |
828 |
|
|
assertTrue(tt <= System.nanoTime()); |
829 |
|
|
if (i != 0) |
830 |
|
|
assertTrue(tt >= last); |
831 |
|
|
last = tt; |
832 |
|
|
} |
833 |
|
|
} |
834 |
|
|
catch(InterruptedException ie) { |
835 |
dl |
1.4 |
unexpectedException(); |
836 |
dl |
1.1 |
} |
837 |
|
|
} |
838 |
|
|
|
839 |
|
|
} |