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