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 |
dl |
1.5 |
* Most tests use Pseudodelays, where delays are all elapsed |
26 |
dl |
1.4 |
* (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 |
dl |
1.5 |
* A new queue has unbounded capacity |
133 |
dl |
1.4 |
*/ |
134 |
|
|
public void testConstructor1() { |
135 |
dl |
1.1 |
assertEquals(NOCAP, new DelayQueue().remainingCapacity()); |
136 |
|
|
} |
137 |
|
|
|
138 |
dl |
1.4 |
/** |
139 |
dl |
1.5 |
* Initializing from null Collection throws NPE |
140 |
dl |
1.4 |
*/ |
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 |
dl |
1.5 |
* Initializing from Collection of null elements throws NPE |
151 |
dl |
1.4 |
*/ |
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 |
dl |
1.5 |
* Initializing from Collection with some null elements throws NPE |
163 |
dl |
1.4 |
*/ |
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 |
dl |
1.5 |
* Queue contains all elements of collection used to initialize |
177 |
dl |
1.4 |
*/ |
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 |
dl |
1.5 |
* isEmpty is true before add, false after |
192 |
dl |
1.4 |
*/ |
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 |
dl |
1.5 |
* remainingCapacity does not change when elementa added or removed, |
207 |
|
|
* but size does |
208 |
dl |
1.4 |
*/ |
209 |
|
|
public void testRemainingCapacity() { |
210 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
211 |
|
|
for (int i = 0; i < SIZE; ++i) { |
212 |
dl |
1.1 |
assertEquals(NOCAP, q.remainingCapacity()); |
213 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
214 |
dl |
1.1 |
q.remove(); |
215 |
|
|
} |
216 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
217 |
dl |
1.1 |
assertEquals(NOCAP, q.remainingCapacity()); |
218 |
|
|
assertEquals(i, q.size()); |
219 |
|
|
q.add(new PDelay(i)); |
220 |
|
|
} |
221 |
|
|
} |
222 |
|
|
|
223 |
dl |
1.4 |
/** |
224 |
dl |
1.5 |
* offer(null) throws NPE |
225 |
dl |
1.4 |
*/ |
226 |
|
|
public void testOfferNull() { |
227 |
dl |
1.1 |
try { |
228 |
|
|
DelayQueue q = new DelayQueue(); |
229 |
|
|
q.offer(null); |
230 |
dl |
1.4 |
shouldThrow(); |
231 |
dl |
1.1 |
} catch (NullPointerException success) { } |
232 |
|
|
} |
233 |
|
|
|
234 |
dl |
1.4 |
/** |
235 |
dl |
1.5 |
* offer non-null succeeds |
236 |
dl |
1.4 |
*/ |
237 |
dl |
1.1 |
public void testOffer() { |
238 |
|
|
DelayQueue q = new DelayQueue(); |
239 |
|
|
assertTrue(q.offer(new PDelay(0))); |
240 |
|
|
assertTrue(q.offer(new PDelay(1))); |
241 |
|
|
} |
242 |
|
|
|
243 |
dl |
1.4 |
/** |
244 |
dl |
1.5 |
* add succeeds |
245 |
dl |
1.4 |
*/ |
246 |
|
|
public void testAdd() { |
247 |
dl |
1.1 |
DelayQueue q = new DelayQueue(); |
248 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
249 |
dl |
1.1 |
assertEquals(i, q.size()); |
250 |
|
|
assertTrue(q.add(new PDelay(i))); |
251 |
|
|
} |
252 |
|
|
} |
253 |
|
|
|
254 |
dl |
1.4 |
/** |
255 |
dl |
1.5 |
* addAll(null) throws NPE |
256 |
dl |
1.4 |
*/ |
257 |
|
|
public void testAddAll1() { |
258 |
dl |
1.1 |
try { |
259 |
|
|
DelayQueue q = new DelayQueue(); |
260 |
|
|
q.addAll(null); |
261 |
dl |
1.4 |
shouldThrow(); |
262 |
dl |
1.1 |
} |
263 |
|
|
catch (NullPointerException success) {} |
264 |
|
|
} |
265 |
dl |
1.4 |
/** |
266 |
dl |
1.5 |
* addAll of a collection with null elements throws NPE |
267 |
dl |
1.4 |
*/ |
268 |
|
|
public void testAddAll2() { |
269 |
dl |
1.1 |
try { |
270 |
|
|
DelayQueue q = new DelayQueue(); |
271 |
dl |
1.3 |
PDelay[] ints = new PDelay[SIZE]; |
272 |
dl |
1.1 |
q.addAll(Arrays.asList(ints)); |
273 |
dl |
1.4 |
shouldThrow(); |
274 |
dl |
1.1 |
} |
275 |
|
|
catch (NullPointerException success) {} |
276 |
|
|
} |
277 |
dl |
1.4 |
/** |
278 |
dl |
1.5 |
* addAll of a collection with any null elements throws NPE after |
279 |
|
|
* possibly adding some elements |
280 |
dl |
1.4 |
*/ |
281 |
|
|
public void testAddAll3() { |
282 |
dl |
1.1 |
try { |
283 |
|
|
DelayQueue q = new DelayQueue(); |
284 |
dl |
1.3 |
PDelay[] ints = new PDelay[SIZE]; |
285 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
286 |
dl |
1.1 |
ints[i] = new PDelay(i); |
287 |
|
|
q.addAll(Arrays.asList(ints)); |
288 |
dl |
1.4 |
shouldThrow(); |
289 |
dl |
1.1 |
} |
290 |
|
|
catch (NullPointerException success) {} |
291 |
|
|
} |
292 |
|
|
|
293 |
dl |
1.4 |
/** |
294 |
dl |
1.5 |
* Queue contains all elements of successful addAll |
295 |
dl |
1.4 |
*/ |
296 |
|
|
public void testAddAll5() { |
297 |
dl |
1.1 |
try { |
298 |
|
|
PDelay[] empty = new PDelay[0]; |
299 |
dl |
1.3 |
PDelay[] ints = new PDelay[SIZE]; |
300 |
|
|
for (int i = SIZE-1; i >= 0; --i) |
301 |
dl |
1.1 |
ints[i] = new PDelay(i); |
302 |
|
|
DelayQueue q = new DelayQueue(); |
303 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
304 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
305 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) |
306 |
dl |
1.1 |
assertEquals(ints[i], q.poll()); |
307 |
|
|
} |
308 |
|
|
finally {} |
309 |
|
|
} |
310 |
|
|
|
311 |
dl |
1.4 |
/** |
312 |
dl |
1.5 |
* put(null) throws NPE |
313 |
dl |
1.4 |
*/ |
314 |
dl |
1.1 |
public void testPutNull() { |
315 |
|
|
try { |
316 |
|
|
DelayQueue q = new DelayQueue(); |
317 |
|
|
q.put(null); |
318 |
dl |
1.4 |
shouldThrow(); |
319 |
dl |
1.1 |
} |
320 |
|
|
catch (NullPointerException success){ |
321 |
|
|
} |
322 |
|
|
} |
323 |
|
|
|
324 |
dl |
1.4 |
/** |
325 |
dl |
1.5 |
* all elements successfully put are contained |
326 |
dl |
1.4 |
*/ |
327 |
dl |
1.1 |
public void testPut() { |
328 |
|
|
try { |
329 |
|
|
DelayQueue q = new DelayQueue(); |
330 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
331 |
dl |
1.1 |
PDelay I = new PDelay(i); |
332 |
|
|
q.put(I); |
333 |
|
|
assertTrue(q.contains(I)); |
334 |
|
|
} |
335 |
dl |
1.3 |
assertEquals(SIZE, q.size()); |
336 |
dl |
1.1 |
} |
337 |
|
|
finally { |
338 |
|
|
} |
339 |
|
|
} |
340 |
|
|
|
341 |
dl |
1.4 |
/** |
342 |
dl |
1.5 |
* put doesn't block waiting for take |
343 |
dl |
1.4 |
*/ |
344 |
dl |
1.1 |
public void testPutWithTake() { |
345 |
|
|
final DelayQueue q = new DelayQueue(); |
346 |
|
|
Thread t = new Thread(new Runnable() { |
347 |
dl |
1.4 |
public void run() { |
348 |
dl |
1.1 |
int added = 0; |
349 |
|
|
try { |
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 |
|
|
q.put(new PDelay(0)); |
357 |
|
|
++added; |
358 |
dl |
1.3 |
threadAssertTrue(added == 4); |
359 |
dl |
1.1 |
} finally { |
360 |
|
|
} |
361 |
|
|
} |
362 |
|
|
}); |
363 |
|
|
try { |
364 |
|
|
t.start(); |
365 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
366 |
|
|
q.take(); |
367 |
|
|
t.interrupt(); |
368 |
|
|
t.join(); |
369 |
|
|
} catch (Exception e){ |
370 |
dl |
1.4 |
unexpectedException(); |
371 |
dl |
1.1 |
} |
372 |
|
|
} |
373 |
|
|
|
374 |
dl |
1.4 |
/** |
375 |
dl |
1.5 |
* timed offer does not time out |
376 |
dl |
1.4 |
*/ |
377 |
dl |
1.1 |
public void testTimedOffer() { |
378 |
|
|
final DelayQueue q = new DelayQueue(); |
379 |
|
|
Thread t = new Thread(new Runnable() { |
380 |
dl |
1.4 |
public void run() { |
381 |
dl |
1.1 |
try { |
382 |
|
|
q.put(new PDelay(0)); |
383 |
|
|
q.put(new PDelay(0)); |
384 |
dl |
1.3 |
threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
385 |
|
|
threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
386 |
dl |
1.1 |
} finally { } |
387 |
|
|
} |
388 |
|
|
}); |
389 |
|
|
|
390 |
|
|
try { |
391 |
|
|
t.start(); |
392 |
dl |
1.3 |
Thread.sleep(SMALL_DELAY_MS); |
393 |
dl |
1.1 |
t.interrupt(); |
394 |
|
|
t.join(); |
395 |
|
|
} catch (Exception e){ |
396 |
dl |
1.4 |
unexpectedException(); |
397 |
dl |
1.1 |
} |
398 |
|
|
} |
399 |
|
|
|
400 |
dl |
1.4 |
/** |
401 |
dl |
1.5 |
* take retrieves elements in priority order |
402 |
dl |
1.4 |
*/ |
403 |
|
|
public void testTake() { |
404 |
dl |
1.1 |
try { |
405 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
406 |
|
|
for (int i = 0; i < SIZE; ++i) { |
407 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
408 |
|
|
} |
409 |
|
|
} catch (InterruptedException e){ |
410 |
dl |
1.4 |
unexpectedException(); |
411 |
dl |
1.1 |
} |
412 |
|
|
} |
413 |
|
|
|
414 |
dl |
1.4 |
/** |
415 |
dl |
1.5 |
* take blocks interruptibly when empty |
416 |
dl |
1.4 |
*/ |
417 |
dl |
1.1 |
public void testTakeFromEmpty() { |
418 |
|
|
final DelayQueue q = new DelayQueue(); |
419 |
|
|
Thread t = new Thread(new Runnable() { |
420 |
dl |
1.4 |
public void run() { |
421 |
dl |
1.1 |
try { |
422 |
|
|
q.take(); |
423 |
dl |
1.4 |
threadShouldThrow(); |
424 |
dl |
1.1 |
} catch (InterruptedException success){ } |
425 |
|
|
} |
426 |
|
|
}); |
427 |
|
|
try { |
428 |
|
|
t.start(); |
429 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
430 |
|
|
t.interrupt(); |
431 |
|
|
t.join(); |
432 |
|
|
} catch (Exception e){ |
433 |
dl |
1.4 |
unexpectedException(); |
434 |
dl |
1.1 |
} |
435 |
|
|
} |
436 |
|
|
|
437 |
dl |
1.4 |
/** |
438 |
dl |
1.5 |
* Take removes existing elements until empty, then blocks interruptibly |
439 |
dl |
1.4 |
*/ |
440 |
|
|
public void testBlockingTake() { |
441 |
dl |
1.1 |
Thread t = new Thread(new Runnable() { |
442 |
|
|
public void run() { |
443 |
|
|
try { |
444 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
445 |
|
|
for (int i = 0; i < SIZE; ++i) { |
446 |
|
|
threadAssertEquals(new PDelay(i), ((PDelay)q.take())); |
447 |
dl |
1.1 |
} |
448 |
|
|
q.take(); |
449 |
dl |
1.4 |
threadShouldThrow(); |
450 |
dl |
1.1 |
} catch (InterruptedException success){ |
451 |
|
|
} |
452 |
|
|
}}); |
453 |
|
|
t.start(); |
454 |
|
|
try { |
455 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
456 |
|
|
t.interrupt(); |
457 |
|
|
t.join(); |
458 |
|
|
} |
459 |
|
|
catch (InterruptedException ie) { |
460 |
dl |
1.4 |
unexpectedException(); |
461 |
dl |
1.1 |
} |
462 |
|
|
} |
463 |
|
|
|
464 |
|
|
|
465 |
dl |
1.4 |
/** |
466 |
dl |
1.5 |
* poll succeeds unless empty |
467 |
dl |
1.4 |
*/ |
468 |
|
|
public void testPoll() { |
469 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
470 |
|
|
for (int i = 0; i < SIZE; ++i) { |
471 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
472 |
|
|
} |
473 |
|
|
assertNull(q.poll()); |
474 |
|
|
} |
475 |
|
|
|
476 |
dl |
1.4 |
/** |
477 |
dl |
1.5 |
* timed pool with zero timeout succeeds when non-empty, else times out |
478 |
dl |
1.4 |
*/ |
479 |
dl |
1.1 |
public void testTimedPoll0() { |
480 |
|
|
try { |
481 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
482 |
|
|
for (int i = 0; i < SIZE; ++i) { |
483 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.poll(0, TimeUnit.MILLISECONDS))); |
484 |
|
|
} |
485 |
|
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
486 |
|
|
} catch (InterruptedException e){ |
487 |
dl |
1.4 |
unexpectedException(); |
488 |
dl |
1.1 |
} |
489 |
|
|
} |
490 |
|
|
|
491 |
dl |
1.4 |
/** |
492 |
dl |
1.5 |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
493 |
dl |
1.4 |
*/ |
494 |
dl |
1.1 |
public void testTimedPoll() { |
495 |
|
|
try { |
496 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
497 |
|
|
for (int i = 0; i < SIZE; ++i) { |
498 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS))); |
499 |
|
|
} |
500 |
|
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
501 |
|
|
} catch (InterruptedException e){ |
502 |
dl |
1.4 |
unexpectedException(); |
503 |
dl |
1.1 |
} |
504 |
|
|
} |
505 |
|
|
|
506 |
dl |
1.4 |
/** |
507 |
dl |
1.5 |
* Interrupted timed poll throws InterruptedException instead of |
508 |
|
|
* returning timeout status |
509 |
dl |
1.4 |
*/ |
510 |
|
|
public void testInterruptedTimedPoll() { |
511 |
dl |
1.1 |
Thread t = new Thread(new Runnable() { |
512 |
|
|
public void run() { |
513 |
|
|
try { |
514 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
515 |
|
|
for (int i = 0; i < SIZE; ++i) { |
516 |
|
|
threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS))); |
517 |
dl |
1.1 |
} |
518 |
dl |
1.3 |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
519 |
dl |
1.1 |
} catch (InterruptedException success){ |
520 |
|
|
} |
521 |
|
|
}}); |
522 |
|
|
t.start(); |
523 |
|
|
try { |
524 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
525 |
|
|
t.interrupt(); |
526 |
|
|
t.join(); |
527 |
|
|
} |
528 |
|
|
catch (InterruptedException ie) { |
529 |
dl |
1.4 |
unexpectedException(); |
530 |
dl |
1.1 |
} |
531 |
|
|
} |
532 |
|
|
|
533 |
dl |
1.4 |
/** |
534 |
dl |
1.5 |
* timed poll before a delayed offer fails; after offer succeeds; |
535 |
|
|
* on interruption throws |
536 |
dl |
1.4 |
*/ |
537 |
|
|
public void testTimedPollWithOffer() { |
538 |
dl |
1.1 |
final DelayQueue q = new DelayQueue(); |
539 |
|
|
Thread t = new Thread(new Runnable() { |
540 |
dl |
1.4 |
public void run() { |
541 |
dl |
1.1 |
try { |
542 |
dl |
1.3 |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
543 |
dl |
1.1 |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
544 |
|
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
545 |
dl |
1.3 |
threadFail("Should block"); |
546 |
dl |
1.1 |
} catch (InterruptedException success) { } |
547 |
|
|
} |
548 |
|
|
}); |
549 |
|
|
try { |
550 |
|
|
t.start(); |
551 |
dl |
1.3 |
Thread.sleep(SMALL_DELAY_MS); |
552 |
dl |
1.1 |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
553 |
|
|
t.interrupt(); |
554 |
|
|
t.join(); |
555 |
|
|
} catch (Exception e){ |
556 |
dl |
1.4 |
unexpectedException(); |
557 |
dl |
1.1 |
} |
558 |
|
|
} |
559 |
|
|
|
560 |
|
|
|
561 |
dl |
1.4 |
/** |
562 |
dl |
1.5 |
* peek returns next element, or null if empty |
563 |
dl |
1.4 |
*/ |
564 |
|
|
public void testPeek() { |
565 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
566 |
|
|
for (int i = 0; i < SIZE; ++i) { |
567 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.peek())); |
568 |
|
|
q.poll(); |
569 |
|
|
assertTrue(q.peek() == null || |
570 |
|
|
i != ((PDelay)q.peek()).intValue()); |
571 |
|
|
} |
572 |
|
|
assertNull(q.peek()); |
573 |
|
|
} |
574 |
|
|
|
575 |
dl |
1.4 |
/** |
576 |
dl |
1.5 |
* element returns next element, or throws NSEE if empty |
577 |
dl |
1.4 |
*/ |
578 |
|
|
public void testElement() { |
579 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
580 |
|
|
for (int i = 0; i < SIZE; ++i) { |
581 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.element())); |
582 |
|
|
q.poll(); |
583 |
|
|
} |
584 |
|
|
try { |
585 |
|
|
q.element(); |
586 |
dl |
1.4 |
shouldThrow(); |
587 |
dl |
1.1 |
} |
588 |
|
|
catch (NoSuchElementException success) {} |
589 |
|
|
} |
590 |
|
|
|
591 |
dl |
1.4 |
/** |
592 |
dl |
1.5 |
* remove removes next element, or throws NSEE if empty |
593 |
dl |
1.4 |
*/ |
594 |
|
|
public void testRemove() { |
595 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
596 |
|
|
for (int i = 0; i < SIZE; ++i) { |
597 |
dl |
1.1 |
assertEquals(new PDelay(i), ((PDelay)q.remove())); |
598 |
|
|
} |
599 |
|
|
try { |
600 |
|
|
q.remove(); |
601 |
dl |
1.4 |
shouldThrow(); |
602 |
dl |
1.1 |
} catch (NoSuchElementException success){ |
603 |
|
|
} |
604 |
|
|
} |
605 |
|
|
|
606 |
dl |
1.4 |
/** |
607 |
dl |
1.5 |
* remove(x) removes x and returns true if present |
608 |
dl |
1.4 |
*/ |
609 |
|
|
public void testRemoveElement() { |
610 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
611 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
612 |
dl |
1.1 |
assertTrue(q.remove(new PDelay(i))); |
613 |
|
|
} |
614 |
dl |
1.3 |
for (int i = 0; i < SIZE; i+=2) { |
615 |
dl |
1.1 |
assertTrue(q.remove(new PDelay(i))); |
616 |
|
|
assertFalse(q.remove(new PDelay(i+1))); |
617 |
|
|
} |
618 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
619 |
dl |
1.1 |
} |
620 |
|
|
|
621 |
dl |
1.4 |
/** |
622 |
dl |
1.5 |
* contains(x) reports true when elements added but not yet removed |
623 |
dl |
1.4 |
*/ |
624 |
|
|
public void testContains() { |
625 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
626 |
|
|
for (int i = 0; i < SIZE; ++i) { |
627 |
dl |
1.1 |
assertTrue(q.contains(new PDelay(i))); |
628 |
|
|
q.poll(); |
629 |
|
|
assertFalse(q.contains(new PDelay(i))); |
630 |
|
|
} |
631 |
|
|
} |
632 |
|
|
|
633 |
dl |
1.4 |
/** |
634 |
dl |
1.5 |
* clear removes all elements |
635 |
dl |
1.4 |
*/ |
636 |
|
|
public void testClear() { |
637 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
638 |
dl |
1.1 |
q.clear(); |
639 |
|
|
assertTrue(q.isEmpty()); |
640 |
|
|
assertEquals(0, q.size()); |
641 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
642 |
|
|
q.add(new PDelay(1)); |
643 |
|
|
assertFalse(q.isEmpty()); |
644 |
|
|
q.clear(); |
645 |
|
|
assertTrue(q.isEmpty()); |
646 |
|
|
} |
647 |
|
|
|
648 |
dl |
1.4 |
/** |
649 |
dl |
1.5 |
* containsAll(c) is true when c contains a subset of elements |
650 |
dl |
1.4 |
*/ |
651 |
|
|
public void testContainsAll() { |
652 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
653 |
dl |
1.1 |
DelayQueue p = new DelayQueue(); |
654 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
655 |
dl |
1.1 |
assertTrue(q.containsAll(p)); |
656 |
|
|
assertFalse(p.containsAll(q)); |
657 |
|
|
p.add(new PDelay(i)); |
658 |
|
|
} |
659 |
|
|
assertTrue(p.containsAll(q)); |
660 |
|
|
} |
661 |
|
|
|
662 |
dl |
1.4 |
/** |
663 |
dl |
1.5 |
* retainAll(c) retains only those elements of c and reports true if changed |
664 |
dl |
1.4 |
*/ |
665 |
|
|
public void testRetainAll() { |
666 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
667 |
|
|
DelayQueue p = populatedQueue(SIZE); |
668 |
|
|
for (int i = 0; i < SIZE; ++i) { |
669 |
dl |
1.1 |
boolean changed = q.retainAll(p); |
670 |
|
|
if (i == 0) |
671 |
|
|
assertFalse(changed); |
672 |
|
|
else |
673 |
|
|
assertTrue(changed); |
674 |
|
|
|
675 |
|
|
assertTrue(q.containsAll(p)); |
676 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
677 |
dl |
1.1 |
p.remove(); |
678 |
|
|
} |
679 |
|
|
} |
680 |
|
|
|
681 |
dl |
1.4 |
/** |
682 |
dl |
1.5 |
* removeAll(c) removes only those elements of c and reports true if changed |
683 |
dl |
1.4 |
*/ |
684 |
|
|
public void testRemoveAll() { |
685 |
dl |
1.3 |
for (int i = 1; i < SIZE; ++i) { |
686 |
|
|
DelayQueue q = populatedQueue(SIZE); |
687 |
|
|
DelayQueue p = populatedQueue(i); |
688 |
dl |
1.1 |
assertTrue(q.removeAll(p)); |
689 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
690 |
dl |
1.1 |
for (int j = 0; j < i; ++j) { |
691 |
|
|
PDelay I = (PDelay)(p.remove()); |
692 |
|
|
assertFalse(q.contains(I)); |
693 |
|
|
} |
694 |
|
|
} |
695 |
|
|
} |
696 |
|
|
|
697 |
dl |
1.4 |
/** |
698 |
dl |
1.5 |
* toArray contains all elements |
699 |
dl |
1.4 |
*/ |
700 |
|
|
public void testToArray() { |
701 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
702 |
dl |
1.1 |
Object[] o = q.toArray(); |
703 |
|
|
Arrays.sort(o); |
704 |
|
|
try { |
705 |
|
|
for(int i = 0; i < o.length; i++) |
706 |
|
|
assertEquals(o[i], q.take()); |
707 |
|
|
} catch (InterruptedException e){ |
708 |
dl |
1.4 |
unexpectedException(); |
709 |
dl |
1.1 |
} |
710 |
|
|
} |
711 |
|
|
|
712 |
dl |
1.4 |
/** |
713 |
dl |
1.5 |
* toArray(a) contains all elements |
714 |
dl |
1.4 |
*/ |
715 |
|
|
public void testToArray2() { |
716 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
717 |
|
|
PDelay[] ints = new PDelay[SIZE]; |
718 |
dl |
1.1 |
ints = (PDelay[])q.toArray(ints); |
719 |
|
|
Arrays.sort(ints); |
720 |
|
|
try { |
721 |
|
|
for(int i = 0; i < ints.length; i++) |
722 |
|
|
assertEquals(ints[i], q.take()); |
723 |
|
|
} catch (InterruptedException e){ |
724 |
dl |
1.4 |
unexpectedException(); |
725 |
dl |
1.1 |
} |
726 |
|
|
} |
727 |
|
|
|
728 |
dl |
1.4 |
/** |
729 |
dl |
1.5 |
* iterator iterates through all elements |
730 |
dl |
1.4 |
*/ |
731 |
|
|
public void testIterator() { |
732 |
dl |
1.3 |
DelayQueue q = populatedQueue(SIZE); |
733 |
dl |
1.1 |
int i = 0; |
734 |
|
|
Iterator it = q.iterator(); |
735 |
|
|
while(it.hasNext()) { |
736 |
|
|
assertTrue(q.contains(it.next())); |
737 |
|
|
++i; |
738 |
|
|
} |
739 |
dl |
1.3 |
assertEquals(i, SIZE); |
740 |
dl |
1.1 |
} |
741 |
|
|
|
742 |
dl |
1.4 |
/** |
743 |
dl |
1.5 |
* iterator.remove removes current element |
744 |
dl |
1.4 |
*/ |
745 |
dl |
1.1 |
public void testIteratorRemove () { |
746 |
|
|
final DelayQueue q = new DelayQueue(); |
747 |
|
|
q.add(new PDelay(2)); |
748 |
|
|
q.add(new PDelay(1)); |
749 |
|
|
q.add(new PDelay(3)); |
750 |
|
|
Iterator it = q.iterator(); |
751 |
|
|
it.next(); |
752 |
|
|
it.remove(); |
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 |
dl |
1.5 |
* toString contains toStrings of elements |
762 |
dl |
1.4 |
*/ |
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 |
dl |
1.5 |
* offer transfers elements across Executor tasks |
773 |
dl |
1.4 |
*/ |
774 |
dl |
1.1 |
public void testPollInExecutor() { |
775 |
|
|
final DelayQueue q = new DelayQueue(); |
776 |
|
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
777 |
|
|
executor.execute(new Runnable() { |
778 |
|
|
public void run() { |
779 |
dl |
1.3 |
threadAssertNull(q.poll()); |
780 |
dl |
1.1 |
try { |
781 |
dl |
1.3 |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
782 |
|
|
threadAssertTrue(q.isEmpty()); |
783 |
dl |
1.1 |
} |
784 |
|
|
catch (InterruptedException e) { |
785 |
dl |
1.4 |
threadUnexpectedException(); |
786 |
dl |
1.1 |
} |
787 |
|
|
} |
788 |
|
|
}); |
789 |
|
|
|
790 |
|
|
executor.execute(new Runnable() { |
791 |
|
|
public void run() { |
792 |
|
|
try { |
793 |
dl |
1.3 |
Thread.sleep(SHORT_DELAY_MS); |
794 |
dl |
1.1 |
q.put(new PDelay(1)); |
795 |
|
|
} |
796 |
|
|
catch (InterruptedException e) { |
797 |
dl |
1.4 |
threadUnexpectedException(); |
798 |
dl |
1.1 |
} |
799 |
|
|
} |
800 |
|
|
}); |
801 |
dl |
1.3 |
joinPool(executor); |
802 |
dl |
1.1 |
|
803 |
|
|
} |
804 |
|
|
|
805 |
|
|
|
806 |
dl |
1.4 |
/** |
807 |
dl |
1.5 |
* Dekayed actions do not occur until their delay elapses |
808 |
dl |
1.4 |
*/ |
809 |
dl |
1.1 |
public void testDelay() { |
810 |
|
|
DelayQueue q = new DelayQueue(); |
811 |
dl |
1.3 |
NanoDelay[] elements = new NanoDelay[SIZE]; |
812 |
|
|
for (int i = 0; i < SIZE; ++i) { |
813 |
|
|
elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i)); |
814 |
dl |
1.1 |
} |
815 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
816 |
dl |
1.1 |
q.add(elements[i]); |
817 |
|
|
} |
818 |
|
|
|
819 |
|
|
try { |
820 |
|
|
long last = 0; |
821 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
822 |
dl |
1.1 |
NanoDelay e = (NanoDelay)(q.take()); |
823 |
|
|
long tt = e.getTriggerTime(); |
824 |
|
|
assertTrue(tt <= System.nanoTime()); |
825 |
|
|
if (i != 0) |
826 |
|
|
assertTrue(tt >= last); |
827 |
|
|
last = tt; |
828 |
|
|
} |
829 |
|
|
} |
830 |
|
|
catch(InterruptedException ie) { |
831 |
dl |
1.4 |
unexpectedException(); |
832 |
dl |
1.1 |
} |
833 |
|
|
} |
834 |
|
|
|
835 |
|
|
} |