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