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