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. |
6 |
*/ |
7 |
|
8 |
import junit.framework.*; |
9 |
import java.util.*; |
10 |
import java.util.concurrent.*; |
11 |
import java.io.*; |
12 |
|
13 |
public class PriorityBlockingQueueTest extends JSR166TestCase { |
14 |
public static void main(String[] args) { |
15 |
junit.textui.TestRunner.run (suite()); |
16 |
} |
17 |
|
18 |
public static Test suite() { |
19 |
return new TestSuite(PriorityBlockingQueueTest.class); |
20 |
} |
21 |
|
22 |
private static final int NOCAP = Integer.MAX_VALUE; |
23 |
|
24 |
/** Sample Comparator */ |
25 |
static class MyReverseComparator implements Comparator { |
26 |
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 |
/** |
37 |
* Create a queue of given size containing consecutive |
38 |
* Integers 0 ... n. |
39 |
*/ |
40 |
private PriorityBlockingQueue populatedQueue(int n) { |
41 |
PriorityBlockingQueue q = new PriorityBlockingQueue(n); |
42 |
assertTrue(q.isEmpty()); |
43 |
for(int i = n-1; i >= 0; i-=2) |
44 |
assertTrue(q.offer(new Integer(i))); |
45 |
for(int i = (n & 1); i < n; i+=2) |
46 |
assertTrue(q.offer(new Integer(i))); |
47 |
assertFalse(q.isEmpty()); |
48 |
assertEquals(NOCAP, q.remainingCapacity()); |
49 |
assertEquals(n, q.size()); |
50 |
return q; |
51 |
} |
52 |
|
53 |
public void testConstructor1(){ |
54 |
assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity()); |
55 |
} |
56 |
|
57 |
public void testConstructor2(){ |
58 |
try { |
59 |
PriorityBlockingQueue q = new PriorityBlockingQueue(0); |
60 |
fail("Cannot make zero-sized"); |
61 |
} |
62 |
catch (IllegalArgumentException success) {} |
63 |
} |
64 |
|
65 |
public void testConstructor3(){ |
66 |
|
67 |
try { |
68 |
PriorityBlockingQueue q = new PriorityBlockingQueue(null); |
69 |
fail("Cannot make from null collection"); |
70 |
} |
71 |
catch (NullPointerException success) {} |
72 |
} |
73 |
|
74 |
public void testConstructor4(){ |
75 |
try { |
76 |
Integer[] ints = new Integer[SIZE]; |
77 |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
78 |
fail("Cannot make with null elements"); |
79 |
} |
80 |
catch (NullPointerException success) {} |
81 |
} |
82 |
|
83 |
public void testConstructor5(){ |
84 |
try { |
85 |
Integer[] ints = new Integer[SIZE]; |
86 |
for (int i = 0; i < SIZE-1; ++i) |
87 |
ints[i] = new Integer(i); |
88 |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
89 |
fail("Cannot make with null elements"); |
90 |
} |
91 |
catch (NullPointerException success) {} |
92 |
} |
93 |
|
94 |
public void testConstructor6(){ |
95 |
try { |
96 |
Integer[] ints = new Integer[SIZE]; |
97 |
for (int i = 0; i < SIZE; ++i) |
98 |
ints[i] = new Integer(i); |
99 |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
100 |
for (int i = 0; i < SIZE; ++i) |
101 |
assertEquals(ints[i], q.poll()); |
102 |
} |
103 |
finally {} |
104 |
} |
105 |
|
106 |
public void testConstructor7(){ |
107 |
try { |
108 |
MyReverseComparator cmp = new MyReverseComparator(); |
109 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp); |
110 |
assertEquals(cmp, q.comparator()); |
111 |
Integer[] ints = new Integer[SIZE]; |
112 |
for (int i = 0; i < SIZE; ++i) |
113 |
ints[i] = new Integer(i); |
114 |
q.addAll(Arrays.asList(ints)); |
115 |
for (int i = SIZE-1; i >= 0; --i) |
116 |
assertEquals(ints[i], q.poll()); |
117 |
} |
118 |
finally {} |
119 |
} |
120 |
|
121 |
public void testEmpty() { |
122 |
PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
123 |
assertTrue(q.isEmpty()); |
124 |
assertEquals(NOCAP, q.remainingCapacity()); |
125 |
q.add(new Integer(1)); |
126 |
assertFalse(q.isEmpty()); |
127 |
q.add(new Integer(2)); |
128 |
q.remove(); |
129 |
q.remove(); |
130 |
assertTrue(q.isEmpty()); |
131 |
} |
132 |
|
133 |
public void testRemainingCapacity(){ |
134 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
135 |
for (int i = 0; i < SIZE; ++i) { |
136 |
assertEquals(NOCAP, q.remainingCapacity()); |
137 |
assertEquals(SIZE-i, q.size()); |
138 |
q.remove(); |
139 |
} |
140 |
for (int i = 0; i < SIZE; ++i) { |
141 |
assertEquals(NOCAP, q.remainingCapacity()); |
142 |
assertEquals(i, q.size()); |
143 |
q.add(new Integer(i)); |
144 |
} |
145 |
} |
146 |
|
147 |
public void testOfferNull(){ |
148 |
try { |
149 |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
150 |
q.offer(null); |
151 |
fail("should throw NPE"); |
152 |
} catch (NullPointerException success) { } |
153 |
} |
154 |
|
155 |
public void testOffer() { |
156 |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
157 |
assertTrue(q.offer(new Integer(0))); |
158 |
assertTrue(q.offer(new Integer(1))); |
159 |
} |
160 |
|
161 |
public void testOfferNonComparable() { |
162 |
try { |
163 |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
164 |
q.offer(new Object()); |
165 |
q.offer(new Object()); |
166 |
q.offer(new Object()); |
167 |
fail("should throw CCE"); |
168 |
} |
169 |
catch(ClassCastException success) {} |
170 |
} |
171 |
|
172 |
public void testAdd(){ |
173 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
174 |
for (int i = 0; i < SIZE; ++i) { |
175 |
assertEquals(i, q.size()); |
176 |
assertTrue(q.add(new Integer(i))); |
177 |
} |
178 |
} |
179 |
|
180 |
public void testAddAll1(){ |
181 |
try { |
182 |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
183 |
q.addAll(null); |
184 |
fail("Cannot add null collection"); |
185 |
} |
186 |
catch (NullPointerException success) {} |
187 |
} |
188 |
public void testAddAll2(){ |
189 |
try { |
190 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
191 |
Integer[] ints = new Integer[SIZE]; |
192 |
q.addAll(Arrays.asList(ints)); |
193 |
fail("Cannot add null elements"); |
194 |
} |
195 |
catch (NullPointerException success) {} |
196 |
} |
197 |
public void testAddAll3(){ |
198 |
try { |
199 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
200 |
Integer[] ints = new Integer[SIZE]; |
201 |
for (int i = 0; i < SIZE-1; ++i) |
202 |
ints[i] = new Integer(i); |
203 |
q.addAll(Arrays.asList(ints)); |
204 |
fail("Cannot add null elements"); |
205 |
} |
206 |
catch (NullPointerException success) {} |
207 |
} |
208 |
|
209 |
public void testAddAll5(){ |
210 |
try { |
211 |
Integer[] empty = new Integer[0]; |
212 |
Integer[] ints = new Integer[SIZE]; |
213 |
for (int i = SIZE-1; i >= 0; --i) |
214 |
ints[i] = new Integer(i); |
215 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
216 |
assertFalse(q.addAll(Arrays.asList(empty))); |
217 |
assertTrue(q.addAll(Arrays.asList(ints))); |
218 |
for (int i = 0; i < SIZE; ++i) |
219 |
assertEquals(ints[i], q.poll()); |
220 |
} |
221 |
finally {} |
222 |
} |
223 |
|
224 |
public void testPutNull() { |
225 |
try { |
226 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
227 |
q.put(null); |
228 |
fail("put should throw NPE"); |
229 |
} |
230 |
catch (NullPointerException success){ |
231 |
} |
232 |
} |
233 |
|
234 |
public void testPut() { |
235 |
try { |
236 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
237 |
for (int i = 0; i < SIZE; ++i) { |
238 |
Integer I = new Integer(i); |
239 |
q.put(I); |
240 |
assertTrue(q.contains(I)); |
241 |
} |
242 |
assertEquals(SIZE, q.size()); |
243 |
} |
244 |
finally { |
245 |
} |
246 |
} |
247 |
|
248 |
public void testPutWithTake() { |
249 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
250 |
Thread t = new Thread(new Runnable() { |
251 |
public void run(){ |
252 |
int added = 0; |
253 |
try { |
254 |
q.put(new Integer(0)); |
255 |
++added; |
256 |
q.put(new Integer(0)); |
257 |
++added; |
258 |
q.put(new Integer(0)); |
259 |
++added; |
260 |
q.put(new Integer(0)); |
261 |
++added; |
262 |
threadAssertTrue(added == 4); |
263 |
} finally { |
264 |
} |
265 |
} |
266 |
}); |
267 |
try { |
268 |
t.start(); |
269 |
Thread.sleep(SHORT_DELAY_MS); |
270 |
q.take(); |
271 |
t.interrupt(); |
272 |
t.join(); |
273 |
} catch (Exception e){ |
274 |
fail("Unexpected exception"); |
275 |
} |
276 |
} |
277 |
|
278 |
public void testTimedOffer() { |
279 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
280 |
Thread t = new Thread(new Runnable() { |
281 |
public void run(){ |
282 |
try { |
283 |
q.put(new Integer(0)); |
284 |
q.put(new Integer(0)); |
285 |
threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
286 |
threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
287 |
} finally { } |
288 |
} |
289 |
}); |
290 |
|
291 |
try { |
292 |
t.start(); |
293 |
Thread.sleep(SMALL_DELAY_MS); |
294 |
t.interrupt(); |
295 |
t.join(); |
296 |
} catch (Exception e){ |
297 |
fail("Unexpected exception"); |
298 |
} |
299 |
} |
300 |
|
301 |
public void testTake(){ |
302 |
try { |
303 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
304 |
for (int i = 0; i < SIZE; ++i) { |
305 |
assertEquals(i, ((Integer)q.take()).intValue()); |
306 |
} |
307 |
} catch (InterruptedException e){ |
308 |
fail("Unexpected exception"); |
309 |
} |
310 |
} |
311 |
|
312 |
public void testTakeFromEmpty() { |
313 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
314 |
Thread t = new Thread(new Runnable() { |
315 |
public void run(){ |
316 |
try { |
317 |
q.take(); |
318 |
threadFail("Should block"); |
319 |
} catch (InterruptedException success){ } |
320 |
} |
321 |
}); |
322 |
try { |
323 |
t.start(); |
324 |
Thread.sleep(SHORT_DELAY_MS); |
325 |
t.interrupt(); |
326 |
t.join(); |
327 |
} catch (Exception e){ |
328 |
fail("Unexpected exception"); |
329 |
} |
330 |
} |
331 |
|
332 |
public void testBlockingTake(){ |
333 |
Thread t = new Thread(new Runnable() { |
334 |
public void run() { |
335 |
try { |
336 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
337 |
for (int i = 0; i < SIZE; ++i) { |
338 |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
339 |
} |
340 |
q.take(); |
341 |
threadFail("take should block"); |
342 |
} catch (InterruptedException success){ |
343 |
} |
344 |
}}); |
345 |
t.start(); |
346 |
try { |
347 |
Thread.sleep(SHORT_DELAY_MS); |
348 |
t.interrupt(); |
349 |
t.join(); |
350 |
} |
351 |
catch (InterruptedException ie) { |
352 |
fail("Unexpected exception"); |
353 |
} |
354 |
} |
355 |
|
356 |
|
357 |
public void testPoll(){ |
358 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
359 |
for (int i = 0; i < SIZE; ++i) { |
360 |
assertEquals(i, ((Integer)q.poll()).intValue()); |
361 |
} |
362 |
assertNull(q.poll()); |
363 |
} |
364 |
|
365 |
public void testTimedPoll0() { |
366 |
try { |
367 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
368 |
for (int i = 0; i < SIZE; ++i) { |
369 |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
370 |
} |
371 |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
372 |
} catch (InterruptedException e){ |
373 |
fail("Unexpected exception"); |
374 |
} |
375 |
} |
376 |
|
377 |
public void testTimedPoll() { |
378 |
try { |
379 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
380 |
for (int i = 0; i < SIZE; ++i) { |
381 |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
382 |
} |
383 |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
384 |
} catch (InterruptedException e){ |
385 |
fail("Unexpected exception"); |
386 |
} |
387 |
} |
388 |
|
389 |
public void testInterruptedTimedPoll(){ |
390 |
Thread t = new Thread(new Runnable() { |
391 |
public void run() { |
392 |
try { |
393 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
394 |
for (int i = 0; i < SIZE; ++i) { |
395 |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
396 |
} |
397 |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
398 |
} catch (InterruptedException success){ |
399 |
} |
400 |
}}); |
401 |
t.start(); |
402 |
try { |
403 |
Thread.sleep(SHORT_DELAY_MS); |
404 |
t.interrupt(); |
405 |
t.join(); |
406 |
} |
407 |
catch (InterruptedException ie) { |
408 |
fail("Unexpected exception"); |
409 |
} |
410 |
} |
411 |
|
412 |
public void testTimedPollWithOffer(){ |
413 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
414 |
Thread t = new Thread(new Runnable() { |
415 |
public void run(){ |
416 |
try { |
417 |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
418 |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
419 |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
420 |
threadFail("Should block"); |
421 |
} catch (InterruptedException success) { } |
422 |
} |
423 |
}); |
424 |
try { |
425 |
t.start(); |
426 |
Thread.sleep(SMALL_DELAY_MS); |
427 |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
428 |
t.interrupt(); |
429 |
t.join(); |
430 |
} catch (Exception e){ |
431 |
fail("Unexpected exception"); |
432 |
} |
433 |
} |
434 |
|
435 |
|
436 |
public void testPeek(){ |
437 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
438 |
for (int i = 0; i < SIZE; ++i) { |
439 |
assertEquals(i, ((Integer)q.peek()).intValue()); |
440 |
q.poll(); |
441 |
assertTrue(q.peek() == null || |
442 |
i != ((Integer)q.peek()).intValue()); |
443 |
} |
444 |
assertNull(q.peek()); |
445 |
} |
446 |
|
447 |
public void testElement(){ |
448 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
449 |
for (int i = 0; i < SIZE; ++i) { |
450 |
assertEquals(i, ((Integer)q.element()).intValue()); |
451 |
q.poll(); |
452 |
} |
453 |
try { |
454 |
q.element(); |
455 |
fail("no such element"); |
456 |
} |
457 |
catch (NoSuchElementException success) {} |
458 |
} |
459 |
|
460 |
public void testRemove(){ |
461 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
462 |
for (int i = 0; i < SIZE; ++i) { |
463 |
assertEquals(i, ((Integer)q.remove()).intValue()); |
464 |
} |
465 |
try { |
466 |
q.remove(); |
467 |
fail("remove should throw"); |
468 |
} catch (NoSuchElementException success){ |
469 |
} |
470 |
} |
471 |
|
472 |
public void testRemoveElement(){ |
473 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
474 |
for (int i = 1; i < SIZE; i+=2) { |
475 |
assertTrue(q.remove(new Integer(i))); |
476 |
} |
477 |
for (int i = 0; i < SIZE; i+=2) { |
478 |
assertTrue(q.remove(new Integer(i))); |
479 |
assertFalse(q.remove(new Integer(i+1))); |
480 |
} |
481 |
assertTrue(q.isEmpty()); |
482 |
} |
483 |
|
484 |
public void testContains(){ |
485 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
486 |
for (int i = 0; i < SIZE; ++i) { |
487 |
assertTrue(q.contains(new Integer(i))); |
488 |
q.poll(); |
489 |
assertFalse(q.contains(new Integer(i))); |
490 |
} |
491 |
} |
492 |
|
493 |
public void testClear(){ |
494 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
495 |
q.clear(); |
496 |
assertTrue(q.isEmpty()); |
497 |
assertEquals(0, q.size()); |
498 |
assertEquals(NOCAP, q.remainingCapacity()); |
499 |
q.add(new Integer(1)); |
500 |
assertFalse(q.isEmpty()); |
501 |
q.clear(); |
502 |
assertTrue(q.isEmpty()); |
503 |
} |
504 |
|
505 |
public void testContainsAll(){ |
506 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
507 |
PriorityBlockingQueue p = new PriorityBlockingQueue(SIZE); |
508 |
for (int i = 0; i < SIZE; ++i) { |
509 |
assertTrue(q.containsAll(p)); |
510 |
assertFalse(p.containsAll(q)); |
511 |
p.add(new Integer(i)); |
512 |
} |
513 |
assertTrue(p.containsAll(q)); |
514 |
} |
515 |
|
516 |
public void testRetainAll(){ |
517 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
518 |
PriorityBlockingQueue p = populatedQueue(SIZE); |
519 |
for (int i = 0; i < SIZE; ++i) { |
520 |
boolean changed = q.retainAll(p); |
521 |
if (i == 0) |
522 |
assertFalse(changed); |
523 |
else |
524 |
assertTrue(changed); |
525 |
|
526 |
assertTrue(q.containsAll(p)); |
527 |
assertEquals(SIZE-i, q.size()); |
528 |
p.remove(); |
529 |
} |
530 |
} |
531 |
|
532 |
public void testRemoveAll(){ |
533 |
for (int i = 1; i < SIZE; ++i) { |
534 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
535 |
PriorityBlockingQueue p = populatedQueue(i); |
536 |
assertTrue(q.removeAll(p)); |
537 |
assertEquals(SIZE-i, q.size()); |
538 |
for (int j = 0; j < i; ++j) { |
539 |
Integer I = (Integer)(p.remove()); |
540 |
assertFalse(q.contains(I)); |
541 |
} |
542 |
} |
543 |
} |
544 |
|
545 |
public void testToArray(){ |
546 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
547 |
Object[] o = q.toArray(); |
548 |
Arrays.sort(o); |
549 |
try { |
550 |
for(int i = 0; i < o.length; i++) |
551 |
assertEquals(o[i], q.take()); |
552 |
} catch (InterruptedException e){ |
553 |
fail("Unexpected exception"); |
554 |
} |
555 |
} |
556 |
|
557 |
public void testToArray2(){ |
558 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
559 |
Integer[] ints = new Integer[SIZE]; |
560 |
ints = (Integer[])q.toArray(ints); |
561 |
Arrays.sort(ints); |
562 |
try { |
563 |
for(int i = 0; i < ints.length; i++) |
564 |
assertEquals(ints[i], q.take()); |
565 |
} catch (InterruptedException e){ |
566 |
fail("Unexpected exception"); |
567 |
} |
568 |
} |
569 |
|
570 |
public void testIterator(){ |
571 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
572 |
int i = 0; |
573 |
Iterator it = q.iterator(); |
574 |
while(it.hasNext()) { |
575 |
assertTrue(q.contains(it.next())); |
576 |
++i; |
577 |
} |
578 |
assertEquals(i, SIZE); |
579 |
} |
580 |
|
581 |
public void testIteratorRemove () { |
582 |
|
583 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
584 |
|
585 |
q.add(new Integer(2)); |
586 |
q.add(new Integer(1)); |
587 |
q.add(new Integer(3)); |
588 |
|
589 |
Iterator it = q.iterator(); |
590 |
it.next(); |
591 |
it.remove(); |
592 |
|
593 |
it = q.iterator(); |
594 |
assertEquals(it.next(), new Integer(2)); |
595 |
assertEquals(it.next(), new Integer(3)); |
596 |
assertFalse(it.hasNext()); |
597 |
} |
598 |
|
599 |
|
600 |
public void testToString(){ |
601 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
602 |
String s = q.toString(); |
603 |
for (int i = 0; i < SIZE; ++i) { |
604 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
605 |
} |
606 |
} |
607 |
|
608 |
public void testPollInExecutor() { |
609 |
|
610 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
611 |
|
612 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
613 |
|
614 |
executor.execute(new Runnable() { |
615 |
public void run() { |
616 |
threadAssertNull(q.poll()); |
617 |
try { |
618 |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
619 |
threadAssertTrue(q.isEmpty()); |
620 |
} |
621 |
catch (InterruptedException e) { |
622 |
threadFail("should not be interrupted"); |
623 |
} |
624 |
} |
625 |
}); |
626 |
|
627 |
executor.execute(new Runnable() { |
628 |
public void run() { |
629 |
try { |
630 |
Thread.sleep(SMALL_DELAY_MS); |
631 |
q.put(new Integer(1)); |
632 |
} |
633 |
catch (InterruptedException e) { |
634 |
threadFail("should not be interrupted"); |
635 |
} |
636 |
} |
637 |
}); |
638 |
|
639 |
joinPool(executor); |
640 |
|
641 |
} |
642 |
|
643 |
public void testSerialization() { |
644 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
645 |
try { |
646 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
647 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
648 |
out.writeObject(q); |
649 |
out.close(); |
650 |
|
651 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
652 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
653 |
PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject(); |
654 |
assertEquals(q.size(), r.size()); |
655 |
while (!q.isEmpty()) |
656 |
assertEquals(q.remove(), r.remove()); |
657 |
} catch(Exception e){ |
658 |
fail("unexpected exception"); |
659 |
} |
660 |
} |
661 |
|
662 |
} |