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()); |
15 |
> |
junit.textui.TestRunner.run(suite()); |
16 |
|
} |
17 |
|
|
18 |
|
public static Test suite() { |
19 |
< |
return new TestSuite(LinkedBlockingDequeTest.class); |
19 |
> |
return new TestSuite(LinkedBlockingDequeTest.class); |
20 |
|
} |
21 |
|
|
22 |
|
/** |
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))); |
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()); |
33 |
> |
assertEquals(n, q.size()); |
34 |
|
return q; |
35 |
|
} |
36 |
|
|
67 |
|
* offer(null) throws NPE |
68 |
|
*/ |
69 |
|
public void testOfferFirstNull() { |
70 |
< |
try { |
70 |
> |
try { |
71 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
72 |
|
q.offerFirst(null); |
73 |
|
shouldThrow(); |
74 |
< |
} catch (NullPointerException success) { |
74 |
< |
} |
74 |
> |
} catch (NullPointerException success) {} |
75 |
|
} |
76 |
|
|
77 |
|
/** |
98 |
|
public void testPollFirst() { |
99 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
100 |
|
for (int i = 0; i < SIZE; ++i) { |
101 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
101 |
> |
assertEquals(i, q.pollFirst()); |
102 |
|
} |
103 |
< |
assertNull(q.pollFirst()); |
103 |
> |
assertNull(q.pollFirst()); |
104 |
|
} |
105 |
|
|
106 |
|
/** |
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()); |
112 |
> |
assertEquals(i, q.pollLast()); |
113 |
|
} |
114 |
< |
assertNull(q.pollLast()); |
114 |
> |
assertNull(q.pollLast()); |
115 |
|
} |
116 |
|
|
117 |
|
/** |
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(); |
123 |
> |
assertEquals(i, q.peekFirst()); |
124 |
> |
assertEquals(i, q.pollFirst()); |
125 |
|
assertTrue(q.peekFirst() == null || |
126 |
< |
i != ((Integer)q.peekFirst()).intValue()); |
126 |
> |
!q.peekFirst().equals(i)); |
127 |
|
} |
128 |
< |
assertNull(q.peekFirst()); |
128 |
> |
assertNull(q.peekFirst()); |
129 |
|
} |
130 |
|
|
131 |
|
/** |
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(); |
137 |
> |
assertEquals(i, q.peek()); |
138 |
> |
assertEquals(i, q.pollFirst()); |
139 |
|
assertTrue(q.peek() == null || |
140 |
< |
i != ((Integer)q.peek()).intValue()); |
140 |
> |
!q.peek().equals(i)); |
141 |
|
} |
142 |
< |
assertNull(q.peek()); |
142 |
> |
assertNull(q.peek()); |
143 |
|
} |
144 |
|
|
145 |
|
/** |
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(); |
151 |
> |
assertEquals(i, q.peekLast()); |
152 |
> |
assertEquals(i, q.pollLast()); |
153 |
|
assertTrue(q.peekLast() == null || |
154 |
< |
i != ((Integer)q.peekLast()).intValue()); |
154 |
> |
!q.peekLast().equals(i)); |
155 |
|
} |
156 |
< |
assertNull(q.peekLast()); |
156 |
> |
assertNull(q.peekLast()); |
157 |
|
} |
158 |
|
|
159 |
|
/** |
160 |
< |
* getFirst returns next getFirst, or throws NSEE if empty |
160 |
> |
* getFirst() returns first element, 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(); |
165 |
> |
assertEquals(i, q.getFirst()); |
166 |
> |
assertEquals(i, q.pollFirst()); |
167 |
|
} |
168 |
|
try { |
169 |
|
q.getFirst(); |
170 |
|
shouldThrow(); |
171 |
< |
} |
172 |
< |
catch (NoSuchElementException success) {} |
171 |
> |
} catch (NoSuchElementException success) {} |
172 |
> |
assertNull(q.peekFirst()); |
173 |
|
} |
174 |
|
|
175 |
|
/** |
176 |
< |
* getLast returns next element, or throws NSEE if empty |
176 |
> |
* getLast() returns last element, or throws NSEE if empty |
177 |
|
*/ |
178 |
|
public void testLastElement() { |
179 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
180 |
|
for (int i = SIZE-1; i >= 0; --i) { |
181 |
< |
assertEquals(i, ((Integer)q.getLast()).intValue()); |
182 |
< |
q.pollLast(); |
181 |
> |
assertEquals(i, q.getLast()); |
182 |
> |
assertEquals(i, q.pollLast()); |
183 |
|
} |
184 |
|
try { |
185 |
|
q.getLast(); |
186 |
|
shouldThrow(); |
187 |
< |
} |
188 |
< |
catch (NoSuchElementException success) {} |
189 |
< |
assertNull(q.peekLast()); |
187 |
> |
} catch (NoSuchElementException success) {} |
188 |
> |
assertNull(q.peekLast()); |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
< |
* removeFirst removes next element, or throws NSEE if empty |
192 |
> |
* removeFirst() removes first element, or throws NSEE if empty |
193 |
|
*/ |
194 |
|
public void testRemoveFirst() { |
195 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
196 |
|
for (int i = 0; i < SIZE; ++i) { |
197 |
< |
assertEquals(i, ((Integer)q.removeFirst()).intValue()); |
197 |
> |
assertEquals(i, q.removeFirst()); |
198 |
|
} |
199 |
|
try { |
200 |
|
q.removeFirst(); |
201 |
|
shouldThrow(); |
202 |
< |
} catch (NoSuchElementException success){ |
203 |
< |
} |
202 |
> |
} catch (NoSuchElementException success) {} |
203 |
> |
assertNull(q.peekFirst()); |
204 |
> |
} |
205 |
> |
|
206 |
> |
/** |
207 |
> |
* removeLast() removes last element, or throws NSEE if empty |
208 |
> |
*/ |
209 |
> |
public void testRemoveLast() { |
210 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
211 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
212 |
> |
assertEquals(i, q.removeLast()); |
213 |
> |
} |
214 |
> |
try { |
215 |
> |
q.removeLast(); |
216 |
> |
shouldThrow(); |
217 |
> |
} catch (NoSuchElementException success) {} |
218 |
> |
assertNull(q.peekLast()); |
219 |
|
} |
220 |
|
|
221 |
|
/** |
224 |
|
public void testRemove() { |
225 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
226 |
|
for (int i = 0; i < SIZE; ++i) { |
227 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
227 |
> |
assertEquals(i, q.remove()); |
228 |
|
} |
229 |
|
try { |
230 |
|
q.remove(); |
231 |
|
shouldThrow(); |
232 |
< |
} catch (NoSuchElementException success){ |
219 |
< |
} |
232 |
> |
} catch (NoSuchElementException success) {} |
233 |
|
} |
234 |
|
|
235 |
|
/** |
268 |
|
public void testAddFirst() { |
269 |
|
LinkedBlockingDeque q = populatedDeque(3); |
270 |
|
q.pollLast(); |
271 |
< |
q.addFirst(four); |
272 |
< |
assertEquals(four,q.peekFirst()); |
271 |
> |
q.addFirst(four); |
272 |
> |
assertSame(four, q.peekFirst()); |
273 |
|
} |
274 |
|
|
275 |
|
/** |
278 |
|
public void testAddLast() { |
279 |
|
LinkedBlockingDeque q = populatedDeque(3); |
280 |
|
q.pollLast(); |
281 |
< |
q.addLast(four); |
282 |
< |
assertEquals(four,q.peekLast()); |
281 |
> |
q.addLast(four); |
282 |
> |
assertSame(four, q.peekLast()); |
283 |
|
} |
284 |
|
|
285 |
|
|
293 |
|
} |
294 |
|
|
295 |
|
/** |
296 |
< |
* Constructor throws IAE if capacity argument nonpositive |
296 |
> |
* Constructor throws IAE if capacity argument nonpositive |
297 |
|
*/ |
298 |
|
public void testConstructor2() { |
299 |
|
try { |
300 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(0); |
301 |
|
shouldThrow(); |
302 |
< |
} |
290 |
< |
catch (IllegalArgumentException success) {} |
302 |
> |
} catch (IllegalArgumentException success) {} |
303 |
|
} |
304 |
|
|
305 |
|
/** |
309 |
|
try { |
310 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(null); |
311 |
|
shouldThrow(); |
312 |
< |
} |
301 |
< |
catch (NullPointerException success) {} |
312 |
> |
} catch (NullPointerException success) {} |
313 |
|
} |
314 |
|
|
315 |
|
/** |
320 |
|
Integer[] ints = new Integer[SIZE]; |
321 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
322 |
|
shouldThrow(); |
323 |
< |
} |
313 |
< |
catch (NullPointerException success) {} |
323 |
> |
} catch (NullPointerException success) {} |
324 |
|
} |
325 |
|
|
326 |
|
/** |
333 |
|
ints[i] = new Integer(i); |
334 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
335 |
|
shouldThrow(); |
336 |
< |
} |
327 |
< |
catch (NullPointerException success) {} |
336 |
> |
} catch (NullPointerException success) {} |
337 |
|
} |
338 |
|
|
339 |
|
/** |
340 |
|
* Deque contains all elements of collection used to initialize |
341 |
|
*/ |
342 |
|
public void testConstructor6() { |
343 |
< |
try { |
344 |
< |
Integer[] ints = new Integer[SIZE]; |
345 |
< |
for (int i = 0; i < SIZE; ++i) |
346 |
< |
ints[i] = new Integer(i); |
347 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
348 |
< |
for (int i = 0; i < SIZE; ++i) |
340 |
< |
assertEquals(ints[i], q.poll()); |
341 |
< |
} |
342 |
< |
finally {} |
343 |
> |
Integer[] ints = new Integer[SIZE]; |
344 |
> |
for (int i = 0; i < SIZE; ++i) |
345 |
> |
ints[i] = new Integer(i); |
346 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
347 |
> |
for (int i = 0; i < SIZE; ++i) |
348 |
> |
assertEquals(ints[i], q.poll()); |
349 |
|
} |
350 |
|
|
351 |
|
/** |
384 |
|
* offer(null) throws NPE |
385 |
|
*/ |
386 |
|
public void testOfferNull() { |
387 |
< |
try { |
387 |
> |
try { |
388 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
389 |
|
q.offer(null); |
390 |
|
shouldThrow(); |
391 |
< |
} catch (NullPointerException success) { } |
391 |
> |
} catch (NullPointerException success) {} |
392 |
|
} |
393 |
|
|
394 |
|
/** |
395 |
|
* add(null) throws NPE |
396 |
|
*/ |
397 |
|
public void testAddNull() { |
398 |
< |
try { |
398 |
> |
try { |
399 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
400 |
|
q.add(null); |
401 |
|
shouldThrow(); |
402 |
< |
} catch (NullPointerException success) { } |
402 |
> |
} catch (NullPointerException success) {} |
403 |
|
} |
404 |
|
|
405 |
|
/** |
406 |
|
* push(null) throws NPE |
407 |
|
*/ |
408 |
|
public void testPushNull() { |
409 |
< |
try { |
409 |
> |
try { |
410 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
411 |
|
q.push(null); |
412 |
|
shouldThrow(); |
413 |
< |
} catch (NullPointerException success) { } |
413 |
> |
} catch (NullPointerException success) {} |
414 |
|
} |
415 |
|
|
416 |
|
/** |
417 |
|
* push succeeds if not full; throws ISE if full |
418 |
|
*/ |
419 |
|
public void testPush() { |
420 |
< |
try { |
420 |
> |
try { |
421 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
422 |
|
for (int i = 0; i < SIZE; ++i) { |
423 |
|
Integer I = new Integer(i); |
426 |
|
} |
427 |
|
assertEquals(0, q.remainingCapacity()); |
428 |
|
q.push(new Integer(SIZE)); |
429 |
< |
} catch (IllegalStateException success){ |
430 |
< |
} |
429 |
> |
shouldThrow(); |
430 |
> |
} catch (IllegalStateException success) {} |
431 |
|
} |
432 |
|
|
433 |
|
/** |
436 |
|
public void testPushWithPeek() { |
437 |
|
LinkedBlockingDeque q = populatedDeque(3); |
438 |
|
q.pollLast(); |
439 |
< |
q.push(four); |
440 |
< |
assertEquals(four,q.peekFirst()); |
439 |
> |
q.push(four); |
440 |
> |
assertSame(four, q.peekFirst()); |
441 |
|
} |
442 |
|
|
443 |
|
|
447 |
|
public void testPop() { |
448 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
449 |
|
for (int i = 0; i < SIZE; ++i) { |
450 |
< |
assertEquals(i, ((Integer)q.pop()).intValue()); |
450 |
> |
assertEquals(i, q.pop()); |
451 |
|
} |
452 |
|
try { |
453 |
|
q.pop(); |
454 |
|
shouldThrow(); |
455 |
< |
} catch (NoSuchElementException success){ |
450 |
< |
} |
455 |
> |
} catch (NoSuchElementException success) {} |
456 |
|
} |
457 |
|
|
458 |
|
|
469 |
|
* add succeeds if not full; throws ISE if full |
470 |
|
*/ |
471 |
|
public void testAdd() { |
472 |
< |
try { |
472 |
> |
try { |
473 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
474 |
|
for (int i = 0; i < SIZE; ++i) { |
475 |
|
assertTrue(q.add(new Integer(i))); |
476 |
|
} |
477 |
|
assertEquals(0, q.remainingCapacity()); |
478 |
|
q.add(new Integer(SIZE)); |
479 |
< |
} catch (IllegalStateException success){ |
480 |
< |
} |
479 |
> |
shouldThrow(); |
480 |
> |
} catch (IllegalStateException success) {} |
481 |
|
} |
482 |
|
|
483 |
|
/** |
488 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
489 |
|
q.addAll(null); |
490 |
|
shouldThrow(); |
491 |
< |
} |
487 |
< |
catch (NullPointerException success) {} |
491 |
> |
} catch (NullPointerException success) {} |
492 |
|
} |
493 |
|
|
494 |
|
/** |
499 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
500 |
|
q.addAll(q); |
501 |
|
shouldThrow(); |
502 |
< |
} |
499 |
< |
catch (IllegalArgumentException success) {} |
502 |
> |
} catch (IllegalArgumentException success) {} |
503 |
|
} |
504 |
|
|
505 |
|
/** |
511 |
|
Integer[] ints = new Integer[SIZE]; |
512 |
|
q.addAll(Arrays.asList(ints)); |
513 |
|
shouldThrow(); |
514 |
< |
} |
512 |
< |
catch (NullPointerException success) {} |
514 |
> |
} catch (NullPointerException success) {} |
515 |
|
} |
516 |
+ |
|
517 |
|
/** |
518 |
|
* addAll of a collection with any null elements throws NPE after |
519 |
|
* possibly adding some elements |
526 |
|
ints[i] = new Integer(i); |
527 |
|
q.addAll(Arrays.asList(ints)); |
528 |
|
shouldThrow(); |
529 |
< |
} |
527 |
< |
catch (NullPointerException success) {} |
529 |
> |
} catch (NullPointerException success) {} |
530 |
|
} |
531 |
+ |
|
532 |
|
/** |
533 |
|
* addAll throws ISE if not enough room |
534 |
|
*/ |
540 |
|
ints[i] = new Integer(i); |
541 |
|
q.addAll(Arrays.asList(ints)); |
542 |
|
shouldThrow(); |
543 |
< |
} |
541 |
< |
catch (IllegalStateException success) {} |
543 |
> |
} catch (IllegalStateException success) {} |
544 |
|
} |
545 |
+ |
|
546 |
|
/** |
547 |
|
* Deque contains all elements, in traversal order, of successful addAll |
548 |
|
*/ |
549 |
|
public void testAddAll5() { |
550 |
< |
try { |
551 |
< |
Integer[] empty = new Integer[0]; |
552 |
< |
Integer[] ints = new Integer[SIZE]; |
553 |
< |
for (int i = 0; i < SIZE; ++i) |
554 |
< |
ints[i] = new Integer(i); |
555 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
556 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
557 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
558 |
< |
for (int i = 0; i < SIZE; ++i) |
556 |
< |
assertEquals(ints[i], q.poll()); |
557 |
< |
} |
558 |
< |
finally {} |
550 |
> |
Integer[] empty = new Integer[0]; |
551 |
> |
Integer[] ints = new Integer[SIZE]; |
552 |
> |
for (int i = 0; i < SIZE; ++i) |
553 |
> |
ints[i] = new Integer(i); |
554 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
555 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
556 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
557 |
> |
for (int i = 0; i < SIZE; ++i) |
558 |
> |
assertEquals(ints[i], q.poll()); |
559 |
|
} |
560 |
|
|
561 |
|
|
562 |
|
/** |
563 |
|
* put(null) throws NPE |
564 |
|
*/ |
565 |
< |
public void testPutNull() { |
566 |
< |
try { |
565 |
> |
public void testPutNull() throws InterruptedException { |
566 |
> |
try { |
567 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
568 |
|
q.put(null); |
569 |
|
shouldThrow(); |
570 |
< |
} |
571 |
< |
catch (NullPointerException success){ |
572 |
< |
} |
573 |
< |
catch (InterruptedException ie) { |
574 |
< |
unexpectedException(); |
575 |
< |
} |
576 |
< |
} |
570 |
> |
} catch (NullPointerException success) {} |
571 |
> |
} |
572 |
|
|
573 |
|
/** |
574 |
|
* all elements successfully put are contained |
575 |
|
*/ |
576 |
< |
public void testPut() { |
577 |
< |
try { |
578 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
579 |
< |
for (int i = 0; i < SIZE; ++i) { |
580 |
< |
Integer I = new Integer(i); |
581 |
< |
q.put(I); |
587 |
< |
assertTrue(q.contains(I)); |
588 |
< |
} |
589 |
< |
assertEquals(0, q.remainingCapacity()); |
590 |
< |
} |
591 |
< |
catch (InterruptedException ie) { |
592 |
< |
unexpectedException(); |
576 |
> |
public void testPut() throws InterruptedException { |
577 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
578 |
> |
for (int i = 0; i < SIZE; ++i) { |
579 |
> |
Integer I = new Integer(i); |
580 |
> |
q.put(I); |
581 |
> |
assertTrue(q.contains(I)); |
582 |
|
} |
583 |
+ |
assertEquals(0, q.remainingCapacity()); |
584 |
|
} |
585 |
|
|
586 |
|
/** |
587 |
|
* put blocks interruptibly if full |
588 |
|
*/ |
589 |
< |
public void testBlockingPut() { |
590 |
< |
Thread t = new Thread(new Runnable() { |
591 |
< |
public void run() { |
592 |
< |
int added = 0; |
593 |
< |
try { |
594 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
595 |
< |
for (int i = 0; i < SIZE; ++i) { |
596 |
< |
q.put(new Integer(i)); |
597 |
< |
++added; |
598 |
< |
} |
599 |
< |
q.put(new Integer(SIZE)); |
600 |
< |
threadShouldThrow(); |
601 |
< |
} catch (InterruptedException ie){ |
602 |
< |
threadAssertEquals(added, SIZE); |
603 |
< |
} |
604 |
< |
}}); |
605 |
< |
t.start(); |
606 |
< |
try { |
607 |
< |
Thread.sleep(SHORT_DELAY_MS); |
608 |
< |
t.interrupt(); |
619 |
< |
t.join(); |
620 |
< |
} |
621 |
< |
catch (InterruptedException ie) { |
622 |
< |
unexpectedException(); |
623 |
< |
} |
589 |
> |
public void testBlockingPut() throws InterruptedException { |
590 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
591 |
> |
Thread t = new Thread(new CheckedRunnable() { |
592 |
> |
public void realRun() throws InterruptedException { |
593 |
> |
for (int i = 0; i < SIZE; ++i) |
594 |
> |
q.put(i); |
595 |
> |
assertEquals(SIZE, q.size()); |
596 |
> |
assertEquals(0, q.remainingCapacity()); |
597 |
> |
try { |
598 |
> |
q.put(99); |
599 |
> |
shouldThrow(); |
600 |
> |
} catch (InterruptedException success) {} |
601 |
> |
}}); |
602 |
> |
|
603 |
> |
t.start(); |
604 |
> |
Thread.sleep(SHORT_DELAY_MS); |
605 |
> |
t.interrupt(); |
606 |
> |
t.join(); |
607 |
> |
assertEquals(SIZE, q.size()); |
608 |
> |
assertEquals(0, q.remainingCapacity()); |
609 |
|
} |
610 |
|
|
611 |
|
/** |
612 |
|
* put blocks waiting for take when full |
613 |
|
*/ |
614 |
< |
public void testPutWithTake() { |
615 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
616 |
< |
Thread t = new Thread(new Runnable() { |
617 |
< |
public void run() { |
618 |
< |
int added = 0; |
619 |
< |
try { |
620 |
< |
q.put(new Object()); |
621 |
< |
++added; |
622 |
< |
q.put(new Object()); |
623 |
< |
++added; |
624 |
< |
q.put(new Object()); |
625 |
< |
++added; |
626 |
< |
q.put(new Object()); |
627 |
< |
++added; |
628 |
< |
threadShouldThrow(); |
629 |
< |
} catch (InterruptedException e){ |
630 |
< |
threadAssertTrue(added >= 2); |
631 |
< |
} |
632 |
< |
} |
633 |
< |
}); |
634 |
< |
try { |
650 |
< |
t.start(); |
651 |
< |
Thread.sleep(SHORT_DELAY_MS); |
652 |
< |
q.take(); |
653 |
< |
t.interrupt(); |
654 |
< |
t.join(); |
655 |
< |
} catch (Exception e){ |
656 |
< |
unexpectedException(); |
657 |
< |
} |
614 |
> |
public void testPutWithTake() throws InterruptedException { |
615 |
> |
final int capacity = 2; |
616 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
617 |
> |
Thread t = new Thread(new CheckedRunnable() { |
618 |
> |
public void realRun() throws InterruptedException { |
619 |
> |
for (int i = 0; i < capacity + 1; i++) |
620 |
> |
q.put(i); |
621 |
> |
try { |
622 |
> |
q.put(99); |
623 |
> |
shouldThrow(); |
624 |
> |
} catch (InterruptedException success) {} |
625 |
> |
}}); |
626 |
> |
|
627 |
> |
t.start(); |
628 |
> |
Thread.sleep(SHORT_DELAY_MS); |
629 |
> |
assertEquals(q.remainingCapacity(), 0); |
630 |
> |
assertEquals(0, q.take()); |
631 |
> |
Thread.sleep(SHORT_DELAY_MS); |
632 |
> |
t.interrupt(); |
633 |
> |
t.join(); |
634 |
> |
assertEquals(q.remainingCapacity(), 0); |
635 |
|
} |
636 |
|
|
637 |
|
/** |
638 |
|
* timed offer times out if full and elements not taken |
639 |
|
*/ |
640 |
< |
public void testTimedOffer() { |
640 |
> |
public void testTimedOffer() throws InterruptedException { |
641 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
642 |
< |
Thread t = new Thread(new Runnable() { |
643 |
< |
public void run() { |
644 |
< |
try { |
645 |
< |
q.put(new Object()); |
646 |
< |
q.put(new Object()); |
647 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
648 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
649 |
< |
threadShouldThrow(); |
650 |
< |
} catch (InterruptedException success){} |
651 |
< |
} |
675 |
< |
}); |
642 |
> |
Thread t = new Thread(new CheckedRunnable() { |
643 |
> |
public void realRun() throws InterruptedException { |
644 |
> |
q.put(new Object()); |
645 |
> |
q.put(new Object()); |
646 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
647 |
> |
try { |
648 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
649 |
> |
shouldThrow(); |
650 |
> |
} catch (InterruptedException success) {} |
651 |
> |
}}); |
652 |
|
|
653 |
< |
try { |
654 |
< |
t.start(); |
655 |
< |
Thread.sleep(SMALL_DELAY_MS); |
656 |
< |
t.interrupt(); |
681 |
< |
t.join(); |
682 |
< |
} catch (Exception e){ |
683 |
< |
unexpectedException(); |
684 |
< |
} |
653 |
> |
t.start(); |
654 |
> |
Thread.sleep(SMALL_DELAY_MS); |
655 |
> |
t.interrupt(); |
656 |
> |
t.join(); |
657 |
|
} |
658 |
|
|
659 |
|
/** |
660 |
|
* take retrieves elements in FIFO order |
661 |
|
*/ |
662 |
< |
public void testTake() { |
663 |
< |
try { |
664 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
665 |
< |
for (int i = 0; i < SIZE; ++i) { |
666 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
695 |
< |
} |
696 |
< |
} catch (InterruptedException e){ |
697 |
< |
unexpectedException(); |
698 |
< |
} |
662 |
> |
public void testTake() throws InterruptedException { |
663 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
664 |
> |
for (int i = 0; i < SIZE; ++i) { |
665 |
> |
assertEquals(i, q.take()); |
666 |
> |
} |
667 |
|
} |
668 |
|
|
669 |
|
/** |
670 |
|
* take blocks interruptibly when empty |
671 |
|
*/ |
672 |
< |
public void testTakeFromEmpty() { |
672 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
673 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
674 |
< |
Thread t = new Thread(new Runnable() { |
675 |
< |
public void run() { |
676 |
< |
try { |
677 |
< |
q.take(); |
678 |
< |
threadShouldThrow(); |
679 |
< |
} catch (InterruptedException success){ } |
680 |
< |
} |
681 |
< |
}); |
682 |
< |
try { |
715 |
< |
t.start(); |
716 |
< |
Thread.sleep(SHORT_DELAY_MS); |
717 |
< |
t.interrupt(); |
718 |
< |
t.join(); |
719 |
< |
} catch (Exception e){ |
720 |
< |
unexpectedException(); |
721 |
< |
} |
674 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
675 |
> |
public void realRun() throws InterruptedException { |
676 |
> |
q.take(); |
677 |
> |
}}; |
678 |
> |
|
679 |
> |
t.start(); |
680 |
> |
Thread.sleep(SHORT_DELAY_MS); |
681 |
> |
t.interrupt(); |
682 |
> |
t.join(); |
683 |
|
} |
684 |
|
|
685 |
|
/** |
686 |
|
* Take removes existing elements until empty, then blocks interruptibly |
687 |
|
*/ |
688 |
< |
public void testBlockingTake() { |
689 |
< |
Thread t = new Thread(new Runnable() { |
690 |
< |
public void run() { |
691 |
< |
try { |
692 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
693 |
< |
for (int i = 0; i < SIZE; ++i) { |
694 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
695 |
< |
} |
696 |
< |
q.take(); |
697 |
< |
threadShouldThrow(); |
698 |
< |
} catch (InterruptedException success){ |
699 |
< |
} |
700 |
< |
}}); |
701 |
< |
t.start(); |
702 |
< |
try { |
703 |
< |
Thread.sleep(SHORT_DELAY_MS); |
704 |
< |
t.interrupt(); |
744 |
< |
t.join(); |
745 |
< |
} |
746 |
< |
catch (InterruptedException ie) { |
747 |
< |
unexpectedException(); |
748 |
< |
} |
688 |
> |
public void testBlockingTake() throws InterruptedException { |
689 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
690 |
> |
Thread t = new Thread(new CheckedRunnable() { |
691 |
> |
public void realRun() throws InterruptedException { |
692 |
> |
for (int i = 0; i < SIZE; ++i) { |
693 |
> |
assertEquals(i, q.take()); |
694 |
> |
} |
695 |
> |
try { |
696 |
> |
q.take(); |
697 |
> |
shouldThrow(); |
698 |
> |
} catch (InterruptedException success) {} |
699 |
> |
}}); |
700 |
> |
|
701 |
> |
t.start(); |
702 |
> |
Thread.sleep(SHORT_DELAY_MS); |
703 |
> |
t.interrupt(); |
704 |
> |
t.join(); |
705 |
|
} |
706 |
|
|
707 |
|
|
711 |
|
public void testPoll() { |
712 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
713 |
|
for (int i = 0; i < SIZE; ++i) { |
714 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
714 |
> |
assertEquals(i, q.poll()); |
715 |
|
} |
716 |
< |
assertNull(q.poll()); |
716 |
> |
assertNull(q.poll()); |
717 |
|
} |
718 |
|
|
719 |
|
/** |
720 |
|
* timed poll with zero timeout succeeds when non-empty, else times out |
721 |
|
*/ |
722 |
< |
public void testTimedPoll0() { |
723 |
< |
try { |
724 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
725 |
< |
for (int i = 0; i < SIZE; ++i) { |
726 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
727 |
< |
} |
772 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
773 |
< |
} catch (InterruptedException e){ |
774 |
< |
unexpectedException(); |
775 |
< |
} |
722 |
> |
public void testTimedPoll0() throws InterruptedException { |
723 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
724 |
> |
for (int i = 0; i < SIZE; ++i) { |
725 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
726 |
> |
} |
727 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
728 |
|
} |
729 |
|
|
730 |
|
/** |
731 |
|
* timed poll with nonzero timeout succeeds when non-empty, else times out |
732 |
|
*/ |
733 |
< |
public void testTimedPoll() { |
734 |
< |
try { |
735 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
736 |
< |
for (int i = 0; i < SIZE; ++i) { |
737 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
738 |
< |
} |
787 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
788 |
< |
} catch (InterruptedException e){ |
789 |
< |
unexpectedException(); |
790 |
< |
} |
733 |
> |
public void testTimedPoll() throws InterruptedException { |
734 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
735 |
> |
for (int i = 0; i < SIZE; ++i) { |
736 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
737 |
> |
} |
738 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
739 |
|
} |
740 |
|
|
741 |
|
/** |
742 |
|
* Interrupted timed poll throws InterruptedException instead of |
743 |
|
* returning timeout status |
744 |
|
*/ |
745 |
< |
public void testInterruptedTimedPoll() { |
746 |
< |
Thread t = new Thread(new Runnable() { |
747 |
< |
public void run() { |
748 |
< |
try { |
749 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
750 |
< |
for (int i = 0; i < SIZE; ++i) { |
751 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
752 |
< |
} |
753 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
754 |
< |
} catch (InterruptedException success){ |
755 |
< |
} |
756 |
< |
}}); |
757 |
< |
t.start(); |
758 |
< |
try { |
759 |
< |
Thread.sleep(SHORT_DELAY_MS); |
760 |
< |
t.interrupt(); |
761 |
< |
t.join(); |
814 |
< |
} |
815 |
< |
catch (InterruptedException ie) { |
816 |
< |
unexpectedException(); |
817 |
< |
} |
745 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
746 |
> |
Thread t = new Thread(new CheckedRunnable() { |
747 |
> |
public void realRun() throws InterruptedException { |
748 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
749 |
> |
for (int i = 0; i < SIZE; ++i) { |
750 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
751 |
> |
} |
752 |
> |
try { |
753 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
754 |
> |
shouldThrow(); |
755 |
> |
} catch (InterruptedException success) {} |
756 |
> |
}}); |
757 |
> |
|
758 |
> |
t.start(); |
759 |
> |
Thread.sleep(SHORT_DELAY_MS); |
760 |
> |
t.interrupt(); |
761 |
> |
t.join(); |
762 |
|
} |
763 |
|
|
764 |
|
/** |
765 |
|
* timed poll before a delayed offer fails; after offer succeeds; |
766 |
|
* on interruption throws |
767 |
|
*/ |
768 |
< |
public void testTimedPollWithOffer() { |
768 |
> |
public void testTimedPollWithOffer() throws InterruptedException { |
769 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
770 |
< |
Thread t = new Thread(new Runnable() { |
771 |
< |
public void run() { |
772 |
< |
try { |
773 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
774 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
775 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
776 |
< |
threadShouldThrow(); |
777 |
< |
} catch (InterruptedException success) { } |
778 |
< |
} |
779 |
< |
}); |
780 |
< |
try { |
781 |
< |
t.start(); |
782 |
< |
Thread.sleep(SMALL_DELAY_MS); |
783 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
784 |
< |
t.interrupt(); |
841 |
< |
t.join(); |
842 |
< |
} catch (Exception e){ |
843 |
< |
unexpectedException(); |
844 |
< |
} |
770 |
> |
Thread t = new Thread(new CheckedRunnable() { |
771 |
> |
public void realRun() throws InterruptedException { |
772 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
773 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
774 |
> |
try { |
775 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
776 |
> |
shouldThrow(); |
777 |
> |
} catch (InterruptedException success) {} |
778 |
> |
}}); |
779 |
> |
|
780 |
> |
t.start(); |
781 |
> |
Thread.sleep(SMALL_DELAY_MS); |
782 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
783 |
> |
t.interrupt(); |
784 |
> |
t.join(); |
785 |
|
} |
786 |
|
|
787 |
|
|
788 |
|
/** |
789 |
|
* putFirst(null) throws NPE |
790 |
|
*/ |
791 |
< |
public void testPutFirstNull() { |
792 |
< |
try { |
791 |
> |
public void testPutFirstNull() throws InterruptedException { |
792 |
> |
try { |
793 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
794 |
|
q.putFirst(null); |
795 |
|
shouldThrow(); |
796 |
< |
} |
857 |
< |
catch (NullPointerException success){ |
858 |
< |
} |
859 |
< |
catch (InterruptedException ie) { |
860 |
< |
unexpectedException(); |
861 |
< |
} |
796 |
> |
} catch (NullPointerException success) {} |
797 |
|
} |
798 |
|
|
799 |
|
/** |
800 |
|
* all elements successfully putFirst are contained |
801 |
|
*/ |
802 |
< |
public void testPutFirst() { |
803 |
< |
try { |
804 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
805 |
< |
for (int i = 0; i < SIZE; ++i) { |
806 |
< |
Integer I = new Integer(i); |
807 |
< |
q.putFirst(I); |
873 |
< |
assertTrue(q.contains(I)); |
874 |
< |
} |
875 |
< |
assertEquals(0, q.remainingCapacity()); |
802 |
> |
public void testPutFirst() throws InterruptedException { |
803 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
804 |
> |
for (int i = 0; i < SIZE; ++i) { |
805 |
> |
Integer I = new Integer(i); |
806 |
> |
q.putFirst(I); |
807 |
> |
assertTrue(q.contains(I)); |
808 |
|
} |
809 |
< |
catch (InterruptedException ie) { |
878 |
< |
unexpectedException(); |
879 |
< |
} |
809 |
> |
assertEquals(0, q.remainingCapacity()); |
810 |
|
} |
811 |
|
|
812 |
|
/** |
813 |
|
* putFirst blocks interruptibly if full |
814 |
|
*/ |
815 |
< |
public void testBlockingPutFirst() { |
816 |
< |
Thread t = new Thread(new Runnable() { |
817 |
< |
public void run() { |
818 |
< |
int added = 0; |
819 |
< |
try { |
820 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
821 |
< |
for (int i = 0; i < SIZE; ++i) { |
822 |
< |
q.putFirst(new Integer(i)); |
823 |
< |
++added; |
824 |
< |
} |
825 |
< |
q.putFirst(new Integer(SIZE)); |
826 |
< |
threadShouldThrow(); |
827 |
< |
} catch (InterruptedException ie){ |
828 |
< |
threadAssertEquals(added, SIZE); |
829 |
< |
} |
830 |
< |
}}); |
831 |
< |
t.start(); |
832 |
< |
try { |
833 |
< |
Thread.sleep(SHORT_DELAY_MS); |
834 |
< |
t.interrupt(); |
905 |
< |
t.join(); |
906 |
< |
} |
907 |
< |
catch (InterruptedException ie) { |
908 |
< |
unexpectedException(); |
909 |
< |
} |
815 |
> |
public void testBlockingPutFirst() throws InterruptedException { |
816 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
817 |
> |
Thread t = new Thread(new CheckedRunnable() { |
818 |
> |
public void realRun() throws InterruptedException { |
819 |
> |
for (int i = 0; i < SIZE; ++i) |
820 |
> |
q.putFirst(i); |
821 |
> |
assertEquals(SIZE, q.size()); |
822 |
> |
assertEquals(0, q.remainingCapacity()); |
823 |
> |
try { |
824 |
> |
q.putFirst(99); |
825 |
> |
shouldThrow(); |
826 |
> |
} catch (InterruptedException success) {} |
827 |
> |
}}); |
828 |
> |
|
829 |
> |
t.start(); |
830 |
> |
Thread.sleep(SHORT_DELAY_MS); |
831 |
> |
t.interrupt(); |
832 |
> |
t.join(); |
833 |
> |
assertEquals(SIZE, q.size()); |
834 |
> |
assertEquals(0, q.remainingCapacity()); |
835 |
|
} |
836 |
|
|
837 |
|
/** |
838 |
|
* putFirst blocks waiting for take when full |
839 |
|
*/ |
840 |
< |
public void testPutFirstWithTake() { |
841 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
842 |
< |
Thread t = new Thread(new Runnable() { |
843 |
< |
public void run() { |
844 |
< |
int added = 0; |
845 |
< |
try { |
846 |
< |
q.putFirst(new Object()); |
847 |
< |
++added; |
848 |
< |
q.putFirst(new Object()); |
849 |
< |
++added; |
850 |
< |
q.putFirst(new Object()); |
851 |
< |
++added; |
852 |
< |
q.putFirst(new Object()); |
853 |
< |
++added; |
854 |
< |
threadShouldThrow(); |
855 |
< |
} catch (InterruptedException e){ |
856 |
< |
threadAssertTrue(added >= 2); |
857 |
< |
} |
858 |
< |
} |
859 |
< |
}); |
860 |
< |
try { |
936 |
< |
t.start(); |
937 |
< |
Thread.sleep(SHORT_DELAY_MS); |
938 |
< |
q.take(); |
939 |
< |
t.interrupt(); |
940 |
< |
t.join(); |
941 |
< |
} catch (Exception e){ |
942 |
< |
unexpectedException(); |
943 |
< |
} |
840 |
> |
public void testPutFirstWithTake() throws InterruptedException { |
841 |
> |
final int capacity = 2; |
842 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
843 |
> |
Thread t = new Thread(new CheckedRunnable() { |
844 |
> |
public void realRun() throws InterruptedException { |
845 |
> |
for (int i = 0; i < capacity + 1; i++) |
846 |
> |
q.putFirst(i); |
847 |
> |
try { |
848 |
> |
q.putFirst(99); |
849 |
> |
shouldThrow(); |
850 |
> |
} catch (InterruptedException success) {} |
851 |
> |
}}); |
852 |
> |
|
853 |
> |
t.start(); |
854 |
> |
Thread.sleep(SHORT_DELAY_MS); |
855 |
> |
assertEquals(q.remainingCapacity(), 0); |
856 |
> |
assertEquals(capacity - 1, q.take()); |
857 |
> |
Thread.sleep(SHORT_DELAY_MS); |
858 |
> |
t.interrupt(); |
859 |
> |
t.join(); |
860 |
> |
assertEquals(q.remainingCapacity(), 0); |
861 |
|
} |
862 |
|
|
863 |
|
/** |
864 |
|
* timed offerFirst times out if full and elements not taken |
865 |
|
*/ |
866 |
< |
public void testTimedOfferFirst() { |
866 |
> |
public void testTimedOfferFirst() throws InterruptedException { |
867 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
868 |
< |
Thread t = new Thread(new Runnable() { |
869 |
< |
public void run() { |
870 |
< |
try { |
871 |
< |
q.putFirst(new Object()); |
872 |
< |
q.putFirst(new Object()); |
873 |
< |
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
874 |
< |
q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
875 |
< |
threadShouldThrow(); |
876 |
< |
} catch (InterruptedException success){} |
877 |
< |
} |
961 |
< |
}); |
868 |
> |
Thread t = new Thread(new CheckedRunnable() { |
869 |
> |
public void realRun() throws InterruptedException { |
870 |
> |
q.putFirst(new Object()); |
871 |
> |
q.putFirst(new Object()); |
872 |
> |
assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
873 |
> |
try { |
874 |
> |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
875 |
> |
shouldThrow(); |
876 |
> |
} catch (InterruptedException success) {} |
877 |
> |
}}); |
878 |
|
|
879 |
< |
try { |
880 |
< |
t.start(); |
881 |
< |
Thread.sleep(SMALL_DELAY_MS); |
882 |
< |
t.interrupt(); |
967 |
< |
t.join(); |
968 |
< |
} catch (Exception e){ |
969 |
< |
unexpectedException(); |
970 |
< |
} |
879 |
> |
t.start(); |
880 |
> |
Thread.sleep(SMALL_DELAY_MS); |
881 |
> |
t.interrupt(); |
882 |
> |
t.join(); |
883 |
|
} |
884 |
|
|
885 |
|
/** |
886 |
|
* take retrieves elements in FIFO order |
887 |
|
*/ |
888 |
< |
public void testTakeFirst() { |
889 |
< |
try { |
890 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
891 |
< |
for (int i = 0; i < SIZE; ++i) { |
892 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
981 |
< |
} |
982 |
< |
} catch (InterruptedException e){ |
983 |
< |
unexpectedException(); |
984 |
< |
} |
888 |
> |
public void testTakeFirst() throws InterruptedException { |
889 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
890 |
> |
for (int i = 0; i < SIZE; ++i) { |
891 |
> |
assertEquals(i, q.takeFirst()); |
892 |
> |
} |
893 |
|
} |
894 |
|
|
895 |
|
/** |
896 |
|
* takeFirst blocks interruptibly when empty |
897 |
|
*/ |
898 |
< |
public void testTakeFirstFromEmpty() { |
898 |
> |
public void testTakeFirstFromEmpty() throws InterruptedException { |
899 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
900 |
< |
Thread t = new Thread(new Runnable() { |
901 |
< |
public void run() { |
902 |
< |
try { |
903 |
< |
q.takeFirst(); |
904 |
< |
threadShouldThrow(); |
905 |
< |
} catch (InterruptedException success){ } |
906 |
< |
} |
907 |
< |
}); |
908 |
< |
try { |
1001 |
< |
t.start(); |
1002 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1003 |
< |
t.interrupt(); |
1004 |
< |
t.join(); |
1005 |
< |
} catch (Exception e){ |
1006 |
< |
unexpectedException(); |
1007 |
< |
} |
900 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
901 |
> |
public void realRun() throws InterruptedException { |
902 |
> |
q.takeFirst(); |
903 |
> |
}}; |
904 |
> |
|
905 |
> |
t.start(); |
906 |
> |
Thread.sleep(SHORT_DELAY_MS); |
907 |
> |
t.interrupt(); |
908 |
> |
t.join(); |
909 |
|
} |
910 |
|
|
911 |
|
/** |
912 |
|
* TakeFirst removes existing elements until empty, then blocks interruptibly |
913 |
|
*/ |
914 |
< |
public void testBlockingTakeFirst() { |
915 |
< |
Thread t = new Thread(new Runnable() { |
916 |
< |
public void run() { |
917 |
< |
try { |
918 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
919 |
< |
for (int i = 0; i < SIZE; ++i) { |
920 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
921 |
< |
} |
922 |
< |
q.takeFirst(); |
923 |
< |
threadShouldThrow(); |
924 |
< |
} catch (InterruptedException success){ |
925 |
< |
} |
926 |
< |
}}); |
927 |
< |
t.start(); |
928 |
< |
try { |
929 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1029 |
< |
t.interrupt(); |
1030 |
< |
t.join(); |
1031 |
< |
} |
1032 |
< |
catch (InterruptedException ie) { |
1033 |
< |
unexpectedException(); |
1034 |
< |
} |
914 |
> |
public void testBlockingTakeFirst() throws InterruptedException { |
915 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
916 |
> |
Thread t = new Thread(new CheckedRunnable() { |
917 |
> |
public void realRun() throws InterruptedException { |
918 |
> |
for (int i = 0; i < SIZE; ++i) |
919 |
> |
assertEquals(i, q.takeFirst()); |
920 |
> |
try { |
921 |
> |
q.takeFirst(); |
922 |
> |
shouldThrow(); |
923 |
> |
} catch (InterruptedException success) {} |
924 |
> |
}}); |
925 |
> |
|
926 |
> |
t.start(); |
927 |
> |
Thread.sleep(SHORT_DELAY_MS); |
928 |
> |
t.interrupt(); |
929 |
> |
t.join(); |
930 |
|
} |
931 |
|
|
932 |
|
|
933 |
|
/** |
934 |
|
* timed pollFirst with zero timeout succeeds when non-empty, else times out |
935 |
|
*/ |
936 |
< |
public void testTimedPollFirst0() { |
937 |
< |
try { |
938 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
939 |
< |
for (int i = 0; i < SIZE; ++i) { |
940 |
< |
assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue()); |
941 |
< |
} |
1047 |
< |
assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS)); |
1048 |
< |
} catch (InterruptedException e){ |
1049 |
< |
unexpectedException(); |
1050 |
< |
} |
936 |
> |
public void testTimedPollFirst0() throws InterruptedException { |
937 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
938 |
> |
for (int i = 0; i < SIZE; ++i) { |
939 |
> |
assertEquals(i, q.pollFirst(0, MILLISECONDS)); |
940 |
> |
} |
941 |
> |
assertNull(q.pollFirst(0, MILLISECONDS)); |
942 |
|
} |
943 |
|
|
944 |
|
/** |
945 |
|
* timed pollFirst with nonzero timeout succeeds when non-empty, else times out |
946 |
|
*/ |
947 |
< |
public void testTimedPollFirst() { |
948 |
< |
try { |
949 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
950 |
< |
for (int i = 0; i < SIZE; ++i) { |
951 |
< |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
952 |
< |
} |
1062 |
< |
assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1063 |
< |
} catch (InterruptedException e){ |
1064 |
< |
unexpectedException(); |
1065 |
< |
} |
947 |
> |
public void testTimedPollFirst() throws InterruptedException { |
948 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
949 |
> |
for (int i = 0; i < SIZE; ++i) { |
950 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
951 |
> |
} |
952 |
> |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
953 |
|
} |
954 |
|
|
955 |
|
/** |
956 |
|
* Interrupted timed pollFirst throws InterruptedException instead of |
957 |
|
* returning timeout status |
958 |
|
*/ |
959 |
< |
public void testInterruptedTimedPollFirst() { |
960 |
< |
Thread t = new Thread(new Runnable() { |
961 |
< |
public void run() { |
962 |
< |
try { |
963 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
964 |
< |
for (int i = 0; i < SIZE; ++i) { |
965 |
< |
threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
966 |
< |
} |
967 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
968 |
< |
} catch (InterruptedException success){ |
969 |
< |
} |
970 |
< |
}}); |
971 |
< |
t.start(); |
972 |
< |
try { |
973 |
< |
Thread.sleep(SHORT_DELAY_MS); |
974 |
< |
t.interrupt(); |
975 |
< |
t.join(); |
1089 |
< |
} |
1090 |
< |
catch (InterruptedException ie) { |
1091 |
< |
unexpectedException(); |
1092 |
< |
} |
959 |
> |
public void testInterruptedTimedPollFirst() throws InterruptedException { |
960 |
> |
Thread t = new Thread(new CheckedRunnable() { |
961 |
> |
public void realRun() throws InterruptedException { |
962 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
963 |
> |
for (int i = 0; i < SIZE; ++i) { |
964 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
965 |
> |
} |
966 |
> |
try { |
967 |
> |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
968 |
> |
shouldThrow(); |
969 |
> |
} catch (InterruptedException success) {} |
970 |
> |
}}); |
971 |
> |
|
972 |
> |
t.start(); |
973 |
> |
Thread.sleep(SHORT_DELAY_MS); |
974 |
> |
t.interrupt(); |
975 |
> |
t.join(); |
976 |
|
} |
977 |
|
|
978 |
|
/** |
979 |
|
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
980 |
|
* on interruption throws |
981 |
|
*/ |
982 |
< |
public void testTimedPollFirstWithOfferFirst() { |
982 |
> |
public void testTimedPollFirstWithOfferFirst() throws InterruptedException { |
983 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
984 |
< |
Thread t = new Thread(new Runnable() { |
985 |
< |
public void run() { |
986 |
< |
try { |
987 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
988 |
< |
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
989 |
< |
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
990 |
< |
threadShouldThrow(); |
991 |
< |
} catch (InterruptedException success) { } |
992 |
< |
} |
993 |
< |
}); |
994 |
< |
try { |
995 |
< |
t.start(); |
996 |
< |
Thread.sleep(SMALL_DELAY_MS); |
997 |
< |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
998 |
< |
t.interrupt(); |
1116 |
< |
t.join(); |
1117 |
< |
} catch (Exception e){ |
1118 |
< |
unexpectedException(); |
1119 |
< |
} |
984 |
> |
Thread t = new Thread(new CheckedRunnable() { |
985 |
> |
public void realRun() throws InterruptedException { |
986 |
> |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
987 |
> |
assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
988 |
> |
try { |
989 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
990 |
> |
shouldThrow(); |
991 |
> |
} catch (InterruptedException success) {} |
992 |
> |
}}); |
993 |
> |
|
994 |
> |
t.start(); |
995 |
> |
Thread.sleep(SMALL_DELAY_MS); |
996 |
> |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS)); |
997 |
> |
t.interrupt(); |
998 |
> |
t.join(); |
999 |
|
} |
1000 |
|
|
1001 |
|
/** |
1002 |
|
* putLast(null) throws NPE |
1003 |
|
*/ |
1004 |
< |
public void testPutLastNull() { |
1005 |
< |
try { |
1004 |
> |
public void testPutLastNull() throws InterruptedException { |
1005 |
> |
try { |
1006 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1007 |
|
q.putLast(null); |
1008 |
|
shouldThrow(); |
1009 |
< |
} |
1131 |
< |
catch (NullPointerException success){ |
1132 |
< |
} |
1133 |
< |
catch (InterruptedException ie) { |
1134 |
< |
unexpectedException(); |
1135 |
< |
} |
1009 |
> |
} catch (NullPointerException success) {} |
1010 |
|
} |
1011 |
|
|
1012 |
|
/** |
1013 |
|
* all elements successfully putLast are contained |
1014 |
|
*/ |
1015 |
< |
public void testPutLast() { |
1016 |
< |
try { |
1017 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1018 |
< |
for (int i = 0; i < SIZE; ++i) { |
1019 |
< |
Integer I = new Integer(i); |
1020 |
< |
q.putLast(I); |
1147 |
< |
assertTrue(q.contains(I)); |
1148 |
< |
} |
1149 |
< |
assertEquals(0, q.remainingCapacity()); |
1015 |
> |
public void testPutLast() throws InterruptedException { |
1016 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1017 |
> |
for (int i = 0; i < SIZE; ++i) { |
1018 |
> |
Integer I = new Integer(i); |
1019 |
> |
q.putLast(I); |
1020 |
> |
assertTrue(q.contains(I)); |
1021 |
|
} |
1022 |
< |
catch (InterruptedException ie) { |
1152 |
< |
unexpectedException(); |
1153 |
< |
} |
1022 |
> |
assertEquals(0, q.remainingCapacity()); |
1023 |
|
} |
1024 |
|
|
1025 |
|
/** |
1026 |
|
* putLast blocks interruptibly if full |
1027 |
|
*/ |
1028 |
< |
public void testBlockingPutLast() { |
1029 |
< |
Thread t = new Thread(new Runnable() { |
1030 |
< |
public void run() { |
1031 |
< |
int added = 0; |
1032 |
< |
try { |
1033 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1034 |
< |
for (int i = 0; i < SIZE; ++i) { |
1035 |
< |
q.putLast(new Integer(i)); |
1036 |
< |
++added; |
1037 |
< |
} |
1038 |
< |
q.putLast(new Integer(SIZE)); |
1039 |
< |
threadShouldThrow(); |
1040 |
< |
} catch (InterruptedException ie){ |
1041 |
< |
threadAssertEquals(added, SIZE); |
1042 |
< |
} |
1043 |
< |
}}); |
1044 |
< |
t.start(); |
1045 |
< |
try { |
1046 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1047 |
< |
t.interrupt(); |
1179 |
< |
t.join(); |
1180 |
< |
} |
1181 |
< |
catch (InterruptedException ie) { |
1182 |
< |
unexpectedException(); |
1183 |
< |
} |
1028 |
> |
public void testBlockingPutLast() throws InterruptedException { |
1029 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1030 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1031 |
> |
public void realRun() throws InterruptedException { |
1032 |
> |
for (int i = 0; i < SIZE; ++i) |
1033 |
> |
q.putLast(i); |
1034 |
> |
assertEquals(SIZE, q.size()); |
1035 |
> |
assertEquals(0, q.remainingCapacity()); |
1036 |
> |
try { |
1037 |
> |
q.putLast(99); |
1038 |
> |
shouldThrow(); |
1039 |
> |
} catch (InterruptedException success) {} |
1040 |
> |
}}); |
1041 |
> |
|
1042 |
> |
t.start(); |
1043 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1044 |
> |
t.interrupt(); |
1045 |
> |
t.join(); |
1046 |
> |
assertEquals(SIZE, q.size()); |
1047 |
> |
assertEquals(0, q.remainingCapacity()); |
1048 |
|
} |
1049 |
|
|
1050 |
|
/** |
1051 |
|
* putLast blocks waiting for take when full |
1052 |
|
*/ |
1053 |
< |
public void testPutLastWithTake() { |
1054 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1055 |
< |
Thread t = new Thread(new Runnable() { |
1056 |
< |
public void run() { |
1057 |
< |
int added = 0; |
1058 |
< |
try { |
1059 |
< |
q.putLast(new Object()); |
1060 |
< |
++added; |
1061 |
< |
q.putLast(new Object()); |
1062 |
< |
++added; |
1063 |
< |
q.putLast(new Object()); |
1064 |
< |
++added; |
1065 |
< |
q.putLast(new Object()); |
1066 |
< |
++added; |
1067 |
< |
threadShouldThrow(); |
1068 |
< |
} catch (InterruptedException e){ |
1069 |
< |
threadAssertTrue(added >= 2); |
1070 |
< |
} |
1071 |
< |
} |
1072 |
< |
}); |
1073 |
< |
try { |
1210 |
< |
t.start(); |
1211 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1212 |
< |
q.take(); |
1213 |
< |
t.interrupt(); |
1214 |
< |
t.join(); |
1215 |
< |
} catch (Exception e){ |
1216 |
< |
unexpectedException(); |
1217 |
< |
} |
1053 |
> |
public void testPutLastWithTake() throws InterruptedException { |
1054 |
> |
final int capacity = 2; |
1055 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
1056 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1057 |
> |
public void realRun() throws InterruptedException { |
1058 |
> |
for (int i = 0; i < capacity + 1; i++) |
1059 |
> |
q.putLast(i); |
1060 |
> |
try { |
1061 |
> |
q.putLast(99); |
1062 |
> |
shouldThrow(); |
1063 |
> |
} catch (InterruptedException success) {} |
1064 |
> |
}}); |
1065 |
> |
|
1066 |
> |
t.start(); |
1067 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1068 |
> |
assertEquals(q.remainingCapacity(), 0); |
1069 |
> |
assertEquals(0, q.take()); |
1070 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1071 |
> |
t.interrupt(); |
1072 |
> |
t.join(); |
1073 |
> |
assertEquals(q.remainingCapacity(), 0); |
1074 |
|
} |
1075 |
|
|
1076 |
|
/** |
1077 |
|
* timed offerLast times out if full and elements not taken |
1078 |
|
*/ |
1079 |
< |
public void testTimedOfferLast() { |
1079 |
> |
public void testTimedOfferLast() throws InterruptedException { |
1080 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1081 |
< |
Thread t = new Thread(new Runnable() { |
1082 |
< |
public void run() { |
1083 |
< |
try { |
1084 |
< |
q.putLast(new Object()); |
1085 |
< |
q.putLast(new Object()); |
1086 |
< |
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1087 |
< |
q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1088 |
< |
threadShouldThrow(); |
1089 |
< |
} catch (InterruptedException success){} |
1090 |
< |
} |
1235 |
< |
}); |
1081 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1082 |
> |
public void realRun() throws InterruptedException { |
1083 |
> |
q.putLast(new Object()); |
1084 |
> |
q.putLast(new Object()); |
1085 |
> |
assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1086 |
> |
try { |
1087 |
> |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1088 |
> |
shouldThrow(); |
1089 |
> |
} catch (InterruptedException success) {} |
1090 |
> |
}}); |
1091 |
|
|
1092 |
< |
try { |
1093 |
< |
t.start(); |
1094 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1095 |
< |
t.interrupt(); |
1241 |
< |
t.join(); |
1242 |
< |
} catch (Exception e){ |
1243 |
< |
unexpectedException(); |
1244 |
< |
} |
1092 |
> |
t.start(); |
1093 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1094 |
> |
t.interrupt(); |
1095 |
> |
t.join(); |
1096 |
|
} |
1097 |
|
|
1098 |
|
/** |
1099 |
|
* takeLast retrieves elements in FIFO order |
1100 |
|
*/ |
1101 |
< |
public void testTakeLast() { |
1102 |
< |
try { |
1103 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1104 |
< |
for (int i = 0; i < SIZE; ++i) { |
1105 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1255 |
< |
} |
1256 |
< |
} catch (InterruptedException e){ |
1257 |
< |
unexpectedException(); |
1258 |
< |
} |
1101 |
> |
public void testTakeLast() throws InterruptedException { |
1102 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1103 |
> |
for (int i = 0; i < SIZE; ++i) { |
1104 |
> |
assertEquals(SIZE-i-1, q.takeLast()); |
1105 |
> |
} |
1106 |
|
} |
1107 |
|
|
1108 |
|
/** |
1109 |
|
* takeLast blocks interruptibly when empty |
1110 |
|
*/ |
1111 |
< |
public void testTakeLastFromEmpty() { |
1111 |
> |
public void testTakeLastFromEmpty() throws InterruptedException { |
1112 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1113 |
< |
Thread t = new Thread(new Runnable() { |
1114 |
< |
public void run() { |
1115 |
< |
try { |
1116 |
< |
q.takeLast(); |
1117 |
< |
threadShouldThrow(); |
1118 |
< |
} catch (InterruptedException success){ } |
1119 |
< |
} |
1120 |
< |
}); |
1121 |
< |
try { |
1275 |
< |
t.start(); |
1276 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1277 |
< |
t.interrupt(); |
1278 |
< |
t.join(); |
1279 |
< |
} catch (Exception e){ |
1280 |
< |
unexpectedException(); |
1281 |
< |
} |
1113 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1114 |
> |
public void realRun() throws InterruptedException { |
1115 |
> |
q.takeLast(); |
1116 |
> |
}}; |
1117 |
> |
|
1118 |
> |
t.start(); |
1119 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1120 |
> |
t.interrupt(); |
1121 |
> |
t.join(); |
1122 |
|
} |
1123 |
|
|
1124 |
|
/** |
1125 |
|
* TakeLast removes existing elements until empty, then blocks interruptibly |
1126 |
|
*/ |
1127 |
< |
public void testBlockingTakeLast() { |
1128 |
< |
Thread t = new Thread(new Runnable() { |
1129 |
< |
public void run() { |
1130 |
< |
try { |
1131 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1132 |
< |
for (int i = 0; i < SIZE; ++i) { |
1133 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1134 |
< |
} |
1135 |
< |
q.takeLast(); |
1136 |
< |
threadShouldThrow(); |
1137 |
< |
} catch (InterruptedException success){ |
1298 |
< |
} |
1299 |
< |
}}); |
1300 |
< |
t.start(); |
1301 |
< |
try { |
1302 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1303 |
< |
t.interrupt(); |
1304 |
< |
t.join(); |
1305 |
< |
} |
1306 |
< |
catch (InterruptedException ie) { |
1307 |
< |
unexpectedException(); |
1308 |
< |
} |
1309 |
< |
} |
1127 |
> |
public void testBlockingTakeLast() throws InterruptedException { |
1128 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1129 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1130 |
> |
public void realRun() throws InterruptedException { |
1131 |
> |
for (int i = 0; i < SIZE; ++i) |
1132 |
> |
assertEquals(SIZE - 1 - i, q.takeLast()); |
1133 |
> |
try { |
1134 |
> |
q.takeLast(); |
1135 |
> |
shouldThrow(); |
1136 |
> |
} catch (InterruptedException success) {} |
1137 |
> |
}}); |
1138 |
|
|
1139 |
+ |
t.start(); |
1140 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1141 |
+ |
t.interrupt(); |
1142 |
+ |
t.join(); |
1143 |
+ |
} |
1144 |
|
|
1145 |
|
/** |
1146 |
|
* timed pollLast with zero timeout succeeds when non-empty, else times out |
1147 |
|
*/ |
1148 |
< |
public void testTimedPollLast0() { |
1149 |
< |
try { |
1150 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1151 |
< |
for (int i = 0; i < SIZE; ++i) { |
1152 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue()); |
1153 |
< |
} |
1321 |
< |
assertNull(q.pollLast(0, TimeUnit.MILLISECONDS)); |
1322 |
< |
} catch (InterruptedException e){ |
1323 |
< |
unexpectedException(); |
1324 |
< |
} |
1148 |
> |
public void testTimedPollLast0() throws InterruptedException { |
1149 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1150 |
> |
for (int i = 0; i < SIZE; ++i) { |
1151 |
> |
assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
1152 |
> |
} |
1153 |
> |
assertNull(q.pollLast(0, MILLISECONDS)); |
1154 |
|
} |
1155 |
|
|
1156 |
|
/** |
1157 |
|
* timed pollLast with nonzero timeout succeeds when non-empty, else times out |
1158 |
|
*/ |
1159 |
< |
public void testTimedPollLast() { |
1160 |
< |
try { |
1161 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1162 |
< |
for (int i = 0; i < SIZE; ++i) { |
1163 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1164 |
< |
} |
1336 |
< |
assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1337 |
< |
} catch (InterruptedException e){ |
1338 |
< |
unexpectedException(); |
1339 |
< |
} |
1159 |
> |
public void testTimedPollLast() throws InterruptedException { |
1160 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1161 |
> |
for (int i = 0; i < SIZE; ++i) { |
1162 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1163 |
> |
} |
1164 |
> |
assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1165 |
|
} |
1166 |
|
|
1167 |
|
/** |
1168 |
|
* Interrupted timed pollLast throws InterruptedException instead of |
1169 |
|
* returning timeout status |
1170 |
|
*/ |
1171 |
< |
public void testInterruptedTimedPollLast() { |
1172 |
< |
Thread t = new Thread(new Runnable() { |
1173 |
< |
public void run() { |
1174 |
< |
try { |
1175 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1176 |
< |
for (int i = 0; i < SIZE; ++i) { |
1177 |
< |
threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1178 |
< |
} |
1179 |
< |
threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1180 |
< |
} catch (InterruptedException success){ |
1181 |
< |
} |
1182 |
< |
}}); |
1183 |
< |
t.start(); |
1184 |
< |
try { |
1185 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1186 |
< |
t.interrupt(); |
1187 |
< |
t.join(); |
1363 |
< |
} |
1364 |
< |
catch (InterruptedException ie) { |
1365 |
< |
unexpectedException(); |
1366 |
< |
} |
1171 |
> |
public void testInterruptedTimedPollLast() throws InterruptedException { |
1172 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1173 |
> |
public void realRun() throws InterruptedException { |
1174 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1175 |
> |
for (int i = 0; i < SIZE; ++i) { |
1176 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1177 |
> |
} |
1178 |
> |
try { |
1179 |
> |
q.pollLast(SMALL_DELAY_MS, MILLISECONDS); |
1180 |
> |
shouldThrow(); |
1181 |
> |
} catch (InterruptedException success) {} |
1182 |
> |
}}); |
1183 |
> |
|
1184 |
> |
t.start(); |
1185 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1186 |
> |
t.interrupt(); |
1187 |
> |
t.join(); |
1188 |
|
} |
1189 |
|
|
1190 |
|
/** |
1191 |
|
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1192 |
|
* on interruption throws |
1193 |
|
*/ |
1194 |
< |
public void testTimedPollWithOfferLast() { |
1194 |
> |
public void testTimedPollWithOfferLast() throws InterruptedException { |
1195 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1196 |
< |
Thread t = new Thread(new Runnable() { |
1197 |
< |
public void run() { |
1198 |
< |
try { |
1199 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1200 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1201 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1202 |
< |
threadShouldThrow(); |
1203 |
< |
} catch (InterruptedException success) { } |
1204 |
< |
} |
1205 |
< |
}); |
1206 |
< |
try { |
1207 |
< |
t.start(); |
1208 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1209 |
< |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1210 |
< |
t.interrupt(); |
1390 |
< |
t.join(); |
1391 |
< |
} catch (Exception e){ |
1392 |
< |
unexpectedException(); |
1393 |
< |
} |
1196 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1197 |
> |
public void realRun() throws InterruptedException { |
1198 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
1199 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1200 |
> |
try { |
1201 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1202 |
> |
shouldThrow(); |
1203 |
> |
} catch (InterruptedException success) {} |
1204 |
> |
}}); |
1205 |
> |
|
1206 |
> |
t.start(); |
1207 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1208 |
> |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1209 |
> |
t.interrupt(); |
1210 |
> |
t.join(); |
1211 |
|
} |
1212 |
|
|
1213 |
|
|
1217 |
|
public void testElement() { |
1218 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1219 |
|
for (int i = 0; i < SIZE; ++i) { |
1220 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
1220 |
> |
assertEquals(i, q.element()); |
1221 |
|
q.poll(); |
1222 |
|
} |
1223 |
|
try { |
1224 |
|
q.element(); |
1225 |
|
shouldThrow(); |
1226 |
< |
} |
1410 |
< |
catch (NoSuchElementException success) {} |
1226 |
> |
} catch (NoSuchElementException success) {} |
1227 |
|
} |
1228 |
|
|
1229 |
|
/** |
1321 |
|
/** |
1322 |
|
* toArray contains all elements |
1323 |
|
*/ |
1324 |
< |
public void testToArray() { |
1324 |
> |
public void testToArray() throws InterruptedException{ |
1325 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1326 |
< |
Object[] o = q.toArray(); |
1327 |
< |
try { |
1328 |
< |
for(int i = 0; i < o.length; i++) |
1513 |
< |
assertEquals(o[i], q.take()); |
1514 |
< |
} catch (InterruptedException e){ |
1515 |
< |
unexpectedException(); |
1516 |
< |
} |
1326 |
> |
Object[] o = q.toArray(); |
1327 |
> |
for (int i = 0; i < o.length; i++) |
1328 |
> |
assertEquals(o[i], q.take()); |
1329 |
|
} |
1330 |
|
|
1331 |
|
/** |
1332 |
|
* toArray(a) contains all elements |
1333 |
|
*/ |
1334 |
< |
public void testToArray2() { |
1334 |
> |
public void testToArray2() throws InterruptedException { |
1335 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1336 |
< |
Integer[] ints = new Integer[SIZE]; |
1337 |
< |
ints = (Integer[])q.toArray(ints); |
1338 |
< |
try { |
1339 |
< |
for(int i = 0; i < ints.length; i++) |
1528 |
< |
assertEquals(ints[i], q.take()); |
1529 |
< |
} catch (InterruptedException e){ |
1530 |
< |
unexpectedException(); |
1531 |
< |
} |
1336 |
> |
Integer[] ints = new Integer[SIZE]; |
1337 |
> |
ints = (Integer[])q.toArray(ints); |
1338 |
> |
for (int i = 0; i < ints.length; i++) |
1339 |
> |
assertEquals(ints[i], q.take()); |
1340 |
|
} |
1341 |
|
|
1342 |
|
/** |
1343 |
|
* toArray(null) throws NPE |
1344 |
|
*/ |
1345 |
|
public void testToArray_BadArg() { |
1346 |
< |
try { |
1347 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1348 |
< |
Object o[] = q.toArray(null); |
1349 |
< |
shouldThrow(); |
1350 |
< |
} catch(NullPointerException success){} |
1346 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1347 |
> |
try { |
1348 |
> |
Object o[] = q.toArray(null); |
1349 |
> |
shouldThrow(); |
1350 |
> |
} catch (NullPointerException success) {} |
1351 |
|
} |
1352 |
|
|
1353 |
|
/** |
1354 |
|
* toArray with incompatible array type throws CCE |
1355 |
|
*/ |
1356 |
|
public void testToArray1_BadArg() { |
1357 |
< |
try { |
1358 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1359 |
< |
Object o[] = q.toArray(new String[10] ); |
1360 |
< |
shouldThrow(); |
1361 |
< |
} catch(ArrayStoreException success){} |
1357 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1358 |
> |
try { |
1359 |
> |
Object o[] = q.toArray(new String[10]); |
1360 |
> |
shouldThrow(); |
1361 |
> |
} catch (ArrayStoreException success) {} |
1362 |
|
} |
1363 |
|
|
1364 |
|
|
1365 |
|
/** |
1366 |
|
* iterator iterates through all elements |
1367 |
|
*/ |
1368 |
< |
public void testIterator() { |
1368 |
> |
public void testIterator() throws InterruptedException { |
1369 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1370 |
< |
Iterator it = q.iterator(); |
1371 |
< |
try { |
1372 |
< |
while(it.hasNext()){ |
1373 |
< |
assertEquals(it.next(), q.take()); |
1566 |
< |
} |
1567 |
< |
} catch (InterruptedException e){ |
1568 |
< |
unexpectedException(); |
1569 |
< |
} |
1370 |
> |
Iterator it = q.iterator(); |
1371 |
> |
while (it.hasNext()) { |
1372 |
> |
assertEquals(it.next(), q.take()); |
1373 |
> |
} |
1374 |
|
} |
1375 |
|
|
1376 |
|
/** |
1377 |
|
* iterator.remove removes current element |
1378 |
|
*/ |
1379 |
< |
public void testIteratorRemove () { |
1379 |
> |
public void testIteratorRemove() { |
1380 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1381 |
|
q.add(two); |
1382 |
|
q.add(one); |
1387 |
|
it.remove(); |
1388 |
|
|
1389 |
|
it = q.iterator(); |
1390 |
< |
assertEquals(it.next(), one); |
1391 |
< |
assertEquals(it.next(), three); |
1390 |
> |
assertSame(it.next(), one); |
1391 |
> |
assertSame(it.next(), three); |
1392 |
|
assertFalse(it.hasNext()); |
1393 |
|
} |
1394 |
|
|
1404 |
|
assertEquals(0, q.remainingCapacity()); |
1405 |
|
int k = 0; |
1406 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
1407 |
< |
int i = ((Integer)(it.next())).intValue(); |
1604 |
< |
assertEquals(++k, i); |
1407 |
> |
assertEquals(++k, it.next()); |
1408 |
|
} |
1409 |
|
assertEquals(3, k); |
1410 |
|
} |
1412 |
|
/** |
1413 |
|
* Modifications do not cause iterators to fail |
1414 |
|
*/ |
1415 |
< |
public void testWeaklyConsistentIteration () { |
1415 |
> |
public void testWeaklyConsistentIteration() { |
1416 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1417 |
|
q.add(one); |
1418 |
|
q.add(two); |
1419 |
|
q.add(three); |
1420 |
< |
try { |
1421 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
1422 |
< |
q.remove(); |
1620 |
< |
it.next(); |
1621 |
< |
} |
1622 |
< |
} |
1623 |
< |
catch (ConcurrentModificationException e) { |
1624 |
< |
unexpectedException(); |
1420 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
1421 |
> |
q.remove(); |
1422 |
> |
it.next(); |
1423 |
|
} |
1424 |
|
assertEquals(0, q.size()); |
1425 |
|
} |
1431 |
|
public void testDescendingIterator() { |
1432 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1433 |
|
int i = 0; |
1434 |
< |
Iterator it = q.descendingIterator(); |
1435 |
< |
while(it.hasNext()) { |
1434 |
> |
Iterator it = q.descendingIterator(); |
1435 |
> |
while (it.hasNext()) { |
1436 |
|
assertTrue(q.contains(it.next())); |
1437 |
|
++i; |
1438 |
|
} |
1440 |
|
assertFalse(it.hasNext()); |
1441 |
|
try { |
1442 |
|
it.next(); |
1443 |
< |
} catch(NoSuchElementException success) { |
1444 |
< |
} |
1443 |
> |
shouldThrow(); |
1444 |
> |
} catch (NoSuchElementException success) {} |
1445 |
|
} |
1446 |
|
|
1447 |
|
/** |
1455 |
|
q.add(new Integer(1)); |
1456 |
|
int k = 0; |
1457 |
|
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
1458 |
< |
int i = ((Integer)(it.next())).intValue(); |
1661 |
< |
assertEquals(++k, i); |
1458 |
> |
assertEquals(++k, it.next()); |
1459 |
|
} |
1460 |
|
|
1461 |
|
assertEquals(3, k); |
1468 |
|
/** |
1469 |
|
* descendingIterator.remove removes current element |
1470 |
|
*/ |
1471 |
< |
public void testDescendingIteratorRemove () { |
1471 |
> |
public void testDescendingIteratorRemove() { |
1472 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1473 |
|
for (int iters = 0; iters < 100; ++iters) { |
1474 |
|
q.add(new Integer(3)); |
1508 |
|
q.add(one); |
1509 |
|
q.add(two); |
1510 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1511 |
< |
executor.execute(new Runnable() { |
1512 |
< |
public void run() { |
1513 |
< |
threadAssertFalse(q.offer(three)); |
1514 |
< |
try { |
1515 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
1516 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
1517 |
< |
} |
1518 |
< |
catch (InterruptedException e) { |
1519 |
< |
threadUnexpectedException(); |
1520 |
< |
} |
1521 |
< |
} |
1522 |
< |
}); |
1726 |
< |
|
1727 |
< |
executor.execute(new Runnable() { |
1728 |
< |
public void run() { |
1729 |
< |
try { |
1730 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1731 |
< |
threadAssertEquals(one, q.take()); |
1732 |
< |
} |
1733 |
< |
catch (InterruptedException e) { |
1734 |
< |
threadUnexpectedException(); |
1735 |
< |
} |
1736 |
< |
} |
1737 |
< |
}); |
1511 |
> |
executor.execute(new CheckedRunnable() { |
1512 |
> |
public void realRun() throws InterruptedException { |
1513 |
> |
assertFalse(q.offer(three)); |
1514 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1515 |
> |
assertEquals(0, q.remainingCapacity()); |
1516 |
> |
}}); |
1517 |
> |
|
1518 |
> |
executor.execute(new CheckedRunnable() { |
1519 |
> |
public void realRun() throws InterruptedException { |
1520 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1521 |
> |
assertSame(one, q.take()); |
1522 |
> |
}}); |
1523 |
|
|
1524 |
|
joinPool(executor); |
1525 |
|
} |
1530 |
|
public void testPollInExecutor() { |
1531 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1532 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1533 |
< |
executor.execute(new Runnable() { |
1534 |
< |
public void run() { |
1535 |
< |
threadAssertNull(q.poll()); |
1536 |
< |
try { |
1537 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
1538 |
< |
threadAssertTrue(q.isEmpty()); |
1539 |
< |
} |
1540 |
< |
catch (InterruptedException e) { |
1541 |
< |
threadUnexpectedException(); |
1542 |
< |
} |
1543 |
< |
} |
1544 |
< |
}); |
1760 |
< |
|
1761 |
< |
executor.execute(new Runnable() { |
1762 |
< |
public void run() { |
1763 |
< |
try { |
1764 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1765 |
< |
q.put(one); |
1766 |
< |
} |
1767 |
< |
catch (InterruptedException e) { |
1768 |
< |
threadUnexpectedException(); |
1769 |
< |
} |
1770 |
< |
} |
1771 |
< |
}); |
1533 |
> |
executor.execute(new CheckedRunnable() { |
1534 |
> |
public void realRun() throws InterruptedException { |
1535 |
> |
assertNull(q.poll()); |
1536 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1537 |
> |
assertTrue(q.isEmpty()); |
1538 |
> |
}}); |
1539 |
> |
|
1540 |
> |
executor.execute(new CheckedRunnable() { |
1541 |
> |
public void realRun() throws InterruptedException { |
1542 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1543 |
> |
q.put(one); |
1544 |
> |
}}); |
1545 |
|
|
1546 |
|
joinPool(executor); |
1547 |
|
} |
1549 |
|
/** |
1550 |
|
* A deserialized serialized deque has same elements in same order |
1551 |
|
*/ |
1552 |
< |
public void testSerialization() { |
1552 |
> |
public void testSerialization() throws Exception { |
1553 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1554 |
|
|
1555 |
< |
try { |
1556 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1557 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1558 |
< |
out.writeObject(q); |
1559 |
< |
out.close(); |
1560 |
< |
|
1561 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1562 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1563 |
< |
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1564 |
< |
assertEquals(q.size(), r.size()); |
1565 |
< |
while (!q.isEmpty()) |
1793 |
< |
assertEquals(q.remove(), r.remove()); |
1794 |
< |
} catch(Exception e){ |
1795 |
< |
unexpectedException(); |
1796 |
< |
} |
1555 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1556 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1557 |
> |
out.writeObject(q); |
1558 |
> |
out.close(); |
1559 |
> |
|
1560 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1561 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1562 |
> |
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1563 |
> |
assertEquals(q.size(), r.size()); |
1564 |
> |
while (!q.isEmpty()) |
1565 |
> |
assertEquals(q.remove(), r.remove()); |
1566 |
|
} |
1567 |
|
|
1568 |
|
/** |
1573 |
|
try { |
1574 |
|
q.drainTo(null); |
1575 |
|
shouldThrow(); |
1576 |
< |
} catch(NullPointerException success) { |
1808 |
< |
} |
1576 |
> |
} catch (NullPointerException success) {} |
1577 |
|
} |
1578 |
|
|
1579 |
|
/** |
1584 |
|
try { |
1585 |
|
q.drainTo(q); |
1586 |
|
shouldThrow(); |
1587 |
< |
} catch(IllegalArgumentException success) { |
1820 |
< |
} |
1587 |
> |
} catch (IllegalArgumentException success) {} |
1588 |
|
} |
1589 |
|
|
1590 |
|
/** |
1614 |
|
/** |
1615 |
|
* drainTo empties full deque, unblocking a waiting put. |
1616 |
|
*/ |
1617 |
< |
public void testDrainToWithActivePut() { |
1617 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
1618 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1619 |
< |
Thread t = new Thread(new Runnable() { |
1620 |
< |
public void run() { |
1621 |
< |
try { |
1622 |
< |
q.put(new Integer(SIZE+1)); |
1623 |
< |
} catch (InterruptedException ie){ |
1624 |
< |
threadUnexpectedException(); |
1625 |
< |
} |
1626 |
< |
} |
1627 |
< |
}); |
1628 |
< |
try { |
1629 |
< |
t.start(); |
1630 |
< |
ArrayList l = new ArrayList(); |
1631 |
< |
q.drainTo(l); |
1865 |
< |
assertTrue(l.size() >= SIZE); |
1866 |
< |
for (int i = 0; i < SIZE; ++i) |
1867 |
< |
assertEquals(l.get(i), new Integer(i)); |
1868 |
< |
t.join(); |
1869 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
1870 |
< |
} catch(Exception e){ |
1871 |
< |
unexpectedException(); |
1872 |
< |
} |
1619 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1620 |
> |
public void realRun() throws InterruptedException { |
1621 |
> |
q.put(new Integer(SIZE+1)); |
1622 |
> |
}}); |
1623 |
> |
|
1624 |
> |
t.start(); |
1625 |
> |
ArrayList l = new ArrayList(); |
1626 |
> |
q.drainTo(l); |
1627 |
> |
assertTrue(l.size() >= SIZE); |
1628 |
> |
for (int i = 0; i < SIZE; ++i) |
1629 |
> |
assertEquals(l.get(i), new Integer(i)); |
1630 |
> |
t.join(); |
1631 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
1632 |
|
} |
1633 |
|
|
1634 |
|
/** |
1639 |
|
try { |
1640 |
|
q.drainTo(null, 0); |
1641 |
|
shouldThrow(); |
1642 |
< |
} catch(NullPointerException success) { |
1884 |
< |
} |
1642 |
> |
} catch (NullPointerException success) {} |
1643 |
|
} |
1644 |
|
|
1645 |
|
/** |
1650 |
|
try { |
1651 |
|
q.drainTo(q, 0); |
1652 |
|
shouldThrow(); |
1653 |
< |
} catch(IllegalArgumentException success) { |
1896 |
< |
} |
1653 |
> |
} catch (IllegalArgumentException success) {} |
1654 |
|
} |
1655 |
|
|
1656 |
|
/** |
1659 |
|
public void testDrainToN() { |
1660 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1661 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
1662 |
< |
for(int j = 0; j < SIZE; j++) |
1662 |
> |
for (int j = 0; j < SIZE; j++) |
1663 |
|
assertTrue(q.offer(new Integer(j))); |
1664 |
|
ArrayList l = new ArrayList(); |
1665 |
|
q.drainTo(l, i); |