9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
|
import java.io.*; |
14 |
|
|
15 |
|
public class PriorityBlockingQueueTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
< |
return new TestSuite(PriorityBlockingQueueTest.class); |
20 |
> |
return new TestSuite(PriorityBlockingQueueTest.class); |
21 |
|
} |
22 |
|
|
23 |
|
private static final int NOCAP = Integer.MAX_VALUE; |
25 |
|
/** Sample Comparator */ |
26 |
|
static class MyReverseComparator implements Comparator { |
27 |
|
public int compare(Object x, Object y) { |
28 |
< |
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; |
28 |
> |
return ((Comparable)y).compareTo(x); |
29 |
|
} |
30 |
|
} |
31 |
|
|
36 |
|
private PriorityBlockingQueue populatedQueue(int n) { |
37 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(n); |
38 |
|
assertTrue(q.isEmpty()); |
39 |
< |
for (int i = n-1; i >= 0; i-=2) |
40 |
< |
assertTrue(q.offer(new Integer(i))); |
41 |
< |
for (int i = (n & 1); i < n; i+=2) |
42 |
< |
assertTrue(q.offer(new Integer(i))); |
39 |
> |
for (int i = n-1; i >= 0; i-=2) |
40 |
> |
assertTrue(q.offer(new Integer(i))); |
41 |
> |
for (int i = (n & 1); i < n; i+=2) |
42 |
> |
assertTrue(q.offer(new Integer(i))); |
43 |
|
assertFalse(q.isEmpty()); |
44 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
45 |
< |
assertEquals(n, q.size()); |
45 |
> |
assertEquals(n, q.size()); |
46 |
|
return q; |
47 |
|
} |
48 |
|
|
54 |
|
} |
55 |
|
|
56 |
|
/** |
57 |
< |
* Constructor throws IAE if capacity argument nonpositive |
57 |
> |
* Constructor throws IAE if capacity argument nonpositive |
58 |
|
*/ |
59 |
|
public void testConstructor2() { |
60 |
|
try { |
61 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(0); |
62 |
|
shouldThrow(); |
63 |
< |
} |
67 |
< |
catch (IllegalArgumentException success) {} |
63 |
> |
} catch (IllegalArgumentException success) {} |
64 |
|
} |
65 |
|
|
66 |
|
/** |
70 |
|
try { |
71 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(null); |
72 |
|
shouldThrow(); |
73 |
< |
} |
78 |
< |
catch (NullPointerException success) {} |
73 |
> |
} catch (NullPointerException success) {} |
74 |
|
} |
75 |
|
|
76 |
|
/** |
81 |
|
Integer[] ints = new Integer[SIZE]; |
82 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
83 |
|
shouldThrow(); |
84 |
< |
} |
90 |
< |
catch (NullPointerException success) {} |
84 |
> |
} catch (NullPointerException success) {} |
85 |
|
} |
86 |
|
|
87 |
|
/** |
94 |
|
ints[i] = new Integer(i); |
95 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
96 |
|
shouldThrow(); |
97 |
< |
} |
104 |
< |
catch (NullPointerException success) {} |
97 |
> |
} catch (NullPointerException success) {} |
98 |
|
} |
99 |
|
|
100 |
|
/** |
101 |
|
* Queue contains all elements of collection used to initialize |
102 |
|
*/ |
103 |
|
public void testConstructor6() { |
104 |
< |
try { |
105 |
< |
Integer[] ints = new Integer[SIZE]; |
106 |
< |
for (int i = 0; i < SIZE; ++i) |
107 |
< |
ints[i] = new Integer(i); |
108 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
109 |
< |
for (int i = 0; i < SIZE; ++i) |
117 |
< |
assertEquals(ints[i], q.poll()); |
118 |
< |
} |
119 |
< |
finally {} |
104 |
> |
Integer[] ints = new Integer[SIZE]; |
105 |
> |
for (int i = 0; i < SIZE; ++i) |
106 |
> |
ints[i] = new Integer(i); |
107 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
108 |
> |
for (int i = 0; i < SIZE; ++i) |
109 |
> |
assertEquals(ints[i], q.poll()); |
110 |
|
} |
111 |
|
|
112 |
|
/** |
113 |
|
* The comparator used in constructor is used |
114 |
|
*/ |
115 |
|
public void testConstructor7() { |
116 |
< |
try { |
117 |
< |
MyReverseComparator cmp = new MyReverseComparator(); |
118 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp); |
119 |
< |
assertEquals(cmp, q.comparator()); |
120 |
< |
Integer[] ints = new Integer[SIZE]; |
121 |
< |
for (int i = 0; i < SIZE; ++i) |
122 |
< |
ints[i] = new Integer(i); |
123 |
< |
q.addAll(Arrays.asList(ints)); |
124 |
< |
for (int i = SIZE-1; i >= 0; --i) |
135 |
< |
assertEquals(ints[i], q.poll()); |
136 |
< |
} |
137 |
< |
finally {} |
116 |
> |
MyReverseComparator cmp = new MyReverseComparator(); |
117 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp); |
118 |
> |
assertEquals(cmp, q.comparator()); |
119 |
> |
Integer[] ints = new Integer[SIZE]; |
120 |
> |
for (int i = 0; i < SIZE; ++i) |
121 |
> |
ints[i] = new Integer(i); |
122 |
> |
q.addAll(Arrays.asList(ints)); |
123 |
> |
for (int i = SIZE-1; i >= 0; --i) |
124 |
> |
assertEquals(ints[i], q.poll()); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
161 |
|
* offer(null) throws NPE |
162 |
|
*/ |
163 |
|
public void testOfferNull() { |
164 |
< |
try { |
164 |
> |
try { |
165 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
166 |
|
q.offer(null); |
167 |
|
shouldThrow(); |
168 |
< |
} catch (NullPointerException success) { } |
168 |
> |
} catch (NullPointerException success) {} |
169 |
|
} |
170 |
|
|
171 |
|
/** |
172 |
|
* add(null) throws NPE |
173 |
|
*/ |
174 |
|
public void testAddNull() { |
175 |
< |
try { |
175 |
> |
try { |
176 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
177 |
|
q.add(null); |
178 |
|
shouldThrow(); |
179 |
< |
} catch (NullPointerException success) { } |
179 |
> |
} catch (NullPointerException success) {} |
180 |
|
} |
181 |
|
|
182 |
|
/** |
198 |
|
q.offer(new Object()); |
199 |
|
q.offer(new Object()); |
200 |
|
shouldThrow(); |
201 |
< |
} |
215 |
< |
catch (ClassCastException success) {} |
201 |
> |
} catch (ClassCastException success) {} |
202 |
|
} |
203 |
|
|
204 |
|
/** |
220 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
221 |
|
q.addAll(null); |
222 |
|
shouldThrow(); |
223 |
< |
} |
238 |
< |
catch (NullPointerException success) {} |
223 |
> |
} catch (NullPointerException success) {} |
224 |
|
} |
225 |
|
|
226 |
|
/** |
231 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
232 |
|
q.addAll(q); |
233 |
|
shouldThrow(); |
234 |
< |
} |
250 |
< |
catch (IllegalArgumentException success) {} |
234 |
> |
} catch (IllegalArgumentException success) {} |
235 |
|
} |
236 |
|
|
237 |
|
/** |
243 |
|
Integer[] ints = new Integer[SIZE]; |
244 |
|
q.addAll(Arrays.asList(ints)); |
245 |
|
shouldThrow(); |
246 |
< |
} |
263 |
< |
catch (NullPointerException success) {} |
246 |
> |
} catch (NullPointerException success) {} |
247 |
|
} |
248 |
|
/** |
249 |
|
* addAll of a collection with any null elements throws NPE after |
257 |
|
ints[i] = new Integer(i); |
258 |
|
q.addAll(Arrays.asList(ints)); |
259 |
|
shouldThrow(); |
260 |
< |
} |
278 |
< |
catch (NullPointerException success) {} |
260 |
> |
} catch (NullPointerException success) {} |
261 |
|
} |
262 |
|
|
263 |
|
/** |
264 |
|
* Queue contains all elements of successful addAll |
265 |
|
*/ |
266 |
|
public void testAddAll5() { |
267 |
< |
try { |
268 |
< |
Integer[] empty = new Integer[0]; |
269 |
< |
Integer[] ints = new Integer[SIZE]; |
270 |
< |
for (int i = SIZE-1; i >= 0; --i) |
271 |
< |
ints[i] = new Integer(i); |
272 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
273 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
274 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
275 |
< |
for (int i = 0; i < SIZE; ++i) |
294 |
< |
assertEquals(ints[i], q.poll()); |
295 |
< |
} |
296 |
< |
finally {} |
267 |
> |
Integer[] empty = new Integer[0]; |
268 |
> |
Integer[] ints = new Integer[SIZE]; |
269 |
> |
for (int i = SIZE-1; i >= 0; --i) |
270 |
> |
ints[i] = new Integer(i); |
271 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
272 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
273 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
274 |
> |
for (int i = 0; i < SIZE; ++i) |
275 |
> |
assertEquals(ints[i], q.poll()); |
276 |
|
} |
277 |
|
|
278 |
|
/** |
279 |
|
* put(null) throws NPE |
280 |
|
*/ |
281 |
|
public void testPutNull() { |
282 |
< |
try { |
282 |
> |
try { |
283 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
284 |
|
q.put(null); |
285 |
|
shouldThrow(); |
286 |
< |
} |
308 |
< |
catch (NullPointerException success){ |
309 |
< |
} |
286 |
> |
} catch (NullPointerException success) {} |
287 |
|
} |
288 |
|
|
289 |
|
/** |
290 |
|
* all elements successfully put are contained |
291 |
|
*/ |
292 |
|
public void testPut() { |
293 |
< |
try { |
294 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
295 |
< |
for (int i = 0; i < SIZE; ++i) { |
296 |
< |
Integer I = new Integer(i); |
297 |
< |
q.put(I); |
321 |
< |
assertTrue(q.contains(I)); |
322 |
< |
} |
323 |
< |
assertEquals(SIZE, q.size()); |
293 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
294 |
> |
for (int i = 0; i < SIZE; ++i) { |
295 |
> |
Integer I = new Integer(i); |
296 |
> |
q.put(I); |
297 |
> |
assertTrue(q.contains(I)); |
298 |
|
} |
299 |
< |
finally { |
326 |
< |
} |
299 |
> |
assertEquals(SIZE, q.size()); |
300 |
|
} |
301 |
|
|
302 |
|
/** |
303 |
|
* put doesn't block waiting for take |
304 |
|
*/ |
305 |
< |
public void testPutWithTake() { |
305 |
> |
public void testPutWithTake() throws InterruptedException { |
306 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
307 |
< |
Thread t = new Thread(new Runnable() { |
308 |
< |
public void run() { |
309 |
< |
int added = 0; |
310 |
< |
try { |
311 |
< |
q.put(new Integer(0)); |
312 |
< |
++added; |
313 |
< |
q.put(new Integer(0)); |
314 |
< |
++added; |
315 |
< |
q.put(new Integer(0)); |
316 |
< |
++added; |
317 |
< |
q.put(new Integer(0)); |
318 |
< |
++added; |
319 |
< |
threadAssertTrue(added == 4); |
347 |
< |
} finally { |
348 |
< |
} |
349 |
< |
} |
350 |
< |
}); |
351 |
< |
try { |
352 |
< |
t.start(); |
353 |
< |
Thread.sleep(SHORT_DELAY_MS); |
354 |
< |
q.take(); |
355 |
< |
t.interrupt(); |
356 |
< |
t.join(); |
357 |
< |
} catch (Exception e){ |
358 |
< |
unexpectedException(); |
359 |
< |
} |
307 |
> |
final int size = 4; |
308 |
> |
Thread t = new Thread(new CheckedRunnable() { |
309 |
> |
public void realRun() { |
310 |
> |
for (int i = 0; i < size; i++) |
311 |
> |
q.put(new Integer(0)); |
312 |
> |
}}); |
313 |
> |
|
314 |
> |
t.start(); |
315 |
> |
Thread.sleep(SHORT_DELAY_MS); |
316 |
> |
assertEquals(q.size(), size); |
317 |
> |
q.take(); |
318 |
> |
t.interrupt(); |
319 |
> |
t.join(); |
320 |
|
} |
321 |
|
|
322 |
|
/** |
323 |
|
* timed offer does not time out |
324 |
|
*/ |
325 |
< |
public void testTimedOffer() { |
325 |
> |
public void testTimedOffer() throws InterruptedException { |
326 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
327 |
< |
Thread t = new Thread(new Runnable() { |
328 |
< |
public void run() { |
329 |
< |
try { |
330 |
< |
q.put(new Integer(0)); |
331 |
< |
q.put(new Integer(0)); |
332 |
< |
threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
333 |
< |
threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
374 |
< |
} finally { } |
375 |
< |
} |
376 |
< |
}); |
327 |
> |
Thread t = new Thread(new CheckedRunnable() { |
328 |
> |
public void realRun() { |
329 |
> |
q.put(new Integer(0)); |
330 |
> |
q.put(new Integer(0)); |
331 |
> |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); |
332 |
> |
assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS)); |
333 |
> |
}}); |
334 |
|
|
335 |
< |
try { |
336 |
< |
t.start(); |
337 |
< |
Thread.sleep(SMALL_DELAY_MS); |
338 |
< |
t.interrupt(); |
382 |
< |
t.join(); |
383 |
< |
} catch (Exception e){ |
384 |
< |
unexpectedException(); |
385 |
< |
} |
335 |
> |
t.start(); |
336 |
> |
Thread.sleep(SMALL_DELAY_MS); |
337 |
> |
t.interrupt(); |
338 |
> |
t.join(); |
339 |
|
} |
340 |
|
|
341 |
|
/** |
342 |
|
* take retrieves elements in priority order |
343 |
|
*/ |
344 |
< |
public void testTake() { |
345 |
< |
try { |
346 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
347 |
< |
for (int i = 0; i < SIZE; ++i) { |
348 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
396 |
< |
} |
397 |
< |
} catch (InterruptedException e){ |
398 |
< |
unexpectedException(); |
399 |
< |
} |
344 |
> |
public void testTake() throws InterruptedException { |
345 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
346 |
> |
for (int i = 0; i < SIZE; ++i) { |
347 |
> |
assertEquals(i, q.take()); |
348 |
> |
} |
349 |
|
} |
350 |
|
|
351 |
|
/** |
352 |
|
* take blocks interruptibly when empty |
353 |
|
*/ |
354 |
< |
public void testTakeFromEmpty() { |
354 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
355 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
356 |
< |
Thread t = new Thread(new Runnable() { |
357 |
< |
public void run() { |
358 |
< |
try { |
359 |
< |
q.take(); |
360 |
< |
threadShouldThrow(); |
361 |
< |
} catch (InterruptedException success){ } |
362 |
< |
} |
363 |
< |
}); |
364 |
< |
try { |
416 |
< |
t.start(); |
417 |
< |
Thread.sleep(SHORT_DELAY_MS); |
418 |
< |
t.interrupt(); |
419 |
< |
t.join(); |
420 |
< |
} catch (Exception e){ |
421 |
< |
unexpectedException(); |
422 |
< |
} |
356 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
357 |
> |
public void realRun() throws InterruptedException { |
358 |
> |
q.take(); |
359 |
> |
}}); |
360 |
> |
|
361 |
> |
t.start(); |
362 |
> |
Thread.sleep(SHORT_DELAY_MS); |
363 |
> |
t.interrupt(); |
364 |
> |
t.join(); |
365 |
|
} |
366 |
|
|
367 |
|
/** |
368 |
|
* Take removes existing elements until empty, then blocks interruptibly |
369 |
|
*/ |
370 |
< |
public void testBlockingTake() { |
371 |
< |
Thread t = new Thread(new Runnable() { |
372 |
< |
public void run() { |
373 |
< |
try { |
374 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
375 |
< |
for (int i = 0; i < SIZE; ++i) { |
376 |
< |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
377 |
< |
} |
378 |
< |
q.take(); |
379 |
< |
threadShouldThrow(); |
380 |
< |
} catch (InterruptedException success){ |
381 |
< |
} |
382 |
< |
}}); |
370 |
> |
public void testBlockingTake() throws InterruptedException { |
371 |
> |
final PriorityBlockingQueue q = populatedQueue(SIZE); |
372 |
> |
Thread t = new Thread(new CheckedRunnable() { |
373 |
> |
public void realRun() throws InterruptedException { |
374 |
> |
for (int i = 0; i < SIZE; ++i) { |
375 |
> |
assertEquals(i, q.take()); |
376 |
> |
} |
377 |
> |
try { |
378 |
> |
q.take(); |
379 |
> |
shouldThrow(); |
380 |
> |
} catch (InterruptedException success) {} |
381 |
> |
}}); |
382 |
> |
|
383 |
|
t.start(); |
384 |
< |
try { |
385 |
< |
Thread.sleep(SHORT_DELAY_MS); |
386 |
< |
t.interrupt(); |
445 |
< |
t.join(); |
446 |
< |
} |
447 |
< |
catch (InterruptedException ie) { |
448 |
< |
unexpectedException(); |
449 |
< |
} |
384 |
> |
Thread.sleep(SHORT_DELAY_MS); |
385 |
> |
t.interrupt(); |
386 |
> |
t.join(); |
387 |
|
} |
388 |
|
|
389 |
|
|
393 |
|
public void testPoll() { |
394 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
395 |
|
for (int i = 0; i < SIZE; ++i) { |
396 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
396 |
> |
assertEquals(i, q.poll()); |
397 |
|
} |
398 |
< |
assertNull(q.poll()); |
398 |
> |
assertNull(q.poll()); |
399 |
|
} |
400 |
|
|
401 |
|
/** |
402 |
|
* timed pool with zero timeout succeeds when non-empty, else times out |
403 |
|
*/ |
404 |
< |
public void testTimedPoll0() { |
405 |
< |
try { |
406 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
407 |
< |
for (int i = 0; i < SIZE; ++i) { |
408 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
409 |
< |
} |
473 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
474 |
< |
} catch (InterruptedException e){ |
475 |
< |
unexpectedException(); |
476 |
< |
} |
404 |
> |
public void testTimedPoll0() throws InterruptedException { |
405 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
406 |
> |
for (int i = 0; i < SIZE; ++i) { |
407 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
408 |
> |
} |
409 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
410 |
|
} |
411 |
|
|
412 |
|
/** |
413 |
|
* timed pool with nonzero timeout succeeds when non-empty, else times out |
414 |
|
*/ |
415 |
< |
public void testTimedPoll() { |
416 |
< |
try { |
417 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
418 |
< |
for (int i = 0; i < SIZE; ++i) { |
419 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
420 |
< |
} |
488 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
489 |
< |
} catch (InterruptedException e){ |
490 |
< |
unexpectedException(); |
491 |
< |
} |
415 |
> |
public void testTimedPoll() throws InterruptedException { |
416 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
417 |
> |
for (int i = 0; i < SIZE; ++i) { |
418 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
419 |
> |
} |
420 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
421 |
|
} |
422 |
|
|
423 |
|
/** |
424 |
|
* Interrupted timed poll throws InterruptedException instead of |
425 |
|
* returning timeout status |
426 |
|
*/ |
427 |
< |
public void testInterruptedTimedPoll() { |
428 |
< |
Thread t = new Thread(new Runnable() { |
429 |
< |
public void run() { |
430 |
< |
try { |
431 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
432 |
< |
for (int i = 0; i < SIZE; ++i) { |
433 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
434 |
< |
} |
435 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
436 |
< |
} catch (InterruptedException success){ |
437 |
< |
} |
438 |
< |
}}); |
427 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
428 |
> |
Thread t = new Thread(new CheckedRunnable() { |
429 |
> |
public void realRun() throws InterruptedException { |
430 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
431 |
> |
for (int i = 0; i < SIZE; ++i) { |
432 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
433 |
> |
} |
434 |
> |
try { |
435 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
436 |
> |
shouldThrow(); |
437 |
> |
} catch (InterruptedException success) {} |
438 |
> |
}}); |
439 |
> |
|
440 |
|
t.start(); |
441 |
< |
try { |
442 |
< |
Thread.sleep(SHORT_DELAY_MS); |
443 |
< |
t.interrupt(); |
514 |
< |
t.join(); |
515 |
< |
} |
516 |
< |
catch (InterruptedException ie) { |
517 |
< |
unexpectedException(); |
518 |
< |
} |
441 |
> |
Thread.sleep(SHORT_DELAY_MS); |
442 |
> |
t.interrupt(); |
443 |
> |
t.join(); |
444 |
|
} |
445 |
|
|
446 |
|
/** |
447 |
|
* timed poll before a delayed offer fails; after offer succeeds; |
448 |
|
* on interruption throws |
449 |
|
*/ |
450 |
< |
public void testTimedPollWithOffer() { |
450 |
> |
public void testTimedPollWithOffer() throws InterruptedException { |
451 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
452 |
< |
Thread t = new Thread(new Runnable() { |
453 |
< |
public void run() { |
454 |
< |
try { |
455 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
456 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
457 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
458 |
< |
threadShouldThrow(); |
459 |
< |
} catch (InterruptedException success) { } |
460 |
< |
} |
461 |
< |
}); |
462 |
< |
try { |
463 |
< |
t.start(); |
464 |
< |
Thread.sleep(SMALL_DELAY_MS); |
465 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
466 |
< |
t.interrupt(); |
542 |
< |
t.join(); |
543 |
< |
} catch (Exception e){ |
544 |
< |
unexpectedException(); |
545 |
< |
} |
452 |
> |
Thread t = new Thread(new CheckedRunnable() { |
453 |
> |
public void realRun() throws InterruptedException { |
454 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
455 |
> |
assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
456 |
> |
try { |
457 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
458 |
> |
shouldThrow(); |
459 |
> |
} catch (InterruptedException success) {} |
460 |
> |
}}); |
461 |
> |
|
462 |
> |
t.start(); |
463 |
> |
Thread.sleep(SMALL_DELAY_MS); |
464 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
465 |
> |
t.interrupt(); |
466 |
> |
t.join(); |
467 |
|
} |
468 |
|
|
469 |
|
|
473 |
|
public void testPeek() { |
474 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
475 |
|
for (int i = 0; i < SIZE; ++i) { |
476 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
477 |
< |
q.poll(); |
476 |
> |
assertEquals(i, q.peek()); |
477 |
> |
assertEquals(i, q.poll()); |
478 |
|
assertTrue(q.peek() == null || |
479 |
< |
i != ((Integer)q.peek()).intValue()); |
479 |
> |
!q.peek().equals(i)); |
480 |
|
} |
481 |
< |
assertNull(q.peek()); |
481 |
> |
assertNull(q.peek()); |
482 |
|
} |
483 |
|
|
484 |
|
/** |
487 |
|
public void testElement() { |
488 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
489 |
|
for (int i = 0; i < SIZE; ++i) { |
490 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
491 |
< |
q.poll(); |
490 |
> |
assertEquals(i, q.element()); |
491 |
> |
assertEquals(i, q.poll()); |
492 |
|
} |
493 |
|
try { |
494 |
|
q.element(); |
495 |
|
shouldThrow(); |
496 |
< |
} |
576 |
< |
catch (NoSuchElementException success) {} |
496 |
> |
} catch (NoSuchElementException success) {} |
497 |
|
} |
498 |
|
|
499 |
|
/** |
502 |
|
public void testRemove() { |
503 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
504 |
|
for (int i = 0; i < SIZE; ++i) { |
505 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
505 |
> |
assertEquals(i, q.remove()); |
506 |
|
} |
507 |
|
try { |
508 |
|
q.remove(); |
509 |
|
shouldThrow(); |
510 |
< |
} catch (NoSuchElementException success){ |
591 |
< |
} |
510 |
> |
} catch (NoSuchElementException success) {} |
511 |
|
} |
512 |
|
|
513 |
|
/** |
604 |
|
/** |
605 |
|
* toArray contains all elements |
606 |
|
*/ |
607 |
< |
public void testToArray() { |
607 |
> |
public void testToArray() throws InterruptedException { |
608 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
609 |
< |
Object[] o = q.toArray(); |
609 |
> |
Object[] o = q.toArray(); |
610 |
|
Arrays.sort(o); |
611 |
< |
try { |
612 |
< |
for (int i = 0; i < o.length; i++) |
694 |
< |
assertEquals(o[i], q.take()); |
695 |
< |
} catch (InterruptedException e){ |
696 |
< |
unexpectedException(); |
697 |
< |
} |
611 |
> |
for (int i = 0; i < o.length; i++) |
612 |
> |
assertEquals(o[i], q.take()); |
613 |
|
} |
614 |
|
|
615 |
|
/** |
616 |
|
* toArray(a) contains all elements |
617 |
|
*/ |
618 |
< |
public void testToArray2() { |
618 |
> |
public void testToArray2() throws InterruptedException { |
619 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
620 |
< |
Integer[] ints = new Integer[SIZE]; |
621 |
< |
ints = (Integer[])q.toArray(ints); |
620 |
> |
Integer[] ints = new Integer[SIZE]; |
621 |
> |
ints = (Integer[])q.toArray(ints); |
622 |
|
Arrays.sort(ints); |
623 |
< |
try { |
624 |
< |
for (int i = 0; i < ints.length; i++) |
710 |
< |
assertEquals(ints[i], q.take()); |
711 |
< |
} catch (InterruptedException e){ |
712 |
< |
unexpectedException(); |
713 |
< |
} |
623 |
> |
for (int i = 0; i < ints.length; i++) |
624 |
> |
assertEquals(ints[i], q.take()); |
625 |
|
} |
626 |
|
|
627 |
|
/** |
628 |
|
* toArray(null) throws NPE |
629 |
|
*/ |
630 |
|
public void testToArray_BadArg() { |
631 |
< |
try { |
632 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
633 |
< |
Object o[] = q.toArray(null); |
634 |
< |
shouldThrow(); |
635 |
< |
} catch (NullPointerException success){} |
631 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
632 |
> |
try { |
633 |
> |
Object o[] = q.toArray(null); |
634 |
> |
shouldThrow(); |
635 |
> |
} catch (NullPointerException success) {} |
636 |
|
} |
637 |
|
|
638 |
|
/** |
639 |
|
* toArray with incompatible array type throws CCE |
640 |
|
*/ |
641 |
|
public void testToArray1_BadArg() { |
642 |
< |
try { |
643 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
644 |
< |
Object o[] = q.toArray(new String[10] ); |
645 |
< |
shouldThrow(); |
646 |
< |
} catch (ArrayStoreException success){} |
642 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
643 |
> |
try { |
644 |
> |
Object o[] = q.toArray(new String[10]); |
645 |
> |
shouldThrow(); |
646 |
> |
} catch (ArrayStoreException success) {} |
647 |
|
} |
648 |
|
|
649 |
|
/** |
652 |
|
public void testIterator() { |
653 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
654 |
|
int i = 0; |
655 |
< |
Iterator it = q.iterator(); |
655 |
> |
Iterator it = q.iterator(); |
656 |
|
while (it.hasNext()) { |
657 |
|
assertTrue(q.contains(it.next())); |
658 |
|
++i; |
697 |
|
public void testPollInExecutor() { |
698 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
699 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
700 |
< |
executor.execute(new Runnable() { |
701 |
< |
public void run() { |
702 |
< |
threadAssertNull(q.poll()); |
703 |
< |
try { |
704 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
705 |
< |
threadAssertTrue(q.isEmpty()); |
706 |
< |
} |
707 |
< |
catch (InterruptedException e) { |
708 |
< |
threadUnexpectedException(); |
709 |
< |
} |
710 |
< |
} |
711 |
< |
}); |
801 |
< |
|
802 |
< |
executor.execute(new Runnable() { |
803 |
< |
public void run() { |
804 |
< |
try { |
805 |
< |
Thread.sleep(SMALL_DELAY_MS); |
806 |
< |
q.put(new Integer(1)); |
807 |
< |
} |
808 |
< |
catch (InterruptedException e) { |
809 |
< |
threadUnexpectedException(); |
810 |
< |
} |
811 |
< |
} |
812 |
< |
}); |
700 |
> |
executor.execute(new CheckedRunnable() { |
701 |
> |
public void realRun() throws InterruptedException { |
702 |
> |
assertNull(q.poll()); |
703 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
704 |
> |
assertTrue(q.isEmpty()); |
705 |
> |
}}); |
706 |
> |
|
707 |
> |
executor.execute(new CheckedRunnable() { |
708 |
> |
public void realRun() throws InterruptedException { |
709 |
> |
Thread.sleep(SMALL_DELAY_MS); |
710 |
> |
q.put(one); |
711 |
> |
}}); |
712 |
|
|
713 |
|
joinPool(executor); |
714 |
|
} |
716 |
|
/** |
717 |
|
* A deserialized serialized queue has same elements |
718 |
|
*/ |
719 |
< |
public void testSerialization() { |
719 |
> |
public void testSerialization() throws Exception { |
720 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
721 |
< |
try { |
722 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
723 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
724 |
< |
out.writeObject(q); |
725 |
< |
out.close(); |
726 |
< |
|
727 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
728 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
729 |
< |
PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject(); |
730 |
< |
assertEquals(q.size(), r.size()); |
731 |
< |
while (!q.isEmpty()) |
833 |
< |
assertEquals(q.remove(), r.remove()); |
834 |
< |
} catch (Exception e){ |
835 |
< |
unexpectedException(); |
836 |
< |
} |
721 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
722 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
723 |
> |
out.writeObject(q); |
724 |
> |
out.close(); |
725 |
> |
|
726 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
727 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
728 |
> |
PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject(); |
729 |
> |
assertEquals(q.size(), r.size()); |
730 |
> |
while (!q.isEmpty()) |
731 |
> |
assertEquals(q.remove(), r.remove()); |
732 |
|
} |
733 |
|
|
734 |
|
/** |
739 |
|
try { |
740 |
|
q.drainTo(null); |
741 |
|
shouldThrow(); |
742 |
< |
} catch (NullPointerException success) { |
848 |
< |
} |
742 |
> |
} catch (NullPointerException success) {} |
743 |
|
} |
744 |
|
|
745 |
|
/** |
750 |
|
try { |
751 |
|
q.drainTo(q); |
752 |
|
shouldThrow(); |
753 |
< |
} catch (IllegalArgumentException success) { |
860 |
< |
} |
753 |
> |
} catch (IllegalArgumentException success) {} |
754 |
|
} |
755 |
|
|
756 |
|
/** |
780 |
|
/** |
781 |
|
* drainTo empties queue |
782 |
|
*/ |
783 |
< |
public void testDrainToWithActivePut() { |
783 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
784 |
|
final PriorityBlockingQueue q = populatedQueue(SIZE); |
785 |
< |
Thread t = new Thread(new Runnable() { |
786 |
< |
public void run() { |
787 |
< |
q.put(new Integer(SIZE+1)); |
788 |
< |
} |
789 |
< |
}); |
790 |
< |
try { |
791 |
< |
t.start(); |
792 |
< |
ArrayList l = new ArrayList(); |
793 |
< |
q.drainTo(l); |
794 |
< |
assertTrue(l.size() >= SIZE); |
795 |
< |
for (int i = 0; i < SIZE; ++i) |
796 |
< |
assertEquals(l.get(i), new Integer(i)); |
797 |
< |
t.join(); |
905 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
906 |
< |
} catch (Exception e){ |
907 |
< |
unexpectedException(); |
908 |
< |
} |
785 |
> |
Thread t = new Thread(new CheckedRunnable() { |
786 |
> |
public void realRun() { |
787 |
> |
q.put(new Integer(SIZE+1)); |
788 |
> |
}}); |
789 |
> |
|
790 |
> |
t.start(); |
791 |
> |
ArrayList l = new ArrayList(); |
792 |
> |
q.drainTo(l); |
793 |
> |
assertTrue(l.size() >= SIZE); |
794 |
> |
for (int i = 0; i < SIZE; ++i) |
795 |
> |
assertEquals(l.get(i), new Integer(i)); |
796 |
> |
t.join(); |
797 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
798 |
|
} |
799 |
|
|
800 |
|
/** |
805 |
|
try { |
806 |
|
q.drainTo(null, 0); |
807 |
|
shouldThrow(); |
808 |
< |
} catch (NullPointerException success) { |
920 |
< |
} |
808 |
> |
} catch (NullPointerException success) {} |
809 |
|
} |
810 |
|
|
811 |
|
/** |
816 |
|
try { |
817 |
|
q.drainTo(q, 0); |
818 |
|
shouldThrow(); |
819 |
< |
} catch (IllegalArgumentException success) { |
932 |
< |
} |
819 |
> |
} catch (IllegalArgumentException success) {} |
820 |
|
} |
821 |
|
|
822 |
|
/** |
838 |
|
} |
839 |
|
} |
840 |
|
|
954 |
– |
|
841 |
|
} |