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 |
|
|
public class DelayQueueTest extends TestCase { |
13 |
|
|
|
14 |
|
|
private static final int N = 10; |
15 |
|
|
private static final long SHORT_DELAY_MS = 100; |
16 |
|
|
private static final long MEDIUM_DELAY_MS = 1000; |
17 |
|
|
private static final long LONG_DELAY_MS = 10000; |
18 |
|
|
private static final int NOCAP = Integer.MAX_VALUE; |
19 |
|
|
|
20 |
|
|
public static void main(String[] args) { |
21 |
|
|
junit.textui.TestRunner.run (suite()); |
22 |
|
|
} |
23 |
|
|
|
24 |
|
|
public static Test suite() { |
25 |
|
|
return new TestSuite(DelayQueueTest.class); |
26 |
|
|
} |
27 |
|
|
|
28 |
|
|
// Most Q/BQ tests use Pseudodelays, where delays are all elapsed |
29 |
|
|
// (so, no blocking solely for delays) but are still ordered |
30 |
|
|
|
31 |
|
|
static class PDelay implements Delayed { |
32 |
|
|
int pseudodelay; |
33 |
|
|
PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } |
34 |
|
|
public int compareTo(Object y) { |
35 |
|
|
int i = pseudodelay; |
36 |
|
|
int j = ((PDelay)y).pseudodelay; |
37 |
|
|
if (i < j) return -1; |
38 |
|
|
if (i > j) return 1; |
39 |
|
|
return 0; |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
public int compareTo(PDelay y) { |
43 |
|
|
int i = pseudodelay; |
44 |
|
|
int j = ((PDelay)y).pseudodelay; |
45 |
|
|
if (i < j) return -1; |
46 |
|
|
if (i > j) return 1; |
47 |
|
|
return 0; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
public boolean equals(Object other) { |
51 |
|
|
return ((PDelay)other).pseudodelay == pseudodelay; |
52 |
|
|
} |
53 |
|
|
public boolean equals(PDelay other) { |
54 |
|
|
return ((PDelay)other).pseudodelay == pseudodelay; |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
|
58 |
|
|
public long getDelay(TimeUnit ignore) { |
59 |
|
|
return pseudodelay; |
60 |
|
|
} |
61 |
|
|
public int intValue() { |
62 |
|
|
return pseudodelay; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
public String toString() { |
66 |
|
|
return String.valueOf(pseudodelay); |
67 |
|
|
} |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
/** |
74 |
|
|
* Create a queue of given size containing consecutive |
75 |
|
|
* PDelays 0 ... n. |
76 |
|
|
*/ |
77 |
|
|
private DelayQueue fullQueue(int n) { |
78 |
|
|
DelayQueue q = new DelayQueue(); |
79 |
|
|
assertTrue(q.isEmpty()); |
80 |
|
|
for(int i = n-1; i >= 0; i-=2) |
81 |
|
|
assertTrue(q.offer(new PDelay(i))); |
82 |
|
|
for(int i = (n & 1); i < n; i+=2) |
83 |
|
|
assertTrue(q.offer(new PDelay(i))); |
84 |
|
|
assertFalse(q.isEmpty()); |
85 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
86 |
|
|
assertEquals(n, q.size()); |
87 |
|
|
return q; |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
public void testConstructor1(){ |
91 |
|
|
assertEquals(NOCAP, new DelayQueue().remainingCapacity()); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
public void testConstructor3(){ |
95 |
|
|
|
96 |
|
|
try { |
97 |
|
|
DelayQueue q = new DelayQueue(null); |
98 |
|
|
fail("Cannot make from null collection"); |
99 |
|
|
} |
100 |
|
|
catch (NullPointerException success) {} |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
public void testConstructor4(){ |
104 |
|
|
try { |
105 |
|
|
PDelay[] ints = new PDelay[N]; |
106 |
|
|
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
107 |
|
|
fail("Cannot make with null elements"); |
108 |
|
|
} |
109 |
|
|
catch (NullPointerException success) {} |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
public void testConstructor5(){ |
113 |
|
|
try { |
114 |
|
|
PDelay[] ints = new PDelay[N]; |
115 |
|
|
for (int i = 0; i < N-1; ++i) |
116 |
|
|
ints[i] = new PDelay(i); |
117 |
|
|
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
118 |
|
|
fail("Cannot make with null elements"); |
119 |
|
|
} |
120 |
|
|
catch (NullPointerException success) {} |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
public void testConstructor6(){ |
124 |
|
|
try { |
125 |
|
|
PDelay[] ints = new PDelay[N]; |
126 |
|
|
for (int i = 0; i < N; ++i) |
127 |
|
|
ints[i] = new PDelay(i); |
128 |
|
|
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
129 |
|
|
for (int i = 0; i < N; ++i) |
130 |
|
|
assertEquals(ints[i], q.poll()); |
131 |
|
|
} |
132 |
|
|
finally {} |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
public void testEmpty() { |
136 |
|
|
DelayQueue q = new DelayQueue(); |
137 |
|
|
assertTrue(q.isEmpty()); |
138 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
139 |
|
|
q.add(new PDelay(1)); |
140 |
|
|
assertFalse(q.isEmpty()); |
141 |
|
|
q.add(new PDelay(2)); |
142 |
|
|
q.remove(); |
143 |
|
|
q.remove(); |
144 |
|
|
assertTrue(q.isEmpty()); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
public void testRemainingCapacity(){ |
148 |
|
|
DelayQueue q = fullQueue(N); |
149 |
|
|
for (int i = 0; i < N; ++i) { |
150 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
151 |
|
|
assertEquals(N-i, q.size()); |
152 |
|
|
q.remove(); |
153 |
|
|
} |
154 |
|
|
for (int i = 0; i < N; ++i) { |
155 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
156 |
|
|
assertEquals(i, q.size()); |
157 |
|
|
q.add(new PDelay(i)); |
158 |
|
|
} |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
public void testOfferNull(){ |
162 |
|
|
try { |
163 |
|
|
DelayQueue q = new DelayQueue(); |
164 |
|
|
q.offer(null); |
165 |
|
|
fail("should throw NPE"); |
166 |
|
|
} catch (NullPointerException success) { } |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
public void testOffer() { |
170 |
|
|
DelayQueue q = new DelayQueue(); |
171 |
|
|
assertTrue(q.offer(new PDelay(0))); |
172 |
|
|
assertTrue(q.offer(new PDelay(1))); |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
public void testAdd(){ |
176 |
|
|
DelayQueue q = new DelayQueue(); |
177 |
|
|
for (int i = 0; i < N; ++i) { |
178 |
|
|
assertEquals(i, q.size()); |
179 |
|
|
assertTrue(q.add(new PDelay(i))); |
180 |
|
|
} |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
public void testAddAll1(){ |
184 |
|
|
try { |
185 |
|
|
DelayQueue q = new DelayQueue(); |
186 |
|
|
q.addAll(null); |
187 |
|
|
fail("Cannot add null collection"); |
188 |
|
|
} |
189 |
|
|
catch (NullPointerException success) {} |
190 |
|
|
} |
191 |
|
|
public void testAddAll2(){ |
192 |
|
|
try { |
193 |
|
|
DelayQueue q = new DelayQueue(); |
194 |
|
|
PDelay[] ints = new PDelay[N]; |
195 |
|
|
q.addAll(Arrays.asList(ints)); |
196 |
|
|
fail("Cannot add null elements"); |
197 |
|
|
} |
198 |
|
|
catch (NullPointerException success) {} |
199 |
|
|
} |
200 |
|
|
public void testAddAll3(){ |
201 |
|
|
try { |
202 |
|
|
DelayQueue q = new DelayQueue(); |
203 |
|
|
PDelay[] ints = new PDelay[N]; |
204 |
|
|
for (int i = 0; i < N-1; ++i) |
205 |
|
|
ints[i] = new PDelay(i); |
206 |
|
|
q.addAll(Arrays.asList(ints)); |
207 |
|
|
fail("Cannot add null elements"); |
208 |
|
|
} |
209 |
|
|
catch (NullPointerException success) {} |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
public void testAddAll5(){ |
213 |
|
|
try { |
214 |
|
|
PDelay[] empty = new PDelay[0]; |
215 |
|
|
PDelay[] ints = new PDelay[N]; |
216 |
|
|
for (int i = N-1; i >= 0; --i) |
217 |
|
|
ints[i] = new PDelay(i); |
218 |
|
|
DelayQueue q = new DelayQueue(); |
219 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
220 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
221 |
|
|
for (int i = 0; i < N; ++i) |
222 |
|
|
assertEquals(ints[i], q.poll()); |
223 |
|
|
} |
224 |
|
|
finally {} |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
public void testPutNull() { |
228 |
|
|
try { |
229 |
|
|
DelayQueue q = new DelayQueue(); |
230 |
|
|
q.put(null); |
231 |
|
|
fail("put should throw NPE"); |
232 |
|
|
} |
233 |
|
|
catch (NullPointerException success){ |
234 |
|
|
} |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
public void testPut() { |
238 |
|
|
try { |
239 |
|
|
DelayQueue q = new DelayQueue(); |
240 |
|
|
for (int i = 0; i < N; ++i) { |
241 |
|
|
PDelay I = new PDelay(i); |
242 |
|
|
q.put(I); |
243 |
|
|
assertTrue(q.contains(I)); |
244 |
|
|
} |
245 |
|
|
assertEquals(N, q.size()); |
246 |
|
|
} |
247 |
|
|
finally { |
248 |
|
|
} |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
public void testPutWithTake() { |
252 |
|
|
final DelayQueue q = new DelayQueue(); |
253 |
|
|
Thread t = new Thread(new Runnable() { |
254 |
|
|
public void run(){ |
255 |
|
|
int added = 0; |
256 |
|
|
try { |
257 |
|
|
q.put(new PDelay(0)); |
258 |
|
|
++added; |
259 |
|
|
q.put(new PDelay(0)); |
260 |
|
|
++added; |
261 |
|
|
q.put(new PDelay(0)); |
262 |
|
|
++added; |
263 |
|
|
q.put(new PDelay(0)); |
264 |
|
|
++added; |
265 |
|
|
assertTrue(added == 4); |
266 |
|
|
} finally { |
267 |
|
|
} |
268 |
|
|
} |
269 |
|
|
}); |
270 |
|
|
try { |
271 |
|
|
t.start(); |
272 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
273 |
|
|
q.take(); |
274 |
|
|
t.interrupt(); |
275 |
|
|
t.join(); |
276 |
|
|
} catch (Exception e){ |
277 |
|
|
fail("Unexpected exception"); |
278 |
|
|
} |
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
public void testTimedOffer() { |
282 |
|
|
final DelayQueue q = new DelayQueue(); |
283 |
|
|
Thread t = new Thread(new Runnable() { |
284 |
|
|
public void run(){ |
285 |
|
|
try { |
286 |
|
|
q.put(new PDelay(0)); |
287 |
|
|
q.put(new PDelay(0)); |
288 |
|
|
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
289 |
|
|
assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
290 |
|
|
} finally { } |
291 |
|
|
} |
292 |
|
|
}); |
293 |
|
|
|
294 |
|
|
try { |
295 |
|
|
t.start(); |
296 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
297 |
|
|
t.interrupt(); |
298 |
|
|
t.join(); |
299 |
|
|
} catch (Exception e){ |
300 |
|
|
fail("Unexpected exception"); |
301 |
|
|
} |
302 |
|
|
} |
303 |
|
|
|
304 |
|
|
public void testTake(){ |
305 |
|
|
try { |
306 |
|
|
DelayQueue q = fullQueue(N); |
307 |
|
|
for (int i = 0; i < N; ++i) { |
308 |
|
|
assertEquals(new PDelay(i), ((PDelay)q.take())); |
309 |
|
|
} |
310 |
|
|
} catch (InterruptedException e){ |
311 |
|
|
fail("Unexpected exception"); |
312 |
|
|
} |
313 |
|
|
} |
314 |
|
|
|
315 |
|
|
public void testTakeFromEmpty() { |
316 |
|
|
final DelayQueue q = new DelayQueue(); |
317 |
|
|
Thread t = new Thread(new Runnable() { |
318 |
|
|
public void run(){ |
319 |
|
|
try { |
320 |
|
|
q.take(); |
321 |
|
|
fail("Should block"); |
322 |
|
|
} catch (InterruptedException success){ } |
323 |
|
|
} |
324 |
|
|
}); |
325 |
|
|
try { |
326 |
|
|
t.start(); |
327 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
328 |
|
|
t.interrupt(); |
329 |
|
|
t.join(); |
330 |
|
|
} catch (Exception e){ |
331 |
|
|
fail("Unexpected exception"); |
332 |
|
|
} |
333 |
|
|
} |
334 |
|
|
|
335 |
|
|
public void testBlockingTake(){ |
336 |
|
|
Thread t = new Thread(new Runnable() { |
337 |
|
|
public void run() { |
338 |
|
|
try { |
339 |
|
|
DelayQueue q = fullQueue(N); |
340 |
|
|
for (int i = 0; i < N; ++i) { |
341 |
|
|
assertEquals(new PDelay(i), ((PDelay)q.take())); |
342 |
|
|
} |
343 |
|
|
q.take(); |
344 |
|
|
fail("take should block"); |
345 |
|
|
} catch (InterruptedException success){ |
346 |
|
|
} |
347 |
|
|
}}); |
348 |
|
|
t.start(); |
349 |
|
|
try { |
350 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
351 |
|
|
t.interrupt(); |
352 |
|
|
t.join(); |
353 |
|
|
} |
354 |
|
|
catch (InterruptedException ie) { |
355 |
|
|
fail("Unexpected exception"); |
356 |
|
|
} |
357 |
|
|
} |
358 |
|
|
|
359 |
|
|
|
360 |
|
|
public void testPoll(){ |
361 |
|
|
DelayQueue q = fullQueue(N); |
362 |
|
|
for (int i = 0; i < N; ++i) { |
363 |
|
|
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
364 |
|
|
} |
365 |
|
|
assertNull(q.poll()); |
366 |
|
|
} |
367 |
|
|
|
368 |
|
|
public void testTimedPoll0() { |
369 |
|
|
try { |
370 |
|
|
DelayQueue q = fullQueue(N); |
371 |
|
|
for (int i = 0; i < N; ++i) { |
372 |
|
|
assertEquals(new PDelay(i), ((PDelay)q.poll(0, TimeUnit.MILLISECONDS))); |
373 |
|
|
} |
374 |
|
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
375 |
|
|
} catch (InterruptedException e){ |
376 |
|
|
fail("Unexpected exception"); |
377 |
|
|
} |
378 |
|
|
} |
379 |
|
|
|
380 |
|
|
public void testTimedPoll() { |
381 |
|
|
try { |
382 |
|
|
DelayQueue q = fullQueue(N); |
383 |
|
|
for (int i = 0; i < N; ++i) { |
384 |
|
|
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS))); |
385 |
|
|
} |
386 |
|
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
387 |
|
|
} catch (InterruptedException e){ |
388 |
|
|
fail("Unexpected exception"); |
389 |
|
|
} |
390 |
|
|
} |
391 |
|
|
|
392 |
|
|
public void testInterruptedTimedPoll(){ |
393 |
|
|
Thread t = new Thread(new Runnable() { |
394 |
|
|
public void run() { |
395 |
|
|
try { |
396 |
|
|
DelayQueue q = fullQueue(N); |
397 |
|
|
for (int i = 0; i < N; ++i) { |
398 |
|
|
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS))); |
399 |
|
|
} |
400 |
|
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
401 |
|
|
} catch (InterruptedException success){ |
402 |
|
|
} |
403 |
|
|
}}); |
404 |
|
|
t.start(); |
405 |
|
|
try { |
406 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
407 |
|
|
t.interrupt(); |
408 |
|
|
t.join(); |
409 |
|
|
} |
410 |
|
|
catch (InterruptedException ie) { |
411 |
|
|
fail("Unexpected exception"); |
412 |
|
|
} |
413 |
|
|
} |
414 |
|
|
|
415 |
|
|
public void testTimedPollWithOffer(){ |
416 |
|
|
final DelayQueue q = new DelayQueue(); |
417 |
|
|
Thread t = new Thread(new Runnable() { |
418 |
|
|
public void run(){ |
419 |
|
|
try { |
420 |
|
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
421 |
|
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
422 |
|
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
423 |
|
|
fail("Should block"); |
424 |
|
|
} catch (InterruptedException success) { } |
425 |
|
|
} |
426 |
|
|
}); |
427 |
|
|
try { |
428 |
|
|
t.start(); |
429 |
|
|
Thread.sleep(SHORT_DELAY_MS * 2); |
430 |
|
|
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
431 |
|
|
t.interrupt(); |
432 |
|
|
t.join(); |
433 |
|
|
} catch (Exception e){ |
434 |
|
|
fail("Unexpected exception"); |
435 |
|
|
} |
436 |
|
|
} |
437 |
|
|
|
438 |
|
|
|
439 |
|
|
public void testPeek(){ |
440 |
|
|
DelayQueue q = fullQueue(N); |
441 |
|
|
for (int i = 0; i < N; ++i) { |
442 |
|
|
assertEquals(new PDelay(i), ((PDelay)q.peek())); |
443 |
|
|
q.poll(); |
444 |
|
|
assertTrue(q.peek() == null || |
445 |
|
|
i != ((PDelay)q.peek()).intValue()); |
446 |
|
|
} |
447 |
|
|
assertNull(q.peek()); |
448 |
|
|
} |
449 |
|
|
|
450 |
|
|
public void testElement(){ |
451 |
|
|
DelayQueue q = fullQueue(N); |
452 |
|
|
for (int i = 0; i < N; ++i) { |
453 |
|
|
assertEquals(new PDelay(i), ((PDelay)q.element())); |
454 |
|
|
q.poll(); |
455 |
|
|
} |
456 |
|
|
try { |
457 |
|
|
q.element(); |
458 |
|
|
fail("no such element"); |
459 |
|
|
} |
460 |
|
|
catch (NoSuchElementException success) {} |
461 |
|
|
} |
462 |
|
|
|
463 |
|
|
public void testRemove(){ |
464 |
|
|
DelayQueue q = fullQueue(N); |
465 |
|
|
for (int i = 0; i < N; ++i) { |
466 |
|
|
assertEquals(new PDelay(i), ((PDelay)q.remove())); |
467 |
|
|
} |
468 |
|
|
try { |
469 |
|
|
q.remove(); |
470 |
|
|
fail("remove should throw"); |
471 |
|
|
} catch (NoSuchElementException success){ |
472 |
|
|
} |
473 |
|
|
} |
474 |
|
|
|
475 |
|
|
public void testRemoveElement(){ |
476 |
|
|
DelayQueue q = fullQueue(N); |
477 |
|
|
for (int i = 1; i < N; i+=2) { |
478 |
|
|
assertTrue(q.remove(new PDelay(i))); |
479 |
|
|
} |
480 |
|
|
for (int i = 0; i < N; i+=2) { |
481 |
|
|
assertTrue(q.remove(new PDelay(i))); |
482 |
|
|
assertFalse(q.remove(new PDelay(i+1))); |
483 |
|
|
} |
484 |
|
|
assert(q.isEmpty()); |
485 |
|
|
} |
486 |
|
|
|
487 |
|
|
public void testContains(){ |
488 |
|
|
DelayQueue q = fullQueue(N); |
489 |
|
|
for (int i = 0; i < N; ++i) { |
490 |
|
|
assertTrue(q.contains(new PDelay(i))); |
491 |
|
|
q.poll(); |
492 |
|
|
assertFalse(q.contains(new PDelay(i))); |
493 |
|
|
} |
494 |
|
|
} |
495 |
|
|
|
496 |
|
|
public void testClear(){ |
497 |
|
|
DelayQueue q = fullQueue(N); |
498 |
|
|
q.clear(); |
499 |
|
|
assertTrue(q.isEmpty()); |
500 |
|
|
assertEquals(0, q.size()); |
501 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
502 |
|
|
q.add(new PDelay(1)); |
503 |
|
|
assertFalse(q.isEmpty()); |
504 |
|
|
q.clear(); |
505 |
|
|
assertTrue(q.isEmpty()); |
506 |
|
|
} |
507 |
|
|
|
508 |
|
|
public void testContainsAll(){ |
509 |
|
|
DelayQueue q = fullQueue(N); |
510 |
|
|
DelayQueue p = new DelayQueue(); |
511 |
|
|
for (int i = 0; i < N; ++i) { |
512 |
|
|
assertTrue(q.containsAll(p)); |
513 |
|
|
assertFalse(p.containsAll(q)); |
514 |
|
|
p.add(new PDelay(i)); |
515 |
|
|
} |
516 |
|
|
assertTrue(p.containsAll(q)); |
517 |
|
|
} |
518 |
|
|
|
519 |
|
|
public void testRetainAll(){ |
520 |
|
|
DelayQueue q = fullQueue(N); |
521 |
|
|
DelayQueue p = fullQueue(N); |
522 |
|
|
for (int i = 0; i < N; ++i) { |
523 |
|
|
boolean changed = q.retainAll(p); |
524 |
|
|
if (i == 0) |
525 |
|
|
assertFalse(changed); |
526 |
|
|
else |
527 |
|
|
assertTrue(changed); |
528 |
|
|
|
529 |
|
|
assertTrue(q.containsAll(p)); |
530 |
|
|
assertEquals(N-i, q.size()); |
531 |
|
|
p.remove(); |
532 |
|
|
} |
533 |
|
|
} |
534 |
|
|
|
535 |
|
|
public void testRemoveAll(){ |
536 |
|
|
for (int i = 1; i < N; ++i) { |
537 |
|
|
DelayQueue q = fullQueue(N); |
538 |
|
|
DelayQueue p = fullQueue(i); |
539 |
|
|
assertTrue(q.removeAll(p)); |
540 |
|
|
assertEquals(N-i, q.size()); |
541 |
|
|
for (int j = 0; j < i; ++j) { |
542 |
|
|
PDelay I = (PDelay)(p.remove()); |
543 |
|
|
assertFalse(q.contains(I)); |
544 |
|
|
} |
545 |
|
|
} |
546 |
|
|
} |
547 |
|
|
|
548 |
|
|
/* |
549 |
|
|
public void testEqualsAndHashCode(){ |
550 |
|
|
DelayQueue q1 = fullQueue(N); |
551 |
|
|
DelayQueue q2 = fullQueue(N); |
552 |
|
|
assertTrue(q1.equals(q2)); |
553 |
|
|
assertTrue(q2.equals(q1)); |
554 |
|
|
assertEquals(q1.hashCode(), q2.hashCode()); |
555 |
|
|
q1.remove(); |
556 |
|
|
assertFalse(q1.equals(q2)); |
557 |
|
|
assertFalse(q2.equals(q1)); |
558 |
|
|
assertFalse(q1.hashCode() == q2.hashCode()); |
559 |
|
|
q2.remove(); |
560 |
|
|
assertTrue(q1.equals(q2)); |
561 |
|
|
assertTrue(q2.equals(q1)); |
562 |
|
|
assertEquals(q1.hashCode(), q2.hashCode()); |
563 |
|
|
} |
564 |
|
|
*/ |
565 |
|
|
|
566 |
|
|
public void testToArray(){ |
567 |
|
|
DelayQueue q = fullQueue(N); |
568 |
|
|
Object[] o = q.toArray(); |
569 |
|
|
Arrays.sort(o); |
570 |
|
|
try { |
571 |
|
|
for(int i = 0; i < o.length; i++) |
572 |
|
|
assertEquals(o[i], q.take()); |
573 |
|
|
} catch (InterruptedException e){ |
574 |
|
|
fail("Unexpected exception"); |
575 |
|
|
} |
576 |
|
|
} |
577 |
|
|
|
578 |
|
|
public void testToArray2(){ |
579 |
|
|
DelayQueue q = fullQueue(N); |
580 |
|
|
PDelay[] ints = new PDelay[N]; |
581 |
|
|
ints = (PDelay[])q.toArray(ints); |
582 |
|
|
Arrays.sort(ints); |
583 |
|
|
try { |
584 |
|
|
for(int i = 0; i < ints.length; i++) |
585 |
|
|
assertEquals(ints[i], q.take()); |
586 |
|
|
} catch (InterruptedException e){ |
587 |
|
|
fail("Unexpected exception"); |
588 |
|
|
} |
589 |
|
|
} |
590 |
|
|
|
591 |
|
|
public void testIterator(){ |
592 |
|
|
DelayQueue q = fullQueue(N); |
593 |
|
|
int i = 0; |
594 |
|
|
Iterator it = q.iterator(); |
595 |
|
|
while(it.hasNext()) { |
596 |
|
|
assertTrue(q.contains(it.next())); |
597 |
|
|
++i; |
598 |
|
|
} |
599 |
|
|
assertEquals(i, N); |
600 |
|
|
} |
601 |
|
|
|
602 |
|
|
public void testIteratorRemove () { |
603 |
|
|
|
604 |
|
|
final DelayQueue q = new DelayQueue(); |
605 |
|
|
|
606 |
|
|
q.add(new PDelay(2)); |
607 |
|
|
q.add(new PDelay(1)); |
608 |
|
|
q.add(new PDelay(3)); |
609 |
|
|
|
610 |
|
|
Iterator it = q.iterator(); |
611 |
|
|
it.next(); |
612 |
|
|
it.remove(); |
613 |
|
|
|
614 |
|
|
it = q.iterator(); |
615 |
|
|
assertEquals(it.next(), new PDelay(2)); |
616 |
|
|
assertEquals(it.next(), new PDelay(3)); |
617 |
|
|
assertFalse(it.hasNext()); |
618 |
|
|
} |
619 |
|
|
|
620 |
|
|
|
621 |
|
|
public void testToString(){ |
622 |
|
|
DelayQueue q = fullQueue(N); |
623 |
|
|
String s = q.toString(); |
624 |
|
|
for (int i = 0; i < N; ++i) { |
625 |
|
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
626 |
|
|
} |
627 |
|
|
} |
628 |
|
|
|
629 |
|
|
public void testPollInExecutor() { |
630 |
|
|
|
631 |
|
|
final DelayQueue q = new DelayQueue(); |
632 |
|
|
|
633 |
|
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
634 |
|
|
|
635 |
|
|
executor.execute(new Runnable() { |
636 |
|
|
public void run() { |
637 |
|
|
assertNull("poll should fail", q.poll()); |
638 |
|
|
try { |
639 |
|
|
assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
640 |
|
|
assertTrue(q.isEmpty()); |
641 |
|
|
} |
642 |
|
|
catch (InterruptedException e) { |
643 |
|
|
fail("should not be interrupted"); |
644 |
|
|
} |
645 |
|
|
} |
646 |
|
|
}); |
647 |
|
|
|
648 |
|
|
executor.execute(new Runnable() { |
649 |
|
|
public void run() { |
650 |
|
|
try { |
651 |
|
|
Thread.sleep(MEDIUM_DELAY_MS); |
652 |
|
|
q.put(new PDelay(1)); |
653 |
|
|
} |
654 |
|
|
catch (InterruptedException e) { |
655 |
|
|
fail("should not be interrupted"); |
656 |
|
|
} |
657 |
|
|
} |
658 |
|
|
}); |
659 |
|
|
|
660 |
|
|
executor.shutdown(); |
661 |
|
|
|
662 |
|
|
} |
663 |
|
|
|
664 |
|
|
static class NanoDelay implements Delayed { |
665 |
|
|
long trigger; |
666 |
|
|
NanoDelay(long i) { |
667 |
|
|
trigger = System.nanoTime() + i; |
668 |
|
|
} |
669 |
|
|
public int compareTo(Object y) { |
670 |
|
|
long i = trigger; |
671 |
|
|
long j = ((NanoDelay)y).trigger; |
672 |
|
|
if (i < j) return -1; |
673 |
|
|
if (i > j) return 1; |
674 |
|
|
return 0; |
675 |
|
|
} |
676 |
|
|
|
677 |
|
|
public int compareTo(NanoDelay y) { |
678 |
|
|
long i = trigger; |
679 |
|
|
long j = ((NanoDelay)y).trigger; |
680 |
|
|
if (i < j) return -1; |
681 |
|
|
if (i > j) return 1; |
682 |
|
|
return 0; |
683 |
|
|
} |
684 |
|
|
|
685 |
|
|
public boolean equals(Object other) { |
686 |
|
|
return ((NanoDelay)other).trigger == trigger; |
687 |
|
|
} |
688 |
|
|
public boolean equals(NanoDelay other) { |
689 |
|
|
return ((NanoDelay)other).trigger == trigger; |
690 |
|
|
} |
691 |
|
|
|
692 |
|
|
public long getDelay(TimeUnit unit) { |
693 |
|
|
long n = trigger - System.nanoTime(); |
694 |
|
|
return unit.convert(n, TimeUnit.NANOSECONDS); |
695 |
|
|
} |
696 |
|
|
|
697 |
|
|
public long getTriggerTime() { |
698 |
|
|
return trigger; |
699 |
|
|
} |
700 |
|
|
|
701 |
|
|
public String toString() { |
702 |
|
|
return String.valueOf(trigger); |
703 |
|
|
} |
704 |
|
|
} |
705 |
|
|
|
706 |
|
|
public void testDelay() { |
707 |
|
|
DelayQueue q = new DelayQueue(); |
708 |
|
|
NanoDelay[] elements = new NanoDelay[N]; |
709 |
|
|
for (int i = 0; i < N; ++i) { |
710 |
|
|
elements[i] = new NanoDelay(1000000000L + 1000000L * (N - i)); |
711 |
|
|
} |
712 |
|
|
for (int i = 0; i < N; ++i) { |
713 |
|
|
q.add(elements[i]); |
714 |
|
|
} |
715 |
|
|
|
716 |
|
|
try { |
717 |
|
|
long last = 0; |
718 |
|
|
for (int i = 0; i < N; ++i) { |
719 |
|
|
NanoDelay e = (NanoDelay)(q.take()); |
720 |
|
|
long tt = e.getTriggerTime(); |
721 |
|
|
assertTrue(tt <= System.nanoTime()); |
722 |
|
|
if (i != 0) |
723 |
|
|
assertTrue(tt >= last); |
724 |
|
|
last = tt; |
725 |
|
|
} |
726 |
|
|
} |
727 |
|
|
catch(InterruptedException ie) { |
728 |
|
|
fail("Unexpected Exception"); |
729 |
|
|
} |
730 |
|
|
} |
731 |
|
|
|
732 |
|
|
} |