1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/licenses/publicdomain |
5 |
*/ |
6 |
|
7 |
import junit.framework.*; |
8 |
import java.util.*; |
9 |
import java.util.concurrent.*; |
10 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
import java.io.*; |
12 |
|
13 |
public class LinkedBlockingDequeTest 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(LinkedBlockingDequeTest.class); |
20 |
} |
21 |
|
22 |
/** |
23 |
* Create a deque of given size containing consecutive |
24 |
* Integers 0 ... n. |
25 |
*/ |
26 |
private LinkedBlockingDeque populatedDeque(int n) { |
27 |
LinkedBlockingDeque q = new LinkedBlockingDeque(n); |
28 |
assertTrue(q.isEmpty()); |
29 |
for (int i = 0; i < n; i++) |
30 |
assertTrue(q.offer(new Integer(i))); |
31 |
assertFalse(q.isEmpty()); |
32 |
assertEquals(0, q.remainingCapacity()); |
33 |
assertEquals(n, q.size()); |
34 |
return q; |
35 |
} |
36 |
|
37 |
/** |
38 |
* isEmpty is true before add, false after |
39 |
*/ |
40 |
public void testEmpty() { |
41 |
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
42 |
assertTrue(q.isEmpty()); |
43 |
q.add(new Integer(1)); |
44 |
assertFalse(q.isEmpty()); |
45 |
q.add(new Integer(2)); |
46 |
q.removeFirst(); |
47 |
q.removeFirst(); |
48 |
assertTrue(q.isEmpty()); |
49 |
} |
50 |
|
51 |
/** |
52 |
* size changes when elements added and removed |
53 |
*/ |
54 |
public void testSize() { |
55 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
56 |
for (int i = 0; i < SIZE; ++i) { |
57 |
assertEquals(SIZE-i, q.size()); |
58 |
q.removeFirst(); |
59 |
} |
60 |
for (int i = 0; i < SIZE; ++i) { |
61 |
assertEquals(i, q.size()); |
62 |
q.add(new Integer(i)); |
63 |
} |
64 |
} |
65 |
|
66 |
/** |
67 |
* offer(null) throws NPE |
68 |
*/ |
69 |
public void testOfferFirstNull() { |
70 |
try { |
71 |
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
72 |
q.offerFirst(null); |
73 |
shouldThrow(); |
74 |
} catch (NullPointerException success) {} |
75 |
} |
76 |
|
77 |
/** |
78 |
* OfferFirst succeeds |
79 |
*/ |
80 |
public void testOfferFirst() { |
81 |
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
82 |
assertTrue(q.offerFirst(new Integer(0))); |
83 |
assertTrue(q.offerFirst(new Integer(1))); |
84 |
} |
85 |
|
86 |
/** |
87 |
* OfferLast succeeds |
88 |
*/ |
89 |
public void testOfferLast() { |
90 |
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
91 |
assertTrue(q.offerLast(new Integer(0))); |
92 |
assertTrue(q.offerLast(new Integer(1))); |
93 |
} |
94 |
|
95 |
/** |
96 |
* pollFirst succeeds unless empty |
97 |
*/ |
98 |
public void testPollFirst() { |
99 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
100 |
for (int i = 0; i < SIZE; ++i) { |
101 |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
102 |
} |
103 |
assertNull(q.pollFirst()); |
104 |
} |
105 |
|
106 |
/** |
107 |
* pollLast succeeds unless empty |
108 |
*/ |
109 |
public void testPollLast() { |
110 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
111 |
for (int i = SIZE-1; i >= 0; --i) { |
112 |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
113 |
} |
114 |
assertNull(q.pollLast()); |
115 |
} |
116 |
|
117 |
/** |
118 |
* peekFirst returns next element, or null if empty |
119 |
*/ |
120 |
public void testPeekFirst() { |
121 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
122 |
for (int i = 0; i < SIZE; ++i) { |
123 |
assertEquals(i, ((Integer)q.peekFirst()).intValue()); |
124 |
q.pollFirst(); |
125 |
assertTrue(q.peekFirst() == null || |
126 |
i != ((Integer)q.peekFirst()).intValue()); |
127 |
} |
128 |
assertNull(q.peekFirst()); |
129 |
} |
130 |
|
131 |
/** |
132 |
* peek returns next element, or null if empty |
133 |
*/ |
134 |
public void testPeek() { |
135 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
136 |
for (int i = 0; i < SIZE; ++i) { |
137 |
assertEquals(i, ((Integer)q.peek()).intValue()); |
138 |
q.pollFirst(); |
139 |
assertTrue(q.peek() == null || |
140 |
i != ((Integer)q.peek()).intValue()); |
141 |
} |
142 |
assertNull(q.peek()); |
143 |
} |
144 |
|
145 |
/** |
146 |
* peekLast returns next element, or null if empty |
147 |
*/ |
148 |
public void testPeekLast() { |
149 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
150 |
for (int i = SIZE-1; i >= 0; --i) { |
151 |
assertEquals(i, ((Integer)q.peekLast()).intValue()); |
152 |
q.pollLast(); |
153 |
assertTrue(q.peekLast() == null || |
154 |
i != ((Integer)q.peekLast()).intValue()); |
155 |
} |
156 |
assertNull(q.peekLast()); |
157 |
} |
158 |
|
159 |
/** |
160 |
* getFirst returns next getFirst, or throws NSEE if empty |
161 |
*/ |
162 |
public void testFirstElement() { |
163 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
164 |
for (int i = 0; i < SIZE; ++i) { |
165 |
assertEquals(i, ((Integer)q.getFirst()).intValue()); |
166 |
q.pollFirst(); |
167 |
} |
168 |
try { |
169 |
q.getFirst(); |
170 |
shouldThrow(); |
171 |
} catch (NoSuchElementException success) {} |
172 |
} |
173 |
|
174 |
/** |
175 |
* getLast returns next element, or throws NSEE if empty |
176 |
*/ |
177 |
public void testLastElement() { |
178 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
179 |
for (int i = SIZE-1; i >= 0; --i) { |
180 |
assertEquals(i, ((Integer)q.getLast()).intValue()); |
181 |
q.pollLast(); |
182 |
} |
183 |
try { |
184 |
q.getLast(); |
185 |
shouldThrow(); |
186 |
} catch (NoSuchElementException success) {} |
187 |
assertNull(q.peekLast()); |
188 |
} |
189 |
|
190 |
/** |
191 |
* removeFirst removes next element, or throws NSEE if empty |
192 |
*/ |
193 |
public void testRemoveFirst() { |
194 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
195 |
for (int i = 0; i < SIZE; ++i) { |
196 |
assertEquals(i, ((Integer)q.removeFirst()).intValue()); |
197 |
} |
198 |
try { |
199 |
q.removeFirst(); |
200 |
shouldThrow(); |
201 |
} catch (NoSuchElementException success) {} |
202 |
} |
203 |
|
204 |
/** |
205 |
* remove removes next element, or throws NSEE if empty |
206 |
*/ |
207 |
public void testRemove() { |
208 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
209 |
for (int i = 0; i < SIZE; ++i) { |
210 |
assertEquals(i, ((Integer)q.remove()).intValue()); |
211 |
} |
212 |
try { |
213 |
q.remove(); |
214 |
shouldThrow(); |
215 |
} catch (NoSuchElementException success) {} |
216 |
} |
217 |
|
218 |
/** |
219 |
* removeFirstOccurrence(x) removes x and returns true if present |
220 |
*/ |
221 |
public void testRemoveFirstOccurrence() { |
222 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
223 |
for (int i = 1; i < SIZE; i+=2) { |
224 |
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
225 |
} |
226 |
for (int i = 0; i < SIZE; i+=2) { |
227 |
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
228 |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
229 |
} |
230 |
assertTrue(q.isEmpty()); |
231 |
} |
232 |
|
233 |
/** |
234 |
* removeLastOccurrence(x) removes x and returns true if present |
235 |
*/ |
236 |
public void testRemoveLastOccurrence() { |
237 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
238 |
for (int i = 1; i < SIZE; i+=2) { |
239 |
assertTrue(q.removeLastOccurrence(new Integer(i))); |
240 |
} |
241 |
for (int i = 0; i < SIZE; i+=2) { |
242 |
assertTrue(q.removeLastOccurrence(new Integer(i))); |
243 |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
244 |
} |
245 |
assertTrue(q.isEmpty()); |
246 |
} |
247 |
|
248 |
/** |
249 |
* peekFirst returns element inserted with addFirst |
250 |
*/ |
251 |
public void testAddFirst() { |
252 |
LinkedBlockingDeque q = populatedDeque(3); |
253 |
q.pollLast(); |
254 |
q.addFirst(four); |
255 |
assertEquals(four,q.peekFirst()); |
256 |
} |
257 |
|
258 |
/** |
259 |
* peekLast returns element inserted with addLast |
260 |
*/ |
261 |
public void testAddLast() { |
262 |
LinkedBlockingDeque q = populatedDeque(3); |
263 |
q.pollLast(); |
264 |
q.addLast(four); |
265 |
assertEquals(four,q.peekLast()); |
266 |
} |
267 |
|
268 |
|
269 |
/** |
270 |
* A new deque has the indicated capacity, or Integer.MAX_VALUE if |
271 |
* none given |
272 |
*/ |
273 |
public void testConstructor1() { |
274 |
assertEquals(SIZE, new LinkedBlockingDeque(SIZE).remainingCapacity()); |
275 |
assertEquals(Integer.MAX_VALUE, new LinkedBlockingDeque().remainingCapacity()); |
276 |
} |
277 |
|
278 |
/** |
279 |
* Constructor throws IAE if capacity argument nonpositive |
280 |
*/ |
281 |
public void testConstructor2() { |
282 |
try { |
283 |
LinkedBlockingDeque q = new LinkedBlockingDeque(0); |
284 |
shouldThrow(); |
285 |
} catch (IllegalArgumentException success) {} |
286 |
} |
287 |
|
288 |
/** |
289 |
* Initializing from null Collection throws NPE |
290 |
*/ |
291 |
public void testConstructor3() { |
292 |
try { |
293 |
LinkedBlockingDeque q = new LinkedBlockingDeque(null); |
294 |
shouldThrow(); |
295 |
} catch (NullPointerException success) {} |
296 |
} |
297 |
|
298 |
/** |
299 |
* Initializing from Collection of null elements throws NPE |
300 |
*/ |
301 |
public void testConstructor4() { |
302 |
try { |
303 |
Integer[] ints = new Integer[SIZE]; |
304 |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
305 |
shouldThrow(); |
306 |
} catch (NullPointerException success) {} |
307 |
} |
308 |
|
309 |
/** |
310 |
* Initializing from Collection with some null elements throws NPE |
311 |
*/ |
312 |
public void testConstructor5() { |
313 |
try { |
314 |
Integer[] ints = new Integer[SIZE]; |
315 |
for (int i = 0; i < SIZE-1; ++i) |
316 |
ints[i] = new Integer(i); |
317 |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
318 |
shouldThrow(); |
319 |
} catch (NullPointerException success) {} |
320 |
} |
321 |
|
322 |
/** |
323 |
* Deque contains all elements of collection used to initialize |
324 |
*/ |
325 |
public void testConstructor6() { |
326 |
try { |
327 |
Integer[] ints = new Integer[SIZE]; |
328 |
for (int i = 0; i < SIZE; ++i) |
329 |
ints[i] = new Integer(i); |
330 |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
331 |
for (int i = 0; i < SIZE; ++i) |
332 |
assertEquals(ints[i], q.poll()); |
333 |
} |
334 |
finally {} |
335 |
} |
336 |
|
337 |
/** |
338 |
* Deque transitions from empty to full when elements added |
339 |
*/ |
340 |
public void testEmptyFull() { |
341 |
LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
342 |
assertTrue(q.isEmpty()); |
343 |
assertEquals("should have room for 2", 2, q.remainingCapacity()); |
344 |
q.add(one); |
345 |
assertFalse(q.isEmpty()); |
346 |
q.add(two); |
347 |
assertFalse(q.isEmpty()); |
348 |
assertEquals(0, q.remainingCapacity()); |
349 |
assertFalse(q.offer(three)); |
350 |
} |
351 |
|
352 |
/** |
353 |
* remainingCapacity decreases on add, increases on remove |
354 |
*/ |
355 |
public void testRemainingCapacity() { |
356 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
357 |
for (int i = 0; i < SIZE; ++i) { |
358 |
assertEquals(i, q.remainingCapacity()); |
359 |
assertEquals(SIZE-i, q.size()); |
360 |
q.remove(); |
361 |
} |
362 |
for (int i = 0; i < SIZE; ++i) { |
363 |
assertEquals(SIZE-i, q.remainingCapacity()); |
364 |
assertEquals(i, q.size()); |
365 |
q.add(new Integer(i)); |
366 |
} |
367 |
} |
368 |
|
369 |
/** |
370 |
* offer(null) throws NPE |
371 |
*/ |
372 |
public void testOfferNull() { |
373 |
try { |
374 |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
375 |
q.offer(null); |
376 |
shouldThrow(); |
377 |
} catch (NullPointerException success) {} |
378 |
} |
379 |
|
380 |
/** |
381 |
* add(null) throws NPE |
382 |
*/ |
383 |
public void testAddNull() { |
384 |
try { |
385 |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
386 |
q.add(null); |
387 |
shouldThrow(); |
388 |
} catch (NullPointerException success) {} |
389 |
} |
390 |
|
391 |
/** |
392 |
* push(null) throws NPE |
393 |
*/ |
394 |
public void testPushNull() { |
395 |
try { |
396 |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
397 |
q.push(null); |
398 |
shouldThrow(); |
399 |
} catch (NullPointerException success) {} |
400 |
} |
401 |
|
402 |
/** |
403 |
* push succeeds if not full; throws ISE if full |
404 |
*/ |
405 |
public void testPush() { |
406 |
try { |
407 |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
408 |
for (int i = 0; i < SIZE; ++i) { |
409 |
Integer I = new Integer(i); |
410 |
q.push(I); |
411 |
assertEquals(I, q.peek()); |
412 |
} |
413 |
assertEquals(0, q.remainingCapacity()); |
414 |
q.push(new Integer(SIZE)); |
415 |
shouldThrow(); |
416 |
} catch (IllegalStateException success) {} |
417 |
} |
418 |
|
419 |
/** |
420 |
* peekFirst returns element inserted with push |
421 |
*/ |
422 |
public void testPushWithPeek() { |
423 |
LinkedBlockingDeque q = populatedDeque(3); |
424 |
q.pollLast(); |
425 |
q.push(four); |
426 |
assertEquals(four,q.peekFirst()); |
427 |
} |
428 |
|
429 |
|
430 |
/** |
431 |
* pop removes next element, or throws NSEE if empty |
432 |
*/ |
433 |
public void testPop() { |
434 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
435 |
for (int i = 0; i < SIZE; ++i) { |
436 |
assertEquals(i, ((Integer)q.pop()).intValue()); |
437 |
} |
438 |
try { |
439 |
q.pop(); |
440 |
shouldThrow(); |
441 |
} catch (NoSuchElementException success) {} |
442 |
} |
443 |
|
444 |
|
445 |
/** |
446 |
* Offer succeeds if not full; fails if full |
447 |
*/ |
448 |
public void testOffer() { |
449 |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
450 |
assertTrue(q.offer(zero)); |
451 |
assertFalse(q.offer(one)); |
452 |
} |
453 |
|
454 |
/** |
455 |
* add succeeds if not full; throws ISE if full |
456 |
*/ |
457 |
public void testAdd() { |
458 |
try { |
459 |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
460 |
for (int i = 0; i < SIZE; ++i) { |
461 |
assertTrue(q.add(new Integer(i))); |
462 |
} |
463 |
assertEquals(0, q.remainingCapacity()); |
464 |
q.add(new Integer(SIZE)); |
465 |
shouldThrow(); |
466 |
} catch (IllegalStateException success) {} |
467 |
} |
468 |
|
469 |
/** |
470 |
* addAll(null) throws NPE |
471 |
*/ |
472 |
public void testAddAll1() { |
473 |
try { |
474 |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
475 |
q.addAll(null); |
476 |
shouldThrow(); |
477 |
} catch (NullPointerException success) {} |
478 |
} |
479 |
|
480 |
/** |
481 |
* addAll(this) throws IAE |
482 |
*/ |
483 |
public void testAddAllSelf() { |
484 |
try { |
485 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
486 |
q.addAll(q); |
487 |
shouldThrow(); |
488 |
} catch (IllegalArgumentException success) {} |
489 |
} |
490 |
|
491 |
/** |
492 |
* addAll of a collection with null elements throws NPE |
493 |
*/ |
494 |
public void testAddAll2() { |
495 |
try { |
496 |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
497 |
Integer[] ints = new Integer[SIZE]; |
498 |
q.addAll(Arrays.asList(ints)); |
499 |
shouldThrow(); |
500 |
} catch (NullPointerException success) {} |
501 |
} |
502 |
/** |
503 |
* addAll of a collection with any null elements throws NPE after |
504 |
* possibly adding some elements |
505 |
*/ |
506 |
public void testAddAll3() { |
507 |
try { |
508 |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
509 |
Integer[] ints = new Integer[SIZE]; |
510 |
for (int i = 0; i < SIZE-1; ++i) |
511 |
ints[i] = new Integer(i); |
512 |
q.addAll(Arrays.asList(ints)); |
513 |
shouldThrow(); |
514 |
} catch (NullPointerException success) {} |
515 |
} |
516 |
/** |
517 |
* addAll throws ISE if not enough room |
518 |
*/ |
519 |
public void testAddAll4() { |
520 |
try { |
521 |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
522 |
Integer[] ints = new Integer[SIZE]; |
523 |
for (int i = 0; i < SIZE; ++i) |
524 |
ints[i] = new Integer(i); |
525 |
q.addAll(Arrays.asList(ints)); |
526 |
shouldThrow(); |
527 |
} catch (IllegalStateException success) {} |
528 |
} |
529 |
/** |
530 |
* Deque contains all elements, in traversal order, of successful addAll |
531 |
*/ |
532 |
public void testAddAll5() { |
533 |
try { |
534 |
Integer[] empty = new Integer[0]; |
535 |
Integer[] ints = new Integer[SIZE]; |
536 |
for (int i = 0; i < SIZE; ++i) |
537 |
ints[i] = new Integer(i); |
538 |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
539 |
assertFalse(q.addAll(Arrays.asList(empty))); |
540 |
assertTrue(q.addAll(Arrays.asList(ints))); |
541 |
for (int i = 0; i < SIZE; ++i) |
542 |
assertEquals(ints[i], q.poll()); |
543 |
} |
544 |
finally {} |
545 |
} |
546 |
|
547 |
|
548 |
/** |
549 |
* put(null) throws NPE |
550 |
*/ |
551 |
public void testPutNull() throws InterruptedException { |
552 |
try { |
553 |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
554 |
q.put(null); |
555 |
shouldThrow(); |
556 |
} catch (NullPointerException success) {} |
557 |
} |
558 |
|
559 |
/** |
560 |
* all elements successfully put are contained |
561 |
*/ |
562 |
public void testPut() throws InterruptedException { |
563 |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
564 |
for (int i = 0; i < SIZE; ++i) { |
565 |
Integer I = new Integer(i); |
566 |
q.put(I); |
567 |
assertTrue(q.contains(I)); |
568 |
} |
569 |
assertEquals(0, q.remainingCapacity()); |
570 |
} |
571 |
|
572 |
/** |
573 |
* put blocks interruptibly if full |
574 |
*/ |
575 |
public void testBlockingPut() throws InterruptedException { |
576 |
Thread t = new Thread(new CheckedRunnable() { |
577 |
public void realRun() { |
578 |
int added = 0; |
579 |
try { |
580 |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
581 |
for (int i = 0; i < SIZE; ++i) { |
582 |
q.put(new Integer(i)); |
583 |
++added; |
584 |
} |
585 |
q.put(new Integer(SIZE)); |
586 |
threadShouldThrow(); |
587 |
} catch (InterruptedException success) { |
588 |
threadAssertEquals(added, SIZE); |
589 |
} |
590 |
}}); |
591 |
|
592 |
t.start(); |
593 |
Thread.sleep(SHORT_DELAY_MS); |
594 |
t.interrupt(); |
595 |
t.join(); |
596 |
} |
597 |
|
598 |
/** |
599 |
* put blocks waiting for take when full |
600 |
*/ |
601 |
public void testPutWithTake() throws InterruptedException { |
602 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
603 |
Thread t = new Thread(new CheckedRunnable() { |
604 |
public void realRun() { |
605 |
int added = 0; |
606 |
try { |
607 |
q.put(new Object()); |
608 |
++added; |
609 |
q.put(new Object()); |
610 |
++added; |
611 |
q.put(new Object()); |
612 |
++added; |
613 |
q.put(new Object()); |
614 |
++added; |
615 |
threadShouldThrow(); |
616 |
} catch (InterruptedException success) { |
617 |
threadAssertTrue(added >= 2); |
618 |
} |
619 |
}}); |
620 |
|
621 |
t.start(); |
622 |
Thread.sleep(SHORT_DELAY_MS); |
623 |
q.take(); |
624 |
t.interrupt(); |
625 |
t.join(); |
626 |
} |
627 |
|
628 |
/** |
629 |
* timed offer times out if full and elements not taken |
630 |
*/ |
631 |
public void testTimedOffer() throws InterruptedException { |
632 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
633 |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
634 |
public void realRun() throws InterruptedException { |
635 |
q.put(new Object()); |
636 |
q.put(new Object()); |
637 |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
638 |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
639 |
}}; |
640 |
|
641 |
t.start(); |
642 |
Thread.sleep(SMALL_DELAY_MS); |
643 |
t.interrupt(); |
644 |
t.join(); |
645 |
} |
646 |
|
647 |
/** |
648 |
* take retrieves elements in FIFO order |
649 |
*/ |
650 |
public void testTake() throws InterruptedException { |
651 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
652 |
for (int i = 0; i < SIZE; ++i) { |
653 |
assertEquals(i, ((Integer)q.take()).intValue()); |
654 |
} |
655 |
} |
656 |
|
657 |
/** |
658 |
* take blocks interruptibly when empty |
659 |
*/ |
660 |
public void testTakeFromEmpty() throws InterruptedException { |
661 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
662 |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
663 |
public void realRun() throws InterruptedException { |
664 |
q.take(); |
665 |
}}; |
666 |
|
667 |
t.start(); |
668 |
Thread.sleep(SHORT_DELAY_MS); |
669 |
t.interrupt(); |
670 |
t.join(); |
671 |
} |
672 |
|
673 |
/** |
674 |
* Take removes existing elements until empty, then blocks interruptibly |
675 |
*/ |
676 |
public void testBlockingTake() throws InterruptedException { |
677 |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
678 |
public void realRun() throws InterruptedException { |
679 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
680 |
for (int i = 0; i < SIZE; ++i) { |
681 |
assertEquals(i, ((Integer)q.take()).intValue()); |
682 |
} |
683 |
q.take(); |
684 |
}}; |
685 |
|
686 |
t.start(); |
687 |
Thread.sleep(SHORT_DELAY_MS); |
688 |
t.interrupt(); |
689 |
t.join(); |
690 |
} |
691 |
|
692 |
|
693 |
/** |
694 |
* poll succeeds unless empty |
695 |
*/ |
696 |
public void testPoll() { |
697 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
698 |
for (int i = 0; i < SIZE; ++i) { |
699 |
assertEquals(i, ((Integer)q.poll()).intValue()); |
700 |
} |
701 |
assertNull(q.poll()); |
702 |
} |
703 |
|
704 |
/** |
705 |
* timed poll with zero timeout succeeds when non-empty, else times out |
706 |
*/ |
707 |
public void testTimedPoll0() throws InterruptedException { |
708 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
709 |
for (int i = 0; i < SIZE; ++i) { |
710 |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
711 |
} |
712 |
assertNull(q.poll(0, MILLISECONDS)); |
713 |
} |
714 |
|
715 |
/** |
716 |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
717 |
*/ |
718 |
public void testTimedPoll() throws InterruptedException { |
719 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
720 |
for (int i = 0; i < SIZE; ++i) { |
721 |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
722 |
} |
723 |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
724 |
} |
725 |
|
726 |
/** |
727 |
* Interrupted timed poll throws InterruptedException instead of |
728 |
* returning timeout status |
729 |
*/ |
730 |
public void testInterruptedTimedPoll() throws InterruptedException { |
731 |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
732 |
public void realRun() throws InterruptedException { |
733 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
734 |
for (int i = 0; i < SIZE; ++i) { |
735 |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
736 |
} |
737 |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
738 |
}}; |
739 |
|
740 |
t.start(); |
741 |
Thread.sleep(SHORT_DELAY_MS); |
742 |
t.interrupt(); |
743 |
t.join(); |
744 |
} |
745 |
|
746 |
/** |
747 |
* timed poll before a delayed offer fails; after offer succeeds; |
748 |
* on interruption throws |
749 |
*/ |
750 |
public void testTimedPollWithOffer() throws InterruptedException { |
751 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
752 |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
753 |
public void realRun() throws InterruptedException { |
754 |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
755 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
756 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
757 |
}}; |
758 |
|
759 |
t.start(); |
760 |
Thread.sleep(SMALL_DELAY_MS); |
761 |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
762 |
t.interrupt(); |
763 |
t.join(); |
764 |
} |
765 |
|
766 |
|
767 |
/** |
768 |
* putFirst(null) throws NPE |
769 |
*/ |
770 |
public void testPutFirstNull() throws InterruptedException { |
771 |
try { |
772 |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
773 |
q.putFirst(null); |
774 |
shouldThrow(); |
775 |
} catch (NullPointerException success) {} |
776 |
} |
777 |
|
778 |
/** |
779 |
* all elements successfully putFirst are contained |
780 |
*/ |
781 |
public void testPutFirst() throws InterruptedException { |
782 |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
783 |
for (int i = 0; i < SIZE; ++i) { |
784 |
Integer I = new Integer(i); |
785 |
q.putFirst(I); |
786 |
assertTrue(q.contains(I)); |
787 |
} |
788 |
assertEquals(0, q.remainingCapacity()); |
789 |
} |
790 |
|
791 |
/** |
792 |
* putFirst blocks interruptibly if full |
793 |
*/ |
794 |
public void testBlockingPutFirst() throws InterruptedException { |
795 |
Thread t = new Thread(new Runnable() { |
796 |
public void run() { |
797 |
int added = 0; |
798 |
try { |
799 |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
800 |
for (int i = 0; i < SIZE; ++i) { |
801 |
q.putFirst(new Integer(i)); |
802 |
++added; |
803 |
} |
804 |
q.putFirst(new Integer(SIZE)); |
805 |
threadShouldThrow(); |
806 |
} catch (InterruptedException success) { |
807 |
threadAssertEquals(added, SIZE); |
808 |
} |
809 |
}}); |
810 |
|
811 |
t.start(); |
812 |
Thread.sleep(SHORT_DELAY_MS); |
813 |
t.interrupt(); |
814 |
t.join(); |
815 |
} |
816 |
|
817 |
/** |
818 |
* putFirst blocks waiting for take when full |
819 |
*/ |
820 |
public void testPutFirstWithTake() throws InterruptedException { |
821 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
822 |
Thread t = new Thread(new Runnable() { |
823 |
public void run() { |
824 |
int added = 0; |
825 |
try { |
826 |
q.putFirst(new Object()); |
827 |
++added; |
828 |
q.putFirst(new Object()); |
829 |
++added; |
830 |
q.putFirst(new Object()); |
831 |
++added; |
832 |
q.putFirst(new Object()); |
833 |
++added; |
834 |
threadShouldThrow(); |
835 |
} catch (InterruptedException success) { |
836 |
threadAssertTrue(added >= 2); |
837 |
} |
838 |
} |
839 |
}); |
840 |
|
841 |
t.start(); |
842 |
Thread.sleep(SHORT_DELAY_MS); |
843 |
q.take(); |
844 |
t.interrupt(); |
845 |
t.join(); |
846 |
} |
847 |
|
848 |
/** |
849 |
* timed offerFirst times out if full and elements not taken |
850 |
*/ |
851 |
public void testTimedOfferFirst() throws InterruptedException { |
852 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
853 |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
854 |
public void realRun() throws InterruptedException { |
855 |
q.putFirst(new Object()); |
856 |
q.putFirst(new Object()); |
857 |
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
858 |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
859 |
}}; |
860 |
|
861 |
t.start(); |
862 |
Thread.sleep(SMALL_DELAY_MS); |
863 |
t.interrupt(); |
864 |
t.join(); |
865 |
} |
866 |
|
867 |
/** |
868 |
* take retrieves elements in FIFO order |
869 |
*/ |
870 |
public void testTakeFirst() throws InterruptedException { |
871 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
872 |
for (int i = 0; i < SIZE; ++i) { |
873 |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
874 |
} |
875 |
} |
876 |
|
877 |
/** |
878 |
* takeFirst blocks interruptibly when empty |
879 |
*/ |
880 |
public void testTakeFirstFromEmpty() throws InterruptedException { |
881 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
882 |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
883 |
public void realRun() throws InterruptedException { |
884 |
q.takeFirst(); |
885 |
}}; |
886 |
|
887 |
t.start(); |
888 |
Thread.sleep(SHORT_DELAY_MS); |
889 |
t.interrupt(); |
890 |
t.join(); |
891 |
} |
892 |
|
893 |
/** |
894 |
* TakeFirst removes existing elements until empty, then blocks interruptibly |
895 |
*/ |
896 |
public void testBlockingTakeFirst() throws InterruptedException { |
897 |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
898 |
public void realRun() throws InterruptedException { |
899 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
900 |
for (int i = 0; i < SIZE; ++i) { |
901 |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
902 |
} |
903 |
q.takeFirst(); |
904 |
}}; |
905 |
|
906 |
t.start(); |
907 |
Thread.sleep(SHORT_DELAY_MS); |
908 |
t.interrupt(); |
909 |
t.join(); |
910 |
} |
911 |
|
912 |
|
913 |
/** |
914 |
* timed pollFirst with zero timeout succeeds when non-empty, else times out |
915 |
*/ |
916 |
public void testTimedPollFirst0() throws InterruptedException { |
917 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
918 |
for (int i = 0; i < SIZE; ++i) { |
919 |
assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue()); |
920 |
} |
921 |
assertNull(q.pollFirst(0, MILLISECONDS)); |
922 |
} |
923 |
|
924 |
/** |
925 |
* timed pollFirst with nonzero timeout succeeds when non-empty, else times out |
926 |
*/ |
927 |
public void testTimedPollFirst() throws InterruptedException { |
928 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
929 |
for (int i = 0; i < SIZE; ++i) { |
930 |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
931 |
} |
932 |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
933 |
} |
934 |
|
935 |
/** |
936 |
* Interrupted timed pollFirst throws InterruptedException instead of |
937 |
* returning timeout status |
938 |
*/ |
939 |
public void testInterruptedTimedPollFirst() throws InterruptedException { |
940 |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
941 |
public void realRun() throws InterruptedException { |
942 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
943 |
for (int i = 0; i < SIZE; ++i) { |
944 |
threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
945 |
} |
946 |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
947 |
}}; |
948 |
|
949 |
t.start(); |
950 |
Thread.sleep(SHORT_DELAY_MS); |
951 |
t.interrupt(); |
952 |
t.join(); |
953 |
} |
954 |
|
955 |
/** |
956 |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
957 |
* on interruption throws |
958 |
*/ |
959 |
public void testTimedPollFirstWithOfferFirst() throws InterruptedException { |
960 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
961 |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
962 |
public void realRun() throws InterruptedException { |
963 |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
964 |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
965 |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
966 |
}}; |
967 |
|
968 |
t.start(); |
969 |
Thread.sleep(SMALL_DELAY_MS); |
970 |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS)); |
971 |
t.interrupt(); |
972 |
t.join(); |
973 |
} |
974 |
|
975 |
/** |
976 |
* putLast(null) throws NPE |
977 |
*/ |
978 |
public void testPutLastNull() throws InterruptedException { |
979 |
try { |
980 |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
981 |
q.putLast(null); |
982 |
shouldThrow(); |
983 |
} catch (NullPointerException success) {} |
984 |
} |
985 |
|
986 |
/** |
987 |
* all elements successfully putLast are contained |
988 |
*/ |
989 |
public void testPutLast() throws InterruptedException { |
990 |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
991 |
for (int i = 0; i < SIZE; ++i) { |
992 |
Integer I = new Integer(i); |
993 |
q.putLast(I); |
994 |
assertTrue(q.contains(I)); |
995 |
} |
996 |
assertEquals(0, q.remainingCapacity()); |
997 |
} |
998 |
|
999 |
/** |
1000 |
* putLast blocks interruptibly if full |
1001 |
*/ |
1002 |
public void testBlockingPutLast() throws InterruptedException { |
1003 |
Thread t = new Thread(new Runnable() { |
1004 |
public void run() { |
1005 |
int added = 0; |
1006 |
try { |
1007 |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1008 |
for (int i = 0; i < SIZE; ++i) { |
1009 |
q.putLast(new Integer(i)); |
1010 |
++added; |
1011 |
} |
1012 |
q.putLast(new Integer(SIZE)); |
1013 |
threadShouldThrow(); |
1014 |
} catch (InterruptedException success) { |
1015 |
threadAssertEquals(added, SIZE); |
1016 |
} |
1017 |
}}); |
1018 |
t.start(); |
1019 |
Thread.sleep(SHORT_DELAY_MS); |
1020 |
t.interrupt(); |
1021 |
t.join(); |
1022 |
} |
1023 |
|
1024 |
/** |
1025 |
* putLast blocks waiting for take when full |
1026 |
*/ |
1027 |
public void testPutLastWithTake() throws InterruptedException { |
1028 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1029 |
Thread t = new Thread(new Runnable() { |
1030 |
public void run() { |
1031 |
int added = 0; |
1032 |
try { |
1033 |
q.putLast(new Object()); |
1034 |
++added; |
1035 |
q.putLast(new Object()); |
1036 |
++added; |
1037 |
q.putLast(new Object()); |
1038 |
++added; |
1039 |
q.putLast(new Object()); |
1040 |
++added; |
1041 |
threadShouldThrow(); |
1042 |
} catch (InterruptedException success) { |
1043 |
threadAssertTrue(added >= 2); |
1044 |
} |
1045 |
} |
1046 |
}); |
1047 |
|
1048 |
t.start(); |
1049 |
Thread.sleep(SHORT_DELAY_MS); |
1050 |
q.take(); |
1051 |
t.interrupt(); |
1052 |
t.join(); |
1053 |
} |
1054 |
|
1055 |
/** |
1056 |
* timed offerLast times out if full and elements not taken |
1057 |
*/ |
1058 |
public void testTimedOfferLast() throws InterruptedException { |
1059 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1060 |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1061 |
public void realRun() throws InterruptedException { |
1062 |
q.putLast(new Object()); |
1063 |
q.putLast(new Object()); |
1064 |
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1065 |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1066 |
}}; |
1067 |
|
1068 |
t.start(); |
1069 |
Thread.sleep(SMALL_DELAY_MS); |
1070 |
t.interrupt(); |
1071 |
t.join(); |
1072 |
} |
1073 |
|
1074 |
/** |
1075 |
* takeLast retrieves elements in FIFO order |
1076 |
*/ |
1077 |
public void testTakeLast() throws InterruptedException { |
1078 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1079 |
for (int i = 0; i < SIZE; ++i) { |
1080 |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1081 |
} |
1082 |
} |
1083 |
|
1084 |
/** |
1085 |
* takeLast blocks interruptibly when empty |
1086 |
*/ |
1087 |
public void testTakeLastFromEmpty() throws InterruptedException { |
1088 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1089 |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1090 |
public void realRun() throws InterruptedException { |
1091 |
q.takeLast(); |
1092 |
}}; |
1093 |
|
1094 |
t.start(); |
1095 |
Thread.sleep(SHORT_DELAY_MS); |
1096 |
t.interrupt(); |
1097 |
t.join(); |
1098 |
} |
1099 |
|
1100 |
/** |
1101 |
* TakeLast removes existing elements until empty, then blocks interruptibly |
1102 |
*/ |
1103 |
public void testBlockingTakeLast() throws InterruptedException { |
1104 |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1105 |
public void realRun() throws InterruptedException { |
1106 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1107 |
for (int i = 0; i < SIZE; ++i) { |
1108 |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1109 |
} |
1110 |
q.takeLast(); |
1111 |
}}; |
1112 |
|
1113 |
t.start(); |
1114 |
Thread.sleep(SHORT_DELAY_MS); |
1115 |
t.interrupt(); |
1116 |
t.join(); |
1117 |
} |
1118 |
|
1119 |
|
1120 |
/** |
1121 |
* timed pollLast with zero timeout succeeds when non-empty, else times out |
1122 |
*/ |
1123 |
public void testTimedPollLast0() throws InterruptedException { |
1124 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1125 |
for (int i = 0; i < SIZE; ++i) { |
1126 |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue()); |
1127 |
} |
1128 |
assertNull(q.pollLast(0, MILLISECONDS)); |
1129 |
} |
1130 |
|
1131 |
/** |
1132 |
* timed pollLast with nonzero timeout succeeds when non-empty, else times out |
1133 |
*/ |
1134 |
public void testTimedPollLast() throws InterruptedException { |
1135 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1136 |
for (int i = 0; i < SIZE; ++i) { |
1137 |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1138 |
} |
1139 |
assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1140 |
} |
1141 |
|
1142 |
/** |
1143 |
* Interrupted timed pollLast throws InterruptedException instead of |
1144 |
* returning timeout status |
1145 |
*/ |
1146 |
public void testInterruptedTimedPollLast() throws InterruptedException { |
1147 |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1148 |
public void realRun() throws InterruptedException { |
1149 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1150 |
for (int i = 0; i < SIZE; ++i) { |
1151 |
threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1152 |
} |
1153 |
q.pollLast(SMALL_DELAY_MS, MILLISECONDS); |
1154 |
}}; |
1155 |
|
1156 |
t.start(); |
1157 |
Thread.sleep(SHORT_DELAY_MS); |
1158 |
t.interrupt(); |
1159 |
t.join(); |
1160 |
} |
1161 |
|
1162 |
/** |
1163 |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1164 |
* on interruption throws |
1165 |
*/ |
1166 |
public void testTimedPollWithOfferLast() throws InterruptedException { |
1167 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1168 |
Thread t = new Thread(new CheckedRunnable() { |
1169 |
public void realRun() throws InterruptedException { |
1170 |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
1171 |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1172 |
try { |
1173 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1174 |
shouldThrow(); |
1175 |
} catch (InterruptedException success) {} |
1176 |
}}); |
1177 |
|
1178 |
t.start(); |
1179 |
Thread.sleep(SMALL_DELAY_MS); |
1180 |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1181 |
t.interrupt(); |
1182 |
t.join(); |
1183 |
} |
1184 |
|
1185 |
|
1186 |
/** |
1187 |
* element returns next element, or throws NSEE if empty |
1188 |
*/ |
1189 |
public void testElement() { |
1190 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1191 |
for (int i = 0; i < SIZE; ++i) { |
1192 |
assertEquals(i, ((Integer)q.element()).intValue()); |
1193 |
q.poll(); |
1194 |
} |
1195 |
try { |
1196 |
q.element(); |
1197 |
shouldThrow(); |
1198 |
} catch (NoSuchElementException success) {} |
1199 |
} |
1200 |
|
1201 |
/** |
1202 |
* remove(x) removes x and returns true if present |
1203 |
*/ |
1204 |
public void testRemoveElement() { |
1205 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1206 |
for (int i = 1; i < SIZE; i+=2) { |
1207 |
assertTrue(q.remove(new Integer(i))); |
1208 |
} |
1209 |
for (int i = 0; i < SIZE; i+=2) { |
1210 |
assertTrue(q.remove(new Integer(i))); |
1211 |
assertFalse(q.remove(new Integer(i+1))); |
1212 |
} |
1213 |
assertTrue(q.isEmpty()); |
1214 |
} |
1215 |
|
1216 |
/** |
1217 |
* contains(x) reports true when elements added but not yet removed |
1218 |
*/ |
1219 |
public void testContains() { |
1220 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1221 |
for (int i = 0; i < SIZE; ++i) { |
1222 |
assertTrue(q.contains(new Integer(i))); |
1223 |
q.poll(); |
1224 |
assertFalse(q.contains(new Integer(i))); |
1225 |
} |
1226 |
} |
1227 |
|
1228 |
/** |
1229 |
* clear removes all elements |
1230 |
*/ |
1231 |
public void testClear() { |
1232 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1233 |
q.clear(); |
1234 |
assertTrue(q.isEmpty()); |
1235 |
assertEquals(0, q.size()); |
1236 |
assertEquals(SIZE, q.remainingCapacity()); |
1237 |
q.add(one); |
1238 |
assertFalse(q.isEmpty()); |
1239 |
assertTrue(q.contains(one)); |
1240 |
q.clear(); |
1241 |
assertTrue(q.isEmpty()); |
1242 |
} |
1243 |
|
1244 |
/** |
1245 |
* containsAll(c) is true when c contains a subset of elements |
1246 |
*/ |
1247 |
public void testContainsAll() { |
1248 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1249 |
LinkedBlockingDeque p = new LinkedBlockingDeque(SIZE); |
1250 |
for (int i = 0; i < SIZE; ++i) { |
1251 |
assertTrue(q.containsAll(p)); |
1252 |
assertFalse(p.containsAll(q)); |
1253 |
p.add(new Integer(i)); |
1254 |
} |
1255 |
assertTrue(p.containsAll(q)); |
1256 |
} |
1257 |
|
1258 |
/** |
1259 |
* retainAll(c) retains only those elements of c and reports true if changed |
1260 |
*/ |
1261 |
public void testRetainAll() { |
1262 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1263 |
LinkedBlockingDeque p = populatedDeque(SIZE); |
1264 |
for (int i = 0; i < SIZE; ++i) { |
1265 |
boolean changed = q.retainAll(p); |
1266 |
if (i == 0) |
1267 |
assertFalse(changed); |
1268 |
else |
1269 |
assertTrue(changed); |
1270 |
|
1271 |
assertTrue(q.containsAll(p)); |
1272 |
assertEquals(SIZE-i, q.size()); |
1273 |
p.remove(); |
1274 |
} |
1275 |
} |
1276 |
|
1277 |
/** |
1278 |
* removeAll(c) removes only those elements of c and reports true if changed |
1279 |
*/ |
1280 |
public void testRemoveAll() { |
1281 |
for (int i = 1; i < SIZE; ++i) { |
1282 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1283 |
LinkedBlockingDeque p = populatedDeque(i); |
1284 |
assertTrue(q.removeAll(p)); |
1285 |
assertEquals(SIZE-i, q.size()); |
1286 |
for (int j = 0; j < i; ++j) { |
1287 |
Integer I = (Integer)(p.remove()); |
1288 |
assertFalse(q.contains(I)); |
1289 |
} |
1290 |
} |
1291 |
} |
1292 |
|
1293 |
/** |
1294 |
* toArray contains all elements |
1295 |
*/ |
1296 |
public void testToArray() throws InterruptedException{ |
1297 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1298 |
Object[] o = q.toArray(); |
1299 |
for (int i = 0; i < o.length; i++) |
1300 |
assertEquals(o[i], q.take()); |
1301 |
} |
1302 |
|
1303 |
/** |
1304 |
* toArray(a) contains all elements |
1305 |
*/ |
1306 |
public void testToArray2() throws InterruptedException { |
1307 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1308 |
Integer[] ints = new Integer[SIZE]; |
1309 |
ints = (Integer[])q.toArray(ints); |
1310 |
for (int i = 0; i < ints.length; i++) |
1311 |
assertEquals(ints[i], q.take()); |
1312 |
} |
1313 |
|
1314 |
/** |
1315 |
* toArray(null) throws NPE |
1316 |
*/ |
1317 |
public void testToArray_BadArg() { |
1318 |
try { |
1319 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1320 |
Object o[] = q.toArray(null); |
1321 |
shouldThrow(); |
1322 |
} catch (NullPointerException success) {} |
1323 |
} |
1324 |
|
1325 |
/** |
1326 |
* toArray with incompatible array type throws CCE |
1327 |
*/ |
1328 |
public void testToArray1_BadArg() { |
1329 |
try { |
1330 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1331 |
Object o[] = q.toArray(new String[10] ); |
1332 |
shouldThrow(); |
1333 |
} catch (ArrayStoreException success) {} |
1334 |
} |
1335 |
|
1336 |
|
1337 |
/** |
1338 |
* iterator iterates through all elements |
1339 |
*/ |
1340 |
public void testIterator() throws InterruptedException { |
1341 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1342 |
Iterator it = q.iterator(); |
1343 |
while (it.hasNext()) { |
1344 |
assertEquals(it.next(), q.take()); |
1345 |
} |
1346 |
} |
1347 |
|
1348 |
/** |
1349 |
* iterator.remove removes current element |
1350 |
*/ |
1351 |
public void testIteratorRemove () { |
1352 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1353 |
q.add(two); |
1354 |
q.add(one); |
1355 |
q.add(three); |
1356 |
|
1357 |
Iterator it = q.iterator(); |
1358 |
it.next(); |
1359 |
it.remove(); |
1360 |
|
1361 |
it = q.iterator(); |
1362 |
assertEquals(it.next(), one); |
1363 |
assertEquals(it.next(), three); |
1364 |
assertFalse(it.hasNext()); |
1365 |
} |
1366 |
|
1367 |
|
1368 |
/** |
1369 |
* iterator ordering is FIFO |
1370 |
*/ |
1371 |
public void testIteratorOrdering() { |
1372 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1373 |
q.add(one); |
1374 |
q.add(two); |
1375 |
q.add(three); |
1376 |
assertEquals(0, q.remainingCapacity()); |
1377 |
int k = 0; |
1378 |
for (Iterator it = q.iterator(); it.hasNext();) { |
1379 |
int i = ((Integer)(it.next())).intValue(); |
1380 |
assertEquals(++k, i); |
1381 |
} |
1382 |
assertEquals(3, k); |
1383 |
} |
1384 |
|
1385 |
/** |
1386 |
* Modifications do not cause iterators to fail |
1387 |
*/ |
1388 |
public void testWeaklyConsistentIteration () { |
1389 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1390 |
q.add(one); |
1391 |
q.add(two); |
1392 |
q.add(three); |
1393 |
for (Iterator it = q.iterator(); it.hasNext();) { |
1394 |
q.remove(); |
1395 |
it.next(); |
1396 |
} |
1397 |
assertEquals(0, q.size()); |
1398 |
} |
1399 |
|
1400 |
|
1401 |
/** |
1402 |
* Descending iterator iterates through all elements |
1403 |
*/ |
1404 |
public void testDescendingIterator() { |
1405 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1406 |
int i = 0; |
1407 |
Iterator it = q.descendingIterator(); |
1408 |
while (it.hasNext()) { |
1409 |
assertTrue(q.contains(it.next())); |
1410 |
++i; |
1411 |
} |
1412 |
assertEquals(i, SIZE); |
1413 |
assertFalse(it.hasNext()); |
1414 |
try { |
1415 |
it.next(); |
1416 |
shouldThrow(); |
1417 |
} catch (NoSuchElementException success) {} |
1418 |
} |
1419 |
|
1420 |
/** |
1421 |
* Descending iterator ordering is reverse FIFO |
1422 |
*/ |
1423 |
public void testDescendingIteratorOrdering() { |
1424 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1425 |
for (int iters = 0; iters < 100; ++iters) { |
1426 |
q.add(new Integer(3)); |
1427 |
q.add(new Integer(2)); |
1428 |
q.add(new Integer(1)); |
1429 |
int k = 0; |
1430 |
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
1431 |
int i = ((Integer)(it.next())).intValue(); |
1432 |
assertEquals(++k, i); |
1433 |
} |
1434 |
|
1435 |
assertEquals(3, k); |
1436 |
q.remove(); |
1437 |
q.remove(); |
1438 |
q.remove(); |
1439 |
} |
1440 |
} |
1441 |
|
1442 |
/** |
1443 |
* descendingIterator.remove removes current element |
1444 |
*/ |
1445 |
public void testDescendingIteratorRemove () { |
1446 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1447 |
for (int iters = 0; iters < 100; ++iters) { |
1448 |
q.add(new Integer(3)); |
1449 |
q.add(new Integer(2)); |
1450 |
q.add(new Integer(1)); |
1451 |
Iterator it = q.descendingIterator(); |
1452 |
assertEquals(it.next(), new Integer(1)); |
1453 |
it.remove(); |
1454 |
assertEquals(it.next(), new Integer(2)); |
1455 |
it = q.descendingIterator(); |
1456 |
assertEquals(it.next(), new Integer(2)); |
1457 |
assertEquals(it.next(), new Integer(3)); |
1458 |
it.remove(); |
1459 |
assertFalse(it.hasNext()); |
1460 |
q.remove(); |
1461 |
} |
1462 |
} |
1463 |
|
1464 |
|
1465 |
/** |
1466 |
* toString contains toStrings of elements |
1467 |
*/ |
1468 |
public void testToString() { |
1469 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1470 |
String s = q.toString(); |
1471 |
for (int i = 0; i < SIZE; ++i) { |
1472 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
1473 |
} |
1474 |
} |
1475 |
|
1476 |
|
1477 |
/** |
1478 |
* offer transfers elements across Executor tasks |
1479 |
*/ |
1480 |
public void testOfferInExecutor() { |
1481 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1482 |
q.add(one); |
1483 |
q.add(two); |
1484 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1485 |
executor.execute(new CheckedRunnable() { |
1486 |
public void realRun() throws InterruptedException { |
1487 |
threadAssertFalse(q.offer(three)); |
1488 |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1489 |
threadAssertEquals(0, q.remainingCapacity()); |
1490 |
}}); |
1491 |
|
1492 |
executor.execute(new CheckedRunnable() { |
1493 |
public void realRun() throws InterruptedException { |
1494 |
Thread.sleep(SMALL_DELAY_MS); |
1495 |
threadAssertEquals(one, q.take()); |
1496 |
}}); |
1497 |
|
1498 |
joinPool(executor); |
1499 |
} |
1500 |
|
1501 |
/** |
1502 |
* poll retrieves elements across Executor threads |
1503 |
*/ |
1504 |
public void testPollInExecutor() { |
1505 |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1506 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1507 |
executor.execute(new CheckedRunnable() { |
1508 |
public void realRun() throws InterruptedException { |
1509 |
threadAssertNull(q.poll()); |
1510 |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1511 |
threadAssertTrue(q.isEmpty()); |
1512 |
}}); |
1513 |
|
1514 |
executor.execute(new CheckedRunnable() { |
1515 |
public void realRun() throws InterruptedException { |
1516 |
Thread.sleep(SMALL_DELAY_MS); |
1517 |
q.put(one); |
1518 |
}}); |
1519 |
|
1520 |
joinPool(executor); |
1521 |
} |
1522 |
|
1523 |
/** |
1524 |
* A deserialized serialized deque has same elements in same order |
1525 |
*/ |
1526 |
public void testSerialization() throws Exception { |
1527 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1528 |
|
1529 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1530 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1531 |
out.writeObject(q); |
1532 |
out.close(); |
1533 |
|
1534 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1535 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1536 |
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1537 |
assertEquals(q.size(), r.size()); |
1538 |
while (!q.isEmpty()) |
1539 |
assertEquals(q.remove(), r.remove()); |
1540 |
} |
1541 |
|
1542 |
/** |
1543 |
* drainTo(null) throws NPE |
1544 |
*/ |
1545 |
public void testDrainToNull() { |
1546 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1547 |
try { |
1548 |
q.drainTo(null); |
1549 |
shouldThrow(); |
1550 |
} catch (NullPointerException success) {} |
1551 |
} |
1552 |
|
1553 |
/** |
1554 |
* drainTo(this) throws IAE |
1555 |
*/ |
1556 |
public void testDrainToSelf() { |
1557 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1558 |
try { |
1559 |
q.drainTo(q); |
1560 |
shouldThrow(); |
1561 |
} catch (IllegalArgumentException success) {} |
1562 |
} |
1563 |
|
1564 |
/** |
1565 |
* drainTo(c) empties deque into another collection c |
1566 |
*/ |
1567 |
public void testDrainTo() { |
1568 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1569 |
ArrayList l = new ArrayList(); |
1570 |
q.drainTo(l); |
1571 |
assertEquals(q.size(), 0); |
1572 |
assertEquals(l.size(), SIZE); |
1573 |
for (int i = 0; i < SIZE; ++i) |
1574 |
assertEquals(l.get(i), new Integer(i)); |
1575 |
q.add(zero); |
1576 |
q.add(one); |
1577 |
assertFalse(q.isEmpty()); |
1578 |
assertTrue(q.contains(zero)); |
1579 |
assertTrue(q.contains(one)); |
1580 |
l.clear(); |
1581 |
q.drainTo(l); |
1582 |
assertEquals(q.size(), 0); |
1583 |
assertEquals(l.size(), 2); |
1584 |
for (int i = 0; i < 2; ++i) |
1585 |
assertEquals(l.get(i), new Integer(i)); |
1586 |
} |
1587 |
|
1588 |
/** |
1589 |
* drainTo empties full deque, unblocking a waiting put. |
1590 |
*/ |
1591 |
public void testDrainToWithActivePut() throws InterruptedException { |
1592 |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1593 |
Thread t = new Thread(new CheckedRunnable() { |
1594 |
public void realRun() throws InterruptedException { |
1595 |
q.put(new Integer(SIZE+1)); |
1596 |
}}); |
1597 |
|
1598 |
t.start(); |
1599 |
ArrayList l = new ArrayList(); |
1600 |
q.drainTo(l); |
1601 |
assertTrue(l.size() >= SIZE); |
1602 |
for (int i = 0; i < SIZE; ++i) |
1603 |
assertEquals(l.get(i), new Integer(i)); |
1604 |
t.join(); |
1605 |
assertTrue(q.size() + l.size() >= SIZE); |
1606 |
} |
1607 |
|
1608 |
/** |
1609 |
* drainTo(null, n) throws NPE |
1610 |
*/ |
1611 |
public void testDrainToNullN() { |
1612 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1613 |
try { |
1614 |
q.drainTo(null, 0); |
1615 |
shouldThrow(); |
1616 |
} catch (NullPointerException success) {} |
1617 |
} |
1618 |
|
1619 |
/** |
1620 |
* drainTo(this, n) throws IAE |
1621 |
*/ |
1622 |
public void testDrainToSelfN() { |
1623 |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1624 |
try { |
1625 |
q.drainTo(q, 0); |
1626 |
shouldThrow(); |
1627 |
} catch (IllegalArgumentException success) {} |
1628 |
} |
1629 |
|
1630 |
/** |
1631 |
* drainTo(c, n) empties first max {n, size} elements of deque into c |
1632 |
*/ |
1633 |
public void testDrainToN() { |
1634 |
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1635 |
for (int i = 0; i < SIZE + 2; ++i) { |
1636 |
for (int j = 0; j < SIZE; j++) |
1637 |
assertTrue(q.offer(new Integer(j))); |
1638 |
ArrayList l = new ArrayList(); |
1639 |
q.drainTo(l, i); |
1640 |
int k = (i < SIZE)? i : SIZE; |
1641 |
assertEquals(l.size(), k); |
1642 |
assertEquals(q.size(), SIZE-k); |
1643 |
for (int j = 0; j < k; ++j) |
1644 |
assertEquals(l.get(j), new Integer(j)); |
1645 |
while (q.poll() != null) ; |
1646 |
} |
1647 |
} |
1648 |
|
1649 |
} |