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 |
|
/** |
78 |
< |
* OfferFirst succeeds |
78 |
> |
* OfferFirst succeeds |
79 |
|
*/ |
80 |
|
public void testOfferFirst() { |
81 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
84 |
|
} |
85 |
|
|
86 |
|
/** |
87 |
< |
* OfferLast succeeds |
87 |
> |
* OfferLast succeeds |
88 |
|
*/ |
89 |
|
public void testOfferLast() { |
90 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
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 |
|
/** |
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 |
|
/** |
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 |
|
/** |
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()); |
273 |
< |
} |
271 |
> |
q.addFirst(four); |
272 |
> |
assertEquals(four,q.peekFirst()); |
273 |
> |
} |
274 |
|
|
275 |
|
/** |
276 |
|
* peekLast returns element inserted with addLast |
278 |
|
public void testAddLast() { |
279 |
|
LinkedBlockingDeque q = populatedDeque(3); |
280 |
|
q.pollLast(); |
281 |
< |
q.addLast(four); |
282 |
< |
assertEquals(four,q.peekLast()); |
283 |
< |
} |
281 |
> |
q.addLast(four); |
282 |
> |
assertEquals(four,q.peekLast()); |
283 |
> |
} |
284 |
|
|
285 |
|
|
286 |
|
/** |
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()); |
441 |
< |
} |
439 |
> |
q.push(four); |
440 |
> |
assertEquals(four,q.peekFirst()); |
441 |
> |
} |
442 |
|
|
443 |
|
|
444 |
|
/** |
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 |
|
* addAll of a collection with any null elements throws NPE after |
525 |
|
ints[i] = new Integer(i); |
526 |
|
q.addAll(Arrays.asList(ints)); |
527 |
|
shouldThrow(); |
528 |
< |
} |
527 |
< |
catch (NullPointerException success) {} |
528 |
> |
} catch (NullPointerException success) {} |
529 |
|
} |
530 |
|
/** |
531 |
|
* addAll throws ISE if not enough room |
538 |
|
ints[i] = new Integer(i); |
539 |
|
q.addAll(Arrays.asList(ints)); |
540 |
|
shouldThrow(); |
541 |
< |
} |
541 |
< |
catch (IllegalStateException success) {} |
541 |
> |
} catch (IllegalStateException success) {} |
542 |
|
} |
543 |
+ |
|
544 |
|
/** |
545 |
|
* Deque contains all elements, in traversal order, of successful addAll |
546 |
|
*/ |
547 |
|
public void testAddAll5() { |
548 |
< |
try { |
549 |
< |
Integer[] empty = new Integer[0]; |
550 |
< |
Integer[] ints = new Integer[SIZE]; |
551 |
< |
for (int i = 0; i < SIZE; ++i) |
552 |
< |
ints[i] = new Integer(i); |
553 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
554 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
555 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
556 |
< |
for (int i = 0; i < SIZE; ++i) |
556 |
< |
assertEquals(ints[i], q.poll()); |
557 |
< |
} |
558 |
< |
finally {} |
548 |
> |
Integer[] empty = new Integer[0]; |
549 |
> |
Integer[] ints = new Integer[SIZE]; |
550 |
> |
for (int i = 0; i < SIZE; ++i) |
551 |
> |
ints[i] = new Integer(i); |
552 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
553 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
554 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
555 |
> |
for (int i = 0; i < SIZE; ++i) |
556 |
> |
assertEquals(ints[i], q.poll()); |
557 |
|
} |
558 |
|
|
559 |
|
|
560 |
|
/** |
561 |
|
* put(null) throws NPE |
562 |
|
*/ |
563 |
< |
public void testPutNull() { |
564 |
< |
try { |
563 |
> |
public void testPutNull() throws InterruptedException { |
564 |
> |
try { |
565 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
566 |
|
q.put(null); |
567 |
|
shouldThrow(); |
568 |
< |
} |
569 |
< |
catch (NullPointerException success){ |
572 |
< |
} |
573 |
< |
catch (InterruptedException ie) { |
574 |
< |
unexpectedException(); |
575 |
< |
} |
576 |
< |
} |
568 |
> |
} catch (NullPointerException success) {} |
569 |
> |
} |
570 |
|
|
571 |
|
/** |
572 |
|
* all elements successfully put are contained |
573 |
|
*/ |
574 |
< |
public void testPut() { |
575 |
< |
try { |
576 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
577 |
< |
for (int i = 0; i < SIZE; ++i) { |
578 |
< |
Integer I = new Integer(i); |
579 |
< |
q.put(I); |
587 |
< |
assertTrue(q.contains(I)); |
588 |
< |
} |
589 |
< |
assertEquals(0, q.remainingCapacity()); |
590 |
< |
} |
591 |
< |
catch (InterruptedException ie) { |
592 |
< |
unexpectedException(); |
574 |
> |
public void testPut() throws InterruptedException { |
575 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
576 |
> |
for (int i = 0; i < SIZE; ++i) { |
577 |
> |
Integer I = new Integer(i); |
578 |
> |
q.put(I); |
579 |
> |
assertTrue(q.contains(I)); |
580 |
|
} |
581 |
+ |
assertEquals(0, q.remainingCapacity()); |
582 |
|
} |
583 |
|
|
584 |
|
/** |
585 |
|
* put blocks interruptibly if full |
586 |
|
*/ |
587 |
< |
public void testBlockingPut() { |
588 |
< |
Thread t = new Thread(new Runnable() { |
589 |
< |
public void run() { |
590 |
< |
int added = 0; |
591 |
< |
try { |
592 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
593 |
< |
for (int i = 0; i < SIZE; ++i) { |
594 |
< |
q.put(new Integer(i)); |
595 |
< |
++added; |
596 |
< |
} |
597 |
< |
q.put(new Integer(SIZE)); |
598 |
< |
threadShouldThrow(); |
599 |
< |
} catch (InterruptedException ie){ |
600 |
< |
threadAssertEquals(added, SIZE); |
601 |
< |
} |
602 |
< |
}}); |
603 |
< |
t.start(); |
604 |
< |
try { |
605 |
< |
Thread.sleep(SHORT_DELAY_MS); |
606 |
< |
t.interrupt(); |
619 |
< |
t.join(); |
620 |
< |
} |
621 |
< |
catch (InterruptedException ie) { |
622 |
< |
unexpectedException(); |
623 |
< |
} |
587 |
> |
public void testBlockingPut() throws InterruptedException { |
588 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
589 |
> |
Thread t = new Thread(new CheckedRunnable() { |
590 |
> |
public void realRun() throws InterruptedException { |
591 |
> |
for (int i = 0; i < SIZE; ++i) |
592 |
> |
q.put(i); |
593 |
> |
assertEquals(SIZE, q.size()); |
594 |
> |
assertEquals(0, q.remainingCapacity()); |
595 |
> |
try { |
596 |
> |
q.put(99); |
597 |
> |
shouldThrow(); |
598 |
> |
} catch (InterruptedException success) {} |
599 |
> |
}}); |
600 |
> |
|
601 |
> |
t.start(); |
602 |
> |
Thread.sleep(SHORT_DELAY_MS); |
603 |
> |
t.interrupt(); |
604 |
> |
t.join(); |
605 |
> |
assertEquals(SIZE, q.size()); |
606 |
> |
assertEquals(0, q.remainingCapacity()); |
607 |
|
} |
608 |
|
|
609 |
|
/** |
610 |
|
* put blocks waiting for take when full |
611 |
|
*/ |
612 |
< |
public void testPutWithTake() { |
613 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
614 |
< |
Thread t = new Thread(new Runnable() { |
615 |
< |
public void run() { |
616 |
< |
int added = 0; |
617 |
< |
try { |
618 |
< |
q.put(new Object()); |
619 |
< |
++added; |
620 |
< |
q.put(new Object()); |
621 |
< |
++added; |
622 |
< |
q.put(new Object()); |
623 |
< |
++added; |
624 |
< |
q.put(new Object()); |
625 |
< |
++added; |
626 |
< |
threadShouldThrow(); |
627 |
< |
} catch (InterruptedException e){ |
628 |
< |
threadAssertTrue(added >= 2); |
629 |
< |
} |
630 |
< |
} |
631 |
< |
}); |
632 |
< |
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 |
< |
} |
612 |
> |
public void testPutWithTake() throws InterruptedException { |
613 |
> |
final int capacity = 2; |
614 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
615 |
> |
Thread t = new Thread(new CheckedRunnable() { |
616 |
> |
public void realRun() throws InterruptedException { |
617 |
> |
for (int i = 0; i < capacity + 1; i++) |
618 |
> |
q.put(i); |
619 |
> |
try { |
620 |
> |
q.put(99); |
621 |
> |
shouldThrow(); |
622 |
> |
} catch (InterruptedException success) {} |
623 |
> |
}}); |
624 |
> |
|
625 |
> |
t.start(); |
626 |
> |
Thread.sleep(SHORT_DELAY_MS); |
627 |
> |
assertEquals(q.remainingCapacity(), 0); |
628 |
> |
assertEquals(0, q.take()); |
629 |
> |
Thread.sleep(SHORT_DELAY_MS); |
630 |
> |
t.interrupt(); |
631 |
> |
t.join(); |
632 |
> |
assertEquals(q.remainingCapacity(), 0); |
633 |
|
} |
634 |
|
|
635 |
|
/** |
636 |
|
* timed offer times out if full and elements not taken |
637 |
|
*/ |
638 |
< |
public void testTimedOffer() { |
638 |
> |
public void testTimedOffer() throws InterruptedException { |
639 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
640 |
< |
Thread t = new Thread(new Runnable() { |
641 |
< |
public void run() { |
642 |
< |
try { |
643 |
< |
q.put(new Object()); |
644 |
< |
q.put(new Object()); |
645 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
646 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
647 |
< |
threadShouldThrow(); |
648 |
< |
} catch (InterruptedException success){} |
649 |
< |
} |
650 |
< |
}); |
651 |
< |
|
652 |
< |
try { |
653 |
< |
t.start(); |
654 |
< |
Thread.sleep(SMALL_DELAY_MS); |
680 |
< |
t.interrupt(); |
681 |
< |
t.join(); |
682 |
< |
} catch (Exception e){ |
683 |
< |
unexpectedException(); |
684 |
< |
} |
640 |
> |
Thread t = new Thread(new CheckedRunnable() { |
641 |
> |
public void realRun() throws InterruptedException { |
642 |
> |
q.put(new Object()); |
643 |
> |
q.put(new Object()); |
644 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
645 |
> |
try { |
646 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
647 |
> |
shouldThrow(); |
648 |
> |
} catch (InterruptedException success) {} |
649 |
> |
}}); |
650 |
> |
|
651 |
> |
t.start(); |
652 |
> |
Thread.sleep(SMALL_DELAY_MS); |
653 |
> |
t.interrupt(); |
654 |
> |
t.join(); |
655 |
|
} |
656 |
|
|
657 |
|
/** |
658 |
|
* take retrieves elements in FIFO order |
659 |
|
*/ |
660 |
< |
public void testTake() { |
661 |
< |
try { |
662 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
663 |
< |
for (int i = 0; i < SIZE; ++i) { |
664 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
695 |
< |
} |
696 |
< |
} catch (InterruptedException e){ |
697 |
< |
unexpectedException(); |
698 |
< |
} |
660 |
> |
public void testTake() throws InterruptedException { |
661 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
662 |
> |
for (int i = 0; i < SIZE; ++i) { |
663 |
> |
assertEquals(i, q.take()); |
664 |
> |
} |
665 |
|
} |
666 |
|
|
667 |
|
/** |
668 |
|
* take blocks interruptibly when empty |
669 |
|
*/ |
670 |
< |
public void testTakeFromEmpty() { |
670 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
671 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
672 |
< |
Thread t = new Thread(new Runnable() { |
673 |
< |
public void run() { |
674 |
< |
try { |
675 |
< |
q.take(); |
676 |
< |
threadShouldThrow(); |
677 |
< |
} catch (InterruptedException success){ } |
678 |
< |
} |
679 |
< |
}); |
680 |
< |
try { |
715 |
< |
t.start(); |
716 |
< |
Thread.sleep(SHORT_DELAY_MS); |
717 |
< |
t.interrupt(); |
718 |
< |
t.join(); |
719 |
< |
} catch (Exception e){ |
720 |
< |
unexpectedException(); |
721 |
< |
} |
672 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
673 |
> |
public void realRun() throws InterruptedException { |
674 |
> |
q.take(); |
675 |
> |
}}; |
676 |
> |
|
677 |
> |
t.start(); |
678 |
> |
Thread.sleep(SHORT_DELAY_MS); |
679 |
> |
t.interrupt(); |
680 |
> |
t.join(); |
681 |
|
} |
682 |
|
|
683 |
|
/** |
684 |
|
* Take removes existing elements until empty, then blocks interruptibly |
685 |
|
*/ |
686 |
< |
public void testBlockingTake() { |
687 |
< |
Thread t = new Thread(new Runnable() { |
688 |
< |
public void run() { |
689 |
< |
try { |
690 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
691 |
< |
for (int i = 0; i < SIZE; ++i) { |
692 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
693 |
< |
} |
694 |
< |
q.take(); |
695 |
< |
threadShouldThrow(); |
696 |
< |
} catch (InterruptedException success){ |
697 |
< |
} |
698 |
< |
}}); |
699 |
< |
t.start(); |
700 |
< |
try { |
701 |
< |
Thread.sleep(SHORT_DELAY_MS); |
702 |
< |
t.interrupt(); |
744 |
< |
t.join(); |
745 |
< |
} |
746 |
< |
catch (InterruptedException ie) { |
747 |
< |
unexpectedException(); |
748 |
< |
} |
686 |
> |
public void testBlockingTake() throws InterruptedException { |
687 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
688 |
> |
Thread t = new Thread(new CheckedRunnable() { |
689 |
> |
public void realRun() throws InterruptedException { |
690 |
> |
for (int i = 0; i < SIZE; ++i) { |
691 |
> |
assertEquals(i, q.take()); |
692 |
> |
} |
693 |
> |
try { |
694 |
> |
q.take(); |
695 |
> |
shouldThrow(); |
696 |
> |
} catch (InterruptedException success) {} |
697 |
> |
}}); |
698 |
> |
|
699 |
> |
t.start(); |
700 |
> |
Thread.sleep(SHORT_DELAY_MS); |
701 |
> |
t.interrupt(); |
702 |
> |
t.join(); |
703 |
|
} |
704 |
|
|
705 |
|
|
709 |
|
public void testPoll() { |
710 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
711 |
|
for (int i = 0; i < SIZE; ++i) { |
712 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
712 |
> |
assertEquals(i, q.poll()); |
713 |
|
} |
714 |
< |
assertNull(q.poll()); |
714 |
> |
assertNull(q.poll()); |
715 |
|
} |
716 |
|
|
717 |
|
/** |
718 |
|
* timed poll with zero timeout succeeds when non-empty, else times out |
719 |
|
*/ |
720 |
< |
public void testTimedPoll0() { |
721 |
< |
try { |
722 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
723 |
< |
for (int i = 0; i < SIZE; ++i) { |
724 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
725 |
< |
} |
772 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
773 |
< |
} catch (InterruptedException e){ |
774 |
< |
unexpectedException(); |
775 |
< |
} |
720 |
> |
public void testTimedPoll0() throws InterruptedException { |
721 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
722 |
> |
for (int i = 0; i < SIZE; ++i) { |
723 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
724 |
> |
} |
725 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
726 |
|
} |
727 |
|
|
728 |
|
/** |
729 |
|
* timed poll with nonzero timeout succeeds when non-empty, else times out |
730 |
|
*/ |
731 |
< |
public void testTimedPoll() { |
732 |
< |
try { |
733 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
734 |
< |
for (int i = 0; i < SIZE; ++i) { |
735 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
736 |
< |
} |
787 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
788 |
< |
} catch (InterruptedException e){ |
789 |
< |
unexpectedException(); |
790 |
< |
} |
731 |
> |
public void testTimedPoll() throws InterruptedException { |
732 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
733 |
> |
for (int i = 0; i < SIZE; ++i) { |
734 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
735 |
> |
} |
736 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
737 |
|
} |
738 |
|
|
739 |
|
/** |
740 |
|
* Interrupted timed poll throws InterruptedException instead of |
741 |
|
* returning timeout status |
742 |
|
*/ |
743 |
< |
public void testInterruptedTimedPoll() { |
744 |
< |
Thread t = new Thread(new Runnable() { |
745 |
< |
public void run() { |
746 |
< |
try { |
747 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
748 |
< |
for (int i = 0; i < SIZE; ++i) { |
749 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
750 |
< |
} |
751 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
752 |
< |
} catch (InterruptedException success){ |
753 |
< |
} |
754 |
< |
}}); |
755 |
< |
t.start(); |
756 |
< |
try { |
757 |
< |
Thread.sleep(SHORT_DELAY_MS); |
758 |
< |
t.interrupt(); |
759 |
< |
t.join(); |
814 |
< |
} |
815 |
< |
catch (InterruptedException ie) { |
816 |
< |
unexpectedException(); |
817 |
< |
} |
743 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
744 |
> |
Thread t = new Thread(new CheckedRunnable() { |
745 |
> |
public void realRun() throws InterruptedException { |
746 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
747 |
> |
for (int i = 0; i < SIZE; ++i) { |
748 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
749 |
> |
} |
750 |
> |
try { |
751 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
752 |
> |
shouldThrow(); |
753 |
> |
} catch (InterruptedException success) {} |
754 |
> |
}}); |
755 |
> |
|
756 |
> |
t.start(); |
757 |
> |
Thread.sleep(SHORT_DELAY_MS); |
758 |
> |
t.interrupt(); |
759 |
> |
t.join(); |
760 |
|
} |
761 |
|
|
762 |
|
/** |
763 |
|
* timed poll before a delayed offer fails; after offer succeeds; |
764 |
|
* on interruption throws |
765 |
|
*/ |
766 |
< |
public void testTimedPollWithOffer() { |
766 |
> |
public void testTimedPollWithOffer() throws InterruptedException { |
767 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
768 |
< |
Thread t = new Thread(new Runnable() { |
769 |
< |
public void run() { |
770 |
< |
try { |
771 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
772 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
773 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
774 |
< |
threadShouldThrow(); |
775 |
< |
} catch (InterruptedException success) { } |
776 |
< |
} |
777 |
< |
}); |
778 |
< |
try { |
779 |
< |
t.start(); |
780 |
< |
Thread.sleep(SMALL_DELAY_MS); |
781 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
782 |
< |
t.interrupt(); |
783 |
< |
t.join(); |
842 |
< |
} catch (Exception e){ |
843 |
< |
unexpectedException(); |
844 |
< |
} |
845 |
< |
} |
768 |
> |
Thread t = new Thread(new CheckedRunnable() { |
769 |
> |
public void realRun() throws InterruptedException { |
770 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
771 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
772 |
> |
try { |
773 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
774 |
> |
shouldThrow(); |
775 |
> |
} catch (InterruptedException success) {} |
776 |
> |
}}); |
777 |
> |
|
778 |
> |
t.start(); |
779 |
> |
Thread.sleep(SMALL_DELAY_MS); |
780 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
781 |
> |
t.interrupt(); |
782 |
> |
t.join(); |
783 |
> |
} |
784 |
|
|
785 |
|
|
786 |
|
/** |
787 |
|
* putFirst(null) throws NPE |
788 |
|
*/ |
789 |
< |
public void testPutFirstNull() { |
790 |
< |
try { |
789 |
> |
public void testPutFirstNull() throws InterruptedException { |
790 |
> |
try { |
791 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
792 |
|
q.putFirst(null); |
793 |
|
shouldThrow(); |
794 |
< |
} |
857 |
< |
catch (NullPointerException success){ |
858 |
< |
} |
859 |
< |
catch (InterruptedException ie) { |
860 |
< |
unexpectedException(); |
861 |
< |
} |
794 |
> |
} catch (NullPointerException success) {} |
795 |
|
} |
796 |
|
|
797 |
|
/** |
798 |
|
* all elements successfully putFirst are contained |
799 |
|
*/ |
800 |
< |
public void testPutFirst() { |
801 |
< |
try { |
802 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
803 |
< |
for (int i = 0; i < SIZE; ++i) { |
804 |
< |
Integer I = new Integer(i); |
805 |
< |
q.putFirst(I); |
873 |
< |
assertTrue(q.contains(I)); |
874 |
< |
} |
875 |
< |
assertEquals(0, q.remainingCapacity()); |
800 |
> |
public void testPutFirst() throws InterruptedException { |
801 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
802 |
> |
for (int i = 0; i < SIZE; ++i) { |
803 |
> |
Integer I = new Integer(i); |
804 |
> |
q.putFirst(I); |
805 |
> |
assertTrue(q.contains(I)); |
806 |
|
} |
807 |
< |
catch (InterruptedException ie) { |
878 |
< |
unexpectedException(); |
879 |
< |
} |
807 |
> |
assertEquals(0, q.remainingCapacity()); |
808 |
|
} |
809 |
|
|
810 |
|
/** |
811 |
|
* putFirst blocks interruptibly if full |
812 |
|
*/ |
813 |
< |
public void testBlockingPutFirst() { |
814 |
< |
Thread t = new Thread(new Runnable() { |
815 |
< |
public void run() { |
816 |
< |
int added = 0; |
817 |
< |
try { |
818 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
819 |
< |
for (int i = 0; i < SIZE; ++i) { |
820 |
< |
q.putFirst(new Integer(i)); |
821 |
< |
++added; |
822 |
< |
} |
823 |
< |
q.putFirst(new Integer(SIZE)); |
824 |
< |
threadShouldThrow(); |
825 |
< |
} catch (InterruptedException ie){ |
826 |
< |
threadAssertEquals(added, SIZE); |
827 |
< |
} |
828 |
< |
}}); |
829 |
< |
t.start(); |
830 |
< |
try { |
831 |
< |
Thread.sleep(SHORT_DELAY_MS); |
832 |
< |
t.interrupt(); |
905 |
< |
t.join(); |
906 |
< |
} |
907 |
< |
catch (InterruptedException ie) { |
908 |
< |
unexpectedException(); |
909 |
< |
} |
813 |
> |
public void testBlockingPutFirst() throws InterruptedException { |
814 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
815 |
> |
Thread t = new Thread(new CheckedRunnable() { |
816 |
> |
public void realRun() throws InterruptedException { |
817 |
> |
for (int i = 0; i < SIZE; ++i) |
818 |
> |
q.putFirst(i); |
819 |
> |
assertEquals(SIZE, q.size()); |
820 |
> |
assertEquals(0, q.remainingCapacity()); |
821 |
> |
try { |
822 |
> |
q.putFirst(99); |
823 |
> |
shouldThrow(); |
824 |
> |
} catch (InterruptedException success) {} |
825 |
> |
}}); |
826 |
> |
|
827 |
> |
t.start(); |
828 |
> |
Thread.sleep(SHORT_DELAY_MS); |
829 |
> |
t.interrupt(); |
830 |
> |
t.join(); |
831 |
> |
assertEquals(SIZE, q.size()); |
832 |
> |
assertEquals(0, q.remainingCapacity()); |
833 |
|
} |
834 |
|
|
835 |
|
/** |
836 |
|
* putFirst blocks waiting for take when full |
837 |
|
*/ |
838 |
< |
public void testPutFirstWithTake() { |
839 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
840 |
< |
Thread t = new Thread(new Runnable() { |
841 |
< |
public void run() { |
842 |
< |
int added = 0; |
843 |
< |
try { |
844 |
< |
q.putFirst(new Object()); |
845 |
< |
++added; |
846 |
< |
q.putFirst(new Object()); |
847 |
< |
++added; |
848 |
< |
q.putFirst(new Object()); |
849 |
< |
++added; |
850 |
< |
q.putFirst(new Object()); |
851 |
< |
++added; |
852 |
< |
threadShouldThrow(); |
853 |
< |
} catch (InterruptedException e){ |
854 |
< |
threadAssertTrue(added >= 2); |
855 |
< |
} |
856 |
< |
} |
857 |
< |
}); |
858 |
< |
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 |
< |
} |
838 |
> |
public void testPutFirstWithTake() throws InterruptedException { |
839 |
> |
final int capacity = 2; |
840 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
841 |
> |
Thread t = new Thread(new CheckedRunnable() { |
842 |
> |
public void realRun() throws InterruptedException { |
843 |
> |
for (int i = 0; i < capacity + 1; i++) |
844 |
> |
q.putFirst(i); |
845 |
> |
try { |
846 |
> |
q.putFirst(99); |
847 |
> |
shouldThrow(); |
848 |
> |
} catch (InterruptedException success) {} |
849 |
> |
}}); |
850 |
> |
|
851 |
> |
t.start(); |
852 |
> |
Thread.sleep(SHORT_DELAY_MS); |
853 |
> |
assertEquals(q.remainingCapacity(), 0); |
854 |
> |
assertEquals(capacity - 1, q.take()); |
855 |
> |
Thread.sleep(SHORT_DELAY_MS); |
856 |
> |
t.interrupt(); |
857 |
> |
t.join(); |
858 |
> |
assertEquals(q.remainingCapacity(), 0); |
859 |
|
} |
860 |
|
|
861 |
|
/** |
862 |
|
* timed offerFirst times out if full and elements not taken |
863 |
|
*/ |
864 |
< |
public void testTimedOfferFirst() { |
864 |
> |
public void testTimedOfferFirst() throws InterruptedException { |
865 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
866 |
< |
Thread t = new Thread(new Runnable() { |
867 |
< |
public void run() { |
868 |
< |
try { |
869 |
< |
q.putFirst(new Object()); |
870 |
< |
q.putFirst(new Object()); |
871 |
< |
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
872 |
< |
q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
873 |
< |
threadShouldThrow(); |
874 |
< |
} catch (InterruptedException success){} |
875 |
< |
} |
876 |
< |
}); |
877 |
< |
|
878 |
< |
try { |
879 |
< |
t.start(); |
880 |
< |
Thread.sleep(SMALL_DELAY_MS); |
966 |
< |
t.interrupt(); |
967 |
< |
t.join(); |
968 |
< |
} catch (Exception e){ |
969 |
< |
unexpectedException(); |
970 |
< |
} |
866 |
> |
Thread t = new Thread(new CheckedRunnable() { |
867 |
> |
public void realRun() throws InterruptedException { |
868 |
> |
q.putFirst(new Object()); |
869 |
> |
q.putFirst(new Object()); |
870 |
> |
assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
871 |
> |
try { |
872 |
> |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
873 |
> |
shouldThrow(); |
874 |
> |
} catch (InterruptedException success) {} |
875 |
> |
}}); |
876 |
> |
|
877 |
> |
t.start(); |
878 |
> |
Thread.sleep(SMALL_DELAY_MS); |
879 |
> |
t.interrupt(); |
880 |
> |
t.join(); |
881 |
|
} |
882 |
|
|
883 |
|
/** |
884 |
|
* take retrieves elements in FIFO order |
885 |
|
*/ |
886 |
< |
public void testTakeFirst() { |
887 |
< |
try { |
888 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
889 |
< |
for (int i = 0; i < SIZE; ++i) { |
890 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
981 |
< |
} |
982 |
< |
} catch (InterruptedException e){ |
983 |
< |
unexpectedException(); |
984 |
< |
} |
886 |
> |
public void testTakeFirst() throws InterruptedException { |
887 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
888 |
> |
for (int i = 0; i < SIZE; ++i) { |
889 |
> |
assertEquals(i, q.takeFirst()); |
890 |
> |
} |
891 |
|
} |
892 |
|
|
893 |
|
/** |
894 |
|
* takeFirst blocks interruptibly when empty |
895 |
|
*/ |
896 |
< |
public void testTakeFirstFromEmpty() { |
896 |
> |
public void testTakeFirstFromEmpty() throws InterruptedException { |
897 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
898 |
< |
Thread t = new Thread(new Runnable() { |
899 |
< |
public void run() { |
900 |
< |
try { |
901 |
< |
q.takeFirst(); |
902 |
< |
threadShouldThrow(); |
903 |
< |
} catch (InterruptedException success){ } |
904 |
< |
} |
905 |
< |
}); |
906 |
< |
try { |
1001 |
< |
t.start(); |
1002 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1003 |
< |
t.interrupt(); |
1004 |
< |
t.join(); |
1005 |
< |
} catch (Exception e){ |
1006 |
< |
unexpectedException(); |
1007 |
< |
} |
898 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
899 |
> |
public void realRun() throws InterruptedException { |
900 |
> |
q.takeFirst(); |
901 |
> |
}}; |
902 |
> |
|
903 |
> |
t.start(); |
904 |
> |
Thread.sleep(SHORT_DELAY_MS); |
905 |
> |
t.interrupt(); |
906 |
> |
t.join(); |
907 |
|
} |
908 |
|
|
909 |
|
/** |
910 |
|
* TakeFirst removes existing elements until empty, then blocks interruptibly |
911 |
|
*/ |
912 |
< |
public void testBlockingTakeFirst() { |
913 |
< |
Thread t = new Thread(new Runnable() { |
914 |
< |
public void run() { |
915 |
< |
try { |
916 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
917 |
< |
for (int i = 0; i < SIZE; ++i) { |
918 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
919 |
< |
} |
920 |
< |
q.takeFirst(); |
921 |
< |
threadShouldThrow(); |
922 |
< |
} catch (InterruptedException success){ |
923 |
< |
} |
924 |
< |
}}); |
925 |
< |
t.start(); |
926 |
< |
try { |
927 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1029 |
< |
t.interrupt(); |
1030 |
< |
t.join(); |
1031 |
< |
} |
1032 |
< |
catch (InterruptedException ie) { |
1033 |
< |
unexpectedException(); |
1034 |
< |
} |
912 |
> |
public void testBlockingTakeFirst() throws InterruptedException { |
913 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
914 |
> |
Thread t = new Thread(new CheckedRunnable() { |
915 |
> |
public void realRun() throws InterruptedException { |
916 |
> |
for (int i = 0; i < SIZE; ++i) |
917 |
> |
assertEquals(i, q.takeFirst()); |
918 |
> |
try { |
919 |
> |
q.takeFirst(); |
920 |
> |
shouldThrow(); |
921 |
> |
} catch (InterruptedException success) {} |
922 |
> |
}}); |
923 |
> |
|
924 |
> |
t.start(); |
925 |
> |
Thread.sleep(SHORT_DELAY_MS); |
926 |
> |
t.interrupt(); |
927 |
> |
t.join(); |
928 |
|
} |
929 |
|
|
930 |
|
|
931 |
|
/** |
932 |
|
* timed pollFirst with zero timeout succeeds when non-empty, else times out |
933 |
|
*/ |
934 |
< |
public void testTimedPollFirst0() { |
935 |
< |
try { |
936 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
937 |
< |
for (int i = 0; i < SIZE; ++i) { |
938 |
< |
assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue()); |
939 |
< |
} |
1047 |
< |
assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS)); |
1048 |
< |
} catch (InterruptedException e){ |
1049 |
< |
unexpectedException(); |
1050 |
< |
} |
934 |
> |
public void testTimedPollFirst0() throws InterruptedException { |
935 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
936 |
> |
for (int i = 0; i < SIZE; ++i) { |
937 |
> |
assertEquals(i, q.pollFirst(0, MILLISECONDS)); |
938 |
> |
} |
939 |
> |
assertNull(q.pollFirst(0, MILLISECONDS)); |
940 |
|
} |
941 |
|
|
942 |
|
/** |
943 |
|
* timed pollFirst with nonzero timeout succeeds when non-empty, else times out |
944 |
|
*/ |
945 |
< |
public void testTimedPollFirst() { |
946 |
< |
try { |
947 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
948 |
< |
for (int i = 0; i < SIZE; ++i) { |
949 |
< |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
950 |
< |
} |
1062 |
< |
assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1063 |
< |
} catch (InterruptedException e){ |
1064 |
< |
unexpectedException(); |
1065 |
< |
} |
945 |
> |
public void testTimedPollFirst() throws InterruptedException { |
946 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
947 |
> |
for (int i = 0; i < SIZE; ++i) { |
948 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
949 |
> |
} |
950 |
> |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
951 |
|
} |
952 |
|
|
953 |
|
/** |
954 |
|
* Interrupted timed pollFirst throws InterruptedException instead of |
955 |
|
* returning timeout status |
956 |
|
*/ |
957 |
< |
public void testInterruptedTimedPollFirst() { |
958 |
< |
Thread t = new Thread(new Runnable() { |
959 |
< |
public void run() { |
960 |
< |
try { |
961 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
962 |
< |
for (int i = 0; i < SIZE; ++i) { |
963 |
< |
threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
964 |
< |
} |
965 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
966 |
< |
} catch (InterruptedException success){ |
967 |
< |
} |
968 |
< |
}}); |
969 |
< |
t.start(); |
970 |
< |
try { |
971 |
< |
Thread.sleep(SHORT_DELAY_MS); |
972 |
< |
t.interrupt(); |
973 |
< |
t.join(); |
1089 |
< |
} |
1090 |
< |
catch (InterruptedException ie) { |
1091 |
< |
unexpectedException(); |
1092 |
< |
} |
957 |
> |
public void testInterruptedTimedPollFirst() throws InterruptedException { |
958 |
> |
Thread t = new Thread(new CheckedRunnable() { |
959 |
> |
public void realRun() throws InterruptedException { |
960 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
961 |
> |
for (int i = 0; i < SIZE; ++i) { |
962 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
963 |
> |
} |
964 |
> |
try { |
965 |
> |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
966 |
> |
shouldThrow(); |
967 |
> |
} catch (InterruptedException success) {} |
968 |
> |
}}); |
969 |
> |
|
970 |
> |
t.start(); |
971 |
> |
Thread.sleep(SHORT_DELAY_MS); |
972 |
> |
t.interrupt(); |
973 |
> |
t.join(); |
974 |
|
} |
975 |
|
|
976 |
|
/** |
977 |
|
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
978 |
|
* on interruption throws |
979 |
|
*/ |
980 |
< |
public void testTimedPollFirstWithOfferFirst() { |
980 |
> |
public void testTimedPollFirstWithOfferFirst() throws InterruptedException { |
981 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
982 |
< |
Thread t = new Thread(new Runnable() { |
983 |
< |
public void run() { |
984 |
< |
try { |
985 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
986 |
< |
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
987 |
< |
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
988 |
< |
threadShouldThrow(); |
989 |
< |
} catch (InterruptedException success) { } |
990 |
< |
} |
991 |
< |
}); |
992 |
< |
try { |
993 |
< |
t.start(); |
994 |
< |
Thread.sleep(SMALL_DELAY_MS); |
995 |
< |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
996 |
< |
t.interrupt(); |
997 |
< |
t.join(); |
1117 |
< |
} catch (Exception e){ |
1118 |
< |
unexpectedException(); |
1119 |
< |
} |
1120 |
< |
} |
982 |
> |
Thread t = new Thread(new CheckedRunnable() { |
983 |
> |
public void realRun() throws InterruptedException { |
984 |
> |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
985 |
> |
assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
986 |
> |
try { |
987 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
988 |
> |
shouldThrow(); |
989 |
> |
} catch (InterruptedException success) {} |
990 |
> |
}}); |
991 |
> |
|
992 |
> |
t.start(); |
993 |
> |
Thread.sleep(SMALL_DELAY_MS); |
994 |
> |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS)); |
995 |
> |
t.interrupt(); |
996 |
> |
t.join(); |
997 |
> |
} |
998 |
|
|
999 |
|
/** |
1000 |
|
* putLast(null) throws NPE |
1001 |
|
*/ |
1002 |
< |
public void testPutLastNull() { |
1003 |
< |
try { |
1002 |
> |
public void testPutLastNull() throws InterruptedException { |
1003 |
> |
try { |
1004 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1005 |
|
q.putLast(null); |
1006 |
|
shouldThrow(); |
1007 |
< |
} |
1131 |
< |
catch (NullPointerException success){ |
1132 |
< |
} |
1133 |
< |
catch (InterruptedException ie) { |
1134 |
< |
unexpectedException(); |
1135 |
< |
} |
1007 |
> |
} catch (NullPointerException success) {} |
1008 |
|
} |
1009 |
|
|
1010 |
|
/** |
1011 |
|
* all elements successfully putLast are contained |
1012 |
|
*/ |
1013 |
< |
public void testPutLast() { |
1014 |
< |
try { |
1015 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1016 |
< |
for (int i = 0; i < SIZE; ++i) { |
1017 |
< |
Integer I = new Integer(i); |
1018 |
< |
q.putLast(I); |
1147 |
< |
assertTrue(q.contains(I)); |
1148 |
< |
} |
1149 |
< |
assertEquals(0, q.remainingCapacity()); |
1013 |
> |
public void testPutLast() throws InterruptedException { |
1014 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1015 |
> |
for (int i = 0; i < SIZE; ++i) { |
1016 |
> |
Integer I = new Integer(i); |
1017 |
> |
q.putLast(I); |
1018 |
> |
assertTrue(q.contains(I)); |
1019 |
|
} |
1020 |
< |
catch (InterruptedException ie) { |
1152 |
< |
unexpectedException(); |
1153 |
< |
} |
1020 |
> |
assertEquals(0, q.remainingCapacity()); |
1021 |
|
} |
1022 |
|
|
1023 |
|
/** |
1024 |
|
* putLast blocks interruptibly if full |
1025 |
|
*/ |
1026 |
< |
public void testBlockingPutLast() { |
1027 |
< |
Thread t = new Thread(new Runnable() { |
1028 |
< |
public void run() { |
1029 |
< |
int added = 0; |
1030 |
< |
try { |
1031 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1032 |
< |
for (int i = 0; i < SIZE; ++i) { |
1033 |
< |
q.putLast(new Integer(i)); |
1034 |
< |
++added; |
1035 |
< |
} |
1036 |
< |
q.putLast(new Integer(SIZE)); |
1037 |
< |
threadShouldThrow(); |
1038 |
< |
} catch (InterruptedException ie){ |
1039 |
< |
threadAssertEquals(added, SIZE); |
1040 |
< |
} |
1041 |
< |
}}); |
1042 |
< |
t.start(); |
1043 |
< |
try { |
1044 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1045 |
< |
t.interrupt(); |
1179 |
< |
t.join(); |
1180 |
< |
} |
1181 |
< |
catch (InterruptedException ie) { |
1182 |
< |
unexpectedException(); |
1183 |
< |
} |
1026 |
> |
public void testBlockingPutLast() throws InterruptedException { |
1027 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1028 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1029 |
> |
public void realRun() throws InterruptedException { |
1030 |
> |
for (int i = 0; i < SIZE; ++i) |
1031 |
> |
q.putLast(i); |
1032 |
> |
assertEquals(SIZE, q.size()); |
1033 |
> |
assertEquals(0, q.remainingCapacity()); |
1034 |
> |
try { |
1035 |
> |
q.putLast(99); |
1036 |
> |
shouldThrow(); |
1037 |
> |
} catch (InterruptedException success) {} |
1038 |
> |
}}); |
1039 |
> |
|
1040 |
> |
t.start(); |
1041 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1042 |
> |
t.interrupt(); |
1043 |
> |
t.join(); |
1044 |
> |
assertEquals(SIZE, q.size()); |
1045 |
> |
assertEquals(0, q.remainingCapacity()); |
1046 |
|
} |
1047 |
|
|
1048 |
|
/** |
1049 |
|
* putLast blocks waiting for take when full |
1050 |
|
*/ |
1051 |
< |
public void testPutLastWithTake() { |
1052 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1053 |
< |
Thread t = new Thread(new Runnable() { |
1054 |
< |
public void run() { |
1055 |
< |
int added = 0; |
1056 |
< |
try { |
1057 |
< |
q.putLast(new Object()); |
1058 |
< |
++added; |
1059 |
< |
q.putLast(new Object()); |
1060 |
< |
++added; |
1061 |
< |
q.putLast(new Object()); |
1062 |
< |
++added; |
1063 |
< |
q.putLast(new Object()); |
1064 |
< |
++added; |
1065 |
< |
threadShouldThrow(); |
1066 |
< |
} catch (InterruptedException e){ |
1067 |
< |
threadAssertTrue(added >= 2); |
1068 |
< |
} |
1069 |
< |
} |
1070 |
< |
}); |
1071 |
< |
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 |
< |
} |
1051 |
> |
public void testPutLastWithTake() throws InterruptedException { |
1052 |
> |
final int capacity = 2; |
1053 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
1054 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1055 |
> |
public void realRun() throws InterruptedException { |
1056 |
> |
for (int i = 0; i < capacity + 1; i++) |
1057 |
> |
q.putLast(i); |
1058 |
> |
try { |
1059 |
> |
q.putLast(99); |
1060 |
> |
shouldThrow(); |
1061 |
> |
} catch (InterruptedException success) {} |
1062 |
> |
}}); |
1063 |
> |
|
1064 |
> |
t.start(); |
1065 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1066 |
> |
assertEquals(q.remainingCapacity(), 0); |
1067 |
> |
assertEquals(0, q.take()); |
1068 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1069 |
> |
t.interrupt(); |
1070 |
> |
t.join(); |
1071 |
> |
assertEquals(q.remainingCapacity(), 0); |
1072 |
|
} |
1073 |
|
|
1074 |
|
/** |
1075 |
|
* timed offerLast times out if full and elements not taken |
1076 |
|
*/ |
1077 |
< |
public void testTimedOfferLast() { |
1077 |
> |
public void testTimedOfferLast() throws InterruptedException { |
1078 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1079 |
< |
Thread t = new Thread(new Runnable() { |
1080 |
< |
public void run() { |
1081 |
< |
try { |
1082 |
< |
q.putLast(new Object()); |
1083 |
< |
q.putLast(new Object()); |
1084 |
< |
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1085 |
< |
q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1086 |
< |
threadShouldThrow(); |
1087 |
< |
} catch (InterruptedException success){} |
1088 |
< |
} |
1089 |
< |
}); |
1090 |
< |
|
1091 |
< |
try { |
1092 |
< |
t.start(); |
1093 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1240 |
< |
t.interrupt(); |
1241 |
< |
t.join(); |
1242 |
< |
} catch (Exception e){ |
1243 |
< |
unexpectedException(); |
1244 |
< |
} |
1079 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1080 |
> |
public void realRun() throws InterruptedException { |
1081 |
> |
q.putLast(new Object()); |
1082 |
> |
q.putLast(new Object()); |
1083 |
> |
assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1084 |
> |
try { |
1085 |
> |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1086 |
> |
shouldThrow(); |
1087 |
> |
} catch (InterruptedException success) {} |
1088 |
> |
}}); |
1089 |
> |
|
1090 |
> |
t.start(); |
1091 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1092 |
> |
t.interrupt(); |
1093 |
> |
t.join(); |
1094 |
|
} |
1095 |
|
|
1096 |
|
/** |
1097 |
|
* takeLast retrieves elements in FIFO order |
1098 |
|
*/ |
1099 |
< |
public void testTakeLast() { |
1100 |
< |
try { |
1101 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1102 |
< |
for (int i = 0; i < SIZE; ++i) { |
1103 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1255 |
< |
} |
1256 |
< |
} catch (InterruptedException e){ |
1257 |
< |
unexpectedException(); |
1258 |
< |
} |
1099 |
> |
public void testTakeLast() throws InterruptedException { |
1100 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1101 |
> |
for (int i = 0; i < SIZE; ++i) { |
1102 |
> |
assertEquals(SIZE-i-1, q.takeLast()); |
1103 |
> |
} |
1104 |
|
} |
1105 |
|
|
1106 |
|
/** |
1107 |
|
* takeLast blocks interruptibly when empty |
1108 |
|
*/ |
1109 |
< |
public void testTakeLastFromEmpty() { |
1109 |
> |
public void testTakeLastFromEmpty() throws InterruptedException { |
1110 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1111 |
< |
Thread t = new Thread(new Runnable() { |
1112 |
< |
public void run() { |
1113 |
< |
try { |
1114 |
< |
q.takeLast(); |
1115 |
< |
threadShouldThrow(); |
1116 |
< |
} catch (InterruptedException success){ } |
1117 |
< |
} |
1118 |
< |
}); |
1119 |
< |
try { |
1275 |
< |
t.start(); |
1276 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1277 |
< |
t.interrupt(); |
1278 |
< |
t.join(); |
1279 |
< |
} catch (Exception e){ |
1280 |
< |
unexpectedException(); |
1281 |
< |
} |
1111 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1112 |
> |
public void realRun() throws InterruptedException { |
1113 |
> |
q.takeLast(); |
1114 |
> |
}}; |
1115 |
> |
|
1116 |
> |
t.start(); |
1117 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1118 |
> |
t.interrupt(); |
1119 |
> |
t.join(); |
1120 |
|
} |
1121 |
|
|
1122 |
|
/** |
1123 |
|
* TakeLast removes existing elements until empty, then blocks interruptibly |
1124 |
|
*/ |
1125 |
< |
public void testBlockingTakeLast() { |
1126 |
< |
Thread t = new Thread(new Runnable() { |
1127 |
< |
public void run() { |
1128 |
< |
try { |
1129 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1130 |
< |
for (int i = 0; i < SIZE; ++i) { |
1131 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1132 |
< |
} |
1133 |
< |
q.takeLast(); |
1134 |
< |
threadShouldThrow(); |
1135 |
< |
} 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 |
< |
} |
1125 |
> |
public void testBlockingTakeLast() throws InterruptedException { |
1126 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1127 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1128 |
> |
public void realRun() throws InterruptedException { |
1129 |
> |
for (int i = 0; i < SIZE; ++i) |
1130 |
> |
assertEquals(SIZE - 1 - i, q.takeLast()); |
1131 |
> |
try { |
1132 |
> |
q.takeLast(); |
1133 |
> |
shouldThrow(); |
1134 |
> |
} catch (InterruptedException success) {} |
1135 |
> |
}}); |
1136 |
|
|
1137 |
+ |
t.start(); |
1138 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1139 |
+ |
t.interrupt(); |
1140 |
+ |
t.join(); |
1141 |
+ |
} |
1142 |
|
|
1143 |
|
/** |
1144 |
|
* timed pollLast with zero timeout succeeds when non-empty, else times out |
1145 |
|
*/ |
1146 |
< |
public void testTimedPollLast0() { |
1147 |
< |
try { |
1148 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1149 |
< |
for (int i = 0; i < SIZE; ++i) { |
1150 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue()); |
1151 |
< |
} |
1321 |
< |
assertNull(q.pollLast(0, TimeUnit.MILLISECONDS)); |
1322 |
< |
} catch (InterruptedException e){ |
1323 |
< |
unexpectedException(); |
1324 |
< |
} |
1146 |
> |
public void testTimedPollLast0() throws InterruptedException { |
1147 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1148 |
> |
for (int i = 0; i < SIZE; ++i) { |
1149 |
> |
assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
1150 |
> |
} |
1151 |
> |
assertNull(q.pollLast(0, MILLISECONDS)); |
1152 |
|
} |
1153 |
|
|
1154 |
|
/** |
1155 |
|
* timed pollLast with nonzero timeout succeeds when non-empty, else times out |
1156 |
|
*/ |
1157 |
< |
public void testTimedPollLast() { |
1158 |
< |
try { |
1159 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1160 |
< |
for (int i = 0; i < SIZE; ++i) { |
1161 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1162 |
< |
} |
1336 |
< |
assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1337 |
< |
} catch (InterruptedException e){ |
1338 |
< |
unexpectedException(); |
1339 |
< |
} |
1157 |
> |
public void testTimedPollLast() throws InterruptedException { |
1158 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1159 |
> |
for (int i = 0; i < SIZE; ++i) { |
1160 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1161 |
> |
} |
1162 |
> |
assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1163 |
|
} |
1164 |
|
|
1165 |
|
/** |
1166 |
|
* Interrupted timed pollLast throws InterruptedException instead of |
1167 |
|
* returning timeout status |
1168 |
|
*/ |
1169 |
< |
public void testInterruptedTimedPollLast() { |
1170 |
< |
Thread t = new Thread(new Runnable() { |
1171 |
< |
public void run() { |
1172 |
< |
try { |
1173 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1174 |
< |
for (int i = 0; i < SIZE; ++i) { |
1175 |
< |
threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1176 |
< |
} |
1177 |
< |
threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1178 |
< |
} catch (InterruptedException success){ |
1179 |
< |
} |
1180 |
< |
}}); |
1181 |
< |
t.start(); |
1182 |
< |
try { |
1183 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1184 |
< |
t.interrupt(); |
1185 |
< |
t.join(); |
1363 |
< |
} |
1364 |
< |
catch (InterruptedException ie) { |
1365 |
< |
unexpectedException(); |
1366 |
< |
} |
1169 |
> |
public void testInterruptedTimedPollLast() throws InterruptedException { |
1170 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1171 |
> |
public void realRun() throws InterruptedException { |
1172 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1173 |
> |
for (int i = 0; i < SIZE; ++i) { |
1174 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1175 |
> |
} |
1176 |
> |
try { |
1177 |
> |
q.pollLast(SMALL_DELAY_MS, MILLISECONDS); |
1178 |
> |
shouldThrow(); |
1179 |
> |
} catch (InterruptedException success) {} |
1180 |
> |
}}); |
1181 |
> |
|
1182 |
> |
t.start(); |
1183 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1184 |
> |
t.interrupt(); |
1185 |
> |
t.join(); |
1186 |
|
} |
1187 |
|
|
1188 |
|
/** |
1189 |
|
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1190 |
|
* on interruption throws |
1191 |
|
*/ |
1192 |
< |
public void testTimedPollWithOfferLast() { |
1192 |
> |
public void testTimedPollWithOfferLast() throws InterruptedException { |
1193 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1194 |
< |
Thread t = new Thread(new Runnable() { |
1195 |
< |
public void run() { |
1196 |
< |
try { |
1197 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1198 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1199 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1200 |
< |
threadShouldThrow(); |
1201 |
< |
} catch (InterruptedException success) { } |
1202 |
< |
} |
1203 |
< |
}); |
1204 |
< |
try { |
1205 |
< |
t.start(); |
1206 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1207 |
< |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1208 |
< |
t.interrupt(); |
1209 |
< |
t.join(); |
1391 |
< |
} catch (Exception e){ |
1392 |
< |
unexpectedException(); |
1393 |
< |
} |
1394 |
< |
} |
1194 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1195 |
> |
public void realRun() throws InterruptedException { |
1196 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
1197 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1198 |
> |
try { |
1199 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1200 |
> |
shouldThrow(); |
1201 |
> |
} catch (InterruptedException success) {} |
1202 |
> |
}}); |
1203 |
> |
|
1204 |
> |
t.start(); |
1205 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1206 |
> |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1207 |
> |
t.interrupt(); |
1208 |
> |
t.join(); |
1209 |
> |
} |
1210 |
|
|
1211 |
|
|
1212 |
|
/** |
1215 |
|
public void testElement() { |
1216 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1217 |
|
for (int i = 0; i < SIZE; ++i) { |
1218 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
1218 |
> |
assertEquals(i, q.element()); |
1219 |
|
q.poll(); |
1220 |
|
} |
1221 |
|
try { |
1222 |
|
q.element(); |
1223 |
|
shouldThrow(); |
1224 |
< |
} |
1410 |
< |
catch (NoSuchElementException success) {} |
1224 |
> |
} catch (NoSuchElementException success) {} |
1225 |
|
} |
1226 |
|
|
1227 |
|
/** |
1238 |
|
} |
1239 |
|
assertTrue(q.isEmpty()); |
1240 |
|
} |
1241 |
< |
|
1241 |
> |
|
1242 |
|
/** |
1243 |
|
* contains(x) reports true when elements added but not yet removed |
1244 |
|
*/ |
1319 |
|
/** |
1320 |
|
* toArray contains all elements |
1321 |
|
*/ |
1322 |
< |
public void testToArray() { |
1322 |
> |
public void testToArray() throws InterruptedException{ |
1323 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1324 |
< |
Object[] o = q.toArray(); |
1325 |
< |
try { |
1326 |
< |
for(int i = 0; i < o.length; i++) |
1513 |
< |
assertEquals(o[i], q.take()); |
1514 |
< |
} catch (InterruptedException e){ |
1515 |
< |
unexpectedException(); |
1516 |
< |
} |
1324 |
> |
Object[] o = q.toArray(); |
1325 |
> |
for (int i = 0; i < o.length; i++) |
1326 |
> |
assertEquals(o[i], q.take()); |
1327 |
|
} |
1328 |
|
|
1329 |
|
/** |
1330 |
|
* toArray(a) contains all elements |
1331 |
|
*/ |
1332 |
< |
public void testToArray2() { |
1332 |
> |
public void testToArray2() throws InterruptedException { |
1333 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1334 |
< |
Integer[] ints = new Integer[SIZE]; |
1335 |
< |
ints = (Integer[])q.toArray(ints); |
1336 |
< |
try { |
1337 |
< |
for(int i = 0; i < ints.length; i++) |
1528 |
< |
assertEquals(ints[i], q.take()); |
1529 |
< |
} catch (InterruptedException e){ |
1530 |
< |
unexpectedException(); |
1531 |
< |
} |
1334 |
> |
Integer[] ints = new Integer[SIZE]; |
1335 |
> |
ints = (Integer[])q.toArray(ints); |
1336 |
> |
for (int i = 0; i < ints.length; i++) |
1337 |
> |
assertEquals(ints[i], q.take()); |
1338 |
|
} |
1339 |
|
|
1340 |
|
/** |
1341 |
|
* toArray(null) throws NPE |
1342 |
|
*/ |
1343 |
|
public void testToArray_BadArg() { |
1344 |
< |
try { |
1345 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1346 |
< |
Object o[] = q.toArray(null); |
1347 |
< |
shouldThrow(); |
1348 |
< |
} catch(NullPointerException success){} |
1344 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1345 |
> |
try { |
1346 |
> |
Object o[] = q.toArray(null); |
1347 |
> |
shouldThrow(); |
1348 |
> |
} catch (NullPointerException success) {} |
1349 |
|
} |
1350 |
|
|
1351 |
|
/** |
1352 |
|
* toArray with incompatible array type throws CCE |
1353 |
|
*/ |
1354 |
|
public void testToArray1_BadArg() { |
1355 |
< |
try { |
1356 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1357 |
< |
Object o[] = q.toArray(new String[10] ); |
1358 |
< |
shouldThrow(); |
1359 |
< |
} catch(ArrayStoreException success){} |
1355 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1356 |
> |
try { |
1357 |
> |
Object o[] = q.toArray(new String[10]); |
1358 |
> |
shouldThrow(); |
1359 |
> |
} catch (ArrayStoreException success) {} |
1360 |
|
} |
1361 |
|
|
1362 |
< |
|
1362 |
> |
|
1363 |
|
/** |
1364 |
|
* iterator iterates through all elements |
1365 |
|
*/ |
1366 |
< |
public void testIterator() { |
1366 |
> |
public void testIterator() throws InterruptedException { |
1367 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1368 |
< |
Iterator it = q.iterator(); |
1369 |
< |
try { |
1370 |
< |
while(it.hasNext()){ |
1371 |
< |
assertEquals(it.next(), q.take()); |
1566 |
< |
} |
1567 |
< |
} catch (InterruptedException e){ |
1568 |
< |
unexpectedException(); |
1569 |
< |
} |
1368 |
> |
Iterator it = q.iterator(); |
1369 |
> |
while (it.hasNext()) { |
1370 |
> |
assertEquals(it.next(), q.take()); |
1371 |
> |
} |
1372 |
|
} |
1373 |
|
|
1374 |
|
/** |
1383 |
|
Iterator it = q.iterator(); |
1384 |
|
it.next(); |
1385 |
|
it.remove(); |
1386 |
< |
|
1386 |
> |
|
1387 |
|
it = q.iterator(); |
1388 |
|
assertEquals(it.next(), one); |
1389 |
|
assertEquals(it.next(), three); |
1402 |
|
assertEquals(0, q.remainingCapacity()); |
1403 |
|
int k = 0; |
1404 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
1405 |
< |
int i = ((Integer)(it.next())).intValue(); |
1604 |
< |
assertEquals(++k, i); |
1405 |
> |
assertEquals(++k, it.next()); |
1406 |
|
} |
1407 |
|
assertEquals(3, k); |
1408 |
|
} |
1415 |
|
q.add(one); |
1416 |
|
q.add(two); |
1417 |
|
q.add(three); |
1418 |
+ |
for (Iterator it = q.iterator(); it.hasNext();) { |
1419 |
+ |
q.remove(); |
1420 |
+ |
it.next(); |
1421 |
+ |
} |
1422 |
+ |
assertEquals(0, q.size()); |
1423 |
+ |
} |
1424 |
+ |
|
1425 |
+ |
|
1426 |
+ |
/** |
1427 |
+ |
* Descending iterator iterates through all elements |
1428 |
+ |
*/ |
1429 |
+ |
public void testDescendingIterator() { |
1430 |
+ |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1431 |
+ |
int i = 0; |
1432 |
+ |
Iterator it = q.descendingIterator(); |
1433 |
+ |
while (it.hasNext()) { |
1434 |
+ |
assertTrue(q.contains(it.next())); |
1435 |
+ |
++i; |
1436 |
+ |
} |
1437 |
+ |
assertEquals(i, SIZE); |
1438 |
+ |
assertFalse(it.hasNext()); |
1439 |
|
try { |
1440 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
1441 |
< |
q.remove(); |
1442 |
< |
it.next(); |
1440 |
> |
it.next(); |
1441 |
> |
shouldThrow(); |
1442 |
> |
} catch (NoSuchElementException success) {} |
1443 |
> |
} |
1444 |
> |
|
1445 |
> |
/** |
1446 |
> |
* Descending iterator ordering is reverse FIFO |
1447 |
> |
*/ |
1448 |
> |
public void testDescendingIteratorOrdering() { |
1449 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1450 |
> |
for (int iters = 0; iters < 100; ++iters) { |
1451 |
> |
q.add(new Integer(3)); |
1452 |
> |
q.add(new Integer(2)); |
1453 |
> |
q.add(new Integer(1)); |
1454 |
> |
int k = 0; |
1455 |
> |
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
1456 |
> |
assertEquals(++k, it.next()); |
1457 |
|
} |
1458 |
+ |
|
1459 |
+ |
assertEquals(3, k); |
1460 |
+ |
q.remove(); |
1461 |
+ |
q.remove(); |
1462 |
+ |
q.remove(); |
1463 |
|
} |
1464 |
< |
catch (ConcurrentModificationException e) { |
1465 |
< |
unexpectedException(); |
1464 |
> |
} |
1465 |
> |
|
1466 |
> |
/** |
1467 |
> |
* descendingIterator.remove removes current element |
1468 |
> |
*/ |
1469 |
> |
public void testDescendingIteratorRemove () { |
1470 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1471 |
> |
for (int iters = 0; iters < 100; ++iters) { |
1472 |
> |
q.add(new Integer(3)); |
1473 |
> |
q.add(new Integer(2)); |
1474 |
> |
q.add(new Integer(1)); |
1475 |
> |
Iterator it = q.descendingIterator(); |
1476 |
> |
assertEquals(it.next(), new Integer(1)); |
1477 |
> |
it.remove(); |
1478 |
> |
assertEquals(it.next(), new Integer(2)); |
1479 |
> |
it = q.descendingIterator(); |
1480 |
> |
assertEquals(it.next(), new Integer(2)); |
1481 |
> |
assertEquals(it.next(), new Integer(3)); |
1482 |
> |
it.remove(); |
1483 |
> |
assertFalse(it.hasNext()); |
1484 |
> |
q.remove(); |
1485 |
|
} |
1626 |
– |
assertEquals(0, q.size()); |
1486 |
|
} |
1487 |
|
|
1488 |
|
|
1495 |
|
for (int i = 0; i < SIZE; ++i) { |
1496 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
1497 |
|
} |
1498 |
< |
} |
1498 |
> |
} |
1499 |
|
|
1500 |
|
|
1501 |
|
/** |
1506 |
|
q.add(one); |
1507 |
|
q.add(two); |
1508 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1509 |
< |
executor.execute(new Runnable() { |
1510 |
< |
public void run() { |
1511 |
< |
threadAssertFalse(q.offer(three)); |
1512 |
< |
try { |
1513 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
1514 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
1515 |
< |
} |
1516 |
< |
catch (InterruptedException e) { |
1517 |
< |
threadUnexpectedException(); |
1518 |
< |
} |
1519 |
< |
} |
1520 |
< |
}); |
1509 |
> |
executor.execute(new CheckedRunnable() { |
1510 |
> |
public void realRun() throws InterruptedException { |
1511 |
> |
assertFalse(q.offer(three)); |
1512 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1513 |
> |
assertEquals(0, q.remainingCapacity()); |
1514 |
> |
}}); |
1515 |
> |
|
1516 |
> |
executor.execute(new CheckedRunnable() { |
1517 |
> |
public void realRun() throws InterruptedException { |
1518 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1519 |
> |
assertSame(one, q.take()); |
1520 |
> |
}}); |
1521 |
|
|
1663 |
– |
executor.execute(new Runnable() { |
1664 |
– |
public void run() { |
1665 |
– |
try { |
1666 |
– |
Thread.sleep(SMALL_DELAY_MS); |
1667 |
– |
threadAssertEquals(one, q.take()); |
1668 |
– |
} |
1669 |
– |
catch (InterruptedException e) { |
1670 |
– |
threadUnexpectedException(); |
1671 |
– |
} |
1672 |
– |
} |
1673 |
– |
}); |
1674 |
– |
|
1522 |
|
joinPool(executor); |
1523 |
|
} |
1524 |
|
|
1528 |
|
public void testPollInExecutor() { |
1529 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1530 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1531 |
< |
executor.execute(new Runnable() { |
1532 |
< |
public void run() { |
1533 |
< |
threadAssertNull(q.poll()); |
1534 |
< |
try { |
1535 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
1536 |
< |
threadAssertTrue(q.isEmpty()); |
1537 |
< |
} |
1538 |
< |
catch (InterruptedException e) { |
1539 |
< |
threadUnexpectedException(); |
1540 |
< |
} |
1541 |
< |
} |
1542 |
< |
}); |
1531 |
> |
executor.execute(new CheckedRunnable() { |
1532 |
> |
public void realRun() throws InterruptedException { |
1533 |
> |
assertNull(q.poll()); |
1534 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1535 |
> |
assertTrue(q.isEmpty()); |
1536 |
> |
}}); |
1537 |
> |
|
1538 |
> |
executor.execute(new CheckedRunnable() { |
1539 |
> |
public void realRun() throws InterruptedException { |
1540 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1541 |
> |
q.put(one); |
1542 |
> |
}}); |
1543 |
|
|
1697 |
– |
executor.execute(new Runnable() { |
1698 |
– |
public void run() { |
1699 |
– |
try { |
1700 |
– |
Thread.sleep(SMALL_DELAY_MS); |
1701 |
– |
q.put(one); |
1702 |
– |
} |
1703 |
– |
catch (InterruptedException e) { |
1704 |
– |
threadUnexpectedException(); |
1705 |
– |
} |
1706 |
– |
} |
1707 |
– |
}); |
1708 |
– |
|
1544 |
|
joinPool(executor); |
1545 |
|
} |
1546 |
|
|
1547 |
|
/** |
1548 |
|
* A deserialized serialized deque has same elements in same order |
1549 |
|
*/ |
1550 |
< |
public void testSerialization() { |
1550 |
> |
public void testSerialization() throws Exception { |
1551 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1552 |
|
|
1553 |
< |
try { |
1554 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1555 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1556 |
< |
out.writeObject(q); |
1557 |
< |
out.close(); |
1558 |
< |
|
1559 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1560 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1561 |
< |
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1562 |
< |
assertEquals(q.size(), r.size()); |
1563 |
< |
while (!q.isEmpty()) |
1729 |
< |
assertEquals(q.remove(), r.remove()); |
1730 |
< |
} catch(Exception e){ |
1731 |
< |
unexpectedException(); |
1732 |
< |
} |
1553 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1554 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1555 |
> |
out.writeObject(q); |
1556 |
> |
out.close(); |
1557 |
> |
|
1558 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1559 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1560 |
> |
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1561 |
> |
assertEquals(q.size(), r.size()); |
1562 |
> |
while (!q.isEmpty()) |
1563 |
> |
assertEquals(q.remove(), r.remove()); |
1564 |
|
} |
1565 |
|
|
1566 |
|
/** |
1567 |
|
* drainTo(null) throws NPE |
1568 |
< |
*/ |
1568 |
> |
*/ |
1569 |
|
public void testDrainToNull() { |
1570 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1571 |
|
try { |
1572 |
|
q.drainTo(null); |
1573 |
|
shouldThrow(); |
1574 |
< |
} catch(NullPointerException success) { |
1744 |
< |
} |
1574 |
> |
} catch (NullPointerException success) {} |
1575 |
|
} |
1576 |
|
|
1577 |
|
/** |
1578 |
|
* drainTo(this) throws IAE |
1579 |
< |
*/ |
1579 |
> |
*/ |
1580 |
|
public void testDrainToSelf() { |
1581 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1582 |
|
try { |
1583 |
|
q.drainTo(q); |
1584 |
|
shouldThrow(); |
1585 |
< |
} catch(IllegalArgumentException success) { |
1756 |
< |
} |
1585 |
> |
} catch (IllegalArgumentException success) {} |
1586 |
|
} |
1587 |
|
|
1588 |
|
/** |
1589 |
|
* drainTo(c) empties deque into another collection c |
1590 |
< |
*/ |
1590 |
> |
*/ |
1591 |
|
public void testDrainTo() { |
1592 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1593 |
|
ArrayList l = new ArrayList(); |
1594 |
|
q.drainTo(l); |
1595 |
|
assertEquals(q.size(), 0); |
1596 |
|
assertEquals(l.size(), SIZE); |
1597 |
< |
for (int i = 0; i < SIZE; ++i) |
1597 |
> |
for (int i = 0; i < SIZE; ++i) |
1598 |
|
assertEquals(l.get(i), new Integer(i)); |
1599 |
|
q.add(zero); |
1600 |
|
q.add(one); |
1605 |
|
q.drainTo(l); |
1606 |
|
assertEquals(q.size(), 0); |
1607 |
|
assertEquals(l.size(), 2); |
1608 |
< |
for (int i = 0; i < 2; ++i) |
1608 |
> |
for (int i = 0; i < 2; ++i) |
1609 |
|
assertEquals(l.get(i), new Integer(i)); |
1610 |
|
} |
1611 |
|
|
1612 |
|
/** |
1613 |
|
* drainTo empties full deque, unblocking a waiting put. |
1614 |
< |
*/ |
1615 |
< |
public void testDrainToWithActivePut() { |
1614 |
> |
*/ |
1615 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
1616 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1617 |
< |
Thread t = new Thread(new Runnable() { |
1618 |
< |
public void run() { |
1619 |
< |
try { |
1620 |
< |
q.put(new Integer(SIZE+1)); |
1621 |
< |
} catch (InterruptedException ie){ |
1622 |
< |
threadUnexpectedException(); |
1623 |
< |
} |
1624 |
< |
} |
1625 |
< |
}); |
1626 |
< |
try { |
1627 |
< |
t.start(); |
1628 |
< |
ArrayList l = new ArrayList(); |
1629 |
< |
q.drainTo(l); |
1801 |
< |
assertTrue(l.size() >= SIZE); |
1802 |
< |
for (int i = 0; i < SIZE; ++i) |
1803 |
< |
assertEquals(l.get(i), new Integer(i)); |
1804 |
< |
t.join(); |
1805 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
1806 |
< |
} catch(Exception e){ |
1807 |
< |
unexpectedException(); |
1808 |
< |
} |
1617 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1618 |
> |
public void realRun() throws InterruptedException { |
1619 |
> |
q.put(new Integer(SIZE+1)); |
1620 |
> |
}}); |
1621 |
> |
|
1622 |
> |
t.start(); |
1623 |
> |
ArrayList l = new ArrayList(); |
1624 |
> |
q.drainTo(l); |
1625 |
> |
assertTrue(l.size() >= SIZE); |
1626 |
> |
for (int i = 0; i < SIZE; ++i) |
1627 |
> |
assertEquals(l.get(i), new Integer(i)); |
1628 |
> |
t.join(); |
1629 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
1630 |
|
} |
1631 |
|
|
1632 |
|
/** |
1633 |
|
* drainTo(null, n) throws NPE |
1634 |
< |
*/ |
1634 |
> |
*/ |
1635 |
|
public void testDrainToNullN() { |
1636 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1637 |
|
try { |
1638 |
|
q.drainTo(null, 0); |
1639 |
|
shouldThrow(); |
1640 |
< |
} catch(NullPointerException success) { |
1820 |
< |
} |
1640 |
> |
} catch (NullPointerException success) {} |
1641 |
|
} |
1642 |
|
|
1643 |
|
/** |
1644 |
|
* drainTo(this, n) throws IAE |
1645 |
< |
*/ |
1645 |
> |
*/ |
1646 |
|
public void testDrainToSelfN() { |
1647 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1648 |
|
try { |
1649 |
|
q.drainTo(q, 0); |
1650 |
|
shouldThrow(); |
1651 |
< |
} catch(IllegalArgumentException success) { |
1832 |
< |
} |
1651 |
> |
} catch (IllegalArgumentException success) {} |
1652 |
|
} |
1653 |
|
|
1654 |
|
/** |
1655 |
|
* drainTo(c, n) empties first max {n, size} elements of deque into c |
1656 |
< |
*/ |
1656 |
> |
*/ |
1657 |
|
public void testDrainToN() { |
1658 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1659 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
1660 |
< |
for(int j = 0; j < SIZE; j++) |
1660 |
> |
for (int j = 0; j < SIZE; j++) |
1661 |
|
assertTrue(q.offer(new Integer(j))); |
1662 |
|
ArrayList l = new ArrayList(); |
1663 |
|
q.drainTo(l, i); |
1664 |
|
int k = (i < SIZE)? i : SIZE; |
1665 |
|
assertEquals(l.size(), k); |
1666 |
|
assertEquals(q.size(), SIZE-k); |
1667 |
< |
for (int j = 0; j < k; ++j) |
1667 |
> |
for (int j = 0; j < k; ++j) |
1668 |
|
assertEquals(l.get(j), new Integer(j)); |
1669 |
|
while (q.poll() != null) ; |
1670 |
|
} |