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