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