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