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