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