1 |
dl |
1.1 |
/* |
2 |
dl |
1.8 |
* 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/licenses/publicdomain |
5 |
jsr166 |
1.12 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.*; |
11 |
|
|
import java.util.concurrent.*; |
12 |
dl |
1.2 |
import java.io.*; |
13 |
dl |
1.1 |
|
14 |
dl |
1.3 |
public class PriorityBlockingQueueTest extends JSR166TestCase { |
15 |
dl |
1.1 |
public static void main(String[] args) { |
16 |
jsr166 |
1.12 |
junit.textui.TestRunner.run (suite()); |
17 |
dl |
1.1 |
} |
18 |
|
|
public static Test suite() { |
19 |
|
|
return new TestSuite(PriorityBlockingQueueTest.class); |
20 |
|
|
} |
21 |
|
|
|
22 |
dl |
1.3 |
private static final int NOCAP = Integer.MAX_VALUE; |
23 |
|
|
|
24 |
|
|
/** Sample Comparator */ |
25 |
jsr166 |
1.12 |
static class MyReverseComparator implements Comparator { |
26 |
dl |
1.1 |
public int compare(Object x, Object y) { |
27 |
|
|
int i = ((Integer)x).intValue(); |
28 |
|
|
int j = ((Integer)y).intValue(); |
29 |
|
|
if (i < j) return 1; |
30 |
|
|
if (i > j) return -1; |
31 |
|
|
return 0; |
32 |
|
|
} |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
/** |
36 |
|
|
* Create a queue of given size containing consecutive |
37 |
|
|
* Integers 0 ... n. |
38 |
|
|
*/ |
39 |
dl |
1.3 |
private PriorityBlockingQueue populatedQueue(int n) { |
40 |
dl |
1.1 |
PriorityBlockingQueue q = new PriorityBlockingQueue(n); |
41 |
|
|
assertTrue(q.isEmpty()); |
42 |
jsr166 |
1.13 |
for (int i = n-1; i >= 0; i-=2) |
43 |
dl |
1.1 |
assertTrue(q.offer(new Integer(i))); |
44 |
jsr166 |
1.13 |
for (int i = (n & 1); i < n; i+=2) |
45 |
dl |
1.1 |
assertTrue(q.offer(new Integer(i))); |
46 |
|
|
assertFalse(q.isEmpty()); |
47 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
48 |
|
|
assertEquals(n, q.size()); |
49 |
|
|
return q; |
50 |
|
|
} |
51 |
jsr166 |
1.12 |
|
52 |
dl |
1.5 |
/** |
53 |
dl |
1.6 |
* A new queue has unbounded capacity |
54 |
dl |
1.5 |
*/ |
55 |
|
|
public void testConstructor1() { |
56 |
dl |
1.3 |
assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity()); |
57 |
dl |
1.1 |
} |
58 |
|
|
|
59 |
dl |
1.5 |
/** |
60 |
dl |
1.6 |
* Constructor throws IAE if capacity argument nonpositive |
61 |
dl |
1.5 |
*/ |
62 |
|
|
public void testConstructor2() { |
63 |
dl |
1.1 |
try { |
64 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(0); |
65 |
dl |
1.5 |
shouldThrow(); |
66 |
jsr166 |
1.15 |
} catch (IllegalArgumentException success) {} |
67 |
dl |
1.1 |
} |
68 |
|
|
|
69 |
dl |
1.5 |
/** |
70 |
dl |
1.6 |
* Initializing from null Collection throws NPE |
71 |
dl |
1.5 |
*/ |
72 |
|
|
public void testConstructor3() { |
73 |
dl |
1.1 |
try { |
74 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(null); |
75 |
dl |
1.5 |
shouldThrow(); |
76 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
77 |
dl |
1.1 |
} |
78 |
|
|
|
79 |
dl |
1.5 |
/** |
80 |
dl |
1.6 |
* Initializing from Collection of null elements throws NPE |
81 |
dl |
1.5 |
*/ |
82 |
|
|
public void testConstructor4() { |
83 |
dl |
1.1 |
try { |
84 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
85 |
dl |
1.1 |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
86 |
dl |
1.5 |
shouldThrow(); |
87 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
88 |
dl |
1.1 |
} |
89 |
|
|
|
90 |
dl |
1.5 |
/** |
91 |
dl |
1.6 |
* Initializing from Collection with some null elements throws NPE |
92 |
dl |
1.5 |
*/ |
93 |
|
|
public void testConstructor5() { |
94 |
dl |
1.1 |
try { |
95 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
96 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
97 |
dl |
1.1 |
ints[i] = new Integer(i); |
98 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
99 |
dl |
1.5 |
shouldThrow(); |
100 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
101 |
dl |
1.1 |
} |
102 |
|
|
|
103 |
dl |
1.5 |
/** |
104 |
dl |
1.6 |
* Queue contains all elements of collection used to initialize |
105 |
dl |
1.5 |
*/ |
106 |
|
|
public void testConstructor6() { |
107 |
jsr166 |
1.15 |
Integer[] ints = new Integer[SIZE]; |
108 |
|
|
for (int i = 0; i < SIZE; ++i) |
109 |
|
|
ints[i] = new Integer(i); |
110 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
111 |
|
|
for (int i = 0; i < SIZE; ++i) |
112 |
|
|
assertEquals(ints[i], q.poll()); |
113 |
dl |
1.1 |
} |
114 |
|
|
|
115 |
dl |
1.5 |
/** |
116 |
dl |
1.6 |
* The comparator used in constructor is used |
117 |
dl |
1.5 |
*/ |
118 |
|
|
public void testConstructor7() { |
119 |
jsr166 |
1.15 |
MyReverseComparator cmp = new MyReverseComparator(); |
120 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp); |
121 |
|
|
assertEquals(cmp, q.comparator()); |
122 |
|
|
Integer[] ints = new Integer[SIZE]; |
123 |
|
|
for (int i = 0; i < SIZE; ++i) |
124 |
|
|
ints[i] = new Integer(i); |
125 |
|
|
q.addAll(Arrays.asList(ints)); |
126 |
|
|
for (int i = SIZE-1; i >= 0; --i) |
127 |
|
|
assertEquals(ints[i], q.poll()); |
128 |
dl |
1.1 |
} |
129 |
|
|
|
130 |
dl |
1.5 |
/** |
131 |
dl |
1.6 |
* isEmpty is true before add, false after |
132 |
dl |
1.5 |
*/ |
133 |
dl |
1.1 |
public void testEmpty() { |
134 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
135 |
|
|
assertTrue(q.isEmpty()); |
136 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
137 |
dl |
1.6 |
q.add(one); |
138 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
139 |
dl |
1.6 |
q.add(two); |
140 |
dl |
1.1 |
q.remove(); |
141 |
|
|
q.remove(); |
142 |
|
|
assertTrue(q.isEmpty()); |
143 |
|
|
} |
144 |
|
|
|
145 |
dl |
1.5 |
/** |
146 |
dl |
1.9 |
* remainingCapacity does not change when elements added or removed, |
147 |
dl |
1.6 |
* but size does |
148 |
dl |
1.5 |
*/ |
149 |
|
|
public void testRemainingCapacity() { |
150 |
dl |
1.3 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
151 |
|
|
for (int i = 0; i < SIZE; ++i) { |
152 |
dl |
1.1 |
assertEquals(NOCAP, q.remainingCapacity()); |
153 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
154 |
dl |
1.1 |
q.remove(); |
155 |
|
|
} |
156 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
157 |
dl |
1.1 |
assertEquals(NOCAP, q.remainingCapacity()); |
158 |
|
|
assertEquals(i, q.size()); |
159 |
|
|
q.add(new Integer(i)); |
160 |
|
|
} |
161 |
|
|
} |
162 |
|
|
|
163 |
dl |
1.5 |
/** |
164 |
dl |
1.6 |
* offer(null) throws NPE |
165 |
dl |
1.5 |
*/ |
166 |
|
|
public void testOfferNull() { |
167 |
dl |
1.1 |
try { |
168 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
169 |
|
|
q.offer(null); |
170 |
dl |
1.5 |
shouldThrow(); |
171 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
172 |
dl |
1.1 |
} |
173 |
|
|
|
174 |
dl |
1.5 |
/** |
175 |
dl |
1.7 |
* add(null) throws NPE |
176 |
|
|
*/ |
177 |
|
|
public void testAddNull() { |
178 |
|
|
try { |
179 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
180 |
|
|
q.add(null); |
181 |
|
|
shouldThrow(); |
182 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
183 |
dl |
1.7 |
} |
184 |
|
|
|
185 |
|
|
/** |
186 |
dl |
1.6 |
* Offer of comparable element succeeds |
187 |
dl |
1.5 |
*/ |
188 |
dl |
1.1 |
public void testOffer() { |
189 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
190 |
dl |
1.6 |
assertTrue(q.offer(zero)); |
191 |
|
|
assertTrue(q.offer(one)); |
192 |
dl |
1.1 |
} |
193 |
|
|
|
194 |
dl |
1.5 |
/** |
195 |
dl |
1.6 |
* Offer of non-Comparable throws CCE |
196 |
dl |
1.5 |
*/ |
197 |
dl |
1.1 |
public void testOfferNonComparable() { |
198 |
|
|
try { |
199 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
200 |
|
|
q.offer(new Object()); |
201 |
|
|
q.offer(new Object()); |
202 |
|
|
q.offer(new Object()); |
203 |
dl |
1.5 |
shouldThrow(); |
204 |
jsr166 |
1.15 |
} catch (ClassCastException success) {} |
205 |
dl |
1.1 |
} |
206 |
|
|
|
207 |
dl |
1.5 |
/** |
208 |
dl |
1.6 |
* add of comparable succeeds |
209 |
dl |
1.5 |
*/ |
210 |
|
|
public void testAdd() { |
211 |
dl |
1.3 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
212 |
|
|
for (int i = 0; i < SIZE; ++i) { |
213 |
dl |
1.1 |
assertEquals(i, q.size()); |
214 |
|
|
assertTrue(q.add(new Integer(i))); |
215 |
|
|
} |
216 |
|
|
} |
217 |
|
|
|
218 |
dl |
1.5 |
/** |
219 |
dl |
1.6 |
* addAll(null) throws NPE |
220 |
dl |
1.5 |
*/ |
221 |
|
|
public void testAddAll1() { |
222 |
dl |
1.1 |
try { |
223 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
224 |
|
|
q.addAll(null); |
225 |
dl |
1.5 |
shouldThrow(); |
226 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
227 |
dl |
1.1 |
} |
228 |
dl |
1.7 |
|
229 |
|
|
/** |
230 |
|
|
* addAll(this) throws IAE |
231 |
|
|
*/ |
232 |
|
|
public void testAddAllSelf() { |
233 |
|
|
try { |
234 |
|
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
235 |
|
|
q.addAll(q); |
236 |
|
|
shouldThrow(); |
237 |
jsr166 |
1.15 |
} catch (IllegalArgumentException success) {} |
238 |
dl |
1.7 |
} |
239 |
|
|
|
240 |
dl |
1.5 |
/** |
241 |
dl |
1.6 |
* addAll of a collection with null elements throws NPE |
242 |
dl |
1.5 |
*/ |
243 |
|
|
public void testAddAll2() { |
244 |
dl |
1.1 |
try { |
245 |
dl |
1.3 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
246 |
|
|
Integer[] ints = new Integer[SIZE]; |
247 |
dl |
1.1 |
q.addAll(Arrays.asList(ints)); |
248 |
dl |
1.5 |
shouldThrow(); |
249 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
250 |
dl |
1.1 |
} |
251 |
dl |
1.5 |
/** |
252 |
dl |
1.6 |
* addAll of a collection with any null elements throws NPE after |
253 |
|
|
* possibly adding some elements |
254 |
dl |
1.5 |
*/ |
255 |
|
|
public void testAddAll3() { |
256 |
dl |
1.1 |
try { |
257 |
dl |
1.3 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
258 |
|
|
Integer[] ints = new Integer[SIZE]; |
259 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
260 |
dl |
1.1 |
ints[i] = new Integer(i); |
261 |
|
|
q.addAll(Arrays.asList(ints)); |
262 |
dl |
1.5 |
shouldThrow(); |
263 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
264 |
dl |
1.1 |
} |
265 |
|
|
|
266 |
dl |
1.5 |
/** |
267 |
dl |
1.6 |
* Queue contains all elements of successful addAll |
268 |
dl |
1.5 |
*/ |
269 |
|
|
public void testAddAll5() { |
270 |
jsr166 |
1.15 |
Integer[] empty = new Integer[0]; |
271 |
|
|
Integer[] ints = new Integer[SIZE]; |
272 |
|
|
for (int i = SIZE-1; i >= 0; --i) |
273 |
|
|
ints[i] = new Integer(i); |
274 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
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 |
dl |
1.1 |
} |
280 |
|
|
|
281 |
dl |
1.5 |
/** |
282 |
dl |
1.6 |
* put(null) throws NPE |
283 |
dl |
1.5 |
*/ |
284 |
dl |
1.1 |
public void testPutNull() { |
285 |
|
|
try { |
286 |
dl |
1.3 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
287 |
dl |
1.1 |
q.put(null); |
288 |
dl |
1.5 |
shouldThrow(); |
289 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
290 |
dl |
1.1 |
} |
291 |
|
|
|
292 |
dl |
1.5 |
/** |
293 |
dl |
1.6 |
* all elements successfully put are contained |
294 |
dl |
1.5 |
*/ |
295 |
dl |
1.1 |
public void testPut() { |
296 |
jsr166 |
1.15 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
297 |
|
|
for (int i = 0; i < SIZE; ++i) { |
298 |
|
|
Integer I = new Integer(i); |
299 |
|
|
q.put(I); |
300 |
|
|
assertTrue(q.contains(I)); |
301 |
dl |
1.1 |
} |
302 |
jsr166 |
1.15 |
assertEquals(SIZE, q.size()); |
303 |
dl |
1.1 |
} |
304 |
|
|
|
305 |
dl |
1.5 |
/** |
306 |
dl |
1.6 |
* put doesn't block waiting for take |
307 |
dl |
1.5 |
*/ |
308 |
jsr166 |
1.15 |
public void testPutWithTake() throws InterruptedException { |
309 |
dl |
1.1 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
310 |
jsr166 |
1.15 |
final int size = 4; |
311 |
|
|
Thread t = new Thread(new CheckedRunnable() { |
312 |
|
|
public void realRun() { |
313 |
|
|
for (int i = 0; i < size; i++) |
314 |
|
|
q.put(new Integer(0)); |
315 |
|
|
}}); |
316 |
|
|
|
317 |
|
|
t.start(); |
318 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
319 |
|
|
assertEquals(q.size(), size); |
320 |
|
|
q.take(); |
321 |
|
|
t.interrupt(); |
322 |
|
|
t.join(); |
323 |
dl |
1.1 |
} |
324 |
|
|
|
325 |
dl |
1.5 |
/** |
326 |
dl |
1.6 |
* timed offer does not time out |
327 |
dl |
1.5 |
*/ |
328 |
jsr166 |
1.15 |
public void testTimedOffer() throws InterruptedException { |
329 |
dl |
1.1 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
330 |
|
|
Thread t = new Thread(new Runnable() { |
331 |
dl |
1.5 |
public void run() { |
332 |
dl |
1.1 |
try { |
333 |
|
|
q.put(new Integer(0)); |
334 |
|
|
q.put(new Integer(0)); |
335 |
dl |
1.3 |
threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
336 |
|
|
threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
337 |
dl |
1.1 |
} finally { } |
338 |
|
|
} |
339 |
|
|
}); |
340 |
jsr166 |
1.12 |
|
341 |
jsr166 |
1.15 |
t.start(); |
342 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
343 |
|
|
t.interrupt(); |
344 |
|
|
t.join(); |
345 |
dl |
1.1 |
} |
346 |
|
|
|
347 |
dl |
1.5 |
/** |
348 |
dl |
1.6 |
* take retrieves elements in priority order |
349 |
dl |
1.5 |
*/ |
350 |
jsr166 |
1.15 |
public void testTake() throws InterruptedException { |
351 |
|
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
352 |
|
|
for (int i = 0; i < SIZE; ++i) { |
353 |
|
|
assertEquals(i, ((Integer)q.take()).intValue()); |
354 |
|
|
} |
355 |
dl |
1.1 |
} |
356 |
|
|
|
357 |
dl |
1.5 |
/** |
358 |
dl |
1.6 |
* take blocks interruptibly when empty |
359 |
dl |
1.5 |
*/ |
360 |
jsr166 |
1.15 |
public void testTakeFromEmpty() throws InterruptedException { |
361 |
dl |
1.1 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
362 |
jsr166 |
1.15 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
363 |
|
|
public void realRun() throws InterruptedException { |
364 |
|
|
q.take(); |
365 |
|
|
}}); |
366 |
|
|
|
367 |
|
|
t.start(); |
368 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
369 |
|
|
t.interrupt(); |
370 |
|
|
t.join(); |
371 |
dl |
1.1 |
} |
372 |
|
|
|
373 |
dl |
1.5 |
/** |
374 |
dl |
1.6 |
* Take removes existing elements until empty, then blocks interruptibly |
375 |
dl |
1.5 |
*/ |
376 |
jsr166 |
1.15 |
public void testBlockingTake() throws InterruptedException { |
377 |
|
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
378 |
|
|
public void realRun() throws InterruptedException { |
379 |
|
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
380 |
|
|
for (int i = 0; i < SIZE; ++i) { |
381 |
|
|
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
382 |
|
|
} |
383 |
|
|
q.take(); |
384 |
|
|
}}); |
385 |
|
|
|
386 |
dl |
1.1 |
t.start(); |
387 |
jsr166 |
1.15 |
Thread.sleep(SHORT_DELAY_MS); |
388 |
|
|
t.interrupt(); |
389 |
|
|
t.join(); |
390 |
dl |
1.1 |
} |
391 |
|
|
|
392 |
|
|
|
393 |
dl |
1.5 |
/** |
394 |
dl |
1.6 |
* poll succeeds unless empty |
395 |
dl |
1.5 |
*/ |
396 |
|
|
public void testPoll() { |
397 |
dl |
1.3 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
398 |
|
|
for (int i = 0; i < SIZE; ++i) { |
399 |
dl |
1.1 |
assertEquals(i, ((Integer)q.poll()).intValue()); |
400 |
|
|
} |
401 |
|
|
assertNull(q.poll()); |
402 |
|
|
} |
403 |
|
|
|
404 |
dl |
1.5 |
/** |
405 |
dl |
1.6 |
* timed pool with zero timeout succeeds when non-empty, else times out |
406 |
dl |
1.5 |
*/ |
407 |
jsr166 |
1.15 |
public void testTimedPoll0() throws InterruptedException { |
408 |
|
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
409 |
|
|
for (int i = 0; i < SIZE; ++i) { |
410 |
|
|
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
411 |
|
|
} |
412 |
|
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
413 |
dl |
1.1 |
} |
414 |
|
|
|
415 |
dl |
1.5 |
/** |
416 |
dl |
1.6 |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
417 |
dl |
1.5 |
*/ |
418 |
jsr166 |
1.15 |
public void testTimedPoll() throws InterruptedException { |
419 |
|
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
420 |
|
|
for (int i = 0; i < SIZE; ++i) { |
421 |
|
|
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
422 |
|
|
} |
423 |
|
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
424 |
dl |
1.1 |
} |
425 |
|
|
|
426 |
dl |
1.5 |
/** |
427 |
dl |
1.6 |
* Interrupted timed poll throws InterruptedException instead of |
428 |
|
|
* returning timeout status |
429 |
dl |
1.5 |
*/ |
430 |
jsr166 |
1.15 |
public void testInterruptedTimedPoll() throws InterruptedException { |
431 |
|
|
Thread t = new Thread(new CheckedRunnable() { |
432 |
|
|
public void realRun() throws InterruptedException { |
433 |
|
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
434 |
|
|
for (int i = 0; i < SIZE; ++i) { |
435 |
|
|
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
436 |
|
|
} |
437 |
|
|
try { |
438 |
|
|
q.poll(SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
439 |
|
|
threadShouldThrow(); |
440 |
|
|
} catch (InterruptedException success) {} |
441 |
|
|
}}); |
442 |
|
|
|
443 |
dl |
1.1 |
t.start(); |
444 |
jsr166 |
1.15 |
Thread.sleep(SHORT_DELAY_MS); |
445 |
|
|
t.interrupt(); |
446 |
|
|
t.join(); |
447 |
dl |
1.1 |
} |
448 |
|
|
|
449 |
dl |
1.5 |
/** |
450 |
dl |
1.6 |
* timed poll before a delayed offer fails; after offer succeeds; |
451 |
|
|
* on interruption throws |
452 |
dl |
1.5 |
*/ |
453 |
jsr166 |
1.15 |
public void testTimedPollWithOffer() throws InterruptedException { |
454 |
dl |
1.1 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
455 |
jsr166 |
1.15 |
Thread t = new Thread(new CheckedRunnable() { |
456 |
|
|
public void realRun() throws InterruptedException { |
457 |
|
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
458 |
|
|
threadAssertEquals(0, q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
459 |
|
|
try { |
460 |
|
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
461 |
|
|
threadShouldThrow(); |
462 |
|
|
} catch (InterruptedException success) {} |
463 |
|
|
}}); |
464 |
|
|
|
465 |
|
|
t.start(); |
466 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
467 |
|
|
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
468 |
|
|
t.interrupt(); |
469 |
|
|
t.join(); |
470 |
jsr166 |
1.12 |
} |
471 |
dl |
1.1 |
|
472 |
|
|
|
473 |
dl |
1.5 |
/** |
474 |
dl |
1.6 |
* peek returns next element, or null if empty |
475 |
dl |
1.5 |
*/ |
476 |
|
|
public void testPeek() { |
477 |
dl |
1.3 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
478 |
|
|
for (int i = 0; i < SIZE; ++i) { |
479 |
dl |
1.1 |
assertEquals(i, ((Integer)q.peek()).intValue()); |
480 |
|
|
q.poll(); |
481 |
|
|
assertTrue(q.peek() == null || |
482 |
|
|
i != ((Integer)q.peek()).intValue()); |
483 |
|
|
} |
484 |
|
|
assertNull(q.peek()); |
485 |
|
|
} |
486 |
|
|
|
487 |
dl |
1.5 |
/** |
488 |
dl |
1.6 |
* element returns next element, or throws NSEE if empty |
489 |
dl |
1.5 |
*/ |
490 |
|
|
public void testElement() { |
491 |
dl |
1.3 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
492 |
|
|
for (int i = 0; i < SIZE; ++i) { |
493 |
dl |
1.1 |
assertEquals(i, ((Integer)q.element()).intValue()); |
494 |
|
|
q.poll(); |
495 |
|
|
} |
496 |
|
|
try { |
497 |
|
|
q.element(); |
498 |
dl |
1.5 |
shouldThrow(); |
499 |
jsr166 |
1.15 |
} catch (NoSuchElementException success) {} |
500 |
dl |
1.1 |
} |
501 |
|
|
|
502 |
dl |
1.5 |
/** |
503 |
dl |
1.6 |
* remove removes next element, or throws NSEE if empty |
504 |
dl |
1.5 |
*/ |
505 |
|
|
public void testRemove() { |
506 |
dl |
1.3 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
507 |
|
|
for (int i = 0; i < SIZE; ++i) { |
508 |
dl |
1.1 |
assertEquals(i, ((Integer)q.remove()).intValue()); |
509 |
|
|
} |
510 |
|
|
try { |
511 |
|
|
q.remove(); |
512 |
dl |
1.5 |
shouldThrow(); |
513 |
jsr166 |
1.15 |
} catch (NoSuchElementException success) {} |
514 |
dl |
1.1 |
} |
515 |
|
|
|
516 |
dl |
1.5 |
/** |
517 |
dl |
1.6 |
* remove(x) removes x and returns true if present |
518 |
dl |
1.5 |
*/ |
519 |
|
|
public void testRemoveElement() { |
520 |
dl |
1.3 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
521 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
522 |
dl |
1.1 |
assertTrue(q.remove(new Integer(i))); |
523 |
|
|
} |
524 |
dl |
1.3 |
for (int i = 0; i < SIZE; i+=2) { |
525 |
dl |
1.1 |
assertTrue(q.remove(new Integer(i))); |
526 |
|
|
assertFalse(q.remove(new Integer(i+1))); |
527 |
|
|
} |
528 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
529 |
dl |
1.1 |
} |
530 |
jsr166 |
1.12 |
|
531 |
dl |
1.5 |
/** |
532 |
dl |
1.6 |
* contains(x) reports true when elements added but not yet removed |
533 |
dl |
1.5 |
*/ |
534 |
|
|
public void testContains() { |
535 |
dl |
1.3 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
536 |
|
|
for (int i = 0; i < SIZE; ++i) { |
537 |
dl |
1.1 |
assertTrue(q.contains(new Integer(i))); |
538 |
|
|
q.poll(); |
539 |
|
|
assertFalse(q.contains(new Integer(i))); |
540 |
|
|
} |
541 |
|
|
} |
542 |
|
|
|
543 |
dl |
1.5 |
/** |
544 |
dl |
1.6 |
* clear removes all elements |
545 |
dl |
1.5 |
*/ |
546 |
|
|
public void testClear() { |
547 |
dl |
1.3 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
548 |
dl |
1.1 |
q.clear(); |
549 |
|
|
assertTrue(q.isEmpty()); |
550 |
|
|
assertEquals(0, q.size()); |
551 |
dl |
1.11 |
q.add(one); |
552 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
553 |
dl |
1.11 |
assertTrue(q.contains(one)); |
554 |
dl |
1.1 |
q.clear(); |
555 |
|
|
assertTrue(q.isEmpty()); |
556 |
|
|
} |
557 |
|
|
|
558 |
dl |
1.5 |
/** |
559 |
dl |
1.6 |
* containsAll(c) is true when c contains a subset of elements |
560 |
dl |
1.5 |
*/ |
561 |
|
|
public void testContainsAll() { |
562 |
dl |
1.3 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
563 |
|
|
PriorityBlockingQueue p = new PriorityBlockingQueue(SIZE); |
564 |
|
|
for (int i = 0; i < SIZE; ++i) { |
565 |
dl |
1.1 |
assertTrue(q.containsAll(p)); |
566 |
|
|
assertFalse(p.containsAll(q)); |
567 |
|
|
p.add(new Integer(i)); |
568 |
|
|
} |
569 |
|
|
assertTrue(p.containsAll(q)); |
570 |
|
|
} |
571 |
|
|
|
572 |
dl |
1.5 |
/** |
573 |
dl |
1.6 |
* retainAll(c) retains only those elements of c and reports true if changed |
574 |
dl |
1.5 |
*/ |
575 |
|
|
public void testRetainAll() { |
576 |
dl |
1.3 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
577 |
|
|
PriorityBlockingQueue p = populatedQueue(SIZE); |
578 |
|
|
for (int i = 0; i < SIZE; ++i) { |
579 |
dl |
1.1 |
boolean changed = q.retainAll(p); |
580 |
|
|
if (i == 0) |
581 |
|
|
assertFalse(changed); |
582 |
|
|
else |
583 |
|
|
assertTrue(changed); |
584 |
|
|
|
585 |
|
|
assertTrue(q.containsAll(p)); |
586 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
587 |
dl |
1.1 |
p.remove(); |
588 |
|
|
} |
589 |
|
|
} |
590 |
|
|
|
591 |
dl |
1.5 |
/** |
592 |
dl |
1.6 |
* removeAll(c) removes only those elements of c and reports true if changed |
593 |
dl |
1.5 |
*/ |
594 |
|
|
public void testRemoveAll() { |
595 |
dl |
1.3 |
for (int i = 1; i < SIZE; ++i) { |
596 |
|
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
597 |
|
|
PriorityBlockingQueue p = populatedQueue(i); |
598 |
dl |
1.1 |
assertTrue(q.removeAll(p)); |
599 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
600 |
dl |
1.1 |
for (int j = 0; j < i; ++j) { |
601 |
|
|
Integer I = (Integer)(p.remove()); |
602 |
|
|
assertFalse(q.contains(I)); |
603 |
|
|
} |
604 |
|
|
} |
605 |
|
|
} |
606 |
|
|
|
607 |
dl |
1.5 |
/** |
608 |
dl |
1.6 |
* toArray contains all elements |
609 |
dl |
1.5 |
*/ |
610 |
jsr166 |
1.15 |
public void testToArray() throws InterruptedException { |
611 |
dl |
1.3 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
612 |
dl |
1.1 |
Object[] o = q.toArray(); |
613 |
|
|
Arrays.sort(o); |
614 |
jsr166 |
1.13 |
for (int i = 0; i < o.length; i++) |
615 |
dl |
1.1 |
assertEquals(o[i], q.take()); |
616 |
|
|
} |
617 |
|
|
|
618 |
dl |
1.5 |
/** |
619 |
dl |
1.6 |
* toArray(a) contains all elements |
620 |
dl |
1.5 |
*/ |
621 |
jsr166 |
1.15 |
public void testToArray2() throws InterruptedException { |
622 |
dl |
1.3 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
623 |
|
|
Integer[] ints = new Integer[SIZE]; |
624 |
dl |
1.1 |
ints = (Integer[])q.toArray(ints); |
625 |
|
|
Arrays.sort(ints); |
626 |
jsr166 |
1.15 |
for (int i = 0; i < ints.length; i++) |
627 |
|
|
assertEquals(ints[i], q.take()); |
628 |
dl |
1.1 |
} |
629 |
dl |
1.7 |
|
630 |
|
|
/** |
631 |
|
|
* toArray(null) throws NPE |
632 |
|
|
*/ |
633 |
|
|
public void testToArray_BadArg() { |
634 |
|
|
try { |
635 |
|
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
636 |
|
|
Object o[] = q.toArray(null); |
637 |
|
|
shouldThrow(); |
638 |
jsr166 |
1.14 |
} catch (NullPointerException success) {} |
639 |
dl |
1.7 |
} |
640 |
|
|
|
641 |
|
|
/** |
642 |
dl |
1.9 |
* toArray with incompatible array type throws CCE |
643 |
dl |
1.7 |
*/ |
644 |
|
|
public void testToArray1_BadArg() { |
645 |
|
|
try { |
646 |
|
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
647 |
|
|
Object o[] = q.toArray(new String[10] ); |
648 |
|
|
shouldThrow(); |
649 |
jsr166 |
1.14 |
} catch (ArrayStoreException success) {} |
650 |
dl |
1.7 |
} |
651 |
jsr166 |
1.12 |
|
652 |
dl |
1.5 |
/** |
653 |
dl |
1.6 |
* iterator iterates through all elements |
654 |
dl |
1.5 |
*/ |
655 |
|
|
public void testIterator() { |
656 |
dl |
1.3 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
657 |
dl |
1.1 |
int i = 0; |
658 |
|
|
Iterator it = q.iterator(); |
659 |
jsr166 |
1.13 |
while (it.hasNext()) { |
660 |
dl |
1.1 |
assertTrue(q.contains(it.next())); |
661 |
|
|
++i; |
662 |
|
|
} |
663 |
dl |
1.3 |
assertEquals(i, SIZE); |
664 |
dl |
1.1 |
} |
665 |
|
|
|
666 |
dl |
1.5 |
/** |
667 |
dl |
1.6 |
* iterator.remove removes current element |
668 |
dl |
1.5 |
*/ |
669 |
dl |
1.1 |
public void testIteratorRemove () { |
670 |
|
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
671 |
|
|
q.add(new Integer(2)); |
672 |
|
|
q.add(new Integer(1)); |
673 |
|
|
q.add(new Integer(3)); |
674 |
|
|
|
675 |
|
|
Iterator it = q.iterator(); |
676 |
|
|
it.next(); |
677 |
|
|
it.remove(); |
678 |
|
|
|
679 |
|
|
it = q.iterator(); |
680 |
|
|
assertEquals(it.next(), new Integer(2)); |
681 |
|
|
assertEquals(it.next(), new Integer(3)); |
682 |
|
|
assertFalse(it.hasNext()); |
683 |
|
|
} |
684 |
|
|
|
685 |
|
|
|
686 |
dl |
1.5 |
/** |
687 |
dl |
1.6 |
* toString contains toStrings of elements |
688 |
dl |
1.5 |
*/ |
689 |
|
|
public void testToString() { |
690 |
dl |
1.3 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
691 |
dl |
1.1 |
String s = q.toString(); |
692 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
693 |
dl |
1.1 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
694 |
|
|
} |
695 |
jsr166 |
1.12 |
} |
696 |
dl |
1.1 |
|
697 |
dl |
1.5 |
/** |
698 |
dl |
1.6 |
* offer transfers elements across Executor tasks |
699 |
dl |
1.5 |
*/ |
700 |
dl |
1.1 |
public void testPollInExecutor() { |
701 |
|
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
702 |
|
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
703 |
jsr166 |
1.15 |
executor.execute(new CheckedRunnable() { |
704 |
|
|
public void realRun() throws InterruptedException { |
705 |
dl |
1.3 |
threadAssertNull(q.poll()); |
706 |
jsr166 |
1.15 |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
707 |
|
|
threadAssertTrue(q.isEmpty()); |
708 |
|
|
}}); |
709 |
|
|
|
710 |
|
|
executor.execute(new CheckedRunnable() { |
711 |
|
|
public void realRun() throws InterruptedException { |
712 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
713 |
|
|
q.put(new Integer(1)); |
714 |
|
|
}}); |
715 |
jsr166 |
1.12 |
|
716 |
dl |
1.3 |
joinPool(executor); |
717 |
dl |
1.2 |
} |
718 |
|
|
|
719 |
dl |
1.5 |
/** |
720 |
jsr166 |
1.12 |
* A deserialized serialized queue has same elements |
721 |
dl |
1.5 |
*/ |
722 |
jsr166 |
1.15 |
public void testSerialization() throws Exception { |
723 |
dl |
1.3 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
724 |
jsr166 |
1.15 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
725 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
726 |
|
|
out.writeObject(q); |
727 |
|
|
out.close(); |
728 |
|
|
|
729 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
730 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
731 |
|
|
PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject(); |
732 |
|
|
assertEquals(q.size(), r.size()); |
733 |
|
|
while (!q.isEmpty()) |
734 |
|
|
assertEquals(q.remove(), r.remove()); |
735 |
dl |
1.1 |
} |
736 |
dl |
1.7 |
|
737 |
|
|
/** |
738 |
|
|
* drainTo(null) throws NPE |
739 |
jsr166 |
1.12 |
*/ |
740 |
dl |
1.7 |
public void testDrainToNull() { |
741 |
|
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
742 |
|
|
try { |
743 |
|
|
q.drainTo(null); |
744 |
|
|
shouldThrow(); |
745 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
746 |
dl |
1.7 |
} |
747 |
|
|
|
748 |
|
|
/** |
749 |
|
|
* drainTo(this) throws IAE |
750 |
jsr166 |
1.12 |
*/ |
751 |
dl |
1.7 |
public void testDrainToSelf() { |
752 |
|
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
753 |
|
|
try { |
754 |
|
|
q.drainTo(q); |
755 |
|
|
shouldThrow(); |
756 |
jsr166 |
1.15 |
} catch (IllegalArgumentException success) {} |
757 |
dl |
1.7 |
} |
758 |
|
|
|
759 |
|
|
/** |
760 |
|
|
* drainTo(c) empties queue into another collection c |
761 |
jsr166 |
1.12 |
*/ |
762 |
dl |
1.7 |
public void testDrainTo() { |
763 |
|
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
764 |
|
|
ArrayList l = new ArrayList(); |
765 |
|
|
q.drainTo(l); |
766 |
|
|
assertEquals(q.size(), 0); |
767 |
|
|
assertEquals(l.size(), SIZE); |
768 |
jsr166 |
1.12 |
for (int i = 0; i < SIZE; ++i) |
769 |
dl |
1.7 |
assertEquals(l.get(i), new Integer(i)); |
770 |
dl |
1.11 |
q.add(zero); |
771 |
|
|
q.add(one); |
772 |
|
|
assertFalse(q.isEmpty()); |
773 |
|
|
assertTrue(q.contains(zero)); |
774 |
|
|
assertTrue(q.contains(one)); |
775 |
|
|
l.clear(); |
776 |
|
|
q.drainTo(l); |
777 |
|
|
assertEquals(q.size(), 0); |
778 |
|
|
assertEquals(l.size(), 2); |
779 |
jsr166 |
1.12 |
for (int i = 0; i < 2; ++i) |
780 |
dl |
1.11 |
assertEquals(l.get(i), new Integer(i)); |
781 |
dl |
1.7 |
} |
782 |
|
|
|
783 |
|
|
/** |
784 |
|
|
* drainTo empties queue |
785 |
jsr166 |
1.12 |
*/ |
786 |
jsr166 |
1.15 |
public void testDrainToWithActivePut() throws InterruptedException { |
787 |
dl |
1.7 |
final PriorityBlockingQueue q = populatedQueue(SIZE); |
788 |
jsr166 |
1.15 |
Thread t = new Thread(new CheckedRunnable() { |
789 |
|
|
public void realRun() { |
790 |
|
|
q.put(new Integer(SIZE+1)); |
791 |
|
|
}}); |
792 |
|
|
|
793 |
|
|
t.start(); |
794 |
|
|
ArrayList l = new ArrayList(); |
795 |
|
|
q.drainTo(l); |
796 |
|
|
assertTrue(l.size() >= SIZE); |
797 |
|
|
for (int i = 0; i < SIZE; ++i) |
798 |
|
|
assertEquals(l.get(i), new Integer(i)); |
799 |
|
|
t.join(); |
800 |
|
|
assertTrue(q.size() + l.size() >= SIZE); |
801 |
dl |
1.7 |
} |
802 |
|
|
|
803 |
|
|
/** |
804 |
|
|
* drainTo(null, n) throws NPE |
805 |
jsr166 |
1.12 |
*/ |
806 |
dl |
1.7 |
public void testDrainToNullN() { |
807 |
|
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
808 |
|
|
try { |
809 |
|
|
q.drainTo(null, 0); |
810 |
|
|
shouldThrow(); |
811 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
812 |
dl |
1.7 |
} |
813 |
|
|
|
814 |
|
|
/** |
815 |
|
|
* drainTo(this, n) throws IAE |
816 |
jsr166 |
1.12 |
*/ |
817 |
dl |
1.7 |
public void testDrainToSelfN() { |
818 |
|
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
819 |
|
|
try { |
820 |
|
|
q.drainTo(q, 0); |
821 |
|
|
shouldThrow(); |
822 |
jsr166 |
1.15 |
} catch (IllegalArgumentException success) {} |
823 |
dl |
1.7 |
} |
824 |
|
|
|
825 |
|
|
/** |
826 |
|
|
* drainTo(c, n) empties first max {n, size} elements of queue into c |
827 |
jsr166 |
1.12 |
*/ |
828 |
dl |
1.7 |
public void testDrainToN() { |
829 |
dl |
1.11 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2); |
830 |
dl |
1.7 |
for (int i = 0; i < SIZE + 2; ++i) { |
831 |
jsr166 |
1.13 |
for (int j = 0; j < SIZE; j++) |
832 |
dl |
1.11 |
assertTrue(q.offer(new Integer(j))); |
833 |
dl |
1.7 |
ArrayList l = new ArrayList(); |
834 |
|
|
q.drainTo(l, i); |
835 |
|
|
int k = (i < SIZE)? i : SIZE; |
836 |
dl |
1.11 |
assertEquals(l.size(), k); |
837 |
dl |
1.7 |
assertEquals(q.size(), SIZE-k); |
838 |
jsr166 |
1.12 |
for (int j = 0; j < k; ++j) |
839 |
dl |
1.11 |
assertEquals(l.get(j), new Integer(j)); |
840 |
|
|
while (q.poll() != null) ; |
841 |
dl |
1.7 |
} |
842 |
|
|
} |
843 |
|
|
|
844 |
dl |
1.1 |
} |