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 |
+ |
|
15 |
+ |
public static class Unbounded extends BlockingQueueTest { |
16 |
+ |
protected BlockingQueue emptyCollection() { |
17 |
+ |
return new LinkedBlockingDeque(); |
18 |
+ |
} |
19 |
+ |
} |
20 |
+ |
|
21 |
+ |
public static class Bounded extends BlockingQueueTest { |
22 |
+ |
protected BlockingQueue emptyCollection() { |
23 |
+ |
return new LinkedBlockingDeque(20); |
24 |
+ |
} |
25 |
+ |
} |
26 |
+ |
|
27 |
|
public static void main(String[] args) { |
28 |
< |
junit.textui.TestRunner.run (suite()); |
28 |
> |
junit.textui.TestRunner.run(suite()); |
29 |
|
} |
30 |
|
|
31 |
|
public static Test suite() { |
32 |
< |
return new TestSuite(LinkedBlockingDequeTest.class); |
32 |
> |
return newTestSuite(LinkedBlockingDequeTest.class, |
33 |
> |
new Unbounded().testSuite(), |
34 |
> |
new Bounded().testSuite()); |
35 |
|
} |
36 |
|
|
37 |
|
/** |
38 |
|
* Create a deque of given size containing consecutive |
39 |
|
* Integers 0 ... n. |
40 |
|
*/ |
41 |
< |
private LinkedBlockingDeque populatedDeque(int n) { |
42 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(n); |
41 |
> |
private LinkedBlockingDeque<Integer> populatedDeque(int n) { |
42 |
> |
LinkedBlockingDeque<Integer> q = |
43 |
> |
new LinkedBlockingDeque<Integer>(n); |
44 |
|
assertTrue(q.isEmpty()); |
45 |
< |
for(int i = 0; i < n; i++) |
46 |
< |
assertTrue(q.offer(new Integer(i))); |
45 |
> |
for (int i = 0; i < n; i++) |
46 |
> |
assertTrue(q.offer(new Integer(i))); |
47 |
|
assertFalse(q.isEmpty()); |
48 |
|
assertEquals(0, q.remainingCapacity()); |
49 |
< |
assertEquals(n, q.size()); |
49 |
> |
assertEquals(n, q.size()); |
50 |
|
return q; |
51 |
|
} |
52 |
|
|
83 |
|
* offer(null) throws NPE |
84 |
|
*/ |
85 |
|
public void testOfferFirstNull() { |
86 |
< |
try { |
86 |
> |
try { |
87 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
88 |
|
q.offerFirst(null); |
89 |
|
shouldThrow(); |
90 |
< |
} catch (NullPointerException success) { |
74 |
< |
} |
90 |
> |
} catch (NullPointerException success) {} |
91 |
|
} |
92 |
|
|
93 |
|
/** |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
< |
* pollFirst succeeds unless empty |
112 |
> |
* pollFirst succeeds unless empty |
113 |
|
*/ |
114 |
|
public void testPollFirst() { |
115 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
116 |
|
for (int i = 0; i < SIZE; ++i) { |
117 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
117 |
> |
assertEquals(i, q.pollFirst()); |
118 |
|
} |
119 |
< |
assertNull(q.pollFirst()); |
119 |
> |
assertNull(q.pollFirst()); |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
< |
* pollLast succeeds unless empty |
123 |
> |
* pollLast succeeds unless empty |
124 |
|
*/ |
125 |
|
public void testPollLast() { |
126 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
127 |
|
for (int i = SIZE-1; i >= 0; --i) { |
128 |
< |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
128 |
> |
assertEquals(i, q.pollLast()); |
129 |
|
} |
130 |
< |
assertNull(q.pollLast()); |
130 |
> |
assertNull(q.pollLast()); |
131 |
|
} |
132 |
|
|
133 |
|
/** |
134 |
< |
* peekFirst returns next element, or null if empty |
134 |
> |
* peekFirst returns next element, or null if empty |
135 |
|
*/ |
136 |
|
public void testPeekFirst() { |
137 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
138 |
|
for (int i = 0; i < SIZE; ++i) { |
139 |
< |
assertEquals(i, ((Integer)q.peekFirst()).intValue()); |
140 |
< |
q.pollFirst(); |
139 |
> |
assertEquals(i, q.peekFirst()); |
140 |
> |
assertEquals(i, q.pollFirst()); |
141 |
|
assertTrue(q.peekFirst() == null || |
142 |
< |
i != ((Integer)q.peekFirst()).intValue()); |
142 |
> |
!q.peekFirst().equals(i)); |
143 |
|
} |
144 |
< |
assertNull(q.peekFirst()); |
144 |
> |
assertNull(q.peekFirst()); |
145 |
|
} |
146 |
|
|
147 |
|
/** |
148 |
< |
* peek returns next element, or null if empty |
148 |
> |
* peek returns next element, or null if empty |
149 |
|
*/ |
150 |
|
public void testPeek() { |
151 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
152 |
|
for (int i = 0; i < SIZE; ++i) { |
153 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
154 |
< |
q.pollFirst(); |
153 |
> |
assertEquals(i, q.peek()); |
154 |
> |
assertEquals(i, q.pollFirst()); |
155 |
|
assertTrue(q.peek() == null || |
156 |
< |
i != ((Integer)q.peek()).intValue()); |
156 |
> |
!q.peek().equals(i)); |
157 |
|
} |
158 |
< |
assertNull(q.peek()); |
158 |
> |
assertNull(q.peek()); |
159 |
|
} |
160 |
|
|
161 |
|
/** |
162 |
< |
* peekLast returns next element, or null if empty |
162 |
> |
* peekLast returns next element, or null if empty |
163 |
|
*/ |
164 |
|
public void testPeekLast() { |
165 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
166 |
|
for (int i = SIZE-1; i >= 0; --i) { |
167 |
< |
assertEquals(i, ((Integer)q.peekLast()).intValue()); |
168 |
< |
q.pollLast(); |
167 |
> |
assertEquals(i, q.peekLast()); |
168 |
> |
assertEquals(i, q.pollLast()); |
169 |
|
assertTrue(q.peekLast() == null || |
170 |
< |
i != ((Integer)q.peekLast()).intValue()); |
170 |
> |
!q.peekLast().equals(i)); |
171 |
|
} |
172 |
< |
assertNull(q.peekLast()); |
172 |
> |
assertNull(q.peekLast()); |
173 |
|
} |
174 |
|
|
175 |
|
/** |
176 |
< |
* getFirst returns next getFirst, or throws NSEE if empty |
176 |
> |
* getFirst() returns first element, or throws NSEE if empty |
177 |
|
*/ |
178 |
|
public void testFirstElement() { |
179 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
180 |
|
for (int i = 0; i < SIZE; ++i) { |
181 |
< |
assertEquals(i, ((Integer)q.getFirst()).intValue()); |
182 |
< |
q.pollFirst(); |
181 |
> |
assertEquals(i, q.getFirst()); |
182 |
> |
assertEquals(i, q.pollFirst()); |
183 |
|
} |
184 |
|
try { |
185 |
|
q.getFirst(); |
186 |
|
shouldThrow(); |
187 |
< |
} |
188 |
< |
catch (NoSuchElementException success) {} |
187 |
> |
} catch (NoSuchElementException success) {} |
188 |
> |
assertNull(q.peekFirst()); |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
< |
* getLast returns next element, or throws NSEE if empty |
192 |
> |
* getLast() returns last element, or throws NSEE if empty |
193 |
|
*/ |
194 |
|
public void testLastElement() { |
195 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
196 |
|
for (int i = SIZE-1; i >= 0; --i) { |
197 |
< |
assertEquals(i, ((Integer)q.getLast()).intValue()); |
198 |
< |
q.pollLast(); |
197 |
> |
assertEquals(i, q.getLast()); |
198 |
> |
assertEquals(i, q.pollLast()); |
199 |
|
} |
200 |
|
try { |
201 |
|
q.getLast(); |
202 |
|
shouldThrow(); |
203 |
< |
} |
204 |
< |
catch (NoSuchElementException success) {} |
189 |
< |
assertNull(q.peekLast()); |
203 |
> |
} catch (NoSuchElementException success) {} |
204 |
> |
assertNull(q.peekLast()); |
205 |
|
} |
206 |
|
|
207 |
|
/** |
208 |
< |
* removeFirst removes next element, or throws NSEE if empty |
208 |
> |
* removeFirst() removes first element, or throws NSEE if empty |
209 |
|
*/ |
210 |
|
public void testRemoveFirst() { |
211 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
212 |
|
for (int i = 0; i < SIZE; ++i) { |
213 |
< |
assertEquals(i, ((Integer)q.removeFirst()).intValue()); |
213 |
> |
assertEquals(i, q.removeFirst()); |
214 |
|
} |
215 |
|
try { |
216 |
|
q.removeFirst(); |
217 |
|
shouldThrow(); |
218 |
< |
} catch (NoSuchElementException success){ |
219 |
< |
} |
218 |
> |
} catch (NoSuchElementException success) {} |
219 |
> |
assertNull(q.peekFirst()); |
220 |
> |
} |
221 |
> |
|
222 |
> |
/** |
223 |
> |
* removeLast() removes last element, or throws NSEE if empty |
224 |
> |
*/ |
225 |
> |
public void testRemoveLast() { |
226 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
227 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
228 |
> |
assertEquals(i, q.removeLast()); |
229 |
> |
} |
230 |
> |
try { |
231 |
> |
q.removeLast(); |
232 |
> |
shouldThrow(); |
233 |
> |
} catch (NoSuchElementException success) {} |
234 |
> |
assertNull(q.peekLast()); |
235 |
|
} |
236 |
|
|
237 |
|
/** |
238 |
< |
* remove removes next element, or throws NSEE if empty |
238 |
> |
* remove removes next element, or throws NSEE if empty |
239 |
|
*/ |
240 |
|
public void testRemove() { |
241 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
242 |
|
for (int i = 0; i < SIZE; ++i) { |
243 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
243 |
> |
assertEquals(i, q.remove()); |
244 |
|
} |
245 |
|
try { |
246 |
|
q.remove(); |
247 |
|
shouldThrow(); |
248 |
< |
} catch (NoSuchElementException success){ |
219 |
< |
} |
248 |
> |
} catch (NoSuchElementException success) {} |
249 |
|
} |
250 |
|
|
251 |
|
/** |
284 |
|
public void testAddFirst() { |
285 |
|
LinkedBlockingDeque q = populatedDeque(3); |
286 |
|
q.pollLast(); |
287 |
< |
q.addFirst(four); |
288 |
< |
assertEquals(four,q.peekFirst()); |
287 |
> |
q.addFirst(four); |
288 |
> |
assertSame(four, q.peekFirst()); |
289 |
|
} |
290 |
|
|
291 |
|
/** |
294 |
|
public void testAddLast() { |
295 |
|
LinkedBlockingDeque q = populatedDeque(3); |
296 |
|
q.pollLast(); |
297 |
< |
q.addLast(four); |
298 |
< |
assertEquals(four,q.peekLast()); |
297 |
> |
q.addLast(four); |
298 |
> |
assertSame(four, q.peekLast()); |
299 |
|
} |
300 |
|
|
301 |
|
|
309 |
|
} |
310 |
|
|
311 |
|
/** |
312 |
< |
* Constructor throws IAE if capacity argument nonpositive |
312 |
> |
* Constructor throws IAE if capacity argument nonpositive |
313 |
|
*/ |
314 |
|
public void testConstructor2() { |
315 |
|
try { |
316 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(0); |
317 |
|
shouldThrow(); |
318 |
< |
} |
290 |
< |
catch (IllegalArgumentException success) {} |
318 |
> |
} catch (IllegalArgumentException success) {} |
319 |
|
} |
320 |
|
|
321 |
|
/** |
325 |
|
try { |
326 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(null); |
327 |
|
shouldThrow(); |
328 |
< |
} |
301 |
< |
catch (NullPointerException success) {} |
328 |
> |
} catch (NullPointerException success) {} |
329 |
|
} |
330 |
|
|
331 |
|
/** |
336 |
|
Integer[] ints = new Integer[SIZE]; |
337 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
338 |
|
shouldThrow(); |
339 |
< |
} |
313 |
< |
catch (NullPointerException success) {} |
339 |
> |
} catch (NullPointerException success) {} |
340 |
|
} |
341 |
|
|
342 |
|
/** |
349 |
|
ints[i] = new Integer(i); |
350 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
351 |
|
shouldThrow(); |
352 |
< |
} |
327 |
< |
catch (NullPointerException success) {} |
352 |
> |
} catch (NullPointerException success) {} |
353 |
|
} |
354 |
|
|
355 |
|
/** |
356 |
|
* Deque contains all elements of collection used to initialize |
357 |
|
*/ |
358 |
|
public void testConstructor6() { |
359 |
< |
try { |
360 |
< |
Integer[] ints = new Integer[SIZE]; |
361 |
< |
for (int i = 0; i < SIZE; ++i) |
362 |
< |
ints[i] = new Integer(i); |
363 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
364 |
< |
for (int i = 0; i < SIZE; ++i) |
340 |
< |
assertEquals(ints[i], q.poll()); |
341 |
< |
} |
342 |
< |
finally {} |
359 |
> |
Integer[] ints = new Integer[SIZE]; |
360 |
> |
for (int i = 0; i < SIZE; ++i) |
361 |
> |
ints[i] = new Integer(i); |
362 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
363 |
> |
for (int i = 0; i < SIZE; ++i) |
364 |
> |
assertEquals(ints[i], q.poll()); |
365 |
|
} |
366 |
|
|
367 |
|
/** |
400 |
|
* offer(null) throws NPE |
401 |
|
*/ |
402 |
|
public void testOfferNull() { |
403 |
< |
try { |
403 |
> |
try { |
404 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
405 |
|
q.offer(null); |
406 |
|
shouldThrow(); |
407 |
< |
} catch (NullPointerException success) { } |
407 |
> |
} catch (NullPointerException success) {} |
408 |
|
} |
409 |
|
|
410 |
|
/** |
411 |
|
* add(null) throws NPE |
412 |
|
*/ |
413 |
|
public void testAddNull() { |
414 |
< |
try { |
414 |
> |
try { |
415 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
416 |
|
q.add(null); |
417 |
|
shouldThrow(); |
418 |
< |
} catch (NullPointerException success) { } |
418 |
> |
} catch (NullPointerException success) {} |
419 |
|
} |
420 |
|
|
421 |
|
/** |
422 |
|
* push(null) throws NPE |
423 |
|
*/ |
424 |
|
public void testPushNull() { |
425 |
< |
try { |
425 |
> |
try { |
426 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
427 |
|
q.push(null); |
428 |
|
shouldThrow(); |
429 |
< |
} catch (NullPointerException success) { } |
429 |
> |
} catch (NullPointerException success) {} |
430 |
|
} |
431 |
|
|
432 |
|
/** |
433 |
|
* push succeeds if not full; throws ISE if full |
434 |
|
*/ |
435 |
|
public void testPush() { |
436 |
< |
try { |
436 |
> |
try { |
437 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
438 |
|
for (int i = 0; i < SIZE; ++i) { |
439 |
|
Integer I = new Integer(i); |
442 |
|
} |
443 |
|
assertEquals(0, q.remainingCapacity()); |
444 |
|
q.push(new Integer(SIZE)); |
445 |
< |
} catch (IllegalStateException success){ |
446 |
< |
} |
445 |
> |
shouldThrow(); |
446 |
> |
} catch (IllegalStateException success) {} |
447 |
|
} |
448 |
|
|
449 |
|
/** |
452 |
|
public void testPushWithPeek() { |
453 |
|
LinkedBlockingDeque q = populatedDeque(3); |
454 |
|
q.pollLast(); |
455 |
< |
q.push(four); |
456 |
< |
assertEquals(four,q.peekFirst()); |
455 |
> |
q.push(four); |
456 |
> |
assertSame(four, q.peekFirst()); |
457 |
|
} |
458 |
|
|
459 |
|
|
460 |
|
/** |
461 |
< |
* pop removes next element, or throws NSEE if empty |
461 |
> |
* pop removes next element, or throws NSEE if empty |
462 |
|
*/ |
463 |
|
public void testPop() { |
464 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
465 |
|
for (int i = 0; i < SIZE; ++i) { |
466 |
< |
assertEquals(i, ((Integer)q.pop()).intValue()); |
466 |
> |
assertEquals(i, q.pop()); |
467 |
|
} |
468 |
|
try { |
469 |
|
q.pop(); |
470 |
|
shouldThrow(); |
471 |
< |
} catch (NoSuchElementException success){ |
450 |
< |
} |
471 |
> |
} catch (NoSuchElementException success) {} |
472 |
|
} |
473 |
|
|
474 |
|
|
485 |
|
* add succeeds if not full; throws ISE if full |
486 |
|
*/ |
487 |
|
public void testAdd() { |
488 |
< |
try { |
488 |
> |
try { |
489 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
490 |
|
for (int i = 0; i < SIZE; ++i) { |
491 |
|
assertTrue(q.add(new Integer(i))); |
492 |
|
} |
493 |
|
assertEquals(0, q.remainingCapacity()); |
494 |
|
q.add(new Integer(SIZE)); |
495 |
< |
} catch (IllegalStateException success){ |
496 |
< |
} |
495 |
> |
shouldThrow(); |
496 |
> |
} catch (IllegalStateException success) {} |
497 |
|
} |
498 |
|
|
499 |
|
/** |
504 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
505 |
|
q.addAll(null); |
506 |
|
shouldThrow(); |
507 |
< |
} |
487 |
< |
catch (NullPointerException success) {} |
507 |
> |
} catch (NullPointerException success) {} |
508 |
|
} |
509 |
|
|
510 |
|
/** |
515 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
516 |
|
q.addAll(q); |
517 |
|
shouldThrow(); |
518 |
< |
} |
499 |
< |
catch (IllegalArgumentException success) {} |
518 |
> |
} catch (IllegalArgumentException success) {} |
519 |
|
} |
520 |
|
|
521 |
|
/** |
527 |
|
Integer[] ints = new Integer[SIZE]; |
528 |
|
q.addAll(Arrays.asList(ints)); |
529 |
|
shouldThrow(); |
530 |
< |
} |
512 |
< |
catch (NullPointerException success) {} |
530 |
> |
} catch (NullPointerException success) {} |
531 |
|
} |
532 |
+ |
|
533 |
|
/** |
534 |
|
* addAll of a collection with any null elements throws NPE after |
535 |
|
* possibly adding some elements |
542 |
|
ints[i] = new Integer(i); |
543 |
|
q.addAll(Arrays.asList(ints)); |
544 |
|
shouldThrow(); |
545 |
< |
} |
527 |
< |
catch (NullPointerException success) {} |
545 |
> |
} catch (NullPointerException success) {} |
546 |
|
} |
547 |
+ |
|
548 |
|
/** |
549 |
|
* addAll throws ISE if not enough room |
550 |
|
*/ |
556 |
|
ints[i] = new Integer(i); |
557 |
|
q.addAll(Arrays.asList(ints)); |
558 |
|
shouldThrow(); |
559 |
< |
} |
541 |
< |
catch (IllegalStateException success) {} |
559 |
> |
} catch (IllegalStateException success) {} |
560 |
|
} |
561 |
+ |
|
562 |
|
/** |
563 |
|
* Deque contains all elements, in traversal order, of successful addAll |
564 |
|
*/ |
565 |
|
public void testAddAll5() { |
566 |
< |
try { |
567 |
< |
Integer[] empty = new Integer[0]; |
568 |
< |
Integer[] ints = new Integer[SIZE]; |
569 |
< |
for (int i = 0; i < SIZE; ++i) |
570 |
< |
ints[i] = new Integer(i); |
571 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
572 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
573 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
574 |
< |
for (int i = 0; i < SIZE; ++i) |
556 |
< |
assertEquals(ints[i], q.poll()); |
557 |
< |
} |
558 |
< |
finally {} |
566 |
> |
Integer[] empty = new Integer[0]; |
567 |
> |
Integer[] ints = new Integer[SIZE]; |
568 |
> |
for (int i = 0; i < SIZE; ++i) |
569 |
> |
ints[i] = new Integer(i); |
570 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
571 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
572 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
573 |
> |
for (int i = 0; i < SIZE; ++i) |
574 |
> |
assertEquals(ints[i], q.poll()); |
575 |
|
} |
576 |
|
|
577 |
|
|
578 |
|
/** |
579 |
|
* put(null) throws NPE |
580 |
|
*/ |
581 |
< |
public void testPutNull() { |
582 |
< |
try { |
581 |
> |
public void testPutNull() throws InterruptedException { |
582 |
> |
try { |
583 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
584 |
|
q.put(null); |
585 |
|
shouldThrow(); |
586 |
< |
} |
587 |
< |
catch (NullPointerException success){ |
572 |
< |
} |
573 |
< |
catch (InterruptedException ie) { |
574 |
< |
unexpectedException(); |
575 |
< |
} |
576 |
< |
} |
586 |
> |
} catch (NullPointerException success) {} |
587 |
> |
} |
588 |
|
|
589 |
|
/** |
590 |
|
* all elements successfully put are contained |
591 |
|
*/ |
592 |
< |
public void testPut() { |
593 |
< |
try { |
594 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
595 |
< |
for (int i = 0; i < SIZE; ++i) { |
596 |
< |
Integer I = new Integer(i); |
597 |
< |
q.put(I); |
587 |
< |
assertTrue(q.contains(I)); |
588 |
< |
} |
589 |
< |
assertEquals(0, q.remainingCapacity()); |
590 |
< |
} |
591 |
< |
catch (InterruptedException ie) { |
592 |
< |
unexpectedException(); |
592 |
> |
public void testPut() throws InterruptedException { |
593 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
594 |
> |
for (int i = 0; i < SIZE; ++i) { |
595 |
> |
Integer I = new Integer(i); |
596 |
> |
q.put(I); |
597 |
> |
assertTrue(q.contains(I)); |
598 |
|
} |
599 |
+ |
assertEquals(0, q.remainingCapacity()); |
600 |
|
} |
601 |
|
|
602 |
|
/** |
603 |
|
* put blocks interruptibly if full |
604 |
|
*/ |
605 |
< |
public void testBlockingPut() { |
606 |
< |
Thread t = new Thread(new Runnable() { |
607 |
< |
public void run() { |
608 |
< |
int added = 0; |
609 |
< |
try { |
610 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
611 |
< |
for (int i = 0; i < SIZE; ++i) { |
612 |
< |
q.put(new Integer(i)); |
613 |
< |
++added; |
614 |
< |
} |
615 |
< |
q.put(new Integer(SIZE)); |
616 |
< |
threadShouldThrow(); |
617 |
< |
} catch (InterruptedException ie){ |
618 |
< |
threadAssertEquals(added, SIZE); |
613 |
< |
} |
614 |
< |
}}); |
605 |
> |
public void testBlockingPut() throws InterruptedException { |
606 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
607 |
> |
Thread t = new Thread(new CheckedRunnable() { |
608 |
> |
public void realRun() throws InterruptedException { |
609 |
> |
for (int i = 0; i < SIZE; ++i) |
610 |
> |
q.put(i); |
611 |
> |
assertEquals(SIZE, q.size()); |
612 |
> |
assertEquals(0, q.remainingCapacity()); |
613 |
> |
try { |
614 |
> |
q.put(99); |
615 |
> |
shouldThrow(); |
616 |
> |
} catch (InterruptedException success) {} |
617 |
> |
}}); |
618 |
> |
|
619 |
|
t.start(); |
620 |
< |
try { |
621 |
< |
Thread.sleep(SHORT_DELAY_MS); |
622 |
< |
t.interrupt(); |
623 |
< |
t.join(); |
624 |
< |
} |
621 |
< |
catch (InterruptedException ie) { |
622 |
< |
unexpectedException(); |
623 |
< |
} |
620 |
> |
Thread.sleep(SHORT_DELAY_MS); |
621 |
> |
t.interrupt(); |
622 |
> |
t.join(); |
623 |
> |
assertEquals(SIZE, q.size()); |
624 |
> |
assertEquals(0, q.remainingCapacity()); |
625 |
|
} |
626 |
|
|
627 |
|
/** |
628 |
|
* put blocks waiting for take when full |
629 |
|
*/ |
630 |
< |
public void testPutWithTake() { |
631 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
632 |
< |
Thread t = new Thread(new Runnable() { |
633 |
< |
public void run() { |
634 |
< |
int added = 0; |
635 |
< |
try { |
636 |
< |
q.put(new Object()); |
637 |
< |
++added; |
638 |
< |
q.put(new Object()); |
639 |
< |
++added; |
640 |
< |
q.put(new Object()); |
641 |
< |
++added; |
642 |
< |
q.put(new Object()); |
643 |
< |
++added; |
644 |
< |
threadShouldThrow(); |
645 |
< |
} catch (InterruptedException e){ |
646 |
< |
threadAssertTrue(added >= 2); |
647 |
< |
} |
648 |
< |
} |
649 |
< |
}); |
650 |
< |
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 |
< |
} |
630 |
> |
public void testPutWithTake() throws InterruptedException { |
631 |
> |
final int capacity = 2; |
632 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
633 |
> |
Thread t = new Thread(new CheckedRunnable() { |
634 |
> |
public void realRun() throws InterruptedException { |
635 |
> |
for (int i = 0; i < capacity + 1; i++) |
636 |
> |
q.put(i); |
637 |
> |
try { |
638 |
> |
q.put(99); |
639 |
> |
shouldThrow(); |
640 |
> |
} catch (InterruptedException success) {} |
641 |
> |
}}); |
642 |
> |
|
643 |
> |
t.start(); |
644 |
> |
Thread.sleep(SHORT_DELAY_MS); |
645 |
> |
assertEquals(q.remainingCapacity(), 0); |
646 |
> |
assertEquals(0, q.take()); |
647 |
> |
Thread.sleep(SHORT_DELAY_MS); |
648 |
> |
t.interrupt(); |
649 |
> |
t.join(); |
650 |
> |
assertEquals(q.remainingCapacity(), 0); |
651 |
|
} |
652 |
|
|
653 |
|
/** |
654 |
|
* timed offer times out if full and elements not taken |
655 |
|
*/ |
656 |
< |
public void testTimedOffer() { |
656 |
> |
public void testTimedOffer() throws InterruptedException { |
657 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
658 |
< |
Thread t = new Thread(new Runnable() { |
659 |
< |
public void run() { |
660 |
< |
try { |
661 |
< |
q.put(new Object()); |
662 |
< |
q.put(new Object()); |
663 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
664 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
665 |
< |
threadShouldThrow(); |
666 |
< |
} catch (InterruptedException success){} |
667 |
< |
} |
675 |
< |
}); |
658 |
> |
Thread t = new Thread(new CheckedRunnable() { |
659 |
> |
public void realRun() throws InterruptedException { |
660 |
> |
q.put(new Object()); |
661 |
> |
q.put(new Object()); |
662 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
663 |
> |
try { |
664 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
665 |
> |
shouldThrow(); |
666 |
> |
} catch (InterruptedException success) {} |
667 |
> |
}}); |
668 |
|
|
669 |
< |
try { |
670 |
< |
t.start(); |
671 |
< |
Thread.sleep(SMALL_DELAY_MS); |
672 |
< |
t.interrupt(); |
681 |
< |
t.join(); |
682 |
< |
} catch (Exception e){ |
683 |
< |
unexpectedException(); |
684 |
< |
} |
669 |
> |
t.start(); |
670 |
> |
Thread.sleep(SMALL_DELAY_MS); |
671 |
> |
t.interrupt(); |
672 |
> |
t.join(); |
673 |
|
} |
674 |
|
|
675 |
|
/** |
676 |
|
* take retrieves elements in FIFO order |
677 |
|
*/ |
678 |
< |
public void testTake() { |
679 |
< |
try { |
680 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
681 |
< |
for (int i = 0; i < SIZE; ++i) { |
694 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
695 |
< |
} |
696 |
< |
} catch (InterruptedException e){ |
697 |
< |
unexpectedException(); |
698 |
< |
} |
699 |
< |
} |
700 |
< |
|
701 |
< |
/** |
702 |
< |
* take blocks interruptibly when empty |
703 |
< |
*/ |
704 |
< |
public void testTakeFromEmpty() { |
705 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
706 |
< |
Thread t = new Thread(new Runnable() { |
707 |
< |
public void run() { |
708 |
< |
try { |
709 |
< |
q.take(); |
710 |
< |
threadShouldThrow(); |
711 |
< |
} catch (InterruptedException success){ } |
712 |
< |
} |
713 |
< |
}); |
714 |
< |
try { |
715 |
< |
t.start(); |
716 |
< |
Thread.sleep(SHORT_DELAY_MS); |
717 |
< |
t.interrupt(); |
718 |
< |
t.join(); |
719 |
< |
} catch (Exception e){ |
720 |
< |
unexpectedException(); |
678 |
> |
public void testTake() throws InterruptedException { |
679 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
680 |
> |
for (int i = 0; i < SIZE; ++i) { |
681 |
> |
assertEquals(i, q.take()); |
682 |
|
} |
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 |
< |
}}); |
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 |
< |
try { |
703 |
< |
Thread.sleep(SHORT_DELAY_MS); |
704 |
< |
t.interrupt(); |
744 |
< |
t.join(); |
745 |
< |
} |
746 |
< |
catch (InterruptedException ie) { |
747 |
< |
unexpectedException(); |
748 |
< |
} |
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) { |
803 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
804 |
< |
} |
805 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
806 |
< |
} catch (InterruptedException success){ |
807 |
< |
} |
808 |
< |
}}); |
809 |
< |
t.start(); |
810 |
< |
try { |
811 |
< |
Thread.sleep(SHORT_DELAY_MS); |
812 |
< |
t.interrupt(); |
813 |
< |
t.join(); |
814 |
< |
} |
815 |
< |
catch (InterruptedException ie) { |
816 |
< |
unexpectedException(); |
817 |
< |
} |
818 |
< |
} |
819 |
< |
|
820 |
< |
/** |
821 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
822 |
< |
* on interruption throws |
823 |
< |
*/ |
824 |
< |
public void testTimedPollWithOffer() { |
825 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
826 |
< |
Thread t = new Thread(new Runnable() { |
827 |
< |
public void run() { |
828 |
< |
try { |
829 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
830 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
831 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
832 |
< |
threadShouldThrow(); |
833 |
< |
} catch (InterruptedException success) { } |
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 |
< |
}); |
753 |
< |
try { |
754 |
< |
t.start(); |
755 |
< |
Thread.sleep(SMALL_DELAY_MS); |
756 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
840 |
< |
t.interrupt(); |
841 |
< |
t.join(); |
842 |
< |
} catch (Exception e){ |
843 |
< |
unexpectedException(); |
844 |
< |
} |
845 |
< |
} |
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 |
|
* putFirst(null) throws NPE |
766 |
|
*/ |
767 |
< |
public void testPutFirstNull() { |
768 |
< |
try { |
767 |
> |
public void testPutFirstNull() throws InterruptedException { |
768 |
> |
try { |
769 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
770 |
|
q.putFirst(null); |
771 |
|
shouldThrow(); |
772 |
< |
} |
857 |
< |
catch (NullPointerException success){ |
858 |
< |
} |
859 |
< |
catch (InterruptedException ie) { |
860 |
< |
unexpectedException(); |
861 |
< |
} |
772 |
> |
} catch (NullPointerException success) {} |
773 |
|
} |
774 |
|
|
775 |
|
/** |
776 |
|
* all elements successfully putFirst are contained |
777 |
|
*/ |
778 |
< |
public void testPutFirst() { |
779 |
< |
try { |
780 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
781 |
< |
for (int i = 0; i < SIZE; ++i) { |
782 |
< |
Integer I = new Integer(i); |
783 |
< |
q.putFirst(I); |
873 |
< |
assertTrue(q.contains(I)); |
874 |
< |
} |
875 |
< |
assertEquals(0, q.remainingCapacity()); |
778 |
> |
public void testPutFirst() throws InterruptedException { |
779 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
780 |
> |
for (int i = 0; i < SIZE; ++i) { |
781 |
> |
Integer I = new Integer(i); |
782 |
> |
q.putFirst(I); |
783 |
> |
assertTrue(q.contains(I)); |
784 |
|
} |
785 |
< |
catch (InterruptedException ie) { |
878 |
< |
unexpectedException(); |
879 |
< |
} |
785 |
> |
assertEquals(0, q.remainingCapacity()); |
786 |
|
} |
787 |
|
|
788 |
|
/** |
789 |
|
* putFirst blocks interruptibly if full |
790 |
|
*/ |
791 |
< |
public void testBlockingPutFirst() { |
792 |
< |
Thread t = new Thread(new Runnable() { |
793 |
< |
public void run() { |
794 |
< |
int added = 0; |
795 |
< |
try { |
796 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
797 |
< |
for (int i = 0; i < SIZE; ++i) { |
798 |
< |
q.putFirst(new Integer(i)); |
799 |
< |
++added; |
800 |
< |
} |
801 |
< |
q.putFirst(new Integer(SIZE)); |
802 |
< |
threadShouldThrow(); |
803 |
< |
} catch (InterruptedException ie){ |
804 |
< |
threadAssertEquals(added, SIZE); |
899 |
< |
} |
900 |
< |
}}); |
791 |
> |
public void testBlockingPutFirst() throws InterruptedException { |
792 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
793 |
> |
Thread t = new Thread(new CheckedRunnable() { |
794 |
> |
public void realRun() throws InterruptedException { |
795 |
> |
for (int i = 0; i < SIZE; ++i) |
796 |
> |
q.putFirst(i); |
797 |
> |
assertEquals(SIZE, q.size()); |
798 |
> |
assertEquals(0, q.remainingCapacity()); |
799 |
> |
try { |
800 |
> |
q.putFirst(99); |
801 |
> |
shouldThrow(); |
802 |
> |
} catch (InterruptedException success) {} |
803 |
> |
}}); |
804 |
> |
|
805 |
|
t.start(); |
806 |
< |
try { |
807 |
< |
Thread.sleep(SHORT_DELAY_MS); |
808 |
< |
t.interrupt(); |
809 |
< |
t.join(); |
810 |
< |
} |
907 |
< |
catch (InterruptedException ie) { |
908 |
< |
unexpectedException(); |
909 |
< |
} |
806 |
> |
Thread.sleep(SHORT_DELAY_MS); |
807 |
> |
t.interrupt(); |
808 |
> |
t.join(); |
809 |
> |
assertEquals(SIZE, q.size()); |
810 |
> |
assertEquals(0, q.remainingCapacity()); |
811 |
|
} |
812 |
|
|
813 |
|
/** |
814 |
|
* putFirst blocks waiting for take when full |
815 |
|
*/ |
816 |
< |
public void testPutFirstWithTake() { |
817 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
818 |
< |
Thread t = new Thread(new Runnable() { |
819 |
< |
public void run() { |
820 |
< |
int added = 0; |
821 |
< |
try { |
822 |
< |
q.putFirst(new Object()); |
823 |
< |
++added; |
824 |
< |
q.putFirst(new Object()); |
825 |
< |
++added; |
826 |
< |
q.putFirst(new Object()); |
827 |
< |
++added; |
828 |
< |
q.putFirst(new Object()); |
829 |
< |
++added; |
830 |
< |
threadShouldThrow(); |
831 |
< |
} catch (InterruptedException e){ |
832 |
< |
threadAssertTrue(added >= 2); |
833 |
< |
} |
834 |
< |
} |
835 |
< |
}); |
836 |
< |
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 |
< |
} |
816 |
> |
public void testPutFirstWithTake() throws InterruptedException { |
817 |
> |
final int capacity = 2; |
818 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
819 |
> |
Thread t = new Thread(new CheckedRunnable() { |
820 |
> |
public void realRun() throws InterruptedException { |
821 |
> |
for (int i = 0; i < capacity + 1; i++) |
822 |
> |
q.putFirst(i); |
823 |
> |
try { |
824 |
> |
q.putFirst(99); |
825 |
> |
shouldThrow(); |
826 |
> |
} catch (InterruptedException success) {} |
827 |
> |
}}); |
828 |
> |
|
829 |
> |
t.start(); |
830 |
> |
Thread.sleep(SHORT_DELAY_MS); |
831 |
> |
assertEquals(q.remainingCapacity(), 0); |
832 |
> |
assertEquals(capacity - 1, q.take()); |
833 |
> |
Thread.sleep(SHORT_DELAY_MS); |
834 |
> |
t.interrupt(); |
835 |
> |
t.join(); |
836 |
> |
assertEquals(q.remainingCapacity(), 0); |
837 |
|
} |
838 |
|
|
839 |
|
/** |
840 |
|
* timed offerFirst times out if full and elements not taken |
841 |
|
*/ |
842 |
< |
public void testTimedOfferFirst() { |
842 |
> |
public void testTimedOfferFirst() throws InterruptedException { |
843 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
844 |
< |
Thread t = new Thread(new Runnable() { |
845 |
< |
public void run() { |
846 |
< |
try { |
847 |
< |
q.putFirst(new Object()); |
848 |
< |
q.putFirst(new Object()); |
849 |
< |
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
850 |
< |
q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
851 |
< |
threadShouldThrow(); |
852 |
< |
} catch (InterruptedException success){} |
853 |
< |
} |
961 |
< |
}); |
844 |
> |
Thread t = new Thread(new CheckedRunnable() { |
845 |
> |
public void realRun() throws InterruptedException { |
846 |
> |
q.putFirst(new Object()); |
847 |
> |
q.putFirst(new Object()); |
848 |
> |
assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
849 |
> |
try { |
850 |
> |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
851 |
> |
shouldThrow(); |
852 |
> |
} catch (InterruptedException success) {} |
853 |
> |
}}); |
854 |
|
|
855 |
< |
try { |
856 |
< |
t.start(); |
857 |
< |
Thread.sleep(SMALL_DELAY_MS); |
858 |
< |
t.interrupt(); |
967 |
< |
t.join(); |
968 |
< |
} catch (Exception e){ |
969 |
< |
unexpectedException(); |
970 |
< |
} |
855 |
> |
t.start(); |
856 |
> |
Thread.sleep(SMALL_DELAY_MS); |
857 |
> |
t.interrupt(); |
858 |
> |
t.join(); |
859 |
|
} |
860 |
|
|
861 |
|
/** |
862 |
|
* take retrieves elements in FIFO order |
863 |
|
*/ |
864 |
< |
public void testTakeFirst() { |
865 |
< |
try { |
866 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
867 |
< |
for (int i = 0; i < SIZE; ++i) { |
868 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
981 |
< |
} |
982 |
< |
} catch (InterruptedException e){ |
983 |
< |
unexpectedException(); |
984 |
< |
} |
864 |
> |
public void testTakeFirst() throws InterruptedException { |
865 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
866 |
> |
for (int i = 0; i < SIZE; ++i) { |
867 |
> |
assertEquals(i, q.takeFirst()); |
868 |
> |
} |
869 |
|
} |
870 |
|
|
871 |
|
/** |
872 |
|
* takeFirst blocks interruptibly when empty |
873 |
|
*/ |
874 |
< |
public void testTakeFirstFromEmpty() { |
874 |
> |
public void testTakeFirstFromEmpty() throws InterruptedException { |
875 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
876 |
< |
Thread t = new Thread(new Runnable() { |
877 |
< |
public void run() { |
878 |
< |
try { |
879 |
< |
q.takeFirst(); |
880 |
< |
threadShouldThrow(); |
881 |
< |
} catch (InterruptedException success){ } |
882 |
< |
} |
883 |
< |
}); |
884 |
< |
try { |
1001 |
< |
t.start(); |
1002 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1003 |
< |
t.interrupt(); |
1004 |
< |
t.join(); |
1005 |
< |
} catch (Exception e){ |
1006 |
< |
unexpectedException(); |
1007 |
< |
} |
876 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
877 |
> |
public void realRun() throws InterruptedException { |
878 |
> |
q.takeFirst(); |
879 |
> |
}}; |
880 |
> |
|
881 |
> |
t.start(); |
882 |
> |
Thread.sleep(SHORT_DELAY_MS); |
883 |
> |
t.interrupt(); |
884 |
> |
t.join(); |
885 |
|
} |
886 |
|
|
887 |
|
/** |
888 |
|
* TakeFirst removes existing elements until empty, then blocks interruptibly |
889 |
|
*/ |
890 |
< |
public void testBlockingTakeFirst() { |
891 |
< |
Thread t = new Thread(new Runnable() { |
892 |
< |
public void run() { |
893 |
< |
try { |
894 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
895 |
< |
for (int i = 0; i < SIZE; ++i) { |
896 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
897 |
< |
} |
898 |
< |
q.takeFirst(); |
899 |
< |
threadShouldThrow(); |
900 |
< |
} catch (InterruptedException success){ |
901 |
< |
} |
1025 |
< |
}}); |
890 |
> |
public void testBlockingTakeFirst() throws InterruptedException { |
891 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
892 |
> |
Thread t = new Thread(new CheckedRunnable() { |
893 |
> |
public void realRun() throws InterruptedException { |
894 |
> |
for (int i = 0; i < SIZE; ++i) |
895 |
> |
assertEquals(i, q.takeFirst()); |
896 |
> |
try { |
897 |
> |
q.takeFirst(); |
898 |
> |
shouldThrow(); |
899 |
> |
} catch (InterruptedException success) {} |
900 |
> |
}}); |
901 |
> |
|
902 |
|
t.start(); |
903 |
< |
try { |
904 |
< |
Thread.sleep(SHORT_DELAY_MS); |
905 |
< |
t.interrupt(); |
1030 |
< |
t.join(); |
1031 |
< |
} |
1032 |
< |
catch (InterruptedException ie) { |
1033 |
< |
unexpectedException(); |
1034 |
< |
} |
903 |
> |
Thread.sleep(SHORT_DELAY_MS); |
904 |
> |
t.interrupt(); |
905 |
> |
t.join(); |
906 |
|
} |
907 |
|
|
908 |
|
|
909 |
|
/** |
910 |
|
* timed pollFirst with zero timeout succeeds when non-empty, else times out |
911 |
|
*/ |
912 |
< |
public void testTimedPollFirst0() { |
913 |
< |
try { |
914 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
915 |
< |
for (int i = 0; i < SIZE; ++i) { |
916 |
< |
assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue()); |
917 |
< |
} |
1047 |
< |
assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS)); |
1048 |
< |
} catch (InterruptedException e){ |
1049 |
< |
unexpectedException(); |
1050 |
< |
} |
912 |
> |
public void testTimedPollFirst0() throws InterruptedException { |
913 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
914 |
> |
for (int i = 0; i < SIZE; ++i) { |
915 |
> |
assertEquals(i, q.pollFirst(0, MILLISECONDS)); |
916 |
> |
} |
917 |
> |
assertNull(q.pollFirst(0, MILLISECONDS)); |
918 |
|
} |
919 |
|
|
920 |
|
/** |
921 |
|
* timed pollFirst with nonzero timeout succeeds when non-empty, else times out |
922 |
|
*/ |
923 |
< |
public void testTimedPollFirst() { |
924 |
< |
try { |
925 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
926 |
< |
for (int i = 0; i < SIZE; ++i) { |
927 |
< |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
928 |
< |
} |
1062 |
< |
assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1063 |
< |
} catch (InterruptedException e){ |
1064 |
< |
unexpectedException(); |
1065 |
< |
} |
923 |
> |
public void testTimedPollFirst() throws InterruptedException { |
924 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
925 |
> |
for (int i = 0; i < SIZE; ++i) { |
926 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
927 |
> |
} |
928 |
> |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
929 |
|
} |
930 |
|
|
931 |
|
/** |
932 |
|
* Interrupted timed pollFirst throws InterruptedException instead of |
933 |
|
* returning timeout status |
934 |
|
*/ |
935 |
< |
public void testInterruptedTimedPollFirst() { |
936 |
< |
Thread t = new Thread(new Runnable() { |
937 |
< |
public void run() { |
938 |
< |
try { |
939 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
940 |
< |
for (int i = 0; i < SIZE; ++i) { |
941 |
< |
threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
942 |
< |
} |
943 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
944 |
< |
} catch (InterruptedException success){ |
945 |
< |
} |
946 |
< |
}}); |
935 |
> |
public void testInterruptedTimedPollFirst() throws InterruptedException { |
936 |
> |
Thread t = new Thread(new CheckedRunnable() { |
937 |
> |
public void realRun() throws InterruptedException { |
938 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
939 |
> |
for (int i = 0; i < SIZE; ++i) { |
940 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
941 |
> |
} |
942 |
> |
try { |
943 |
> |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
944 |
> |
shouldThrow(); |
945 |
> |
} catch (InterruptedException success) {} |
946 |
> |
}}); |
947 |
> |
|
948 |
|
t.start(); |
949 |
< |
try { |
950 |
< |
Thread.sleep(SHORT_DELAY_MS); |
951 |
< |
t.interrupt(); |
1088 |
< |
t.join(); |
1089 |
< |
} |
1090 |
< |
catch (InterruptedException ie) { |
1091 |
< |
unexpectedException(); |
1092 |
< |
} |
949 |
> |
Thread.sleep(SHORT_DELAY_MS); |
950 |
> |
t.interrupt(); |
951 |
> |
t.join(); |
952 |
|
} |
953 |
|
|
954 |
|
/** |
955 |
< |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
956 |
< |
* on interruption throws |
955 |
> |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
956 |
> |
* on interruption throws |
957 |
|
*/ |
958 |
< |
public void testTimedPollFirstWithOfferFirst() { |
958 |
> |
public void testTimedPollFirstWithOfferFirst() throws InterruptedException { |
959 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
960 |
< |
Thread t = new Thread(new Runnable() { |
961 |
< |
public void run() { |
962 |
< |
try { |
963 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
964 |
< |
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
965 |
< |
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
966 |
< |
threadShouldThrow(); |
967 |
< |
} catch (InterruptedException success) { } |
968 |
< |
} |
969 |
< |
}); |
970 |
< |
try { |
971 |
< |
t.start(); |
972 |
< |
Thread.sleep(SMALL_DELAY_MS); |
973 |
< |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
974 |
< |
t.interrupt(); |
1116 |
< |
t.join(); |
1117 |
< |
} catch (Exception e){ |
1118 |
< |
unexpectedException(); |
1119 |
< |
} |
960 |
> |
Thread t = new Thread(new CheckedRunnable() { |
961 |
> |
public void realRun() throws InterruptedException { |
962 |
> |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
963 |
> |
assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
964 |
> |
try { |
965 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
966 |
> |
shouldThrow(); |
967 |
> |
} catch (InterruptedException success) {} |
968 |
> |
}}); |
969 |
> |
|
970 |
> |
t.start(); |
971 |
> |
Thread.sleep(SMALL_DELAY_MS); |
972 |
> |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS)); |
973 |
> |
t.interrupt(); |
974 |
> |
t.join(); |
975 |
|
} |
976 |
|
|
977 |
|
/** |
978 |
|
* putLast(null) throws NPE |
979 |
|
*/ |
980 |
< |
public void testPutLastNull() { |
981 |
< |
try { |
980 |
> |
public void testPutLastNull() throws InterruptedException { |
981 |
> |
try { |
982 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
983 |
|
q.putLast(null); |
984 |
|
shouldThrow(); |
985 |
< |
} |
1131 |
< |
catch (NullPointerException success){ |
1132 |
< |
} |
1133 |
< |
catch (InterruptedException ie) { |
1134 |
< |
unexpectedException(); |
1135 |
< |
} |
985 |
> |
} catch (NullPointerException success) {} |
986 |
|
} |
987 |
|
|
988 |
|
/** |
989 |
|
* all elements successfully putLast are contained |
990 |
|
*/ |
991 |
< |
public void testPutLast() { |
992 |
< |
try { |
993 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
994 |
< |
for (int i = 0; i < SIZE; ++i) { |
995 |
< |
Integer I = new Integer(i); |
996 |
< |
q.putLast(I); |
1147 |
< |
assertTrue(q.contains(I)); |
1148 |
< |
} |
1149 |
< |
assertEquals(0, q.remainingCapacity()); |
991 |
> |
public void testPutLast() throws InterruptedException { |
992 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
993 |
> |
for (int i = 0; i < SIZE; ++i) { |
994 |
> |
Integer I = new Integer(i); |
995 |
> |
q.putLast(I); |
996 |
> |
assertTrue(q.contains(I)); |
997 |
|
} |
998 |
< |
catch (InterruptedException ie) { |
1152 |
< |
unexpectedException(); |
1153 |
< |
} |
998 |
> |
assertEquals(0, q.remainingCapacity()); |
999 |
|
} |
1000 |
|
|
1001 |
|
/** |
1002 |
|
* putLast blocks interruptibly if full |
1003 |
|
*/ |
1004 |
< |
public void testBlockingPutLast() { |
1005 |
< |
Thread t = new Thread(new Runnable() { |
1006 |
< |
public void run() { |
1007 |
< |
int added = 0; |
1008 |
< |
try { |
1009 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1010 |
< |
for (int i = 0; i < SIZE; ++i) { |
1011 |
< |
q.putLast(new Integer(i)); |
1012 |
< |
++added; |
1013 |
< |
} |
1014 |
< |
q.putLast(new Integer(SIZE)); |
1015 |
< |
threadShouldThrow(); |
1016 |
< |
} catch (InterruptedException ie){ |
1017 |
< |
threadAssertEquals(added, SIZE); |
1173 |
< |
} |
1174 |
< |
}}); |
1004 |
> |
public void testBlockingPutLast() throws InterruptedException { |
1005 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1006 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1007 |
> |
public void realRun() throws InterruptedException { |
1008 |
> |
for (int i = 0; i < SIZE; ++i) |
1009 |
> |
q.putLast(i); |
1010 |
> |
assertEquals(SIZE, q.size()); |
1011 |
> |
assertEquals(0, q.remainingCapacity()); |
1012 |
> |
try { |
1013 |
> |
q.putLast(99); |
1014 |
> |
shouldThrow(); |
1015 |
> |
} catch (InterruptedException success) {} |
1016 |
> |
}}); |
1017 |
> |
|
1018 |
|
t.start(); |
1019 |
< |
try { |
1020 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1021 |
< |
t.interrupt(); |
1022 |
< |
t.join(); |
1023 |
< |
} |
1181 |
< |
catch (InterruptedException ie) { |
1182 |
< |
unexpectedException(); |
1183 |
< |
} |
1019 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1020 |
> |
t.interrupt(); |
1021 |
> |
t.join(); |
1022 |
> |
assertEquals(SIZE, q.size()); |
1023 |
> |
assertEquals(0, q.remainingCapacity()); |
1024 |
|
} |
1025 |
|
|
1026 |
|
/** |
1027 |
|
* putLast blocks waiting for take when full |
1028 |
|
*/ |
1029 |
< |
public void testPutLastWithTake() { |
1030 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1031 |
< |
Thread t = new Thread(new Runnable() { |
1032 |
< |
public void run() { |
1033 |
< |
int added = 0; |
1034 |
< |
try { |
1035 |
< |
q.putLast(new Object()); |
1036 |
< |
++added; |
1037 |
< |
q.putLast(new Object()); |
1038 |
< |
++added; |
1039 |
< |
q.putLast(new Object()); |
1040 |
< |
++added; |
1041 |
< |
q.putLast(new Object()); |
1042 |
< |
++added; |
1043 |
< |
threadShouldThrow(); |
1044 |
< |
} catch (InterruptedException e){ |
1045 |
< |
threadAssertTrue(added >= 2); |
1046 |
< |
} |
1047 |
< |
} |
1048 |
< |
}); |
1049 |
< |
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 |
< |
} |
1029 |
> |
public void testPutLastWithTake() throws InterruptedException { |
1030 |
> |
final int capacity = 2; |
1031 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
1032 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1033 |
> |
public void realRun() throws InterruptedException { |
1034 |
> |
for (int i = 0; i < capacity + 1; i++) |
1035 |
> |
q.putLast(i); |
1036 |
> |
try { |
1037 |
> |
q.putLast(99); |
1038 |
> |
shouldThrow(); |
1039 |
> |
} catch (InterruptedException success) {} |
1040 |
> |
}}); |
1041 |
> |
|
1042 |
> |
t.start(); |
1043 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1044 |
> |
assertEquals(q.remainingCapacity(), 0); |
1045 |
> |
assertEquals(0, q.take()); |
1046 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1047 |
> |
t.interrupt(); |
1048 |
> |
t.join(); |
1049 |
> |
assertEquals(q.remainingCapacity(), 0); |
1050 |
|
} |
1051 |
|
|
1052 |
|
/** |
1053 |
|
* timed offerLast times out if full and elements not taken |
1054 |
|
*/ |
1055 |
< |
public void testTimedOfferLast() { |
1055 |
> |
public void testTimedOfferLast() throws InterruptedException { |
1056 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1057 |
< |
Thread t = new Thread(new Runnable() { |
1058 |
< |
public void run() { |
1059 |
< |
try { |
1060 |
< |
q.putLast(new Object()); |
1061 |
< |
q.putLast(new Object()); |
1062 |
< |
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1063 |
< |
q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1064 |
< |
threadShouldThrow(); |
1065 |
< |
} catch (InterruptedException success){} |
1066 |
< |
} |
1235 |
< |
}); |
1057 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1058 |
> |
public void realRun() throws InterruptedException { |
1059 |
> |
q.putLast(new Object()); |
1060 |
> |
q.putLast(new Object()); |
1061 |
> |
assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1062 |
> |
try { |
1063 |
> |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1064 |
> |
shouldThrow(); |
1065 |
> |
} catch (InterruptedException success) {} |
1066 |
> |
}}); |
1067 |
|
|
1068 |
< |
try { |
1069 |
< |
t.start(); |
1070 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1071 |
< |
t.interrupt(); |
1241 |
< |
t.join(); |
1242 |
< |
} catch (Exception e){ |
1243 |
< |
unexpectedException(); |
1244 |
< |
} |
1068 |
> |
t.start(); |
1069 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1070 |
> |
t.interrupt(); |
1071 |
> |
t.join(); |
1072 |
|
} |
1073 |
|
|
1074 |
|
/** |
1075 |
|
* takeLast retrieves elements in FIFO order |
1076 |
|
*/ |
1077 |
< |
public void testTakeLast() { |
1078 |
< |
try { |
1079 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1080 |
< |
for (int i = 0; i < SIZE; ++i) { |
1081 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1255 |
< |
} |
1256 |
< |
} catch (InterruptedException e){ |
1257 |
< |
unexpectedException(); |
1258 |
< |
} |
1077 |
> |
public void testTakeLast() throws InterruptedException { |
1078 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1079 |
> |
for (int i = 0; i < SIZE; ++i) { |
1080 |
> |
assertEquals(SIZE-i-1, q.takeLast()); |
1081 |
> |
} |
1082 |
|
} |
1083 |
|
|
1084 |
|
/** |
1085 |
|
* takeLast blocks interruptibly when empty |
1086 |
|
*/ |
1087 |
< |
public void testTakeLastFromEmpty() { |
1087 |
> |
public void testTakeLastFromEmpty() throws InterruptedException { |
1088 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1089 |
< |
Thread t = new Thread(new Runnable() { |
1090 |
< |
public void run() { |
1091 |
< |
try { |
1092 |
< |
q.takeLast(); |
1093 |
< |
threadShouldThrow(); |
1094 |
< |
} catch (InterruptedException success){ } |
1095 |
< |
} |
1096 |
< |
}); |
1097 |
< |
try { |
1275 |
< |
t.start(); |
1276 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1277 |
< |
t.interrupt(); |
1278 |
< |
t.join(); |
1279 |
< |
} catch (Exception e){ |
1280 |
< |
unexpectedException(); |
1281 |
< |
} |
1089 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1090 |
> |
public void realRun() throws InterruptedException { |
1091 |
> |
q.takeLast(); |
1092 |
> |
}}; |
1093 |
> |
|
1094 |
> |
t.start(); |
1095 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1096 |
> |
t.interrupt(); |
1097 |
> |
t.join(); |
1098 |
|
} |
1099 |
|
|
1100 |
|
/** |
1101 |
|
* TakeLast removes existing elements until empty, then blocks interruptibly |
1102 |
|
*/ |
1103 |
< |
public void testBlockingTakeLast() { |
1104 |
< |
Thread t = new Thread(new Runnable() { |
1105 |
< |
public void run() { |
1106 |
< |
try { |
1107 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1108 |
< |
for (int i = 0; i < SIZE; ++i) { |
1109 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1110 |
< |
} |
1111 |
< |
q.takeLast(); |
1112 |
< |
threadShouldThrow(); |
1113 |
< |
} catch (InterruptedException success){ |
1114 |
< |
} |
1299 |
< |
}}); |
1103 |
> |
public void testBlockingTakeLast() throws InterruptedException { |
1104 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1105 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1106 |
> |
public void realRun() throws InterruptedException { |
1107 |
> |
for (int i = 0; i < SIZE; ++i) |
1108 |
> |
assertEquals(SIZE - 1 - i, q.takeLast()); |
1109 |
> |
try { |
1110 |
> |
q.takeLast(); |
1111 |
> |
shouldThrow(); |
1112 |
> |
} catch (InterruptedException success) {} |
1113 |
> |
}}); |
1114 |
> |
|
1115 |
|
t.start(); |
1116 |
< |
try { |
1117 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1118 |
< |
t.interrupt(); |
1304 |
< |
t.join(); |
1305 |
< |
} |
1306 |
< |
catch (InterruptedException ie) { |
1307 |
< |
unexpectedException(); |
1308 |
< |
} |
1116 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1117 |
> |
t.interrupt(); |
1118 |
> |
t.join(); |
1119 |
|
} |
1120 |
|
|
1311 |
– |
|
1121 |
|
/** |
1122 |
|
* timed pollLast with zero timeout succeeds when non-empty, else times out |
1123 |
|
*/ |
1124 |
< |
public void testTimedPollLast0() { |
1125 |
< |
try { |
1126 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1127 |
< |
for (int i = 0; i < SIZE; ++i) { |
1128 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue()); |
1129 |
< |
} |
1321 |
< |
assertNull(q.pollLast(0, TimeUnit.MILLISECONDS)); |
1322 |
< |
} catch (InterruptedException e){ |
1323 |
< |
unexpectedException(); |
1324 |
< |
} |
1124 |
> |
public void testTimedPollLast0() throws InterruptedException { |
1125 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1126 |
> |
for (int i = 0; i < SIZE; ++i) { |
1127 |
> |
assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
1128 |
> |
} |
1129 |
> |
assertNull(q.pollLast(0, MILLISECONDS)); |
1130 |
|
} |
1131 |
|
|
1132 |
|
/** |
1133 |
|
* timed pollLast with nonzero timeout succeeds when non-empty, else times out |
1134 |
|
*/ |
1135 |
< |
public void testTimedPollLast() { |
1136 |
< |
try { |
1137 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1138 |
< |
for (int i = 0; i < SIZE; ++i) { |
1139 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1140 |
< |
} |
1336 |
< |
assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1337 |
< |
} catch (InterruptedException e){ |
1338 |
< |
unexpectedException(); |
1339 |
< |
} |
1135 |
> |
public void testTimedPollLast() throws InterruptedException { |
1136 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1137 |
> |
for (int i = 0; i < SIZE; ++i) { |
1138 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1139 |
> |
} |
1140 |
> |
assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1141 |
|
} |
1142 |
|
|
1143 |
|
/** |
1144 |
|
* Interrupted timed pollLast throws InterruptedException instead of |
1145 |
|
* returning timeout status |
1146 |
|
*/ |
1147 |
< |
public void testInterruptedTimedPollLast() { |
1148 |
< |
Thread t = new Thread(new Runnable() { |
1149 |
< |
public void run() { |
1150 |
< |
try { |
1151 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1152 |
< |
for (int i = 0; i < SIZE; ++i) { |
1153 |
< |
threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1154 |
< |
} |
1155 |
< |
threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1156 |
< |
} catch (InterruptedException success){ |
1157 |
< |
} |
1158 |
< |
}}); |
1147 |
> |
public void testInterruptedTimedPollLast() throws InterruptedException { |
1148 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1149 |
> |
public void realRun() throws InterruptedException { |
1150 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1151 |
> |
for (int i = 0; i < SIZE; ++i) { |
1152 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1153 |
> |
} |
1154 |
> |
try { |
1155 |
> |
q.pollLast(SMALL_DELAY_MS, MILLISECONDS); |
1156 |
> |
shouldThrow(); |
1157 |
> |
} catch (InterruptedException success) {} |
1158 |
> |
}}); |
1159 |
> |
|
1160 |
|
t.start(); |
1161 |
< |
try { |
1162 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1163 |
< |
t.interrupt(); |
1362 |
< |
t.join(); |
1363 |
< |
} |
1364 |
< |
catch (InterruptedException ie) { |
1365 |
< |
unexpectedException(); |
1366 |
< |
} |
1161 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1162 |
> |
t.interrupt(); |
1163 |
> |
t.join(); |
1164 |
|
} |
1165 |
|
|
1166 |
|
/** |
1167 |
< |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1168 |
< |
* on interruption throws |
1167 |
> |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1168 |
> |
* on interruption throws |
1169 |
|
*/ |
1170 |
< |
public void testTimedPollWithOfferLast() { |
1170 |
> |
public void testTimedPollWithOfferLast() throws InterruptedException { |
1171 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1172 |
< |
Thread t = new Thread(new Runnable() { |
1173 |
< |
public void run() { |
1174 |
< |
try { |
1175 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1176 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1177 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1178 |
< |
threadShouldThrow(); |
1179 |
< |
} catch (InterruptedException success) { } |
1180 |
< |
} |
1181 |
< |
}); |
1182 |
< |
try { |
1183 |
< |
t.start(); |
1184 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1185 |
< |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1186 |
< |
t.interrupt(); |
1390 |
< |
t.join(); |
1391 |
< |
} catch (Exception e){ |
1392 |
< |
unexpectedException(); |
1393 |
< |
} |
1172 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1173 |
> |
public void realRun() throws InterruptedException { |
1174 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
1175 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1176 |
> |
try { |
1177 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1178 |
> |
shouldThrow(); |
1179 |
> |
} catch (InterruptedException success) {} |
1180 |
> |
}}); |
1181 |
> |
|
1182 |
> |
t.start(); |
1183 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1184 |
> |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1185 |
> |
t.interrupt(); |
1186 |
> |
t.join(); |
1187 |
|
} |
1188 |
|
|
1189 |
|
|
1193 |
|
public void testElement() { |
1194 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1195 |
|
for (int i = 0; i < SIZE; ++i) { |
1196 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
1196 |
> |
assertEquals(i, q.element()); |
1197 |
|
q.poll(); |
1198 |
|
} |
1199 |
|
try { |
1200 |
|
q.element(); |
1201 |
|
shouldThrow(); |
1202 |
< |
} |
1410 |
< |
catch (NoSuchElementException success) {} |
1202 |
> |
} catch (NoSuchElementException success) {} |
1203 |
|
} |
1204 |
|
|
1205 |
|
/** |
1208 |
|
public void testRemoveElement() { |
1209 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1210 |
|
for (int i = 1; i < SIZE; i+=2) { |
1211 |
< |
assertTrue(q.remove(new Integer(i))); |
1211 |
> |
assertTrue(q.contains(i)); |
1212 |
> |
assertTrue(q.remove(i)); |
1213 |
> |
assertFalse(q.contains(i)); |
1214 |
> |
assertTrue(q.contains(i-1)); |
1215 |
|
} |
1216 |
|
for (int i = 0; i < SIZE; i+=2) { |
1217 |
< |
assertTrue(q.remove(new Integer(i))); |
1218 |
< |
assertFalse(q.remove(new Integer(i+1))); |
1217 |
> |
assertTrue(q.contains(i)); |
1218 |
> |
assertTrue(q.remove(i)); |
1219 |
> |
assertFalse(q.contains(i)); |
1220 |
> |
assertFalse(q.remove(i+1)); |
1221 |
> |
assertFalse(q.contains(i+1)); |
1222 |
|
} |
1223 |
|
assertTrue(q.isEmpty()); |
1224 |
|
} |
1301 |
|
} |
1302 |
|
|
1303 |
|
/** |
1304 |
< |
* toArray contains all elements |
1304 |
> |
* toArray contains all elements in FIFO order |
1305 |
|
*/ |
1306 |
< |
public void testToArray() { |
1306 |
> |
public void testToArray() throws InterruptedException{ |
1307 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1308 |
< |
Object[] o = q.toArray(); |
1309 |
< |
try { |
1310 |
< |
for(int i = 0; i < o.length; i++) |
1513 |
< |
assertEquals(o[i], q.take()); |
1514 |
< |
} catch (InterruptedException e){ |
1515 |
< |
unexpectedException(); |
1516 |
< |
} |
1308 |
> |
Object[] o = q.toArray(); |
1309 |
> |
for (int i = 0; i < o.length; i++) |
1310 |
> |
assertSame(o[i], q.poll()); |
1311 |
|
} |
1312 |
|
|
1313 |
|
/** |
1314 |
< |
* toArray(a) contains all elements |
1314 |
> |
* toArray(a) contains all elements in FIFO order |
1315 |
|
*/ |
1316 |
|
public void testToArray2() { |
1317 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1318 |
< |
Integer[] ints = new Integer[SIZE]; |
1319 |
< |
ints = (Integer[])q.toArray(ints); |
1320 |
< |
try { |
1321 |
< |
for(int i = 0; i < ints.length; i++) |
1322 |
< |
assertEquals(ints[i], q.take()); |
1529 |
< |
} catch (InterruptedException e){ |
1530 |
< |
unexpectedException(); |
1531 |
< |
} |
1317 |
> |
LinkedBlockingDeque<Integer> q = populatedDeque(SIZE); |
1318 |
> |
Integer[] ints = new Integer[SIZE]; |
1319 |
> |
Integer[] array = q.toArray(ints); |
1320 |
> |
assertSame(ints, array); |
1321 |
> |
for (int i = 0; i < ints.length; i++) |
1322 |
> |
assertSame(ints[i], q.remove()); |
1323 |
|
} |
1324 |
|
|
1325 |
|
/** |
1326 |
< |
* toArray(null) throws NPE |
1326 |
> |
* toArray(null) throws NullPointerException |
1327 |
|
*/ |
1328 |
< |
public void testToArray_BadArg() { |
1329 |
< |
try { |
1330 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1331 |
< |
Object o[] = q.toArray(null); |
1332 |
< |
shouldThrow(); |
1333 |
< |
} catch(NullPointerException success){} |
1328 |
> |
public void testToArray_NullArg() { |
1329 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1330 |
> |
try { |
1331 |
> |
q.toArray(null); |
1332 |
> |
shouldThrow(); |
1333 |
> |
} catch (NullPointerException success) {} |
1334 |
|
} |
1335 |
|
|
1336 |
|
/** |
1337 |
< |
* toArray with incompatible array type throws CCE |
1337 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
1338 |
|
*/ |
1339 |
|
public void testToArray1_BadArg() { |
1340 |
< |
try { |
1341 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1342 |
< |
Object o[] = q.toArray(new String[10] ); |
1343 |
< |
shouldThrow(); |
1344 |
< |
} catch(ArrayStoreException success){} |
1340 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1341 |
> |
try { |
1342 |
> |
q.toArray(new String[10]); |
1343 |
> |
shouldThrow(); |
1344 |
> |
} catch (ArrayStoreException success) {} |
1345 |
|
} |
1346 |
|
|
1347 |
|
|
1348 |
|
/** |
1349 |
|
* iterator iterates through all elements |
1350 |
|
*/ |
1351 |
< |
public void testIterator() { |
1351 |
> |
public void testIterator() throws InterruptedException { |
1352 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1353 |
< |
Iterator it = q.iterator(); |
1354 |
< |
try { |
1355 |
< |
while(it.hasNext()){ |
1356 |
< |
assertEquals(it.next(), q.take()); |
1566 |
< |
} |
1567 |
< |
} catch (InterruptedException e){ |
1568 |
< |
unexpectedException(); |
1569 |
< |
} |
1353 |
> |
Iterator it = q.iterator(); |
1354 |
> |
while (it.hasNext()) { |
1355 |
> |
assertEquals(it.next(), q.take()); |
1356 |
> |
} |
1357 |
|
} |
1358 |
|
|
1359 |
|
/** |
1360 |
|
* iterator.remove removes current element |
1361 |
|
*/ |
1362 |
< |
public void testIteratorRemove () { |
1362 |
> |
public void testIteratorRemove() { |
1363 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1364 |
|
q.add(two); |
1365 |
|
q.add(one); |
1370 |
|
it.remove(); |
1371 |
|
|
1372 |
|
it = q.iterator(); |
1373 |
< |
assertEquals(it.next(), one); |
1374 |
< |
assertEquals(it.next(), three); |
1373 |
> |
assertSame(it.next(), one); |
1374 |
> |
assertSame(it.next(), three); |
1375 |
|
assertFalse(it.hasNext()); |
1376 |
|
} |
1377 |
|
|
1387 |
|
assertEquals(0, q.remainingCapacity()); |
1388 |
|
int k = 0; |
1389 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
1390 |
< |
int i = ((Integer)(it.next())).intValue(); |
1604 |
< |
assertEquals(++k, i); |
1390 |
> |
assertEquals(++k, it.next()); |
1391 |
|
} |
1392 |
|
assertEquals(3, k); |
1393 |
|
} |
1395 |
|
/** |
1396 |
|
* Modifications do not cause iterators to fail |
1397 |
|
*/ |
1398 |
< |
public void testWeaklyConsistentIteration () { |
1398 |
> |
public void testWeaklyConsistentIteration() { |
1399 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1400 |
|
q.add(one); |
1401 |
|
q.add(two); |
1402 |
|
q.add(three); |
1403 |
< |
try { |
1404 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
1405 |
< |
q.remove(); |
1620 |
< |
it.next(); |
1621 |
< |
} |
1622 |
< |
} |
1623 |
< |
catch (ConcurrentModificationException e) { |
1624 |
< |
unexpectedException(); |
1403 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
1404 |
> |
q.remove(); |
1405 |
> |
it.next(); |
1406 |
|
} |
1407 |
|
assertEquals(0, q.size()); |
1408 |
|
} |
1409 |
|
|
1410 |
|
|
1411 |
|
/** |
1412 |
< |
* Descending iterator iterates through all elements |
1412 |
> |
* Descending iterator iterates through all elements |
1413 |
|
*/ |
1414 |
|
public void testDescendingIterator() { |
1415 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1416 |
|
int i = 0; |
1417 |
< |
Iterator it = q.descendingIterator(); |
1418 |
< |
while(it.hasNext()) { |
1417 |
> |
Iterator it = q.descendingIterator(); |
1418 |
> |
while (it.hasNext()) { |
1419 |
|
assertTrue(q.contains(it.next())); |
1420 |
|
++i; |
1421 |
|
} |
1423 |
|
assertFalse(it.hasNext()); |
1424 |
|
try { |
1425 |
|
it.next(); |
1426 |
< |
} catch(NoSuchElementException success) { |
1427 |
< |
} |
1426 |
> |
shouldThrow(); |
1427 |
> |
} catch (NoSuchElementException success) {} |
1428 |
|
} |
1429 |
|
|
1430 |
|
/** |
1431 |
< |
* Descending iterator ordering is reverse FIFO |
1431 |
> |
* Descending iterator ordering is reverse FIFO |
1432 |
|
*/ |
1433 |
|
public void testDescendingIteratorOrdering() { |
1434 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1438 |
|
q.add(new Integer(1)); |
1439 |
|
int k = 0; |
1440 |
|
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
1441 |
< |
int i = ((Integer)(it.next())).intValue(); |
1661 |
< |
assertEquals(++k, i); |
1441 |
> |
assertEquals(++k, it.next()); |
1442 |
|
} |
1443 |
|
|
1444 |
|
assertEquals(3, k); |
1451 |
|
/** |
1452 |
|
* descendingIterator.remove removes current element |
1453 |
|
*/ |
1454 |
< |
public void testDescendingIteratorRemove () { |
1454 |
> |
public void testDescendingIteratorRemove() { |
1455 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1456 |
|
for (int iters = 0; iters < 100; ++iters) { |
1457 |
|
q.add(new Integer(3)); |
1491 |
|
q.add(one); |
1492 |
|
q.add(two); |
1493 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1494 |
< |
executor.execute(new Runnable() { |
1495 |
< |
public void run() { |
1496 |
< |
threadAssertFalse(q.offer(three)); |
1497 |
< |
try { |
1498 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
1499 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
1500 |
< |
} |
1501 |
< |
catch (InterruptedException e) { |
1502 |
< |
threadUnexpectedException(); |
1503 |
< |
} |
1504 |
< |
} |
1505 |
< |
}); |
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 |
< |
}); |
1494 |
> |
executor.execute(new CheckedRunnable() { |
1495 |
> |
public void realRun() throws InterruptedException { |
1496 |
> |
assertFalse(q.offer(three)); |
1497 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1498 |
> |
assertEquals(0, q.remainingCapacity()); |
1499 |
> |
}}); |
1500 |
> |
|
1501 |
> |
executor.execute(new CheckedRunnable() { |
1502 |
> |
public void realRun() throws InterruptedException { |
1503 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1504 |
> |
assertSame(one, q.take()); |
1505 |
> |
}}); |
1506 |
|
|
1507 |
|
joinPool(executor); |
1508 |
|
} |
1513 |
|
public void testPollInExecutor() { |
1514 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1515 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1516 |
< |
executor.execute(new Runnable() { |
1517 |
< |
public void run() { |
1518 |
< |
threadAssertNull(q.poll()); |
1519 |
< |
try { |
1520 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
1521 |
< |
threadAssertTrue(q.isEmpty()); |
1522 |
< |
} |
1523 |
< |
catch (InterruptedException e) { |
1524 |
< |
threadUnexpectedException(); |
1525 |
< |
} |
1526 |
< |
} |
1527 |
< |
}); |
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 |
< |
}); |
1516 |
> |
executor.execute(new CheckedRunnable() { |
1517 |
> |
public void realRun() throws InterruptedException { |
1518 |
> |
assertNull(q.poll()); |
1519 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1520 |
> |
assertTrue(q.isEmpty()); |
1521 |
> |
}}); |
1522 |
> |
|
1523 |
> |
executor.execute(new CheckedRunnable() { |
1524 |
> |
public void realRun() throws InterruptedException { |
1525 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1526 |
> |
q.put(one); |
1527 |
> |
}}); |
1528 |
|
|
1529 |
|
joinPool(executor); |
1530 |
|
} |
1532 |
|
/** |
1533 |
|
* A deserialized serialized deque has same elements in same order |
1534 |
|
*/ |
1535 |
< |
public void testSerialization() { |
1535 |
> |
public void testSerialization() throws Exception { |
1536 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1537 |
|
|
1538 |
< |
try { |
1539 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1540 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1541 |
< |
out.writeObject(q); |
1542 |
< |
out.close(); |
1543 |
< |
|
1544 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1545 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1546 |
< |
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1547 |
< |
assertEquals(q.size(), r.size()); |
1548 |
< |
while (!q.isEmpty()) |
1793 |
< |
assertEquals(q.remove(), r.remove()); |
1794 |
< |
} catch(Exception e){ |
1795 |
< |
unexpectedException(); |
1796 |
< |
} |
1538 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1539 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1540 |
> |
out.writeObject(q); |
1541 |
> |
out.close(); |
1542 |
> |
|
1543 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1544 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1545 |
> |
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1546 |
> |
assertEquals(q.size(), r.size()); |
1547 |
> |
while (!q.isEmpty()) |
1548 |
> |
assertEquals(q.remove(), r.remove()); |
1549 |
|
} |
1550 |
|
|
1551 |
|
/** |
1556 |
|
try { |
1557 |
|
q.drainTo(null); |
1558 |
|
shouldThrow(); |
1559 |
< |
} catch(NullPointerException success) { |
1808 |
< |
} |
1559 |
> |
} catch (NullPointerException success) {} |
1560 |
|
} |
1561 |
|
|
1562 |
|
/** |
1567 |
|
try { |
1568 |
|
q.drainTo(q); |
1569 |
|
shouldThrow(); |
1570 |
< |
} catch(IllegalArgumentException success) { |
1820 |
< |
} |
1570 |
> |
} catch (IllegalArgumentException success) {} |
1571 |
|
} |
1572 |
|
|
1573 |
|
/** |
1597 |
|
/** |
1598 |
|
* drainTo empties full deque, unblocking a waiting put. |
1599 |
|
*/ |
1600 |
< |
public void testDrainToWithActivePut() { |
1600 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
1601 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1602 |
< |
Thread t = new Thread(new Runnable() { |
1603 |
< |
public void run() { |
1604 |
< |
try { |
1605 |
< |
q.put(new Integer(SIZE+1)); |
1606 |
< |
} catch (InterruptedException ie){ |
1607 |
< |
threadUnexpectedException(); |
1608 |
< |
} |
1609 |
< |
} |
1610 |
< |
}); |
1611 |
< |
try { |
1612 |
< |
t.start(); |
1613 |
< |
ArrayList l = new ArrayList(); |
1614 |
< |
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 |
< |
} |
1602 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1603 |
> |
public void realRun() throws InterruptedException { |
1604 |
> |
q.put(new Integer(SIZE+1)); |
1605 |
> |
}}); |
1606 |
> |
|
1607 |
> |
t.start(); |
1608 |
> |
ArrayList l = new ArrayList(); |
1609 |
> |
q.drainTo(l); |
1610 |
> |
assertTrue(l.size() >= SIZE); |
1611 |
> |
for (int i = 0; i < SIZE; ++i) |
1612 |
> |
assertEquals(l.get(i), new Integer(i)); |
1613 |
> |
t.join(); |
1614 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
1615 |
|
} |
1616 |
|
|
1617 |
|
/** |
1622 |
|
try { |
1623 |
|
q.drainTo(null, 0); |
1624 |
|
shouldThrow(); |
1625 |
< |
} catch(NullPointerException success) { |
1884 |
< |
} |
1625 |
> |
} catch (NullPointerException success) {} |
1626 |
|
} |
1627 |
|
|
1628 |
|
/** |
1633 |
|
try { |
1634 |
|
q.drainTo(q, 0); |
1635 |
|
shouldThrow(); |
1636 |
< |
} catch(IllegalArgumentException success) { |
1896 |
< |
} |
1636 |
> |
} catch (IllegalArgumentException success) {} |
1637 |
|
} |
1638 |
|
|
1639 |
|
/** |
1640 |
< |
* drainTo(c, n) empties first max {n, size} elements of deque into c |
1640 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
1641 |
|
*/ |
1642 |
|
public void testDrainToN() { |
1643 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1644 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
1645 |
< |
for(int j = 0; j < SIZE; j++) |
1645 |
> |
for (int j = 0; j < SIZE; j++) |
1646 |
|
assertTrue(q.offer(new Integer(j))); |
1647 |
|
ArrayList l = new ArrayList(); |
1648 |
|
q.drainTo(l, i); |
1649 |
< |
int k = (i < SIZE)? i : SIZE; |
1649 |
> |
int k = (i < SIZE) ? i : SIZE; |
1650 |
|
assertEquals(l.size(), k); |
1651 |
|
assertEquals(q.size(), SIZE-k); |
1652 |
|
for (int j = 0; j < k; ++j) |