1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
8 |
|
import java.util.*; |
9 |
|
import java.util.concurrent.*; |
10 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
|
import java.io.*; |
12 |
|
|
13 |
|
public class LinkedBlockingDequeTest extends JSR166TestCase { |
14 |
+ |
|
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 |
|
/** |
94 |
< |
* OfferFirst succeeds |
94 |
> |
* OfferFirst succeeds |
95 |
|
*/ |
96 |
|
public void testOfferFirst() { |
97 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
100 |
|
} |
101 |
|
|
102 |
|
/** |
103 |
< |
* OfferLast succeeds |
103 |
> |
* OfferLast succeeds |
104 |
|
*/ |
105 |
|
public void testOfferLast() { |
106 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
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 |
< |
* remove removes next element, or throws NSEE if empty |
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 |
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()); |
289 |
< |
} |
287 |
> |
q.addFirst(four); |
288 |
> |
assertSame(four, q.peekFirst()); |
289 |
> |
} |
290 |
|
|
291 |
|
/** |
292 |
|
* peekLast returns element inserted with addLast |
294 |
|
public void testAddLast() { |
295 |
|
LinkedBlockingDeque q = populatedDeque(3); |
296 |
|
q.pollLast(); |
297 |
< |
q.addLast(four); |
298 |
< |
assertEquals(four,q.peekLast()); |
299 |
< |
} |
271 |
< |
|
297 |
> |
q.addLast(four); |
298 |
> |
assertSame(four, q.peekLast()); |
299 |
> |
} |
300 |
|
|
301 |
|
/** |
302 |
|
* A new deque has the indicated capacity, or Integer.MAX_VALUE if |
308 |
|
} |
309 |
|
|
310 |
|
/** |
311 |
< |
* Constructor throws IAE if capacity argument nonpositive |
311 |
> |
* Constructor throws IAE if capacity argument nonpositive |
312 |
|
*/ |
313 |
|
public void testConstructor2() { |
314 |
|
try { |
315 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(0); |
316 |
|
shouldThrow(); |
317 |
< |
} |
290 |
< |
catch (IllegalArgumentException success) {} |
317 |
> |
} catch (IllegalArgumentException success) {} |
318 |
|
} |
319 |
|
|
320 |
|
/** |
324 |
|
try { |
325 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(null); |
326 |
|
shouldThrow(); |
327 |
< |
} |
301 |
< |
catch (NullPointerException success) {} |
327 |
> |
} catch (NullPointerException success) {} |
328 |
|
} |
329 |
|
|
330 |
|
/** |
335 |
|
Integer[] ints = new Integer[SIZE]; |
336 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
337 |
|
shouldThrow(); |
338 |
< |
} |
313 |
< |
catch (NullPointerException success) {} |
338 |
> |
} catch (NullPointerException success) {} |
339 |
|
} |
340 |
|
|
341 |
|
/** |
348 |
|
ints[i] = new Integer(i); |
349 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
350 |
|
shouldThrow(); |
351 |
< |
} |
327 |
< |
catch (NullPointerException success) {} |
351 |
> |
} catch (NullPointerException success) {} |
352 |
|
} |
353 |
|
|
354 |
|
/** |
355 |
|
* Deque contains all elements of collection used to initialize |
356 |
|
*/ |
357 |
|
public void testConstructor6() { |
358 |
< |
try { |
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) |
340 |
< |
assertEquals(ints[i], q.poll()); |
341 |
< |
} |
342 |
< |
finally {} |
358 |
> |
Integer[] ints = new Integer[SIZE]; |
359 |
> |
for (int i = 0; i < SIZE; ++i) |
360 |
> |
ints[i] = new Integer(i); |
361 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
362 |
> |
for (int i = 0; i < SIZE; ++i) |
363 |
> |
assertEquals(ints[i], q.poll()); |
364 |
|
} |
365 |
|
|
366 |
|
/** |
399 |
|
* offer(null) throws NPE |
400 |
|
*/ |
401 |
|
public void testOfferNull() { |
402 |
< |
try { |
402 |
> |
try { |
403 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
404 |
|
q.offer(null); |
405 |
|
shouldThrow(); |
406 |
< |
} catch (NullPointerException success) { } |
406 |
> |
} catch (NullPointerException success) {} |
407 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
|
* add(null) throws NPE |
411 |
|
*/ |
412 |
|
public void testAddNull() { |
413 |
< |
try { |
413 |
> |
try { |
414 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
415 |
|
q.add(null); |
416 |
|
shouldThrow(); |
417 |
< |
} catch (NullPointerException success) { } |
417 |
> |
} catch (NullPointerException success) {} |
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
|
* push(null) throws NPE |
422 |
|
*/ |
423 |
|
public void testPushNull() { |
424 |
< |
try { |
424 |
> |
try { |
425 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
426 |
|
q.push(null); |
427 |
|
shouldThrow(); |
428 |
< |
} catch (NullPointerException success) { } |
428 |
> |
} catch (NullPointerException success) {} |
429 |
|
} |
430 |
|
|
431 |
|
/** |
432 |
|
* push succeeds if not full; throws ISE if full |
433 |
|
*/ |
434 |
|
public void testPush() { |
435 |
< |
try { |
435 |
> |
try { |
436 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
437 |
|
for (int i = 0; i < SIZE; ++i) { |
438 |
|
Integer I = new Integer(i); |
441 |
|
} |
442 |
|
assertEquals(0, q.remainingCapacity()); |
443 |
|
q.push(new Integer(SIZE)); |
444 |
< |
} catch (IllegalStateException success){ |
445 |
< |
} |
444 |
> |
shouldThrow(); |
445 |
> |
} catch (IllegalStateException success) {} |
446 |
|
} |
447 |
|
|
448 |
|
/** |
451 |
|
public void testPushWithPeek() { |
452 |
|
LinkedBlockingDeque q = populatedDeque(3); |
453 |
|
q.pollLast(); |
454 |
< |
q.push(four); |
455 |
< |
assertEquals(four,q.peekFirst()); |
456 |
< |
} |
436 |
< |
|
454 |
> |
q.push(four); |
455 |
> |
assertSame(four, q.peekFirst()); |
456 |
> |
} |
457 |
|
|
458 |
|
/** |
459 |
< |
* pop removes next element, or throws NSEE if empty |
459 |
> |
* pop removes next element, or throws NSEE if empty |
460 |
|
*/ |
461 |
|
public void testPop() { |
462 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
463 |
|
for (int i = 0; i < SIZE; ++i) { |
464 |
< |
assertEquals(i, ((Integer)q.pop()).intValue()); |
464 |
> |
assertEquals(i, q.pop()); |
465 |
|
} |
466 |
|
try { |
467 |
|
q.pop(); |
468 |
|
shouldThrow(); |
469 |
< |
} catch (NoSuchElementException success){ |
450 |
< |
} |
469 |
> |
} catch (NoSuchElementException success) {} |
470 |
|
} |
471 |
|
|
453 |
– |
|
472 |
|
/** |
473 |
|
* Offer succeeds if not full; fails if full |
474 |
|
*/ |
482 |
|
* add succeeds if not full; throws ISE if full |
483 |
|
*/ |
484 |
|
public void testAdd() { |
485 |
< |
try { |
485 |
> |
try { |
486 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
487 |
|
for (int i = 0; i < SIZE; ++i) { |
488 |
|
assertTrue(q.add(new Integer(i))); |
489 |
|
} |
490 |
|
assertEquals(0, q.remainingCapacity()); |
491 |
|
q.add(new Integer(SIZE)); |
492 |
< |
} catch (IllegalStateException success){ |
493 |
< |
} |
492 |
> |
shouldThrow(); |
493 |
> |
} catch (IllegalStateException success) {} |
494 |
|
} |
495 |
|
|
496 |
|
/** |
501 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
502 |
|
q.addAll(null); |
503 |
|
shouldThrow(); |
504 |
< |
} |
487 |
< |
catch (NullPointerException success) {} |
504 |
> |
} catch (NullPointerException success) {} |
505 |
|
} |
506 |
|
|
507 |
|
/** |
512 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
513 |
|
q.addAll(q); |
514 |
|
shouldThrow(); |
515 |
< |
} |
499 |
< |
catch (IllegalArgumentException success) {} |
515 |
> |
} catch (IllegalArgumentException success) {} |
516 |
|
} |
517 |
|
|
518 |
|
/** |
524 |
|
Integer[] ints = new Integer[SIZE]; |
525 |
|
q.addAll(Arrays.asList(ints)); |
526 |
|
shouldThrow(); |
527 |
< |
} |
512 |
< |
catch (NullPointerException success) {} |
527 |
> |
} catch (NullPointerException success) {} |
528 |
|
} |
529 |
+ |
|
530 |
|
/** |
531 |
|
* addAll of a collection with any null elements throws NPE after |
532 |
|
* possibly adding some elements |
539 |
|
ints[i] = new Integer(i); |
540 |
|
q.addAll(Arrays.asList(ints)); |
541 |
|
shouldThrow(); |
542 |
< |
} |
527 |
< |
catch (NullPointerException success) {} |
542 |
> |
} catch (NullPointerException success) {} |
543 |
|
} |
544 |
+ |
|
545 |
|
/** |
546 |
|
* addAll throws ISE if not enough room |
547 |
|
*/ |
553 |
|
ints[i] = new Integer(i); |
554 |
|
q.addAll(Arrays.asList(ints)); |
555 |
|
shouldThrow(); |
556 |
< |
} |
541 |
< |
catch (IllegalStateException success) {} |
556 |
> |
} catch (IllegalStateException success) {} |
557 |
|
} |
558 |
+ |
|
559 |
|
/** |
560 |
|
* Deque contains all elements, in traversal order, of successful addAll |
561 |
|
*/ |
562 |
|
public void testAddAll5() { |
563 |
< |
try { |
564 |
< |
Integer[] empty = new Integer[0]; |
565 |
< |
Integer[] ints = new Integer[SIZE]; |
566 |
< |
for (int i = 0; i < SIZE; ++i) |
567 |
< |
ints[i] = new Integer(i); |
568 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
569 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
570 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
571 |
< |
for (int i = 0; i < SIZE; ++i) |
556 |
< |
assertEquals(ints[i], q.poll()); |
557 |
< |
} |
558 |
< |
finally {} |
563 |
> |
Integer[] empty = new Integer[0]; |
564 |
> |
Integer[] ints = new Integer[SIZE]; |
565 |
> |
for (int i = 0; i < SIZE; ++i) |
566 |
> |
ints[i] = new Integer(i); |
567 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
568 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
569 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
570 |
> |
for (int i = 0; i < SIZE; ++i) |
571 |
> |
assertEquals(ints[i], q.poll()); |
572 |
|
} |
573 |
|
|
561 |
– |
|
574 |
|
/** |
575 |
|
* put(null) throws NPE |
576 |
|
*/ |
577 |
< |
public void testPutNull() { |
578 |
< |
try { |
577 |
> |
public void testPutNull() throws InterruptedException { |
578 |
> |
try { |
579 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
580 |
|
q.put(null); |
581 |
|
shouldThrow(); |
582 |
< |
} |
583 |
< |
catch (NullPointerException success){ |
572 |
< |
} |
573 |
< |
catch (InterruptedException ie) { |
574 |
< |
unexpectedException(); |
575 |
< |
} |
576 |
< |
} |
582 |
> |
} catch (NullPointerException success) {} |
583 |
> |
} |
584 |
|
|
585 |
|
/** |
586 |
|
* all elements successfully put are contained |
587 |
|
*/ |
588 |
< |
public void testPut() { |
589 |
< |
try { |
590 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
591 |
< |
for (int i = 0; i < SIZE; ++i) { |
592 |
< |
Integer I = new Integer(i); |
593 |
< |
q.put(I); |
587 |
< |
assertTrue(q.contains(I)); |
588 |
< |
} |
589 |
< |
assertEquals(0, q.remainingCapacity()); |
590 |
< |
} |
591 |
< |
catch (InterruptedException ie) { |
592 |
< |
unexpectedException(); |
588 |
> |
public void testPut() throws InterruptedException { |
589 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
590 |
> |
for (int i = 0; i < SIZE; ++i) { |
591 |
> |
Integer I = new Integer(i); |
592 |
> |
q.put(I); |
593 |
> |
assertTrue(q.contains(I)); |
594 |
|
} |
595 |
+ |
assertEquals(0, q.remainingCapacity()); |
596 |
|
} |
597 |
|
|
598 |
|
/** |
599 |
|
* put blocks interruptibly if full |
600 |
|
*/ |
601 |
< |
public void testBlockingPut() { |
602 |
< |
Thread t = new Thread(new Runnable() { |
603 |
< |
public void run() { |
604 |
< |
int added = 0; |
605 |
< |
try { |
606 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
607 |
< |
for (int i = 0; i < SIZE; ++i) { |
608 |
< |
q.put(new Integer(i)); |
609 |
< |
++added; |
610 |
< |
} |
611 |
< |
q.put(new Integer(SIZE)); |
612 |
< |
threadShouldThrow(); |
613 |
< |
} catch (InterruptedException ie){ |
614 |
< |
threadAssertEquals(added, SIZE); |
615 |
< |
} |
616 |
< |
}}); |
617 |
< |
t.start(); |
618 |
< |
try { |
619 |
< |
Thread.sleep(SHORT_DELAY_MS); |
620 |
< |
t.interrupt(); |
621 |
< |
t.join(); |
622 |
< |
} |
623 |
< |
catch (InterruptedException ie) { |
624 |
< |
unexpectedException(); |
625 |
< |
} |
601 |
> |
public void testBlockingPut() throws InterruptedException { |
602 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
603 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
604 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
605 |
> |
public void realRun() throws InterruptedException { |
606 |
> |
for (int i = 0; i < SIZE; ++i) |
607 |
> |
q.put(i); |
608 |
> |
assertEquals(SIZE, q.size()); |
609 |
> |
assertEquals(0, q.remainingCapacity()); |
610 |
> |
|
611 |
> |
Thread.currentThread().interrupt(); |
612 |
> |
try { |
613 |
> |
q.put(99); |
614 |
> |
shouldThrow(); |
615 |
> |
} catch (InterruptedException success) {} |
616 |
> |
assertFalse(Thread.interrupted()); |
617 |
> |
|
618 |
> |
pleaseInterrupt.countDown(); |
619 |
> |
try { |
620 |
> |
q.put(99); |
621 |
> |
shouldThrow(); |
622 |
> |
} catch (InterruptedException success) {} |
623 |
> |
assertFalse(Thread.interrupted()); |
624 |
> |
}}); |
625 |
> |
|
626 |
> |
await(pleaseInterrupt); |
627 |
> |
assertThreadStaysAlive(t); |
628 |
> |
t.interrupt(); |
629 |
> |
awaitTermination(t); |
630 |
> |
assertEquals(SIZE, q.size()); |
631 |
> |
assertEquals(0, q.remainingCapacity()); |
632 |
|
} |
633 |
|
|
634 |
|
/** |
635 |
< |
* put blocks waiting for take when full |
635 |
> |
* put blocks interruptibly waiting for take when full |
636 |
|
*/ |
637 |
< |
public void testPutWithTake() { |
638 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
639 |
< |
Thread t = new Thread(new Runnable() { |
640 |
< |
public void run() { |
641 |
< |
int added = 0; |
642 |
< |
try { |
643 |
< |
q.put(new Object()); |
644 |
< |
++added; |
645 |
< |
q.put(new Object()); |
646 |
< |
++added; |
647 |
< |
q.put(new Object()); |
648 |
< |
++added; |
649 |
< |
q.put(new Object()); |
650 |
< |
++added; |
651 |
< |
threadShouldThrow(); |
652 |
< |
} catch (InterruptedException e){ |
653 |
< |
threadAssertTrue(added >= 2); |
654 |
< |
} |
655 |
< |
} |
656 |
< |
}); |
657 |
< |
try { |
658 |
< |
t.start(); |
659 |
< |
Thread.sleep(SHORT_DELAY_MS); |
660 |
< |
q.take(); |
661 |
< |
t.interrupt(); |
662 |
< |
t.join(); |
663 |
< |
} catch (Exception e){ |
664 |
< |
unexpectedException(); |
665 |
< |
} |
637 |
> |
public void testPutWithTake() throws InterruptedException { |
638 |
> |
final int capacity = 2; |
639 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
640 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
641 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
642 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
643 |
> |
public void realRun() throws InterruptedException { |
644 |
> |
for (int i = 0; i < capacity; i++) |
645 |
> |
q.put(i); |
646 |
> |
pleaseTake.countDown(); |
647 |
> |
q.put(86); |
648 |
> |
|
649 |
> |
pleaseInterrupt.countDown(); |
650 |
> |
try { |
651 |
> |
q.put(99); |
652 |
> |
shouldThrow(); |
653 |
> |
} catch (InterruptedException success) {} |
654 |
> |
assertFalse(Thread.interrupted()); |
655 |
> |
}}); |
656 |
> |
|
657 |
> |
await(pleaseTake); |
658 |
> |
assertEquals(q.remainingCapacity(), 0); |
659 |
> |
assertEquals(0, q.take()); |
660 |
> |
|
661 |
> |
await(pleaseInterrupt); |
662 |
> |
assertThreadStaysAlive(t); |
663 |
> |
t.interrupt(); |
664 |
> |
awaitTermination(t); |
665 |
> |
assertEquals(q.remainingCapacity(), 0); |
666 |
|
} |
667 |
|
|
668 |
|
/** |
669 |
|
* timed offer times out if full and elements not taken |
670 |
|
*/ |
671 |
< |
public void testTimedOffer() { |
671 |
> |
public void testTimedOffer() throws InterruptedException { |
672 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
673 |
< |
Thread t = new Thread(new Runnable() { |
674 |
< |
public void run() { |
675 |
< |
try { |
676 |
< |
q.put(new Object()); |
677 |
< |
q.put(new Object()); |
678 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
679 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
680 |
< |
threadShouldThrow(); |
681 |
< |
} catch (InterruptedException success){} |
682 |
< |
} |
683 |
< |
}); |
684 |
< |
|
685 |
< |
try { |
686 |
< |
t.start(); |
687 |
< |
Thread.sleep(SMALL_DELAY_MS); |
688 |
< |
t.interrupt(); |
689 |
< |
t.join(); |
690 |
< |
} catch (Exception e){ |
691 |
< |
unexpectedException(); |
684 |
< |
} |
673 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
674 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
675 |
> |
public void realRun() throws InterruptedException { |
676 |
> |
q.put(new Object()); |
677 |
> |
q.put(new Object()); |
678 |
> |
long startTime = System.nanoTime(); |
679 |
> |
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
680 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
681 |
> |
pleaseInterrupt.countDown(); |
682 |
> |
try { |
683 |
> |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
684 |
> |
shouldThrow(); |
685 |
> |
} catch (InterruptedException success) {} |
686 |
> |
}}); |
687 |
> |
|
688 |
> |
await(pleaseInterrupt); |
689 |
> |
assertThreadStaysAlive(t); |
690 |
> |
t.interrupt(); |
691 |
> |
awaitTermination(t); |
692 |
|
} |
693 |
|
|
694 |
|
/** |
695 |
|
* take retrieves elements in FIFO order |
696 |
|
*/ |
697 |
< |
public void testTake() { |
698 |
< |
try { |
699 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
700 |
< |
for (int i = 0; i < SIZE; ++i) { |
701 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
695 |
< |
} |
696 |
< |
} catch (InterruptedException e){ |
697 |
< |
unexpectedException(); |
698 |
< |
} |
697 |
> |
public void testTake() throws InterruptedException { |
698 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
699 |
> |
for (int i = 0; i < SIZE; ++i) { |
700 |
> |
assertEquals(i, q.take()); |
701 |
> |
} |
702 |
|
} |
703 |
|
|
704 |
|
/** |
705 |
< |
* take blocks interruptibly when empty |
705 |
> |
* take removes existing elements until empty, then blocks interruptibly |
706 |
|
*/ |
707 |
< |
public void testTakeFromEmpty() { |
708 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
709 |
< |
Thread t = new Thread(new Runnable() { |
710 |
< |
public void run() { |
711 |
< |
try { |
712 |
< |
q.take(); |
713 |
< |
threadShouldThrow(); |
711 |
< |
} catch (InterruptedException success){ } |
707 |
> |
public void testBlockingTake() throws InterruptedException { |
708 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
709 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
710 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
711 |
> |
public void realRun() throws InterruptedException { |
712 |
> |
for (int i = 0; i < SIZE; ++i) { |
713 |
> |
assertEquals(i, q.take()); |
714 |
|
} |
713 |
– |
}); |
714 |
– |
try { |
715 |
– |
t.start(); |
716 |
– |
Thread.sleep(SHORT_DELAY_MS); |
717 |
– |
t.interrupt(); |
718 |
– |
t.join(); |
719 |
– |
} catch (Exception e){ |
720 |
– |
unexpectedException(); |
721 |
– |
} |
722 |
– |
} |
715 |
|
|
716 |
< |
/** |
717 |
< |
* Take removes existing elements until empty, then blocks interruptibly |
718 |
< |
*/ |
719 |
< |
public void testBlockingTake() { |
720 |
< |
Thread t = new Thread(new Runnable() { |
721 |
< |
public void run() { |
730 |
< |
try { |
731 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
732 |
< |
for (int i = 0; i < SIZE; ++i) { |
733 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
734 |
< |
} |
735 |
< |
q.take(); |
736 |
< |
threadShouldThrow(); |
737 |
< |
} catch (InterruptedException success){ |
738 |
< |
} |
739 |
< |
}}); |
740 |
< |
t.start(); |
741 |
< |
try { |
742 |
< |
Thread.sleep(SHORT_DELAY_MS); |
743 |
< |
t.interrupt(); |
744 |
< |
t.join(); |
745 |
< |
} |
746 |
< |
catch (InterruptedException ie) { |
747 |
< |
unexpectedException(); |
748 |
< |
} |
749 |
< |
} |
716 |
> |
Thread.currentThread().interrupt(); |
717 |
> |
try { |
718 |
> |
q.take(); |
719 |
> |
shouldThrow(); |
720 |
> |
} catch (InterruptedException success) {} |
721 |
> |
assertFalse(Thread.interrupted()); |
722 |
|
|
723 |
+ |
pleaseInterrupt.countDown(); |
724 |
+ |
try { |
725 |
+ |
q.take(); |
726 |
+ |
shouldThrow(); |
727 |
+ |
} catch (InterruptedException success) {} |
728 |
+ |
assertFalse(Thread.interrupted()); |
729 |
+ |
}}); |
730 |
+ |
|
731 |
+ |
await(pleaseInterrupt); |
732 |
+ |
assertThreadStaysAlive(t); |
733 |
+ |
t.interrupt(); |
734 |
+ |
awaitTermination(t); |
735 |
+ |
} |
736 |
|
|
737 |
|
/** |
738 |
|
* poll succeeds unless empty |
740 |
|
public void testPoll() { |
741 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
742 |
|
for (int i = 0; i < SIZE; ++i) { |
743 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
743 |
> |
assertEquals(i, q.poll()); |
744 |
|
} |
745 |
< |
assertNull(q.poll()); |
745 |
> |
assertNull(q.poll()); |
746 |
|
} |
747 |
|
|
748 |
|
/** |
749 |
|
* timed poll with zero timeout succeeds when non-empty, else times out |
750 |
|
*/ |
751 |
< |
public void testTimedPoll0() { |
752 |
< |
try { |
753 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
754 |
< |
for (int i = 0; i < SIZE; ++i) { |
755 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
756 |
< |
} |
772 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
773 |
< |
} catch (InterruptedException e){ |
774 |
< |
unexpectedException(); |
775 |
< |
} |
751 |
> |
public void testTimedPoll0() throws InterruptedException { |
752 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
753 |
> |
for (int i = 0; i < SIZE; ++i) { |
754 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
755 |
> |
} |
756 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
757 |
|
} |
758 |
|
|
759 |
|
/** |
760 |
|
* timed poll with nonzero timeout succeeds when non-empty, else times out |
761 |
|
*/ |
762 |
< |
public void testTimedPoll() { |
763 |
< |
try { |
764 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
765 |
< |
for (int i = 0; i < SIZE; ++i) { |
766 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
767 |
< |
} |
768 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
769 |
< |
} catch (InterruptedException e){ |
770 |
< |
unexpectedException(); |
771 |
< |
} |
762 |
> |
public void testTimedPoll() throws InterruptedException { |
763 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
764 |
> |
for (int i = 0; i < SIZE; ++i) { |
765 |
> |
long startTime = System.nanoTime(); |
766 |
> |
assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
767 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
768 |
> |
} |
769 |
> |
long startTime = System.nanoTime(); |
770 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
771 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
772 |
> |
checkEmpty(q); |
773 |
|
} |
774 |
|
|
775 |
|
/** |
776 |
|
* Interrupted timed poll throws InterruptedException instead of |
777 |
|
* returning timeout status |
778 |
|
*/ |
779 |
< |
public void testInterruptedTimedPoll() { |
780 |
< |
Thread t = new Thread(new Runnable() { |
781 |
< |
public void run() { |
782 |
< |
try { |
783 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
784 |
< |
for (int i = 0; i < SIZE; ++i) { |
785 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
786 |
< |
} |
787 |
< |
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) { } |
779 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
780 |
> |
final BlockingQueue<Integer> q = populatedDeque(SIZE); |
781 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
782 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
783 |
> |
public void realRun() throws InterruptedException { |
784 |
> |
for (int i = 0; i < SIZE; ++i) { |
785 |
> |
long t0 = System.nanoTime(); |
786 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
787 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
788 |
|
} |
789 |
< |
}); |
790 |
< |
try { |
791 |
< |
t.start(); |
792 |
< |
Thread.sleep(SMALL_DELAY_MS); |
793 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
794 |
< |
t.interrupt(); |
795 |
< |
t.join(); |
796 |
< |
} catch (Exception e){ |
797 |
< |
unexpectedException(); |
798 |
< |
} |
799 |
< |
} |
800 |
< |
|
789 |
> |
long t0 = System.nanoTime(); |
790 |
> |
aboutToWait.countDown(); |
791 |
> |
try { |
792 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
793 |
> |
shouldThrow(); |
794 |
> |
} catch (InterruptedException success) { |
795 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
796 |
> |
} |
797 |
> |
}}); |
798 |
> |
|
799 |
> |
aboutToWait.await(); |
800 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
801 |
> |
t.interrupt(); |
802 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
803 |
> |
checkEmpty(q); |
804 |
> |
} |
805 |
|
|
806 |
|
/** |
807 |
|
* putFirst(null) throws NPE |
808 |
|
*/ |
809 |
< |
public void testPutFirstNull() { |
810 |
< |
try { |
809 |
> |
public void testPutFirstNull() throws InterruptedException { |
810 |
> |
try { |
811 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
812 |
|
q.putFirst(null); |
813 |
|
shouldThrow(); |
814 |
< |
} |
815 |
< |
catch (NullPointerException success){ |
858 |
< |
} |
859 |
< |
catch (InterruptedException ie) { |
860 |
< |
unexpectedException(); |
861 |
< |
} |
862 |
< |
} |
814 |
> |
} catch (NullPointerException success) {} |
815 |
> |
} |
816 |
|
|
817 |
|
/** |
818 |
|
* all elements successfully putFirst are contained |
819 |
|
*/ |
820 |
< |
public void testPutFirst() { |
821 |
< |
try { |
822 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
823 |
< |
for (int i = 0; i < SIZE; ++i) { |
824 |
< |
Integer I = new Integer(i); |
825 |
< |
q.putFirst(I); |
873 |
< |
assertTrue(q.contains(I)); |
874 |
< |
} |
875 |
< |
assertEquals(0, q.remainingCapacity()); |
876 |
< |
} |
877 |
< |
catch (InterruptedException ie) { |
878 |
< |
unexpectedException(); |
820 |
> |
public void testPutFirst() throws InterruptedException { |
821 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
822 |
> |
for (int i = 0; i < SIZE; ++i) { |
823 |
> |
Integer I = new Integer(i); |
824 |
> |
q.putFirst(I); |
825 |
> |
assertTrue(q.contains(I)); |
826 |
|
} |
827 |
+ |
assertEquals(0, q.remainingCapacity()); |
828 |
|
} |
829 |
|
|
830 |
|
/** |
831 |
|
* putFirst blocks interruptibly if full |
832 |
|
*/ |
833 |
< |
public void testBlockingPutFirst() { |
834 |
< |
Thread t = new Thread(new Runnable() { |
835 |
< |
public void run() { |
836 |
< |
int added = 0; |
837 |
< |
try { |
838 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
839 |
< |
for (int i = 0; i < SIZE; ++i) { |
840 |
< |
q.putFirst(new Integer(i)); |
841 |
< |
++added; |
842 |
< |
} |
843 |
< |
q.putFirst(new Integer(SIZE)); |
844 |
< |
threadShouldThrow(); |
845 |
< |
} catch (InterruptedException ie){ |
846 |
< |
threadAssertEquals(added, SIZE); |
847 |
< |
} |
848 |
< |
}}); |
849 |
< |
t.start(); |
850 |
< |
try { |
851 |
< |
Thread.sleep(SHORT_DELAY_MS); |
852 |
< |
t.interrupt(); |
853 |
< |
t.join(); |
854 |
< |
} |
855 |
< |
catch (InterruptedException ie) { |
856 |
< |
unexpectedException(); |
857 |
< |
} |
833 |
> |
public void testBlockingPutFirst() throws InterruptedException { |
834 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
835 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
836 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
837 |
> |
public void realRun() throws InterruptedException { |
838 |
> |
for (int i = 0; i < SIZE; ++i) |
839 |
> |
q.putFirst(i); |
840 |
> |
assertEquals(SIZE, q.size()); |
841 |
> |
assertEquals(0, q.remainingCapacity()); |
842 |
> |
|
843 |
> |
Thread.currentThread().interrupt(); |
844 |
> |
try { |
845 |
> |
q.putFirst(99); |
846 |
> |
shouldThrow(); |
847 |
> |
} catch (InterruptedException success) {} |
848 |
> |
assertFalse(Thread.interrupted()); |
849 |
> |
|
850 |
> |
pleaseInterrupt.countDown(); |
851 |
> |
try { |
852 |
> |
q.putFirst(99); |
853 |
> |
shouldThrow(); |
854 |
> |
} catch (InterruptedException success) {} |
855 |
> |
assertFalse(Thread.interrupted()); |
856 |
> |
}}); |
857 |
> |
|
858 |
> |
await(pleaseInterrupt); |
859 |
> |
assertThreadStaysAlive(t); |
860 |
> |
t.interrupt(); |
861 |
> |
awaitTermination(t); |
862 |
> |
assertEquals(SIZE, q.size()); |
863 |
> |
assertEquals(0, q.remainingCapacity()); |
864 |
|
} |
865 |
|
|
866 |
|
/** |
867 |
< |
* putFirst blocks waiting for take when full |
867 |
> |
* putFirst blocks interruptibly waiting for take when full |
868 |
|
*/ |
869 |
< |
public void testPutFirstWithTake() { |
870 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
871 |
< |
Thread t = new Thread(new Runnable() { |
872 |
< |
public void run() { |
873 |
< |
int added = 0; |
874 |
< |
try { |
875 |
< |
q.putFirst(new Object()); |
876 |
< |
++added; |
877 |
< |
q.putFirst(new Object()); |
878 |
< |
++added; |
879 |
< |
q.putFirst(new Object()); |
880 |
< |
++added; |
881 |
< |
q.putFirst(new Object()); |
882 |
< |
++added; |
883 |
< |
threadShouldThrow(); |
884 |
< |
} catch (InterruptedException e){ |
885 |
< |
threadAssertTrue(added >= 2); |
886 |
< |
} |
887 |
< |
} |
888 |
< |
}); |
889 |
< |
try { |
890 |
< |
t.start(); |
891 |
< |
Thread.sleep(SHORT_DELAY_MS); |
892 |
< |
q.take(); |
893 |
< |
t.interrupt(); |
894 |
< |
t.join(); |
895 |
< |
} catch (Exception e){ |
896 |
< |
unexpectedException(); |
897 |
< |
} |
869 |
> |
public void testPutFirstWithTake() throws InterruptedException { |
870 |
> |
final int capacity = 2; |
871 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
872 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
873 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
874 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
875 |
> |
public void realRun() throws InterruptedException { |
876 |
> |
for (int i = 0; i < capacity; i++) |
877 |
> |
q.putFirst(i); |
878 |
> |
pleaseTake.countDown(); |
879 |
> |
q.putFirst(86); |
880 |
> |
|
881 |
> |
pleaseInterrupt.countDown(); |
882 |
> |
try { |
883 |
> |
q.putFirst(99); |
884 |
> |
shouldThrow(); |
885 |
> |
} catch (InterruptedException success) {} |
886 |
> |
assertFalse(Thread.interrupted()); |
887 |
> |
}}); |
888 |
> |
|
889 |
> |
await(pleaseTake); |
890 |
> |
assertEquals(q.remainingCapacity(), 0); |
891 |
> |
assertEquals(capacity - 1, q.take()); |
892 |
> |
|
893 |
> |
await(pleaseInterrupt); |
894 |
> |
assertThreadStaysAlive(t); |
895 |
> |
t.interrupt(); |
896 |
> |
awaitTermination(t); |
897 |
> |
assertEquals(q.remainingCapacity(), 0); |
898 |
|
} |
899 |
|
|
900 |
|
/** |
901 |
|
* timed offerFirst times out if full and elements not taken |
902 |
|
*/ |
903 |
< |
public void testTimedOfferFirst() { |
903 |
> |
public void testTimedOfferFirst() throws InterruptedException { |
904 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
905 |
< |
Thread t = new Thread(new Runnable() { |
906 |
< |
public void run() { |
907 |
< |
try { |
908 |
< |
q.putFirst(new Object()); |
909 |
< |
q.putFirst(new Object()); |
910 |
< |
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
911 |
< |
q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
912 |
< |
threadShouldThrow(); |
913 |
< |
} catch (InterruptedException success){} |
914 |
< |
} |
915 |
< |
}); |
916 |
< |
|
917 |
< |
try { |
918 |
< |
t.start(); |
919 |
< |
Thread.sleep(SMALL_DELAY_MS); |
920 |
< |
t.interrupt(); |
921 |
< |
t.join(); |
922 |
< |
} catch (Exception e){ |
923 |
< |
unexpectedException(); |
970 |
< |
} |
905 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
906 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
907 |
> |
public void realRun() throws InterruptedException { |
908 |
> |
q.putFirst(new Object()); |
909 |
> |
q.putFirst(new Object()); |
910 |
> |
long startTime = System.nanoTime(); |
911 |
> |
assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS)); |
912 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
913 |
> |
pleaseInterrupt.countDown(); |
914 |
> |
try { |
915 |
> |
q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
916 |
> |
shouldThrow(); |
917 |
> |
} catch (InterruptedException success) {} |
918 |
> |
}}); |
919 |
> |
|
920 |
> |
await(pleaseInterrupt); |
921 |
> |
assertThreadStaysAlive(t); |
922 |
> |
t.interrupt(); |
923 |
> |
awaitTermination(t); |
924 |
|
} |
925 |
|
|
926 |
|
/** |
927 |
|
* take retrieves elements in FIFO order |
928 |
|
*/ |
929 |
< |
public void testTakeFirst() { |
930 |
< |
try { |
931 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
932 |
< |
for (int i = 0; i < SIZE; ++i) { |
933 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
981 |
< |
} |
982 |
< |
} catch (InterruptedException e){ |
983 |
< |
unexpectedException(); |
984 |
< |
} |
929 |
> |
public void testTakeFirst() throws InterruptedException { |
930 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
931 |
> |
for (int i = 0; i < SIZE; ++i) { |
932 |
> |
assertEquals(i, q.takeFirst()); |
933 |
> |
} |
934 |
|
} |
935 |
|
|
936 |
|
/** |
937 |
< |
* takeFirst blocks interruptibly when empty |
937 |
> |
* takeFirst() blocks interruptibly when empty |
938 |
|
*/ |
939 |
< |
public void testTakeFirstFromEmpty() { |
940 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
941 |
< |
Thread t = new Thread(new Runnable() { |
942 |
< |
public void run() { |
943 |
< |
try { |
944 |
< |
q.takeFirst(); |
945 |
< |
threadShouldThrow(); |
946 |
< |
} catch (InterruptedException success){ } |
947 |
< |
} |
948 |
< |
}); |
949 |
< |
try { |
950 |
< |
t.start(); |
951 |
< |
Thread.sleep(SHORT_DELAY_MS); |
952 |
< |
t.interrupt(); |
953 |
< |
t.join(); |
954 |
< |
} catch (Exception e){ |
955 |
< |
unexpectedException(); |
1007 |
< |
} |
939 |
> |
public void testTakeFirstFromEmptyBlocksInterruptibly() { |
940 |
> |
final BlockingDeque q = new LinkedBlockingDeque(); |
941 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
942 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
943 |
> |
public void realRun() { |
944 |
> |
threadStarted.countDown(); |
945 |
> |
try { |
946 |
> |
q.takeFirst(); |
947 |
> |
shouldThrow(); |
948 |
> |
} catch (InterruptedException success) {} |
949 |
> |
assertFalse(Thread.interrupted()); |
950 |
> |
}}); |
951 |
> |
|
952 |
> |
await(threadStarted); |
953 |
> |
assertThreadStaysAlive(t); |
954 |
> |
t.interrupt(); |
955 |
> |
awaitTermination(t); |
956 |
|
} |
957 |
|
|
958 |
|
/** |
959 |
< |
* TakeFirst removes existing elements until empty, then blocks interruptibly |
960 |
< |
*/ |
961 |
< |
public void testBlockingTakeFirst() { |
962 |
< |
Thread t = new Thread(new Runnable() { |
963 |
< |
public void run() { |
964 |
< |
try { |
965 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
966 |
< |
for (int i = 0; i < SIZE; ++i) { |
967 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
968 |
< |
} |
969 |
< |
q.takeFirst(); |
970 |
< |
threadShouldThrow(); |
971 |
< |
} catch (InterruptedException success){ |
972 |
< |
} |
973 |
< |
}}); |
974 |
< |
t.start(); |
975 |
< |
try { |
976 |
< |
Thread.sleep(SHORT_DELAY_MS); |
977 |
< |
t.interrupt(); |
978 |
< |
t.join(); |
979 |
< |
} |
980 |
< |
catch (InterruptedException ie) { |
981 |
< |
unexpectedException(); |
982 |
< |
} |
959 |
> |
* takeFirst() throws InterruptedException immediately if interrupted |
960 |
> |
* before waiting |
961 |
> |
*/ |
962 |
> |
public void testTakeFirstFromEmptyAfterInterrupt() { |
963 |
> |
final BlockingDeque q = new LinkedBlockingDeque(); |
964 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
965 |
> |
public void realRun() { |
966 |
> |
Thread.currentThread().interrupt(); |
967 |
> |
try { |
968 |
> |
q.takeFirst(); |
969 |
> |
shouldThrow(); |
970 |
> |
} catch (InterruptedException success) {} |
971 |
> |
assertFalse(Thread.interrupted()); |
972 |
> |
}}); |
973 |
> |
|
974 |
> |
awaitTermination(t); |
975 |
> |
} |
976 |
> |
|
977 |
> |
/** |
978 |
> |
* takeLast() blocks interruptibly when empty |
979 |
> |
*/ |
980 |
> |
public void testTakeLastFromEmptyBlocksInterruptibly() { |
981 |
> |
final BlockingDeque q = new LinkedBlockingDeque(); |
982 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
983 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
984 |
> |
public void realRun() { |
985 |
> |
threadStarted.countDown(); |
986 |
> |
try { |
987 |
> |
q.takeLast(); |
988 |
> |
shouldThrow(); |
989 |
> |
} catch (InterruptedException success) {} |
990 |
> |
assertFalse(Thread.interrupted()); |
991 |
> |
}}); |
992 |
> |
|
993 |
> |
await(threadStarted); |
994 |
> |
assertThreadStaysAlive(t); |
995 |
> |
t.interrupt(); |
996 |
> |
awaitTermination(t); |
997 |
|
} |
998 |
|
|
999 |
+ |
/** |
1000 |
+ |
* takeLast() throws InterruptedException immediately if interrupted |
1001 |
+ |
* before waiting |
1002 |
+ |
*/ |
1003 |
+ |
public void testTakeLastFromEmptyAfterInterrupt() { |
1004 |
+ |
final BlockingDeque q = new LinkedBlockingDeque(); |
1005 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
1006 |
+ |
public void realRun() { |
1007 |
+ |
Thread.currentThread().interrupt(); |
1008 |
+ |
try { |
1009 |
+ |
q.takeLast(); |
1010 |
+ |
shouldThrow(); |
1011 |
+ |
} catch (InterruptedException success) {} |
1012 |
+ |
assertFalse(Thread.interrupted()); |
1013 |
+ |
}}); |
1014 |
+ |
|
1015 |
+ |
awaitTermination(t); |
1016 |
+ |
} |
1017 |
+ |
|
1018 |
+ |
/** |
1019 |
+ |
* takeFirst removes existing elements until empty, then blocks interruptibly |
1020 |
+ |
*/ |
1021 |
+ |
public void testBlockingTakeFirst() throws InterruptedException { |
1022 |
+ |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1023 |
+ |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1024 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
1025 |
+ |
public void realRun() throws InterruptedException { |
1026 |
+ |
for (int i = 0; i < SIZE; ++i) { |
1027 |
+ |
assertEquals(i, q.takeFirst()); |
1028 |
+ |
} |
1029 |
+ |
|
1030 |
+ |
Thread.currentThread().interrupt(); |
1031 |
+ |
try { |
1032 |
+ |
q.takeFirst(); |
1033 |
+ |
shouldThrow(); |
1034 |
+ |
} catch (InterruptedException success) {} |
1035 |
+ |
assertFalse(Thread.interrupted()); |
1036 |
+ |
|
1037 |
+ |
pleaseInterrupt.countDown(); |
1038 |
+ |
try { |
1039 |
+ |
q.takeFirst(); |
1040 |
+ |
shouldThrow(); |
1041 |
+ |
} catch (InterruptedException success) {} |
1042 |
+ |
assertFalse(Thread.interrupted()); |
1043 |
+ |
}}); |
1044 |
+ |
|
1045 |
+ |
await(pleaseInterrupt); |
1046 |
+ |
assertThreadStaysAlive(t); |
1047 |
+ |
t.interrupt(); |
1048 |
+ |
awaitTermination(t); |
1049 |
+ |
} |
1050 |
|
|
1051 |
|
/** |
1052 |
|
* timed pollFirst with zero timeout succeeds when non-empty, else times out |
1053 |
|
*/ |
1054 |
< |
public void testTimedPollFirst0() { |
1055 |
< |
try { |
1056 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1057 |
< |
for (int i = 0; i < SIZE; ++i) { |
1058 |
< |
assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue()); |
1059 |
< |
} |
1047 |
< |
assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS)); |
1048 |
< |
} catch (InterruptedException e){ |
1049 |
< |
unexpectedException(); |
1050 |
< |
} |
1054 |
> |
public void testTimedPollFirst0() throws InterruptedException { |
1055 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1056 |
> |
for (int i = 0; i < SIZE; ++i) { |
1057 |
> |
assertEquals(i, q.pollFirst(0, MILLISECONDS)); |
1058 |
> |
} |
1059 |
> |
assertNull(q.pollFirst(0, MILLISECONDS)); |
1060 |
|
} |
1061 |
|
|
1062 |
|
/** |
1063 |
|
* timed pollFirst with nonzero timeout succeeds when non-empty, else times out |
1064 |
|
*/ |
1065 |
< |
public void testTimedPollFirst() { |
1066 |
< |
try { |
1067 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1068 |
< |
for (int i = 0; i < SIZE; ++i) { |
1069 |
< |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1070 |
< |
} |
1071 |
< |
assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1072 |
< |
} catch (InterruptedException e){ |
1073 |
< |
unexpectedException(); |
1074 |
< |
} |
1065 |
> |
public void testTimedPollFirst() throws InterruptedException { |
1066 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1067 |
> |
for (int i = 0; i < SIZE; ++i) { |
1068 |
> |
long startTime = System.nanoTime(); |
1069 |
> |
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1070 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1071 |
> |
} |
1072 |
> |
long startTime = System.nanoTime(); |
1073 |
> |
assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS)); |
1074 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1075 |
> |
checkEmpty(q); |
1076 |
|
} |
1077 |
|
|
1078 |
|
/** |
1079 |
|
* Interrupted timed pollFirst throws InterruptedException instead of |
1080 |
|
* returning timeout status |
1081 |
|
*/ |
1082 |
< |
public void testInterruptedTimedPollFirst() { |
1083 |
< |
Thread t = new Thread(new Runnable() { |
1084 |
< |
public void run() { |
1085 |
< |
try { |
1086 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1087 |
< |
for (int i = 0; i < SIZE; ++i) { |
1088 |
< |
threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1089 |
< |
} |
1090 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1091 |
< |
} catch (InterruptedException success){ |
1092 |
< |
} |
1093 |
< |
}}); |
1094 |
< |
t.start(); |
1095 |
< |
try { |
1096 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1097 |
< |
t.interrupt(); |
1098 |
< |
t.join(); |
1099 |
< |
} |
1100 |
< |
catch (InterruptedException ie) { |
1101 |
< |
unexpectedException(); |
1102 |
< |
} |
1082 |
> |
public void testInterruptedTimedPollFirst() throws InterruptedException { |
1083 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1084 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1085 |
> |
public void realRun() throws InterruptedException { |
1086 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1087 |
> |
for (int i = 0; i < SIZE; ++i) { |
1088 |
> |
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1089 |
> |
} |
1090 |
> |
|
1091 |
> |
Thread.currentThread().interrupt(); |
1092 |
> |
try { |
1093 |
> |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1094 |
> |
shouldThrow(); |
1095 |
> |
} catch (InterruptedException success) {} |
1096 |
> |
assertFalse(Thread.interrupted()); |
1097 |
> |
|
1098 |
> |
pleaseInterrupt.countDown(); |
1099 |
> |
try { |
1100 |
> |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1101 |
> |
shouldThrow(); |
1102 |
> |
} catch (InterruptedException success) {} |
1103 |
> |
assertFalse(Thread.interrupted()); |
1104 |
> |
}}); |
1105 |
> |
|
1106 |
> |
await(pleaseInterrupt); |
1107 |
> |
assertThreadStaysAlive(t); |
1108 |
> |
t.interrupt(); |
1109 |
> |
awaitTermination(t); |
1110 |
|
} |
1111 |
|
|
1112 |
|
/** |
1113 |
< |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
1114 |
< |
* on interruption throws |
1113 |
> |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
1114 |
> |
* on interruption throws |
1115 |
|
*/ |
1116 |
< |
public void testTimedPollFirstWithOfferFirst() { |
1116 |
> |
public void testTimedPollFirstWithOfferFirst() throws InterruptedException { |
1117 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1118 |
< |
Thread t = new Thread(new Runnable() { |
1119 |
< |
public void run() { |
1120 |
< |
try { |
1121 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1122 |
< |
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1123 |
< |
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1124 |
< |
threadShouldThrow(); |
1125 |
< |
} catch (InterruptedException success) { } |
1126 |
< |
} |
1127 |
< |
}); |
1128 |
< |
try { |
1129 |
< |
t.start(); |
1130 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1131 |
< |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1132 |
< |
t.interrupt(); |
1133 |
< |
t.join(); |
1134 |
< |
} catch (Exception e){ |
1135 |
< |
unexpectedException(); |
1136 |
< |
} |
1137 |
< |
} |
1118 |
> |
final CheckedBarrier barrier = new CheckedBarrier(2); |
1119 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1120 |
> |
public void realRun() throws InterruptedException { |
1121 |
> |
long startTime = System.nanoTime(); |
1122 |
> |
assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS)); |
1123 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1124 |
> |
|
1125 |
> |
barrier.await(); |
1126 |
> |
|
1127 |
> |
assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1128 |
> |
|
1129 |
> |
Thread.currentThread().interrupt(); |
1130 |
> |
try { |
1131 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1132 |
> |
shouldThrow(); |
1133 |
> |
} catch (InterruptedException success) {} |
1134 |
> |
|
1135 |
> |
barrier.await(); |
1136 |
> |
try { |
1137 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1138 |
> |
shouldThrow(); |
1139 |
> |
} catch (InterruptedException success) {} |
1140 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1141 |
> |
}}); |
1142 |
> |
|
1143 |
> |
barrier.await(); |
1144 |
> |
long startTime = System.nanoTime(); |
1145 |
> |
assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS)); |
1146 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1147 |
> |
barrier.await(); |
1148 |
> |
assertThreadStaysAlive(t); |
1149 |
> |
t.interrupt(); |
1150 |
> |
awaitTermination(t); |
1151 |
> |
} |
1152 |
|
|
1153 |
|
/** |
1154 |
|
* putLast(null) throws NPE |
1155 |
|
*/ |
1156 |
< |
public void testPutLastNull() { |
1157 |
< |
try { |
1156 |
> |
public void testPutLastNull() throws InterruptedException { |
1157 |
> |
try { |
1158 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1159 |
|
q.putLast(null); |
1160 |
|
shouldThrow(); |
1161 |
< |
} |
1162 |
< |
catch (NullPointerException success){ |
1132 |
< |
} |
1133 |
< |
catch (InterruptedException ie) { |
1134 |
< |
unexpectedException(); |
1135 |
< |
} |
1136 |
< |
} |
1161 |
> |
} catch (NullPointerException success) {} |
1162 |
> |
} |
1163 |
|
|
1164 |
|
/** |
1165 |
|
* all elements successfully putLast are contained |
1166 |
|
*/ |
1167 |
< |
public void testPutLast() { |
1168 |
< |
try { |
1169 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1170 |
< |
for (int i = 0; i < SIZE; ++i) { |
1171 |
< |
Integer I = new Integer(i); |
1172 |
< |
q.putLast(I); |
1147 |
< |
assertTrue(q.contains(I)); |
1148 |
< |
} |
1149 |
< |
assertEquals(0, q.remainingCapacity()); |
1150 |
< |
} |
1151 |
< |
catch (InterruptedException ie) { |
1152 |
< |
unexpectedException(); |
1167 |
> |
public void testPutLast() throws InterruptedException { |
1168 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1169 |
> |
for (int i = 0; i < SIZE; ++i) { |
1170 |
> |
Integer I = new Integer(i); |
1171 |
> |
q.putLast(I); |
1172 |
> |
assertTrue(q.contains(I)); |
1173 |
|
} |
1174 |
+ |
assertEquals(0, q.remainingCapacity()); |
1175 |
|
} |
1176 |
|
|
1177 |
|
/** |
1178 |
|
* putLast blocks interruptibly if full |
1179 |
|
*/ |
1180 |
< |
public void testBlockingPutLast() { |
1181 |
< |
Thread t = new Thread(new Runnable() { |
1182 |
< |
public void run() { |
1183 |
< |
int added = 0; |
1184 |
< |
try { |
1185 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1186 |
< |
for (int i = 0; i < SIZE; ++i) { |
1187 |
< |
q.putLast(new Integer(i)); |
1188 |
< |
++added; |
1189 |
< |
} |
1190 |
< |
q.putLast(new Integer(SIZE)); |
1191 |
< |
threadShouldThrow(); |
1192 |
< |
} catch (InterruptedException ie){ |
1193 |
< |
threadAssertEquals(added, SIZE); |
1194 |
< |
} |
1195 |
< |
}}); |
1196 |
< |
t.start(); |
1197 |
< |
try { |
1198 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1199 |
< |
t.interrupt(); |
1200 |
< |
t.join(); |
1201 |
< |
} |
1202 |
< |
catch (InterruptedException ie) { |
1203 |
< |
unexpectedException(); |
1204 |
< |
} |
1180 |
> |
public void testBlockingPutLast() throws InterruptedException { |
1181 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1182 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1183 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1184 |
> |
public void realRun() throws InterruptedException { |
1185 |
> |
for (int i = 0; i < SIZE; ++i) |
1186 |
> |
q.putLast(i); |
1187 |
> |
assertEquals(SIZE, q.size()); |
1188 |
> |
assertEquals(0, q.remainingCapacity()); |
1189 |
> |
|
1190 |
> |
Thread.currentThread().interrupt(); |
1191 |
> |
try { |
1192 |
> |
q.putLast(99); |
1193 |
> |
shouldThrow(); |
1194 |
> |
} catch (InterruptedException success) {} |
1195 |
> |
assertFalse(Thread.interrupted()); |
1196 |
> |
|
1197 |
> |
pleaseInterrupt.countDown(); |
1198 |
> |
try { |
1199 |
> |
q.putLast(99); |
1200 |
> |
shouldThrow(); |
1201 |
> |
} catch (InterruptedException success) {} |
1202 |
> |
assertFalse(Thread.interrupted()); |
1203 |
> |
}}); |
1204 |
> |
|
1205 |
> |
await(pleaseInterrupt); |
1206 |
> |
assertThreadStaysAlive(t); |
1207 |
> |
t.interrupt(); |
1208 |
> |
awaitTermination(t); |
1209 |
> |
assertEquals(SIZE, q.size()); |
1210 |
> |
assertEquals(0, q.remainingCapacity()); |
1211 |
|
} |
1212 |
|
|
1213 |
|
/** |
1214 |
< |
* putLast blocks waiting for take when full |
1214 |
> |
* putLast blocks interruptibly waiting for take when full |
1215 |
|
*/ |
1216 |
< |
public void testPutLastWithTake() { |
1217 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1218 |
< |
Thread t = new Thread(new Runnable() { |
1219 |
< |
public void run() { |
1220 |
< |
int added = 0; |
1221 |
< |
try { |
1222 |
< |
q.putLast(new Object()); |
1223 |
< |
++added; |
1224 |
< |
q.putLast(new Object()); |
1225 |
< |
++added; |
1226 |
< |
q.putLast(new Object()); |
1227 |
< |
++added; |
1228 |
< |
q.putLast(new Object()); |
1229 |
< |
++added; |
1230 |
< |
threadShouldThrow(); |
1231 |
< |
} catch (InterruptedException e){ |
1232 |
< |
threadAssertTrue(added >= 2); |
1233 |
< |
} |
1234 |
< |
} |
1235 |
< |
}); |
1236 |
< |
try { |
1237 |
< |
t.start(); |
1238 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1239 |
< |
q.take(); |
1240 |
< |
t.interrupt(); |
1241 |
< |
t.join(); |
1242 |
< |
} catch (Exception e){ |
1243 |
< |
unexpectedException(); |
1244 |
< |
} |
1216 |
> |
public void testPutLastWithTake() throws InterruptedException { |
1217 |
> |
final int capacity = 2; |
1218 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
1219 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
1220 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1221 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1222 |
> |
public void realRun() throws InterruptedException { |
1223 |
> |
for (int i = 0; i < capacity; i++) |
1224 |
> |
q.putLast(i); |
1225 |
> |
pleaseTake.countDown(); |
1226 |
> |
q.putLast(86); |
1227 |
> |
|
1228 |
> |
pleaseInterrupt.countDown(); |
1229 |
> |
try { |
1230 |
> |
q.putLast(99); |
1231 |
> |
shouldThrow(); |
1232 |
> |
} catch (InterruptedException success) {} |
1233 |
> |
assertFalse(Thread.interrupted()); |
1234 |
> |
}}); |
1235 |
> |
|
1236 |
> |
await(pleaseTake); |
1237 |
> |
assertEquals(q.remainingCapacity(), 0); |
1238 |
> |
assertEquals(0, q.take()); |
1239 |
> |
|
1240 |
> |
await(pleaseInterrupt); |
1241 |
> |
assertThreadStaysAlive(t); |
1242 |
> |
t.interrupt(); |
1243 |
> |
awaitTermination(t); |
1244 |
> |
assertEquals(q.remainingCapacity(), 0); |
1245 |
|
} |
1246 |
|
|
1247 |
|
/** |
1248 |
|
* timed offerLast times out if full and elements not taken |
1249 |
|
*/ |
1250 |
< |
public void testTimedOfferLast() { |
1250 |
> |
public void testTimedOfferLast() throws InterruptedException { |
1251 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1252 |
< |
Thread t = new Thread(new Runnable() { |
1253 |
< |
public void run() { |
1254 |
< |
try { |
1255 |
< |
q.putLast(new Object()); |
1256 |
< |
q.putLast(new Object()); |
1257 |
< |
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1258 |
< |
q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1259 |
< |
threadShouldThrow(); |
1260 |
< |
} catch (InterruptedException success){} |
1261 |
< |
} |
1262 |
< |
}); |
1263 |
< |
|
1264 |
< |
try { |
1265 |
< |
t.start(); |
1266 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1267 |
< |
t.interrupt(); |
1268 |
< |
t.join(); |
1269 |
< |
} catch (Exception e){ |
1270 |
< |
unexpectedException(); |
1244 |
< |
} |
1252 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1253 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1254 |
> |
public void realRun() throws InterruptedException { |
1255 |
> |
q.putLast(new Object()); |
1256 |
> |
q.putLast(new Object()); |
1257 |
> |
long startTime = System.nanoTime(); |
1258 |
> |
assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS)); |
1259 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1260 |
> |
pleaseInterrupt.countDown(); |
1261 |
> |
try { |
1262 |
> |
q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
1263 |
> |
shouldThrow(); |
1264 |
> |
} catch (InterruptedException success) {} |
1265 |
> |
}}); |
1266 |
> |
|
1267 |
> |
await(pleaseInterrupt); |
1268 |
> |
assertThreadStaysAlive(t); |
1269 |
> |
t.interrupt(); |
1270 |
> |
awaitTermination(t); |
1271 |
|
} |
1272 |
|
|
1273 |
|
/** |
1274 |
|
* takeLast retrieves elements in FIFO order |
1275 |
|
*/ |
1276 |
< |
public void testTakeLast() { |
1277 |
< |
try { |
1278 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1279 |
< |
for (int i = 0; i < SIZE; ++i) { |
1280 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1255 |
< |
} |
1256 |
< |
} catch (InterruptedException e){ |
1257 |
< |
unexpectedException(); |
1258 |
< |
} |
1276 |
> |
public void testTakeLast() throws InterruptedException { |
1277 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1278 |
> |
for (int i = 0; i < SIZE; ++i) { |
1279 |
> |
assertEquals(SIZE-i-1, q.takeLast()); |
1280 |
> |
} |
1281 |
|
} |
1282 |
|
|
1283 |
|
/** |
1284 |
< |
* takeLast blocks interruptibly when empty |
1284 |
> |
* takeLast removes existing elements until empty, then blocks interruptibly |
1285 |
|
*/ |
1286 |
< |
public void testTakeLastFromEmpty() { |
1287 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1288 |
< |
Thread t = new Thread(new Runnable() { |
1289 |
< |
public void run() { |
1290 |
< |
try { |
1291 |
< |
q.takeLast(); |
1292 |
< |
threadShouldThrow(); |
1271 |
< |
} catch (InterruptedException success){ } |
1286 |
> |
public void testBlockingTakeLast() throws InterruptedException { |
1287 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1288 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1289 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1290 |
> |
public void realRun() throws InterruptedException { |
1291 |
> |
for (int i = 0; i < SIZE; ++i) { |
1292 |
> |
assertEquals(SIZE-i-1, q.takeLast()); |
1293 |
|
} |
1273 |
– |
}); |
1274 |
– |
try { |
1275 |
– |
t.start(); |
1276 |
– |
Thread.sleep(SHORT_DELAY_MS); |
1277 |
– |
t.interrupt(); |
1278 |
– |
t.join(); |
1279 |
– |
} catch (Exception e){ |
1280 |
– |
unexpectedException(); |
1281 |
– |
} |
1282 |
– |
} |
1294 |
|
|
1295 |
< |
/** |
1296 |
< |
* TakeLast removes existing elements until empty, then blocks interruptibly |
1297 |
< |
*/ |
1298 |
< |
public void testBlockingTakeLast() { |
1299 |
< |
Thread t = new Thread(new Runnable() { |
1300 |
< |
public void run() { |
1290 |
< |
try { |
1291 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1292 |
< |
for (int i = 0; i < SIZE; ++i) { |
1293 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1294 |
< |
} |
1295 |
< |
q.takeLast(); |
1296 |
< |
threadShouldThrow(); |
1297 |
< |
} 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 |
< |
} |
1295 |
> |
Thread.currentThread().interrupt(); |
1296 |
> |
try { |
1297 |
> |
q.takeLast(); |
1298 |
> |
shouldThrow(); |
1299 |
> |
} catch (InterruptedException success) {} |
1300 |
> |
assertFalse(Thread.interrupted()); |
1301 |
|
|
1302 |
+ |
pleaseInterrupt.countDown(); |
1303 |
+ |
try { |
1304 |
+ |
q.takeLast(); |
1305 |
+ |
shouldThrow(); |
1306 |
+ |
} catch (InterruptedException success) {} |
1307 |
+ |
assertFalse(Thread.interrupted()); |
1308 |
+ |
}}); |
1309 |
+ |
|
1310 |
+ |
await(pleaseInterrupt); |
1311 |
+ |
assertThreadStaysAlive(t); |
1312 |
+ |
t.interrupt(); |
1313 |
+ |
awaitTermination(t); |
1314 |
+ |
} |
1315 |
|
|
1316 |
|
/** |
1317 |
|
* timed pollLast with zero timeout succeeds when non-empty, else times out |
1318 |
|
*/ |
1319 |
< |
public void testTimedPollLast0() { |
1320 |
< |
try { |
1321 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1322 |
< |
for (int i = 0; i < SIZE; ++i) { |
1323 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue()); |
1324 |
< |
} |
1321 |
< |
assertNull(q.pollLast(0, TimeUnit.MILLISECONDS)); |
1322 |
< |
} catch (InterruptedException e){ |
1323 |
< |
unexpectedException(); |
1324 |
< |
} |
1319 |
> |
public void testTimedPollLast0() throws InterruptedException { |
1320 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1321 |
> |
for (int i = 0; i < SIZE; ++i) { |
1322 |
> |
assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
1323 |
> |
} |
1324 |
> |
assertNull(q.pollLast(0, MILLISECONDS)); |
1325 |
|
} |
1326 |
|
|
1327 |
|
/** |
1328 |
|
* timed pollLast with nonzero timeout succeeds when non-empty, else times out |
1329 |
|
*/ |
1330 |
< |
public void testTimedPollLast() { |
1331 |
< |
try { |
1332 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1333 |
< |
for (int i = 0; i < SIZE; ++i) { |
1334 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1335 |
< |
} |
1336 |
< |
assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1337 |
< |
} catch (InterruptedException e){ |
1338 |
< |
unexpectedException(); |
1339 |
< |
} |
1330 |
> |
public void testTimedPollLast() throws InterruptedException { |
1331 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1332 |
> |
for (int i = 0; i < SIZE; ++i) { |
1333 |
> |
long startTime = System.nanoTime(); |
1334 |
> |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1335 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1336 |
> |
} |
1337 |
> |
long startTime = System.nanoTime(); |
1338 |
> |
assertNull(q.pollLast(timeoutMillis(), MILLISECONDS)); |
1339 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1340 |
> |
checkEmpty(q); |
1341 |
|
} |
1342 |
|
|
1343 |
|
/** |
1344 |
|
* Interrupted timed pollLast throws InterruptedException instead of |
1345 |
|
* returning timeout status |
1346 |
|
*/ |
1347 |
< |
public void testInterruptedTimedPollLast() { |
1348 |
< |
Thread t = new Thread(new Runnable() { |
1349 |
< |
public void run() { |
1350 |
< |
try { |
1351 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1352 |
< |
for (int i = 0; i < SIZE; ++i) { |
1353 |
< |
threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1354 |
< |
} |
1355 |
< |
threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1356 |
< |
} catch (InterruptedException success){ |
1357 |
< |
} |
1358 |
< |
}}); |
1359 |
< |
t.start(); |
1360 |
< |
try { |
1361 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1362 |
< |
t.interrupt(); |
1363 |
< |
t.join(); |
1364 |
< |
} |
1365 |
< |
catch (InterruptedException ie) { |
1366 |
< |
unexpectedException(); |
1367 |
< |
} |
1347 |
> |
public void testInterruptedTimedPollLast() throws InterruptedException { |
1348 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1349 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1350 |
> |
public void realRun() throws InterruptedException { |
1351 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1352 |
> |
for (int i = 0; i < SIZE; ++i) { |
1353 |
> |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1354 |
> |
} |
1355 |
> |
|
1356 |
> |
Thread.currentThread().interrupt(); |
1357 |
> |
try { |
1358 |
> |
q.pollLast(LONG_DELAY_MS, MILLISECONDS); |
1359 |
> |
shouldThrow(); |
1360 |
> |
} catch (InterruptedException success) {} |
1361 |
> |
assertFalse(Thread.interrupted()); |
1362 |
> |
|
1363 |
> |
pleaseInterrupt.countDown(); |
1364 |
> |
try { |
1365 |
> |
q.pollLast(LONG_DELAY_MS, MILLISECONDS); |
1366 |
> |
shouldThrow(); |
1367 |
> |
} catch (InterruptedException success) {} |
1368 |
> |
assertFalse(Thread.interrupted()); |
1369 |
> |
}}); |
1370 |
> |
|
1371 |
> |
await(pleaseInterrupt); |
1372 |
> |
assertThreadStaysAlive(t); |
1373 |
> |
t.interrupt(); |
1374 |
> |
awaitTermination(t); |
1375 |
|
} |
1376 |
|
|
1377 |
|
/** |
1378 |
< |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1379 |
< |
* on interruption throws |
1378 |
> |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1379 |
> |
* on interruption throws |
1380 |
|
*/ |
1381 |
< |
public void testTimedPollWithOfferLast() { |
1381 |
> |
public void testTimedPollWithOfferLast() throws InterruptedException { |
1382 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1383 |
< |
Thread t = new Thread(new Runnable() { |
1384 |
< |
public void run() { |
1385 |
< |
try { |
1386 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1387 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1388 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1389 |
< |
threadShouldThrow(); |
1390 |
< |
} catch (InterruptedException success) { } |
1383 |
< |
} |
1384 |
< |
}); |
1385 |
< |
try { |
1386 |
< |
t.start(); |
1387 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1388 |
< |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1389 |
< |
t.interrupt(); |
1390 |
< |
t.join(); |
1391 |
< |
} catch (Exception e){ |
1392 |
< |
unexpectedException(); |
1393 |
< |
} |
1394 |
< |
} |
1383 |
> |
final CheckedBarrier barrier = new CheckedBarrier(2); |
1384 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1385 |
> |
public void realRun() throws InterruptedException { |
1386 |
> |
long startTime = System.nanoTime(); |
1387 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
1388 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1389 |
> |
|
1390 |
> |
barrier.await(); |
1391 |
|
|
1392 |
+ |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1393 |
+ |
|
1394 |
+ |
Thread.currentThread().interrupt(); |
1395 |
+ |
try { |
1396 |
+ |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1397 |
+ |
shouldThrow(); |
1398 |
+ |
} catch (InterruptedException success) {} |
1399 |
+ |
assertFalse(Thread.interrupted()); |
1400 |
+ |
|
1401 |
+ |
barrier.await(); |
1402 |
+ |
try { |
1403 |
+ |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1404 |
+ |
shouldThrow(); |
1405 |
+ |
} catch (InterruptedException success) {} |
1406 |
+ |
assertFalse(Thread.interrupted()); |
1407 |
+ |
}}); |
1408 |
+ |
|
1409 |
+ |
barrier.await(); |
1410 |
+ |
long startTime = System.nanoTime(); |
1411 |
+ |
assertTrue(q.offerLast(zero, LONG_DELAY_MS, MILLISECONDS)); |
1412 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1413 |
+ |
|
1414 |
+ |
barrier.await(); |
1415 |
+ |
assertThreadStaysAlive(t); |
1416 |
+ |
t.interrupt(); |
1417 |
+ |
awaitTermination(t); |
1418 |
+ |
} |
1419 |
|
|
1420 |
|
/** |
1421 |
|
* element returns next element, or throws NSEE if empty |
1423 |
|
public void testElement() { |
1424 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1425 |
|
for (int i = 0; i < SIZE; ++i) { |
1426 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
1426 |
> |
assertEquals(i, q.element()); |
1427 |
|
q.poll(); |
1428 |
|
} |
1429 |
|
try { |
1430 |
|
q.element(); |
1431 |
|
shouldThrow(); |
1432 |
< |
} |
1410 |
< |
catch (NoSuchElementException success) {} |
1432 |
> |
} catch (NoSuchElementException success) {} |
1433 |
|
} |
1434 |
|
|
1435 |
|
/** |
1438 |
|
public void testRemoveElement() { |
1439 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1440 |
|
for (int i = 1; i < SIZE; i+=2) { |
1441 |
< |
assertTrue(q.remove(new Integer(i))); |
1441 |
> |
assertTrue(q.contains(i)); |
1442 |
> |
assertTrue(q.remove(i)); |
1443 |
> |
assertFalse(q.contains(i)); |
1444 |
> |
assertTrue(q.contains(i-1)); |
1445 |
|
} |
1446 |
|
for (int i = 0; i < SIZE; i+=2) { |
1447 |
< |
assertTrue(q.remove(new Integer(i))); |
1448 |
< |
assertFalse(q.remove(new Integer(i+1))); |
1447 |
> |
assertTrue(q.contains(i)); |
1448 |
> |
assertTrue(q.remove(i)); |
1449 |
> |
assertFalse(q.contains(i)); |
1450 |
> |
assertFalse(q.remove(i+1)); |
1451 |
> |
assertFalse(q.contains(i+1)); |
1452 |
|
} |
1453 |
|
assertTrue(q.isEmpty()); |
1454 |
|
} |
1455 |
< |
|
1455 |
> |
|
1456 |
|
/** |
1457 |
|
* contains(x) reports true when elements added but not yet removed |
1458 |
|
*/ |
1531 |
|
} |
1532 |
|
|
1533 |
|
/** |
1534 |
< |
* toArray contains all elements |
1534 |
> |
* toArray contains all elements in FIFO order |
1535 |
|
*/ |
1536 |
< |
public void testToArray() { |
1536 |
> |
public void testToArray() throws InterruptedException{ |
1537 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1538 |
< |
Object[] o = q.toArray(); |
1539 |
< |
try { |
1540 |
< |
for(int i = 0; i < o.length; i++) |
1513 |
< |
assertEquals(o[i], q.take()); |
1514 |
< |
} catch (InterruptedException e){ |
1515 |
< |
unexpectedException(); |
1516 |
< |
} |
1538 |
> |
Object[] o = q.toArray(); |
1539 |
> |
for (int i = 0; i < o.length; i++) |
1540 |
> |
assertSame(o[i], q.poll()); |
1541 |
|
} |
1542 |
|
|
1543 |
|
/** |
1544 |
< |
* toArray(a) contains all elements |
1544 |
> |
* toArray(a) contains all elements in FIFO order |
1545 |
|
*/ |
1546 |
|
public void testToArray2() { |
1547 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1548 |
< |
Integer[] ints = new Integer[SIZE]; |
1549 |
< |
ints = (Integer[])q.toArray(ints); |
1550 |
< |
try { |
1551 |
< |
for(int i = 0; i < ints.length; i++) |
1552 |
< |
assertEquals(ints[i], q.take()); |
1529 |
< |
} catch (InterruptedException e){ |
1530 |
< |
unexpectedException(); |
1531 |
< |
} |
1547 |
> |
LinkedBlockingDeque<Integer> q = populatedDeque(SIZE); |
1548 |
> |
Integer[] ints = new Integer[SIZE]; |
1549 |
> |
Integer[] array = q.toArray(ints); |
1550 |
> |
assertSame(ints, array); |
1551 |
> |
for (int i = 0; i < ints.length; i++) |
1552 |
> |
assertSame(ints[i], q.remove()); |
1553 |
|
} |
1554 |
|
|
1555 |
|
/** |
1556 |
< |
* toArray(null) throws NPE |
1556 |
> |
* toArray(null) throws NullPointerException |
1557 |
|
*/ |
1558 |
< |
public void testToArray_BadArg() { |
1559 |
< |
try { |
1560 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1561 |
< |
Object o[] = q.toArray(null); |
1562 |
< |
shouldThrow(); |
1563 |
< |
} catch(NullPointerException success){} |
1558 |
> |
public void testToArray_NullArg() { |
1559 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1560 |
> |
try { |
1561 |
> |
q.toArray(null); |
1562 |
> |
shouldThrow(); |
1563 |
> |
} catch (NullPointerException success) {} |
1564 |
|
} |
1565 |
|
|
1566 |
|
/** |
1567 |
< |
* toArray with incompatible array type throws CCE |
1567 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
1568 |
|
*/ |
1569 |
|
public void testToArray1_BadArg() { |
1570 |
< |
try { |
1571 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1572 |
< |
Object o[] = q.toArray(new String[10] ); |
1573 |
< |
shouldThrow(); |
1574 |
< |
} catch(ArrayStoreException success){} |
1570 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1571 |
> |
try { |
1572 |
> |
q.toArray(new String[10]); |
1573 |
> |
shouldThrow(); |
1574 |
> |
} catch (ArrayStoreException success) {} |
1575 |
|
} |
1576 |
|
|
1556 |
– |
|
1577 |
|
/** |
1578 |
|
* iterator iterates through all elements |
1579 |
|
*/ |
1580 |
< |
public void testIterator() { |
1580 |
> |
public void testIterator() throws InterruptedException { |
1581 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1582 |
< |
Iterator it = q.iterator(); |
1583 |
< |
try { |
1584 |
< |
while(it.hasNext()){ |
1585 |
< |
assertEquals(it.next(), q.take()); |
1566 |
< |
} |
1567 |
< |
} catch (InterruptedException e){ |
1568 |
< |
unexpectedException(); |
1569 |
< |
} |
1582 |
> |
Iterator it = q.iterator(); |
1583 |
> |
while (it.hasNext()) { |
1584 |
> |
assertEquals(it.next(), q.take()); |
1585 |
> |
} |
1586 |
|
} |
1587 |
|
|
1588 |
|
/** |
1589 |
|
* iterator.remove removes current element |
1590 |
|
*/ |
1591 |
< |
public void testIteratorRemove () { |
1591 |
> |
public void testIteratorRemove() { |
1592 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1593 |
|
q.add(two); |
1594 |
|
q.add(one); |
1597 |
|
Iterator it = q.iterator(); |
1598 |
|
it.next(); |
1599 |
|
it.remove(); |
1600 |
< |
|
1600 |
> |
|
1601 |
|
it = q.iterator(); |
1602 |
< |
assertEquals(it.next(), one); |
1603 |
< |
assertEquals(it.next(), three); |
1602 |
> |
assertSame(it.next(), one); |
1603 |
> |
assertSame(it.next(), three); |
1604 |
|
assertFalse(it.hasNext()); |
1605 |
|
} |
1606 |
|
|
1591 |
– |
|
1607 |
|
/** |
1608 |
|
* iterator ordering is FIFO |
1609 |
|
*/ |
1615 |
|
assertEquals(0, q.remainingCapacity()); |
1616 |
|
int k = 0; |
1617 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
1618 |
< |
int i = ((Integer)(it.next())).intValue(); |
1604 |
< |
assertEquals(++k, i); |
1618 |
> |
assertEquals(++k, it.next()); |
1619 |
|
} |
1620 |
|
assertEquals(3, k); |
1621 |
|
} |
1623 |
|
/** |
1624 |
|
* Modifications do not cause iterators to fail |
1625 |
|
*/ |
1626 |
< |
public void testWeaklyConsistentIteration () { |
1626 |
> |
public void testWeaklyConsistentIteration() { |
1627 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1628 |
|
q.add(one); |
1629 |
|
q.add(two); |
1630 |
|
q.add(three); |
1631 |
< |
try { |
1632 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
1633 |
< |
q.remove(); |
1620 |
< |
it.next(); |
1621 |
< |
} |
1622 |
< |
} |
1623 |
< |
catch (ConcurrentModificationException e) { |
1624 |
< |
unexpectedException(); |
1631 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
1632 |
> |
q.remove(); |
1633 |
> |
it.next(); |
1634 |
|
} |
1635 |
|
assertEquals(0, q.size()); |
1636 |
|
} |
1637 |
|
|
1629 |
– |
|
1638 |
|
/** |
1639 |
< |
* Descending iterator iterates through all elements |
1639 |
> |
* Descending iterator iterates through all elements |
1640 |
|
*/ |
1641 |
|
public void testDescendingIterator() { |
1642 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1643 |
|
int i = 0; |
1644 |
< |
Iterator it = q.descendingIterator(); |
1645 |
< |
while(it.hasNext()) { |
1644 |
> |
Iterator it = q.descendingIterator(); |
1645 |
> |
while (it.hasNext()) { |
1646 |
|
assertTrue(q.contains(it.next())); |
1647 |
|
++i; |
1648 |
|
} |
1650 |
|
assertFalse(it.hasNext()); |
1651 |
|
try { |
1652 |
|
it.next(); |
1653 |
< |
} catch(NoSuchElementException success) { |
1654 |
< |
} |
1653 |
> |
shouldThrow(); |
1654 |
> |
} catch (NoSuchElementException success) {} |
1655 |
|
} |
1656 |
|
|
1657 |
|
/** |
1658 |
< |
* Descending iterator ordering is reverse FIFO |
1658 |
> |
* Descending iterator ordering is reverse FIFO |
1659 |
|
*/ |
1660 |
|
public void testDescendingIteratorOrdering() { |
1661 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1665 |
|
q.add(new Integer(1)); |
1666 |
|
int k = 0; |
1667 |
|
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
1668 |
< |
int i = ((Integer)(it.next())).intValue(); |
1661 |
< |
assertEquals(++k, i); |
1668 |
> |
assertEquals(++k, it.next()); |
1669 |
|
} |
1670 |
< |
|
1670 |
> |
|
1671 |
|
assertEquals(3, k); |
1672 |
|
q.remove(); |
1673 |
|
q.remove(); |
1678 |
|
/** |
1679 |
|
* descendingIterator.remove removes current element |
1680 |
|
*/ |
1681 |
< |
public void testDescendingIteratorRemove () { |
1681 |
> |
public void testDescendingIteratorRemove() { |
1682 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1683 |
|
for (int iters = 0; iters < 100; ++iters) { |
1684 |
|
q.add(new Integer(3)); |
1697 |
|
} |
1698 |
|
} |
1699 |
|
|
1693 |
– |
|
1700 |
|
/** |
1701 |
|
* toString contains toStrings of elements |
1702 |
|
*/ |
1704 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1705 |
|
String s = q.toString(); |
1706 |
|
for (int i = 0; i < SIZE; ++i) { |
1707 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
1707 |
> |
assertTrue(s.contains(String.valueOf(i))); |
1708 |
|
} |
1709 |
< |
} |
1704 |
< |
|
1709 |
> |
} |
1710 |
|
|
1711 |
|
/** |
1712 |
|
* offer transfers elements across Executor tasks |
1716 |
|
q.add(one); |
1717 |
|
q.add(two); |
1718 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1719 |
< |
executor.execute(new Runnable() { |
1720 |
< |
public void run() { |
1721 |
< |
threadAssertFalse(q.offer(three)); |
1722 |
< |
try { |
1723 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
1724 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
1725 |
< |
} |
1726 |
< |
catch (InterruptedException e) { |
1727 |
< |
threadUnexpectedException(); |
1728 |
< |
} |
1729 |
< |
} |
1730 |
< |
}); |
1719 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1720 |
> |
executor.execute(new CheckedRunnable() { |
1721 |
> |
public void realRun() throws InterruptedException { |
1722 |
> |
assertFalse(q.offer(three)); |
1723 |
> |
threadsStarted.await(); |
1724 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1725 |
> |
assertEquals(0, q.remainingCapacity()); |
1726 |
> |
}}); |
1727 |
> |
|
1728 |
> |
executor.execute(new CheckedRunnable() { |
1729 |
> |
public void realRun() throws InterruptedException { |
1730 |
> |
threadsStarted.await(); |
1731 |
> |
assertSame(one, q.take()); |
1732 |
> |
}}); |
1733 |
|
|
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 |
– |
}); |
1738 |
– |
|
1734 |
|
joinPool(executor); |
1735 |
|
} |
1736 |
|
|
1737 |
|
/** |
1738 |
< |
* poll retrieves elements across Executor threads |
1738 |
> |
* timed poll retrieves elements across Executor threads |
1739 |
|
*/ |
1740 |
|
public void testPollInExecutor() { |
1741 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1742 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1743 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1744 |
< |
executor.execute(new Runnable() { |
1745 |
< |
public void run() { |
1746 |
< |
threadAssertNull(q.poll()); |
1747 |
< |
try { |
1748 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
1749 |
< |
threadAssertTrue(q.isEmpty()); |
1750 |
< |
} |
1751 |
< |
catch (InterruptedException e) { |
1752 |
< |
threadUnexpectedException(); |
1753 |
< |
} |
1754 |
< |
} |
1755 |
< |
}); |
1744 |
> |
executor.execute(new CheckedRunnable() { |
1745 |
> |
public void realRun() throws InterruptedException { |
1746 |
> |
assertNull(q.poll()); |
1747 |
> |
threadsStarted.await(); |
1748 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1749 |
> |
checkEmpty(q); |
1750 |
> |
}}); |
1751 |
> |
|
1752 |
> |
executor.execute(new CheckedRunnable() { |
1753 |
> |
public void realRun() throws InterruptedException { |
1754 |
> |
threadsStarted.await(); |
1755 |
> |
q.put(one); |
1756 |
> |
}}); |
1757 |
|
|
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 |
– |
}); |
1772 |
– |
|
1758 |
|
joinPool(executor); |
1759 |
|
} |
1760 |
|
|
1761 |
|
/** |
1762 |
|
* A deserialized serialized deque has same elements in same order |
1763 |
|
*/ |
1764 |
< |
public void testSerialization() { |
1764 |
> |
public void testSerialization() throws Exception { |
1765 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1766 |
|
|
1767 |
< |
try { |
1768 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1769 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1770 |
< |
out.writeObject(q); |
1771 |
< |
out.close(); |
1772 |
< |
|
1773 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1774 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1775 |
< |
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1776 |
< |
assertEquals(q.size(), r.size()); |
1777 |
< |
while (!q.isEmpty()) |
1793 |
< |
assertEquals(q.remove(), r.remove()); |
1794 |
< |
} catch(Exception e){ |
1795 |
< |
unexpectedException(); |
1796 |
< |
} |
1767 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1768 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1769 |
> |
out.writeObject(q); |
1770 |
> |
out.close(); |
1771 |
> |
|
1772 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1773 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1774 |
> |
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1775 |
> |
assertEquals(q.size(), r.size()); |
1776 |
> |
while (!q.isEmpty()) |
1777 |
> |
assertEquals(q.remove(), r.remove()); |
1778 |
|
} |
1779 |
|
|
1780 |
|
/** |
1781 |
|
* drainTo(null) throws NPE |
1782 |
< |
*/ |
1782 |
> |
*/ |
1783 |
|
public void testDrainToNull() { |
1784 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1785 |
|
try { |
1786 |
|
q.drainTo(null); |
1787 |
|
shouldThrow(); |
1788 |
< |
} catch(NullPointerException success) { |
1808 |
< |
} |
1788 |
> |
} catch (NullPointerException success) {} |
1789 |
|
} |
1790 |
|
|
1791 |
|
/** |
1792 |
|
* drainTo(this) throws IAE |
1793 |
< |
*/ |
1793 |
> |
*/ |
1794 |
|
public void testDrainToSelf() { |
1795 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1796 |
|
try { |
1797 |
|
q.drainTo(q); |
1798 |
|
shouldThrow(); |
1799 |
< |
} catch(IllegalArgumentException success) { |
1820 |
< |
} |
1799 |
> |
} catch (IllegalArgumentException success) {} |
1800 |
|
} |
1801 |
|
|
1802 |
|
/** |
1803 |
|
* drainTo(c) empties deque into another collection c |
1804 |
< |
*/ |
1804 |
> |
*/ |
1805 |
|
public void testDrainTo() { |
1806 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1807 |
|
ArrayList l = new ArrayList(); |
1808 |
|
q.drainTo(l); |
1809 |
|
assertEquals(q.size(), 0); |
1810 |
|
assertEquals(l.size(), SIZE); |
1811 |
< |
for (int i = 0; i < SIZE; ++i) |
1811 |
> |
for (int i = 0; i < SIZE; ++i) |
1812 |
|
assertEquals(l.get(i), new Integer(i)); |
1813 |
|
q.add(zero); |
1814 |
|
q.add(one); |
1819 |
|
q.drainTo(l); |
1820 |
|
assertEquals(q.size(), 0); |
1821 |
|
assertEquals(l.size(), 2); |
1822 |
< |
for (int i = 0; i < 2; ++i) |
1822 |
> |
for (int i = 0; i < 2; ++i) |
1823 |
|
assertEquals(l.get(i), new Integer(i)); |
1824 |
|
} |
1825 |
|
|
1826 |
|
/** |
1827 |
|
* drainTo empties full deque, unblocking a waiting put. |
1828 |
< |
*/ |
1829 |
< |
public void testDrainToWithActivePut() { |
1828 |
> |
*/ |
1829 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
1830 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1831 |
< |
Thread t = new Thread(new Runnable() { |
1832 |
< |
public void run() { |
1833 |
< |
try { |
1834 |
< |
q.put(new Integer(SIZE+1)); |
1835 |
< |
} catch (InterruptedException ie){ |
1836 |
< |
threadUnexpectedException(); |
1837 |
< |
} |
1838 |
< |
} |
1839 |
< |
}); |
1840 |
< |
try { |
1841 |
< |
t.start(); |
1842 |
< |
ArrayList l = new ArrayList(); |
1843 |
< |
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 |
< |
} |
1831 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1832 |
> |
public void realRun() throws InterruptedException { |
1833 |
> |
q.put(new Integer(SIZE+1)); |
1834 |
> |
}}); |
1835 |
> |
|
1836 |
> |
t.start(); |
1837 |
> |
ArrayList l = new ArrayList(); |
1838 |
> |
q.drainTo(l); |
1839 |
> |
assertTrue(l.size() >= SIZE); |
1840 |
> |
for (int i = 0; i < SIZE; ++i) |
1841 |
> |
assertEquals(l.get(i), new Integer(i)); |
1842 |
> |
t.join(); |
1843 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
1844 |
|
} |
1845 |
|
|
1846 |
|
/** |
1847 |
|
* drainTo(null, n) throws NPE |
1848 |
< |
*/ |
1848 |
> |
*/ |
1849 |
|
public void testDrainToNullN() { |
1850 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1851 |
|
try { |
1852 |
|
q.drainTo(null, 0); |
1853 |
|
shouldThrow(); |
1854 |
< |
} catch(NullPointerException success) { |
1884 |
< |
} |
1854 |
> |
} catch (NullPointerException success) {} |
1855 |
|
} |
1856 |
|
|
1857 |
|
/** |
1858 |
|
* drainTo(this, n) throws IAE |
1859 |
< |
*/ |
1859 |
> |
*/ |
1860 |
|
public void testDrainToSelfN() { |
1861 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1862 |
|
try { |
1863 |
|
q.drainTo(q, 0); |
1864 |
|
shouldThrow(); |
1865 |
< |
} catch(IllegalArgumentException success) { |
1896 |
< |
} |
1865 |
> |
} catch (IllegalArgumentException success) {} |
1866 |
|
} |
1867 |
|
|
1868 |
|
/** |
1869 |
< |
* drainTo(c, n) empties first max {n, size} elements of deque into c |
1870 |
< |
*/ |
1869 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
1870 |
> |
*/ |
1871 |
|
public void testDrainToN() { |
1872 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1873 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
1874 |
< |
for(int j = 0; j < SIZE; j++) |
1874 |
> |
for (int j = 0; j < SIZE; j++) |
1875 |
|
assertTrue(q.offer(new Integer(j))); |
1876 |
|
ArrayList l = new ArrayList(); |
1877 |
|
q.drainTo(l, i); |
1878 |
< |
int k = (i < SIZE)? i : SIZE; |
1878 |
> |
int k = (i < SIZE) ? i : SIZE; |
1879 |
|
assertEquals(l.size(), k); |
1880 |
|
assertEquals(q.size(), SIZE-k); |
1881 |
< |
for (int j = 0; j < k; ++j) |
1881 |
> |
for (int j = 0; j < k; ++j) |
1882 |
|
assertEquals(l.get(j), new Integer(j)); |
1883 |
|
while (q.poll() != null) ; |
1884 |
|
} |