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